• No results found

Energy-Aware Task Scheduling in Contiki

N/A
N/A
Protected

Academic year: 2021

Share "Energy-Aware Task Scheduling in Contiki"

Copied!
65
0
0

Loading.... (view fulltext now)

Full text

(1)

IT 17 078

Examensarbete 30 hp

Oktober 2017

Energy-Aware Task Scheduling

in Contiki

Derrick Alabi

(2)
(3)

Teknisk- naturvetenskaplig fakultet UTH-enheten Besöksadress: Ångströmlaboratoriet Lägerhyddsvägen 1 Hus 4, Plan 0 Postadress: Box 536 751 21 Uppsala Telefon: 018 – 471 30 03 Telefax: 018 – 471 30 00 Hemsida: http://www.teknat.uu.se/student

Abstract

Energy-Aware Task Scheduling in Contiki

Derrick Alabi

Applications for the Internet of Things often run on devices that have very limited energy capacity. Energy harvesting can offset this inherent weakness of these devices by extracting energy from the environment. Energy harvesting increases the total energy available to a device, but efficient energy consumption is still important to maximize the availability of the device. Energy-aware task scheduling is a way to efficiently consume energy in an energy constrained device with energy harvesting capabilities to extend the device's availability.

In this thesis, prediction of future incoming harvest energy is combined with hard real-time and reward-based weakly-hard real-time task scheduling schemes to achieve efficient energy usage. Linear regression and artificial neural networks are evaluated individually on their ability to predict future energy harvests. The artificial neural network used contains a single hidden layer and is evaluated with ReLU, Leaky ReLU, and sine as activation functions.

The performance of linear regression and the artificial neural network with varying activation functions and number of hidden nodes are tested and compared. Linear regression is shown to be a sufficient means of predicting future energy harvests. A hard real-time and a reward-based weakly-hard real-time task scheduling scheme are also presented and compared. The experimental results show that the hard real-time scheme can extend the life of the device compared to a non-energy-aware scheduler, but the weakly-hard real-time scheme will allow the device to function indefinitely.

IT 17 078

Examinator: Arnold Neville Pears Ämnesgranskare: Thiemo Voigt

(4)
(5)

Acknowledgements

I would like to thank my supervisors, Niklas Wirström and Joel Höglund for their advice and feedback throughout the thesis project.

I would also like to thank Thiemo Voigt and the Networked Embedded Systems (NES) group at RISE SICS for the good working environment and allowing me the opportunity to gain greater understanding into the inner-workings of a research group.

A special thanks goes out to Christoph Ellmer for the invigorating and insightful discussions during the thesis project and master’s program.

I would also like to acknowledge the financial support provided by the strategic innovation pro-gramme InfraSweden2030, a joint effort of Sweden’s Innovation Agency (Vinnova), the Swedish Research Council (Formas) and the Swedish Energy Agency (Energimyndigheten). This thesis project was carried out within the scope of the project “Smart condition assessment, surveillance and management of critical bridges” funded by InfraSweden2030.

(6)
(7)

Contents

1 Introduction . . . 9

1.1 Setting . . . 9

1.2 Project Purpose and Goal . . . .9

1.3 Contributions . . . 9

1.4 Structure of the Thesis Report . . . 10

2 Related Work . . . 11

2.1 Energy Prediction . . . 11

2.1.1 Comparison of Statistical Methods. . . .11

2.1.2 Artificial Neural Networks for Time-Series Predictions . . . 11

2.1.3 Weather-Conditioned Moving Average . . . 11

2.2 Scheduling and Task Management. . . 12

2.2.1 Harvesting-Aware Real-Time Scheduling Algorithms . . . 12

2.2.2 Energy-Aware Real-Time Scheduling Algorithms using Rewards. . . 14

2.2.3 Other Approaches . . . 16

3 Background . . . 19

3.1 Moving Average . . . 19

3.2 Exponential Smoothing . . . .19

3.3 Regression Analysis . . . 19

3.4 Artificial Neural Networks (ANNs) . . . 20

3.4.1 Stochastic Gradient Descent . . . 21

3.4.2 Nesterov Accelerated Gradient Descent . . . .21

4 Design and Implementation . . . 23

4.1 Design Overview . . . 23

4.2 Task Management. . . 24

4.2.1 Simple Task Model . . . 24

4.2.2 Expanded Task Model . . . 27

4.3 Energy Predictor Implementation. . . .27

4.3.1 Fixed-Point Math . . . 28

4.3.2 Energy Predictor Interface. . . .29

4.3.3 Simple Linear Regression . . . 29

4.3.4 Single-Input Single-Output Artificial Neural Network (SISO ANN) . . . 30

4.3.5 Helper Functions. . . 35

4.4 Policies . . . 35

4.4.1 Simple Task Model Policy . . . 35

4.4.2 Expanded Task Model Policy . . . .36

5 Results . . . 39

5.1 Energy Predictor Accuracy . . . 39

5.1.1 Simple Linear Regression . . . 41

5.1.2 ANNs with leakyrelu activation . . . 43

5.1.3 ANNs with bhaskara_sin activation . . . 45

5.1.4 ANNs in the Long-Term. . . .50

5.2 Simple Task Model Scheduling . . . .55

(8)

6 Conclusions . . . 63

6.1 Conclusion . . . 63

6.2 Future Work . . . 63

(9)

1. Introduction

1.1 Setting

The Contiki operating system is an open source operating system designed for the Internet of Things [1]. It runs on low-cost and low-power microcontroller-based devices and contains many implementations of networking protocols and low-power wireless standards such as 6LoWPAN, RPL, and CoAP. It is one of the key technologies of the Networked Embedded Systems (NES) group at RISE SICS (Research Institutes of Sweden, Swedish Institute of Computer Science).

Applications for the Internet of Things often run on devices that have very limited energy ca-pacity. Energy harvesting can offset this inherent weakness of these devices by extracting energy from the environment. Common sources of energy include: solar energy, wind energy, vibra-tions, thermal differences, or energy extracted from radio waves. Energy harvesting increases the total energy available to a device, but efficient energy usage is still important to maximize the availability of the device within a larger wireless network [9, 10]. Currently, Contiki has mecha-nisms for estimating the power consumption and finding where power was spent, but there are no mechanisms aimed specifically at energy harvesting.

1.2 Project Purpose and Goal

The purpose of this project is to implement an energy-aware task scheduler that can be integrated into the Contiki operating system. The scheduler should plan the use of peripherals including radio and sensors and processing according to the current amount of available energy and the predicted energy that can be harvested in the foreseeable future. The scheduler is part of a larger project where the device nodes will be deployed on a bridge to measure and log vibrations from trains crossing over the bridge. Ideally, the goal is to schedule the tasks in such a way that the energy harvested and the energy consumed by peripherals and processing are perfectly balanced, so that the device can function indefinitely. If the energy usage cannot be balanced, then the scheduler should be able to schedule the tasks, so that the device can operate for a predetermined amount of time. The most basic requirement is that the energy-aware task scheduler should perform better than the standard behavior in the current version of Contiki for a given quality of service.

1.3 Contributions

This master’s thesis presents (Section 4.1) and evaluates (Chapter 5) a framework for composing an energy-aware task scheduler that schedules tasks based on predictions of energy harvests. The framework contains two interchangable parts: the energy predictor and the scheduling policy.

This thesis evaluates and compares the following as two different possible means of prediction within the energy predictor (Section 5.1):

• linear regression

• artificial neural networks

The following activation functions were evaluated within the artificial neural networks on their ability to make energy predictions using stochastic gradient descent and random initialization:

• Rectified Linear Unit (ReLU)

• Leaky Rectified Linear Unit (Leaky ReLU) • Bhaskara I’s approximation of the sine function

(10)

Fast Fourier transformation (FFT) initialization of an artificial neural network using Bhaskara I’s approximation of the sine function as an activation function is also used and evaluated.

This thesis also presents (Section 4.4) and evaluates (Chapter 5) two different scheduling poli-cies that accomplish each of the following two goals separately:

• The device continues to function as long as possible [while executing hard real-time tasks] (Section 4.4.1 and Section 5.2)

• Task energy consumption does not exceed incoming energy [while executing weakly hard real-time tasks] (Section 4.4.2 and 5.3)

1.4 Structure of the Thesis Report

The thesis is divided into six chapters and is organized as follows.

This chapter introduces the context under which this thesis was performed, the purpose and goal of the project, contributions made by this thesis project and the structure of the report.

Chapter 2 presents work that is related to the two main topics discussed: energy prediction and task management and scheduling.

Chapter 3 gives relevant background information for the energy prediction used within the energy-aware task scheduler.

Chapter 4 describes the design and implementation of the energy-aware task scheduler and its parts.

Chapter 5 presents the resulting performance achieved from the presented design and its parts in simulation.

Chapter 6 concludes the thesis report and provides suggestions for future work.

(11)

2. Related Work

The task of energy-aware scheduling is divided into two main research areas. One research area involves finding the best way to predict the amount of energy that will be reclaimed by the device from energy harvesting in the foreseeable future and the other involves task management and scheduling to achieve certain energy-related goals. For task management, the combined power consumption associated with a given task and the switching between tasks should be considered. As there are many ways these two tasks can be accomplished, the state-of-the-art is observed to find the best combination of methods to optimize performance while considering code size, execution time, and memory usage.

2.1 Energy Prediction

2.1.1 Comparison of Statistical Methods

Lu et al. [8] have compared the use of a moving average, exponential smoothing, and regression analysis (specifically linear regression) in their ability to predict future energy harvests at run-time within a task scheduling system on energy-harvesting devices. They propose an algorithm that utilizes dynamic voltage and frequency scaling (DVFS) to schedule tasks for efficient energy consumption called the Model-Accurate Predictive DVFS Algorithm (MAP-DVFS).

The use of a moving average, exponential smoothing, and regression analysis for energy harvest prediction is relevant to this thesis. Of the three methods, exponential smoothing performed the worst in their experiments. The moving average and regression analysis were close to each other in performance, but regression analysis performed slightly better and had the best fit to the actual data points. Both exponential smoothing and the moving average generate predictions that have a time delay compared to the actual values.

2.1.2 Artificial Neural Networks for Time-Series Predictions

Gashler and Ashmore [7] use deep artificial neural networks containing sinusoidal activation func-tions to make predicfunc-tions on time-series data. The weights in the network are initialized using a fast Fourier transform. The fast Fourier transform causes the artificial neural network to fit closely to initial data, but the network is also capable of predicting future trends as it is trained using stochastic gradient descent. Regularization is used to simplify the model to achieve better non-linear extrapolation. They demonstrate that their method is effective in extrapolation of non-non-linear trends in time-series data. Their method is also demonstrated to work well with few training data points. Initialization using the fast Fourier transform proved crucial to achieving good results.

2.1.3 Weather-Conditioned Moving Average

Piorno et al. [11] propose the weather-conditioned moving average (WCMA) as a way to estimate solar energy harvests. WCMA takes into account the solar conditions at a certain time of the day with self-adjustments for fast changing weather conditions during the day. The WCMA algorithm uses a D × N matrix of sampled power values that stores N sampled values per day for D past days. The prediction is a weighted sum of the desired time’s value within the matrix and the average of the values for that specific time of day for the last D days. The averaged values are scaled by a factor that measures the ratio between the solar conditions of the current day to the

(12)

solar conditions of the previous days. Due to the use of a matrix, this prediction method requires several times more memory than linear regression or a regular moving average which stores only the last n samples. WCMA was shown to have up to 10% error in their experiments.

2.2 Scheduling and Task Management

2.2.1 Harvesting-Aware Real-Time Scheduling Algorithms

The following proposed algorithms have task models that specify timing constraints for tasks within the system. These algorithms are designed specifically for devices that have some type of energy harvesting device attached. The goal of these algorithms is to avoid two types of violations within a system. One type of violation is a timing violation where a task is not able to complete before its specified deadline. The other violation is an energy violation. Energy violations occur when there is no more energy in the system to run any more tasks.

Lazy Scheduling Algorithms (LSA)

The Lazy Scheduling Algorithms (LSA) proposed by Moser et al. [9, 10] are dynamic algorithms that find a balance between an Earliest Deadline First (EDF) approach and an As Late As Possible (ALAP) approach. EDF ensures that all tasks are executed before their hard real-time deadline, given that the tasks are schedulable by any algorithm in a way that all deadlines are met. EDF does not take available energy into consideration and executes tasks as soon as possible. As a result, EDF causes the energy to be drained as quickly as possible. ALAP delays the execution of tasks, so that there is time for energy to be replenished in the system.

A task i is characterized in LSA by:

ai arrival time

ei energy demand

di deadline

Table 2.1. Lazy Scheduling Algorithms (LSA) Task Characterization

The arrival time aiis the time at which the task i arrives in the system. The energy demand eiis

the energy required by the task. The deadline di is the time at which the task must be completed.

The system constraints are formalized in (2.1). PH(t) is the power that is harvested at a certain

time t. The energy harvested is represented by EH(t1,t2). PD(t) is the power consumed by the

device. The energy consumed is represented by ED(t1,t2). Pmaxis the maximum possible power

consumption of the device. The authors discuss two versions of LSA. LSA-I where Pmax= +∞

and LSA-II where Pmaxis limited. LSA-II is discussed here.

LSA-II relies on the following definitions: EH(t1,t2) = Z t2 t1 PH(t)dt ED(t1,t2) = Z t2 t1 PD(t)dt 0 < PD(t) < Pmax ∀t 0 ≤ EC(t) ≤ C ∀t (2.1)

In (2.1), the first two equations show that the energy between two points in time is calculated by integrating underneath the respective power curve between the two time points. The third equation states that the power consumed by the device must be in between 0 and Pmax, for all time

t. The last equation states that stored energy EC(t) is always less than or equal to the maximum

energy capacity C, for all time t. 12

(13)

Tasks are assumed to be independent and preemptive. A task i with starting time si completes

when the required amount of energy ei is consumed by the task. The minimal execution time is

ei/Pmax. This execution time can only occur when the task is not interrupted or preempted.

If a task A executes at the latest possible time to make its deadline and a new task B, with an earlier deadline, arrives and preempts task A after task A has started executing, then task A is guaranteed to miss its deadline and cause a timing violation. The timing violation can be avoided by executing task A earlier, but then task A runs the risk of stealing energy that task B needs to run.

The authors note that the optimal starting time simust guarantee that within the interval [si, di]

the processor could continuously use Pmax and empty the energy storage (EC(di) = 0) exactly at

the deadline di. Before this optimal starting time si, the scheduler should keep the stored energy

ECas high as possible.

To find the optimal starting time, the following is assumed:

PH(t) < Pmax ∀t (2.2)

To determine the optimal starting time, the minimum amount of time to process the maximum amount of energy available to the task before its deadline, without interruption, is calculated. This value is then subtracted from the deadline di, as shown in (2.3).

s∗i = di−

EC(ai) + EH(ai, di)

Pmax

(2.3) When the battery is full, any incoming harvest energy will be wasted, because there is no more capacity to store it. If the battery becomes full, before s∗i then it is desirable to run the task earlier to avoid an energy overflow. In this case, there is less energy that is available to the task, due to the overflow, and the equation in (2.4) reflects that. s0iis obtained by solving (2.4) numerically.

s0i= di−

C+ EH(s0i, di)

Pmax

(2.4) The optimal starting time, balancing both energy and time constraints, is then determined by the maximum of (2.3) and (2.4).

si= max(s0i, s ∗

i) (2.5)

LSA-II first picks the task with the earliest deadline. Then, the optimal starting time is calcu-lated using the equations above. If the starting time is earlier or equal to the current time, then the task is processed with the maximum power (PD(t) = Pmax). If the starting time is in the future,

then time is allowed to pass until the starting time at which the task is executed, until the battery is full, or until a new task with an earlier deadline arrives. To avoid wasting energy when the battery is full, LSA processes the task with the earliest deadline immediately, if there is a waiting task and the battery is full. The task is run with power equal to the harvest power (PD(t) = PH(t)) in

this case.

Smooth to Average Method (STAM)

The Smooth to Average Method proposed by Audet et al. [6] is a static scheduling algorithm that does not require predictions of incoming energy. In STAM, a task i is characterized as shown in Table 2.2. All tasks in the system arrive at time 0. The deadline of a task is assumed to be equal to its period. At the end of every period Ti, the current instance of the task expires and a new

instance of the task arrives. Tasks are assumed to have a constant execution duration and drain energy at a constant rate.

Ti task period

Di task execution duration

Pi task energy consumption per time unit

(14)

For STAM, there is the concept of a ‘virtual task’. The ‘virtual task’ associated with a given task is characterized by Ti, Di, and Pi.

The virtual task is derived from the following equations: Di= d Di× Pi / P e Pi= Di× Pi/ Di P=

i (Pi× Di/Ti) (2.6)

P is called the power threshold. When the power threshold is greater than Pi then the virtual

task and the actual task are the same. When the power threshold is less than Pi, the virtual task

and the actual task will consume the same amount of energy, but the virtual task is spread out over a longer execution duration. The actual task is scheduled at the end of the window created by the virtual task to maximize the amount of energy harvested before the task is executed. The virtual task is introduced to smooth the energy consumption in the long run. This smooths the power consumption to the average power consumption of all tasks.

Smooth to Full Utilization

Because virtual tasks in the Smooth to Average Method can have longer execution durations, the likeliness that a task set is unschedulable is higher compared to scheduling algorithms that operate based on execution time and deadline alone. This happens when a task requires a lot of energy, so the system must wait a long time for the required energy to be harvested. The Smooth to Full Utilization method, also proposed by Audet et al. [6], addresses this issue by maximizing the virtual tasks to 100% processor utilization. The task characterization and virtual task definition are the same as STAM above (Table 2.2 and the equations in 2.6, respectively).

The utilization U for the virtual tasks is calculated as follows: U= k

i=1 Di Ti (2.7) The task duration is modified using the following equations (2.8) and the task is scheduled using [Ti, dV, PV]. Ei= Di× Pi/Ti Etotal= k

i=1 Ei dV = max(Di, bTi× Ei/Etotalc) PV = Pi× Di/dV (2.8)

In the experiments performed by the authors, running tasks with the earliest deadline first after Smooth to Full Utilization resulted in fewer violations than doing the same after STAM smooth-ing. On their test task set, STAM had a probability of violation of 0.045 and Smooth to Full Utilization had about 0.02. The Lazy Scheduling Algorithms were shown to outperform both with a probability of violation of about 0.01.

2.2.2 Energy-Aware Real-Time Scheduling Algorithms using Rewards

The following algorithms also have a task model that specify timing constraints for tasks within the system like the algorithms above, but are primarily designed for devices where the only energy source is a battery. Despite the fact that they do not consider energy harvesting, they still aim to avoid both timing and energy violations. In addition to these two types of violations, these algorithms use a reward system. Reward values are used to make a decision which tasks should be run and/or how they should be run when it is not possible to run all of the tasks. Tasks can be run with a different energy profile with a different execution time or not at all. Each task or each 14

(15)

task version is assigned a reward value, based on its importance, and the scheduling algorithm aims to maximize the sum of the rewards during the runtime of the device.

The REW-Pack Algorithm

The REW-Pack algorithm proposed by Rusu et al. [13] characterizes tasks in the following man-ner. The deadline of a task is assumed to be equal to its period. All task periods are identical and all tasks are ready at time 0. It is not required that all of the tasks must be scheduled. It is assumed that the tasks are run on a processor where the voltage and frequency can be dynamically adjusted before task execution. It is also assumed that the tasks are simply computational tasks that do not use peripherals and that the energy consumption of the task increases proportionally to the processor speed.

Di task period

ti,si task execution duration

ei,si task energy consumption

vi task reward

*where siis the processor speed Table 2.3. REW-Pack Algorithm Task Characterization

The algorithm aims to find the subset of tasks S with the speeds si such that the following

constraints are satisfied. Emax in Equation 2.11 is the amount of energy available in the system.

There are N tasks and M processor speeds.

Maximize

i∈ S vi (2.9)

i∈ S ti,si≤ D (2.10)

i∈ S ei,si≤ Emax (2.11) S⊆ {1, 2, ..., N} (2.12) si∈ {1, 2, ..., M} (2.13)

There are three steps in the algorithm: adding tasks, increasing task speed, and dropping tasks. The following ratio is used to add tasks to the schedule:

v(i)

t(i, 1) · e(i, 1) (2.14) The algorithm attempts to add the task with the highest ratio of the tasks that have not yet been considered. The task is added with the minimum processor speed, which is assumed to have the minimum energy footprint. Adding the task must not violate the global deadline or cause the energy required by the selected tasks to exceed the amount of energy available in the system. If the task cannot be added, then the algorithm uses the following ratio to increase the speed of tasks:

∆ t ∆ E =

t(i, si) − t(i, si + 1)

e(i, si + 1) − e(i, si)

(2.15) The scheduled task with the highest ratio is moved to the next speed level as long the increase would not result in the energy required by the scheduled tasks to exceed Emax. The task also

cannot be increased past the maximum speed. This step is repeated until the task that needed to be added can be added or there are no more tasks that can be increased in speed. If there are no more tasks that can be increased in speed and the task still cannot be added, then tasks are dropped from the schedule.

(16)

Once a task has been dropped from the schedule, it cannot be added again. The scheduled task with the lowest reward ratio (2.14) is dropped. Tasks are dropped until the next task can be added. When all tasks have been considered, the final subset S has been selected and these are the tasks that will be scheduled.

The authors also presented an algorithm called Unpack that is mostly the same as REW-Pack, but instead tasks are added with the maximum speed and then the speed of the tasks are decreased to create energy for new tasks to be inserted. This new algorithm is shown to have virtually the same performance as the REW-Pack algorithm.

2.2.3 Other Approaches

The following approaches are presented and analyzed by AlEnawy et al. [5] for energy-constrained systems that have periodic tasks that can allow for skipped instances.

Weakly-Hard Real-Time Systems

For some periodic real-time applications, some deadline misses are acceptable given that the deadline misses are spaced evenly. In their work, they analyze a (m, k)-firm deadline model where a task must meet at least m deadlines in every consecutive k instances. Any violation of this constraint is considered a dynamic failure. This approach can help maintain device availabilty in low-energy situations or when the system is overloaded.

In a system with a hard energy constraint, if the total energy required to run each and every task mi times in every consecutive kiinstances using their worst-case execution times is less than

the system energy budget then all dynamic failures can be avoided. It is assumed that the tasks are simply computational tasks that do not use peripherals and that they consume less energy as the CPU runs at a lower speed.

Energy Density Schemes

In situations where energy is extremely scarce and dynamic failures are unavoidable, greedy schemes will inevitably cause an energy violation and the device will completely deplete its en-ergy. The authors propose energy density as a means of determining which tasks should not be run at all. The energy density EDifor each task is calculated by the following formula (2.16), where

Ei is the energy consumption of the task, DFimax is the maximum number of dynamic failures

that the task will cause within a certain time window, and wi is the relative impact that the task’s

dynamic failures have on the performance of the overall system. EDi=

Ei

wi · DFimax

(2.16) The energy budget is then allocated incrementally from the task with the lowest energy density towards the task with the highest energy density, using the energy consumed by the task at the lowest processor speed. When the energy budget is depleted, the process is complete and the remaining tasks are not scheduled. This ratio is used to give preference to tasks that have the lowest energy requirements and can potentially cause more dynamic failures. The execution and deadlines of the selected tasks are guaranteed, but the remaining tasks may be executed if slack time can be reclaimed due to early completion of any of the selected tasks or if there is extra energy in the system.

The Dynamic Reclaiming Algorithm (DRA)

The Dynamic Reclaiming Algorithm (DRA) is used to perform dynamic speed slow-down and therefore save energy by running tasks at a slower speed when slack time is detected in the sched-ule. This is done by comparing the actual runtime schedule with the optimal static task schedule built ahead of time. The static schedule is built using the minimum speed where all of the tasks 16

(17)

are able to meet their deadlines in the worst-case. When the actual task completes before its com-pletion time in the static schedule, later lower priority tasks can take advantage of the additional CPU time due to the early completion.

Experimental Results

In the experiments performed by the authors, using energy density to select which tasks to run re-sulted in fewer dynamic failures overall compared to simply using the (m, k)-firm deadline model. They also noted that use of the dynamic reclamation proved more important than the minimum processor speed for greedy schemes, but a low minimum processor speed was more important for the energy density scheme.

(18)
(19)

3. Background

The following sections introduce background information related to energy prediction. Energy prediction allows the scheduler to balance the consumption of energy with the amount of energy that will be available in the near future. The methods are introduced from the simplest to the most complex and form the foundation for the prediction methods used in the design of the energy-aware task scheduler. Linear regression and artificial neural networks are used directly in the implementation evaluated in this thesis.

3.1 Moving Average

A simple way to predict future values is to use the average of past values. Prediction is done using a moving average of the last n observations. Equal weight is given to all past data and the prediction is only done for the next value in the stream. Prediction is done using the following equation: b y=∑ n i=1yyyn n (3.1)

3.2 Exponential Smoothing

Exponential smoothing is an approach presented by Lu et al. [8]. The weight of the previous values decay exponentially. Simple exponential smoothing is shown in (3.2). ye(t)is the

exponen-tially smoothed value and y(t)is the observed value. α is known as the smoothing constant and is a value between 0 and 1, exclusive.

b

y= ye(t)= αy(t)+ (1 − α)ye(t−1) (3.2)

3.3 Regression Analysis

To allow for energy prediction within any time window, the power available can be sampled and then with integration over time, the energy available within the time window can be calculated, as shown in (3.3). The same equation is used by Moser et al. in the Lazy Scheduling Algorithms [9, 10] shown in Section 2.2.1.

E=

Z t2

t1

P(t)dt (3.3)

If harvester voltage and the consumption voltage are assumed to be constant, the harvest current available is sampled. Integration would yield the available charge from the energy harvesting device, as shown in (3.4), but it is easily scaled using the voltage to obtain the energy value.

Q=

Z t2

t1

I(t)dt (3.4)

Methods such as a moving average and exponential smoothing, presented in Section 3.1 and Section 3.2 respectively, can predict the next value within a sequence, but predictions obtained

(20)

via regression methods are more readily integrated through the indefinite integral of the regres-sion function. Through regresregres-sion, the independent variables of a chosen regresregres-sion function are estimated to minimize the error between the output of the function and the sampled observational data.

Lu et al. [8] also propose regression analysis as a means of predicting future energy. More specifically, they propose simple linear regression by the method of minimizing least squares. Regression analysis has the advantage that predictions farther into the future can be made. It is possible to get more than just the next value in the sequence unlike in a moving average or expo-nential smoothing. Any arbitrary time value can be input into the equation to make predictions. Simple linear regression forecasts are based on the following equation:

b

y= b0+ b1z+ ε (3.5)

First, b0 and b1 are estimated using the following equations, given n observations (xxx1, yyy1)

through (xxxn, yyyn), where y is the value to be predicted and x is time. xxx and yyy are the arithmetic

means of xxx and yyy.

b b1=∑

n

i=1(xxxi− xxx) ∑ni=1(yyyi− yyy)

∑ni=1(xxxi− xxx)2

b

b0= yyy − bb1xxx

(3.6)

The prediction for y is then made by evaluating (3.7), where z is the time to make the prediction for.

b

y= bb0+ bb1z (3.7)

3.4 Artificial Neural Networks (ANNs)

An artificial neural network or ANN is a structure that comes from the field of machine learning. Artificial neural networks are inspired by biological neural networks that consist of a network of interconnected neurons that pass information in the form of electrical signals. The artificial neurons in an ANN receive signals from the neuron(s) connected as an input, process the signals, and then generate an output signal for the neuron(s) that are connected to its output. Artificial neurons are commonly referred to as neural nodes. ANNs are typically built up of an input layer, an output layer, and one or more hidden layers in between the input layer and output layer. Signals pass from the input layer through the hidden layer(s) to the output.

Each connection in the neural network has an associated value called a weight. This value determines how much the signal contributes to the output. The output of a neural node is generated as the sum of the product of each input value and the weight value associated with the connection to the input. An activation function is then applied to the sum to generate the output of the node. The sigmoid, hyperbolic tangent, and rectified linear unit (ReLU) functions are examples of common activation functions found in artificial neural networks. Gashler and Ashmore [7] have also used sine as an activation function when using a neural network to make time-series predictions. Altering the values of the weights alter how certain inputs to the neural network map to its output(s).

A neural network with one input neural node and one output node can approximate a function with one input and one output. For example, the network can take a time value and output the corresponding harvest electrical current for the time value. The approximation generally increases in quality as more hidden nodes are added to the network. The choice of activation function also affects the quality of the approximation. The network learns through an iterative process of adjusting the weights so that the output more closely corresponds to the observed data. Using the supervised learning method, the weights are adjusted based on past events. One way of adjusting the weights is through the gradient descent algorithm.

(21)

3.4.1 Stochastic Gradient Descent

The gradient descent optimization method aims to minimize the error between the current output of the neural network for a given input and the desired output by adjusting the weights based on the gradient of a function of the error [12]. The output of a neural node in the network (hθθθ(xxx)) is

in (3.8). n is the number of inputs for the node. θ are the weights. hθθθ(xxx) =

n

i=1

θθθixxxi (3.8)

The weights are then adjusted through the “backpropagation” method. The “backpropagation” method calculates the error on the output and propagates the error backwards through the network to adjust the weights. This is done by using the negative gradient of the cost function (a function of the error) to alter the weights and minimize the error of the output. A common cost function is the square difference between the output of the neural network and the desired output divided by 2. Traditionally, the weights are adjusted by training the neural network on a batch of sampled values from the desired function. For embedded devices with small amounts of memory, this is undesirable due to the need to store many values in memory until the time that the network needs to be trained. Instead, the training can be done incrementally by adjusting the weights on every new sample, so that at most one sample needs to be in memory at any given time. The stochastic gradient descent method grants this ability. The cost function, J(θ ), for a single sample is in (3.9), where hθ(x) is the current output of the node given the input x and y is the current observed output

value from sampling corresponding to x. The weights are then adjusted individually using (3.10). η is the learning rate. It controls how aggressively the weight moves towards the desired output. A common value for the learning rate is 0.01.

J(θ ) = 1

2(hθ(x) − y)

2 (3.9)

θ = θ − η · ∇θJ(θ ) (3.10)

3.4.2 Nesterov Accelerated Gradient Descent

Stochastic gradient descent, on its own, can have problems moving towards the minimum error in regions where the cost function surface is steeper in one dimension than the others [12]. To resolve this problem, a momentum term can be added to the weight update equation to keep the weights moving in the relevant direction. The momentum term takes a portion of the previous update and uses it in the current update. The new update rule then becomes a combination of (3.11) and (3.12). γ controls the amount of influence that the previous updates have. A common value for γ is 0.9.

vt = γvt−1+ η · ∇θJ(θ ) (3.11)

θ = θ − vt (3.12)

Nesterov accelerated gradient descent provides a smarter way to apply momentum to the stan-dard stochastic gradient descent update rule. Since it is known that momentum term will in-variably move the weight by a known amount, it makes sense to calculate the gradient using a “lookahead” derived from the weight movement due to the momentum (θ − γvt−1) [2]. This

al-lows the updates to take smaller steps as the minimum is approached and avoids overshooting the minimum. The updated Nesterov update rule is a combination of (3.13) and (3.14).

vt = γvt−1− η · ∇θJ(θ ) (3.13)

(22)
(23)

4. Design and Implementation

4.1 Design Overview

Figure 4.1. Energy-Aware Task Scheduler (EATS) Design Overview

The scheduler consists of three functional parts: the energy predictor, the task management sys-tem, and the scheduling policy. The energy predictor and the scheduling policy are interchange-able. The task management system executes tasks and moves tasks between the awake and sleep-ing task lists. The schedulsleep-ing policy decides which task should be executed (if any), which version of a task should be executed, and whether or not that task should be executed with a delayed start time. Tasks are independent and non-preemptive, due to the constraints of Contiki and many embedded devices. The policy uses the energy prediction unit to aid in policy decision making, but updates to the predictor are done externally by the interrupt handler that receives the sampled harvest current or power readings from the energy harvesting circuitry.

The predictor is updated with the usable electrical current or power that is provided by the energy harvester in milliamps or milliwatts normalized by dividing the values by the maximum possible value. Normalization is used to avoid overflows in the predictor calculations. The time values are also normalized within a window of 1 day. When the scheduler reaches the end of the time window, it wraps to the beginning to simulate a continuous time axis. Interpolation, as opposed to extrapolation, is used here due to the limited range of fixed-point numbers. The

(24)

predictor has a standard interface that allows for many predictor implementations. The options presented and used here are linear regression, introduced in Section 3.3, and the artificial neural network scheme, introduced in Section 3.4.

Over the course of the project, two different task models were considered. The simpler task model only allows one version of each task and the other is a more complex expanded task model that allows for multiple versions within a task as well as tasks with reward values. The simpler task model is designed for use with scheduling algorithms such as the ones in Section 2.2.1. The expanded task model is designed for use with scheduling algorithms like the ones presented in Section 2.2.2.

The predictors used with the two task models remain the same, but the task management and available policies differ due to the difference in the task structure. The policy for the simpler task model presented here is inspired by the ideas behind the Lazy Scheduling Algorithms (LSA) presented in Section 2.2.1. The policy for the more complex expanded task model uses a reward system combined with weakly real-time behavior similar to the algorithms discussed in Section 2.2.2. In the later sections, the simpler task model will be referred to simply as simple and the model allowing for multiple versions will be referred to as expanded.

4.2 Task Management

The task management part of the scheduler maintains the task lists in the scheduler and keeps task information updated. When tasks complete, the tasks are put in the appropriate list. When the next execution window for a task begins, it is moved from the sleeping list to the awake list and the deadline of the task is updated. If there is at least one task in the awake list, the policy is called upon to make decisions on task execution. The task manager also determines the amount of time that the system can sleep for between task executions.

4.2.1 Simple Task Model

The task variables that are important for scheduling are outlined in Table 4.1. All of these variables are held as 32-bit unsigned integer values. The tasks are held in a doubly linked list, so the structure for the task also contains pointers to the previous and next nodes in the list. A doubly linked list is chosen for simplicity of implementation and the ability to easily move tasks from any point in one list to any point in another list cheaply without heap allocations. These qualities are good for embedded devices that have limited memory for both data and code. The structure also contains a function pointer to run the task and a pointer to a null-terminated string with the task’s name. For tasks that use peripherals, such as radio, the energy required to run the task within a certain time period will be different compared to the energy required to run a purely computational task in the same time period. Because of this, the current or power draw during the execution of the task must be specified. The current is used, here, because it is a value that can be easily obtained from the datasheet for a microcontroller or system-on-a-chip (SoC). If power is used, then the predictor must sample the power from the harvesting circuitry, but when the current is used, the current is sampled. If the voltage used to power the microcontroller or SoC differs from the voltage where the current is measured in the harvesting circuitry, then the current readings must be scaled or the power must be used in place of the current. In cases where the current varies within a task, the average current is used.

(25)

Ti task period (ms)

Di task execution duration (ms)

Ii task electrical current draw (mA)*

si latest possible start time

di task deadline

*or optionally the power draw (mW)

Table 4.1. Energy-Aware Task Scheduler (EATS) Task Characterization (Simple)

Ti, Di, and Ii in Table 4.1 are set by the developer of the task. Any of these values may be

dynamically updated during runtime. The task period Ti defines the window of time in which

the task must execute exactly once. The task may execute at any time within the window. Task execution windows for a task do not overlap and occur immediately one after the other. Tasks are independent and non-preemptive. Figure 4.2 illustrates this with a task A that has a period of 10 seconds and a task B that has a period of 15 seconds. The latest possible start time siand the task

deadline diare determined by the scheduler when it initializes.

Figure 4.2.Energy-Aware Task Scheduler (EATS) Task Period

The scheduler maintains two lists. The first is a list of awake tasks that are ready to be executed. The second is a list of sleeping tasks that have already been executed within their current time window and are waiting for the next window of execution. When the scheduler initializes, all tasks are placed in the awake list. The sleeping list is empty. The tasks in the awake list are sorted by period. The task with the shortest period is first. Indirectly, this will also sort the tasks by deadline where the task with the earliest deadline is first, because the first window for all tasks begins at time 0.

The deadline di for each task is initialized to 0 and then incremented by the period for the

task. The deadline variable must wrap around on a value that is a multiple of days for the energy predictor to function properly, because the predictors perform regression within a window that is a day long. A time period of 49 days is chosen for this, because it is the largest number of whole

(26)

days that can fit in a 32-bit unsigned integer that holds milliseconds. This means that task periods longer than 49 days are not allowed. The result modulo w in (4.1) is then stored in the deadline variable.

w= 4, 233, 600, 000 = 86400 s

day · 1000 ms

s · 49 days (4.1) The initialization code then traverses the list in reverse from the task with the latest deadline to the task with the earliest deadline. The latest possible start time siinitialization is shown in (4.2).

Ntasksis the total number of tasks. The time t is initialized to 0. If the latest start time for the task

with the earliest deadline is before the initial time, then it is not possible to schedule the task set. In this case, tasks must either be removed from the system or a faster processor must be used.

si=



(di− Di) mod w, if i = Ntasks or (di− t) mod w ≤ (si+1− t) mod w

(si+1− Di) mod w, otherwise

(4.2) After the task lists are initialized, the scheduler loop begins and calls upon the policy to select a task to run. The policy can also decide to not select any task. If a task is selected, the policy is allowed to specify an optional start time delay. If a task is requested to be run immediately, the task is executed. After execution, if the deadline is still ahead of the current time, that is (di− t) mod w < Ti, then the task is moved to the sleeping list, because it is not yet time for the

next window of execution. Otherwise, the task remains in the awake list and the awake list is resorted. A task will remain in the awake list when the deadline is missed. In either case, the deadline for the task is updated afterwards using (4.3).

di⇐ (di+ Ti) mod w (4.3)

After the deadline is updated, the task manager updates the latest possible start times starting with the task with the latest deadline to the task with the earliest deadline using (4.2).

If the policy requested a delayed start time for the selected task, then the delayed start time is saved. Now, the scheduler moves on to the next event of interest. The time until the next event of interest must be calculated. The events of interest are the next arrival time (the earliest start of a new execution window) for the tasks in the sleeping list, the deadline of the task with the earliest deadline, and the start time of the delayed task, if there is one.

The next arrival time is calculated by (4.4). NS are the number of tasks in the sleeping list.

anext= min{(di− Ti) mod w : i = 1, ..., NS} (4.4)

The time until the next event of interest is then calculated by (4.5). dnext is the deadline of the

first task in the awake list. sdelayed is the execution start time of the delayed task, if there is one.

During this time, it is recommended to put the device in a low-power state. In any case, nothing should be done in this time period.

∆ t = min

x∈E ((x − t) mod w) where E = {anext, dnext, sdelayed} (4.5)

After the time until the next event has passed, if there is a requested task with a delayed start time and it is time to execute the task, the task is executed. Afterwards, the task manager updates the task’s deadline and puts the task in the appropriate list. Whether a delayed task is executed or not, maintainance on the task lists must be performed. Due to task skipping, it is possible for tasks in the awake list to have an expired deadline. A task has an expired deadline when (di− t) mod w ≥ Ti. The task manager updates the deadlines for all of the tasks with expired

deadlines in the awake list using (4.3).

Tasks in the sleeping list must be woken up when the current time passes the beginning of the execution window. That is when (di− t) mod w < Ti. The task manager moves all of the tasks

that satisfy this condition from the sleeping list to the awake list. 26

(27)

If any deadline was updated or any task was moved to the awake list from the sleeping list, then the task manager resorts the awake list and updates the latest possible start times. Any requested task with a delayed start time that has not yet executed becomes unselected so that the policy can make a new decision based off of the new task set.

After maintainance is performed on the task lists or the delayed task is executed, the scheduler loop starts again and the policy is called to select the next task.

4.2.2 Expanded Task Model

The task management for the expanded scheme works mostly the same as the simple scheme, except no latest starting time is maintained. Because it is not known which versions of each task will be selected, it is not trivial to keep track of the latest starting time for each task. Each task version has its own execution duration and current draw.

The task structure for the expanded scheme is shown in Table 4.2.

Ti task period (ms)

ri reward or ratio

V V

Vi task versions

ni number of task versions

di task deadline

VVVi∈

(

Dj task execution duration (ms)

Ij task electrical current draw (mA)* *or optionally the power draw (mW)

Table 4.2. Energy-Aware Task Scheduler (EATS) Task Characterization (Expanded)

4.3 Energy Predictor Implementation

As presented in the background information in chapter 3 and reprinted below, integration of the power function yields the energy within the time window and integration of the function of elec-trical current yields the elecelec-trical charge.

E= Z t2 t1 P(t)dt (3.3) Q= Z t2 t1 I(t)dt (3.4)

Predictions obtained via regression methods are more readily integrated through the indefinite integral of the regression function. Linear regression has the best performance of the prediction methods presented by Lu et al. [8]. For this reason and the ability to integrate the regression function, linear regression is one of the methods used here for energy prediction. This is a simple form of regression.

This provides a good starting point, but through artificial neural networks it is possible to provide improved regression with greater detail. Artificial neural networks are general function approximators. Through clever choice of the activation function, regression using an ANN can closely approximate a function with complex and non-linear elements. ANNs are also config-urable and by varying the number of hidden nodes within the network, the amount of detail in the output function can be tuned as required for the energy harvester used. ANNs provide a con-figurable means of regression that surpasses the level of detail granted by linear regression while sacrificing simplicity.

(28)

Because fractional numbers are required for regression methods, a suitable data type that grants fractional numbers must be used for calculations within the predictor. The following subsection presents fixed-point numbers as they are used within the implementation for the thesis project as a data type that satisfies this requirement.

4.3.1 Fixed-Point Math

In many embedded system devices, floating-point computation is very costly due to the lack of dedicated floating-point hardware. On these devices, floating-point arithmetic is commonly im-plemented in software where the operations take significantly more time to execute than the equiv-alent integer operation. Scheduling should be as lightweight as possible and leave as much time and energy as possible to the tasks that the scheduler manages. To achieve this, the energy pre-dictors use fixed-point saturating math. Fixed-point math is performed using integer operations and takes less time to execute in devices that lack a floating-point unit. Saturating math causes re-turned values to be pinned to the maximum representable value when the result is greater than the maximum and pinned to the minimum value when the result is less than the minimum. Saturating math is used to keep returned values as close as possible to the real value on overflow.

The fixed-point numbers are held in 32-bit signed integer variables. In adding or subtracting fixed-point numbers, there are two cases. If the addition or subtraction would result in overflow, then the maximum or minimum 32-bit integer value is returned. Otherwise, the operation is performed by simply using the equivalent integer operation.

Multiplication and division of fixed-point numbers requires a little bit more consideration. Fixed-point numbers use some of the bits in the value to store the fractional part of the fixed-point number and the rest of the bits to store the integer part. Multiplication of a number with m fractional bits with another number with n fractional bits results in a number with m + n fractional bits, when only the integer multiplication is used. Division of a number with n fractional bits by a number with m fractional bits results in a value with n − m fractional bits, when only the integer division is used. For the energy predictor, it is desirable to have the same number of fractional bits for the inputs and output for every operation, so that the value stays within the bounds of a 32-bit number and that the number of fractional bits can be implied rather than stored explicitly for each value.

For the 32-bit fixed-point multiplication, the product of the integer multiplication is allowed to overflow into a 64-bit number to avoid an overflow. The resulting value is a fixed-point value with 2n fractional bits, where both inputs have n fractional bits. To generate an output that also has n fractional bits, the value must be divided by 2n to remove n fractional bits. This will result in some underflow, so half of the divisor 2nis added to the value before dividing, so that rounding is performed. If the result is higher than the maximum 32-bit value or lower than the minimum 32-bit value, then the number is saturated. Otherwise, the value is returned as a 32-bit value.

32-bit fixed-point division is handled in a similar fashion to multiplication. The order of op-erations is the same. The difference is that the multiplier for the multiplication step is 2n to add nfractional bits to the dividend to compensate for the bits that would have been lost in the divi-sion. The result is then divided by the divisor of the 32-bit fixed-point division operation after adding half of the divisor, so that rounding in performed. The same checks for saturation are also performed afterward.

(29)

4.3.2 Energy Predictor Interface

To provide a modular system, a common interface is declared for the energy predictors. The interface is the following:

• add_observation(x, y) • by= predict(x)

• Y = integrate_predictions(a, b)

The add_observation(x, y) function takes in a time value x and the sampled harvest current or power in y and updates the predictor state using the data point. predict(x) takes a time value and returns the predicted harvest value for that time. integrate_predictions(a, b) integrates under the curve of predictions from time a to time b. This gives the predicted amount of charge or energy that will be harvested within the given time period.

4.3.3 Simple Linear Regression

Simple linear regression simply fits a line to the last n samples as shown in Figure 4.3. The number of samples are configurable. The samples are stored in the predictor’s memory.

Figure 4.3.Simple Linear Regression

The implementation of add_observation(x, y) for simple linear regression replaces the oldest entry in the sample array with the new entry and recalculates bb0and bb1. The calculation of these two values is in (3.7) from Section 3.3 and is repeated below. The sample array is initialized with zeros.

b b1=∑

n

i=1(xxxi− xxx) ∑ni=1(yyyi− yyy)

∑ni=1(xxxi− xxx)2

b

b0= yyy − bb1xxx

(3.6)

Calculation of the prediction value is shown in (3.6) from Section 3.3 and is repeated below. b

y= bb0+ bb1z (3.7) To integrate beneath the line and obtain the charge or energy that will be harvested within the given time period, (4.6) is used.

Y =  b b1· (b − a) 2 + bb0  · (b − a) (4.6)

(30)

4.3.4 Single-Input Single-Output Artificial Neural Network (SISO ANN)

To design an artificial neural network predictor that can predict current values given a time value, the neural network only needs a single input and a single output. The input is time and the output is the current value at that time. Supervised learning using regression is then used to incrementally modify the weight values to approximate the curve based on the sampled harvest current values. The weights are initialized with random values with Gaussian distribution with a mean of 1 and variance equal to 2 divided by the number of nodes in the previous layer. The variance is chosen to make the output of the neurons initially have approximately the same output variance to increase the rate of convergence in the network [3].

Because regression is being used, the output node has a linear activation function where the sum of the products of the inputs to the node and their respective weights is passed as the output unaltered. To keep the neural network simple to implement and to keep the memory requirement for the neural network low, only one hidden layer is used. The number of nodes in the hidden layer is customizable. The equation that shows how the weights map the input to the output would then be (4.7). N is the number of nodes in the hidden layer. Ai is the weight for the path between the

hidden node i and the output node. ωi is the weight for the path between the input and hidden

node i. σ (z) is the activation function of the hidden nodes.

b y= h(x) = N

i=1 Ai· σ (ωix) (4.7)

It becomes evident that the activation function can only be dilated and contracted horizontally and vertically. The quality of the regression can be greatly improved by allowing some form of translation of the activation function to expand the scope of the functions that can be represented. To allow horizontal translation of the activation function, a bias node is introduced. The bias node is a special additional input node where the input value is always 1. Figure 4.4 shows the final neural network structure after adding the bias node.

Figure 4.4.Single-Input Single-Output Artificial Neural Network (SISO ANN)

After adding the bias node, the prediction equation becomes (4.8). ϕiis the weight for the path

between the bias node and the hidden node i.

b y= h(x) = N

i=1 Ai· σ (ωix + ϕi) (4.8) 30

(31)

Considering the prediction equation in (4.8), the integration by substitution method (also known as u-substitution) is used to integrate underneath the prediction curve. The result is (4.9).

Y = N

i=1  Ai ωi · Z ωib+ϕi ωia+ϕi σ (z)dz  (4.9) To adjust the weights in the add_observation(x, y) implementation, the current prediction and the observed value are used to calculate the gradient for each weight. The equations to calculate the gradients are given in (4.10). σ0(z) is the derivative of the activation function for the hidden nodes. ∇AiJ(Ai) = (h(x) − y) · (ωi x + ϕi) ∇ωiJ(ωi) = (h(x) − y) · Ai· σ 0 (ωi x + ϕi) · x ∇ϕiJ(ϕi) = (h(x) − y) · Ai· σ 0 (ωi x + ϕi) (4.10)

The weights are then updated using the Nesterov update rule in (3.13) and (3.14) from Section 3.4.2. The equations are repeated below. A separate v value is maintained for each weight.

vt = γvt−1− η · ∇θJ(θ ) (3.13)

θ = θ + vt+ γ · (vt− vt−1) (3.14)

Activation Functions

For the hidden nodes, an activation function that can be easily integrated and differentiated must be chosen. The function must not be too computationally expensive, but also provide a regression curve that closely matches the real curve. The function should also not have large regions where the derivative of the function is very small or zero, because learning will be slow or non-existent in these regions. The options available for the activation function in the implementation for this project are the Rectified Linear Unit (ReLU), the Leaky Rectified Linear Unit (Leaky ReLU), and an approximation of the sine function.

The Rectified Linear Unit (ReLU), shown in Figure 4.5, is a popular choice for an activation function [4]. Its piecewise nature introduces non-linearity into the network and the function is very easy to compute. It tends to accelerate the convergence of stochastic gradient descent compared to other activation functions, such as sigmoid and tanh, because the gradient does not taper off into small values in the positive portion of the curve. Neural network nodes that use ReLU activation can “die” though if the learning rate is too high, due to the zero gradient in the negative portion of its curve. Updates can no longer be performed if the gradient is zero.

(32)

Figure 4.5. Rectified Linear Unit (ReLU)

The ReLU definition is shown in (4.11).

σ (z) = relu(z) = max(0, z) σ0(z) = ( 1, if z ≥ 0 0, otherwise Z b a σ (z)dz =(σ (a) + σ (b)) 2 · (σ (b) − σ (a)) (4.11)

Leaky ReLUs, shown in Figure 4.6, are an attempt to remedy the zero gradient problem in ReLU. A small slope of 0.01 is given to the negative part of the curve. It is just enough to allow movement in that part of the curve while maintaining the non-linearity.

Figure 4.6.Leaky Rectified Linear Unit (LReLU)

(33)

The Leaky ReLU definition is shown in (4.12). σ (z) = ( z, if z ≥ 0 0.01z, otherwise σ0(z) = ( 1, if z ≥ 0 0.01, otherwise Z b a σ (z)dz =(relu(a) + relu(b)) 2 · (relu(b) − relu(a)) + (−relu(−a) − relu(−b)) 2 · (relu(−a) − relu(−b)) · 0.01 (4.12)

Neural networks are commonly initialized with small random values, so that state for each weight is different. While this prevents the weights from generating the same updates, it means that the system will take some time to converge after initialization, because no information about the data is known when the system first starts. By looking at the equation for the output of the neural network in (4.8), it is easy to see that it resembles the equation for a sum of sines with amplitudes AAA, angular frequencies ωωω , and phases ϕϕϕ . If the activation function is chosen to be the sine function then it is possible to perform weight initialization using the output of the fast Fourier transform (FFT) on training data.

The amplitudes, angular frequencies, and phases are calculated using the equations in (4.13). Since the imaginary components of the input samples are all zero, the second half of the output mirrors the first half and so it must be discarded. Gashler and Ashmore [7] have also used sine as an activation function and weight initialization by FFT with success when using a neural network to make time-series predictions. In their research they used deep ANNs with many hidden node layers, but due to the high memory demand and long computation times associated with deep ANNs, deep ANNs are not used here. In the implementation described here, the weights are initialized with the top n magnitudes in the FFT result, where n is the number of hidden nodes, along with their associated frequency and phase.

N= number of samples ccc= FFT output array of size N fs= sample rate in Hz i∈ {1, ...,N 2 + 1} A A Ai=    ccci N , if i = 1 2 ccci N , otherwise ϕϕϕi= angle ccci N  +π 2 = atan2(imag ccci N  , real ccci N  ) +π 2 ω ωωi= 2π · fs N · (i − 1) (4.13)

(34)

The sine function can be difficult to compute efficiently on embedded devices, so a good ap-proximation must be used. Bhaskara I’s apap-proximation of the sine function is used here as an option for the activation function for the hidden nodes. Its implementation is shown in (4.14).

x0= x mod π y∗= 16x 0(π − x0) 5π2− 4x0(π − x0) bhaskara_sin(x) = ( y∗, if (x mod 2π) ≤ π −y∗, otherwise bhaskara_cos(x) = bhaskara_sin(x + π/2) (4.14)

It then follows that the activation function is as in (4.15). σ (z) = bhaskara_sin(z) σ0(z) = bhaskara_cos(z) Z b a σ (z)dz = −bhaskara_cos(b) + bhaskara_cos(a) (4.15) 34

(35)

4.3.5 Helper Functions

To ease the task of energy prediction, it is assumed that the energy or charge curve does not change drastically from day to day. Results from the previous days are used to make predictions for the current day by wrapping updates over the period of a day in the predictor. This is facilitated by the following functions:

• x0= get_wrapping_x(x)

• wrapping_add_observation(x, y) • by= wrapping_predict(x)

• Y = wrapping_integrate_predictions(a, b)

The actual wrapping of time values is performed by get_wrapping_x(x) as shown in (4.16). 86, 400, 000 is the number of milliseconds in a day.

get_wrapping_x(x) = x mod 86, 400, 000 (4.16) wrapping_add_observation(x, y) is then implemented by using get_wrapping_x(x) to get the new x-value to pass to the normal add_observation(x, y) function.

wrapping_predict(x) is also implemented by using get_wrapping_x(x) to get the new x-value to pass to the normal predict(x) function.

wrapping_integrate_predictions(a, b)works by wrapping integrations, so that integrations are only performed over the window of a day. Every time the end of the day is reached, the integration is continued in a piecewise fashion from the beginning of the day window.

4.4 Policies

Policies use information about the current state of the system to decide which task should be executed. Policies may also specify an optional delayed start time for the selected task or not select a task at all. For the expanded task model, the task version must also be specified. The information provided to the policy is shown in Table 4.3.

A AA awake list S S S sleeping list t current time

Ccurr current battery level (mAh)*

Cmax battery capacity (mAh)*

p predictor

ynorm y-normalization value

hI last sampled harvest current

*If power is being used instead of current, the units for the values given here must be mWh.

Table 4.3. Energy-Aware Task Scheduler (EATS) Policy Input

4.4.1 Simple Task Model Policy

The policy for the simple task model is inspired by the basic idea behind the Lazy Scheduling Algorithms proposed by Moser et al. [9, 10] and discussed in Section 2.2.1. The Lazy Scheduling Algorithms modulate between an earliest deadline first (EDF) and an as late as possible (ALAP) scheme. When the device’s battery is full, there is nowhere to store any further harvested energy. In this case, the scheduler should execute tasks as soon as possible to avoid losing the incoming energy and maximize efficiency. At all other times, it is better to execute as late as possible to maximize the amount of energy that is harvested before executing the task. In the Lazy Scheduling Algorithms, the optimal starting time is determined by solving an equation numerically. Because

(36)

of the possible non-deterministic nature of solving an equation numerically, the policy here takes a simpler, more binary approach to determine when to run a task.

When the battery is full, this policy searches through the awake task list, starting with the task with the earliest deadline, to find the first task with an energy demand that is less than or equal to the predicted energy harvest within the duration of the task’s execution. If no task has an energy demand that is low enough, then the task with the smallest energy demand is executed. This attempts to execute a task for “free” by avoiding energy consumption from the battery, but if no task can be found that achieves this then, the consumption from the battery is minimized.

When the battery is not full, the task with the earliest deadline is simply executed at its lat-est possible starting time. With this policy, tasks are only executed immediately or at the latlat-est possible starting time. The tasks are never executed at any time in between, unlike in the Lazy Scheduling Algorithms. The policy algorithm is shown in Algorithm 4.1. Because values in the predictor are normalized within the window of a day, the units of the integration value is mA · day (or mW · day). This value is converted to mAh or mW h by multiplying the result by 24. Be-cause the task durations are stored as millisecond values, they must be divided by the number of milliseconds in an hour to get a comparable value with units mAh or mW h.

Algorithm 4.1 Policy for the simple task model

1: function SIMPLEPOLICY(AAA, SS,t,CS curr,Cmax, p, ynorm, hI) 2: requested_run_time ← t

3: if Ccurr= Cmaxthen

4: for i ∈ {1, . . . , length(AAA)} do

5: Hb← p.wrapping_integrate_predictions(t, t + AAAi.D) × 24 6: ereq← AAAi.I / ynorm× (AAAi.D / 3.6 × 106)

7: if ereq≤Hbthen

8: task← AAAi

9: break

10: else if i = 1 or ereq< ecreq then

11: ecreq← ereq 12: task← AAAi 13: end if 14: end for 15: else 16: task← AAA1 17: requested_run_time ← AAA1.s 18: end if

19: return {task, requested_run_time}

20: end function

4.4.2 Expanded Task Model Policy

For the expanded task model, the policy uses a reward system combined with weakly real-time behavior similar to the algorithms discussed in Section 2.2.2. This allows the scheduler to avoid depleting the battery entirely. This type of scheme is very useful in systems where writing to non-volatile memory is too costly or not possible. The data can remain in RAM and the scheduler increases the availability of the system.

The policy has two modes. There is a mode for when the harvest current is low and a mode for standard operation. Under the standard operation, the policy runs the task with the earliest deadline. For the task version, the policy searches through the versions from the most preferred version to the least preferred version to find the first version that has an energy demand that is less than or equal to the predicted energy harvest within the duration of the task’s execution. If no task version has an energy demand that is low enough, then the task version with the smallest energy 36

(37)

demand is executed. This scheme avoids consuming energy from the battery when there is harvest energy available and instead saves the battery for times of low harvest energy. For example, in the case of a solar panel as an energy harvester, the battery energy is saved for the nights.

In the low energy mode, an energy budget is calculated for the task with the earliest deadline by dividing the task’s reward value by the sum of all the reward values of both the sleeping and awake tasks. This ratio is then multiplied by the current battery level to get the energy allocation for the current task. The policy then calculates how many times the task version with the smallest energy demand can execute with the given energy allocation. If the number of possible executions is greater than the number of executions that the task is requested to execute in a day, then it is executed using the version with the smallest energy demand. Otherwise, the task is skipped. A day is chosen, because it is assumed that there will be more energy available within a day from any time point. Under this policy, tasks are always executed as soon as possible and are never delayed. The policy algorithm for the extended task model is shown in Algorithm 4.2. 8.64 × 107 is the number of milliseconds in a day.

Figure

Figure 4.1. Energy-Aware Task Scheduler (EATS) Design Overview
Table 4.1. Energy-Aware Task Scheduler (EATS) Task Characterization (Simple)
Figure 4.3. Simple Linear Regression
Figure 4.4. Single-Input Single-Output Artificial Neural Network (SISO ANN)
+7

References

Related documents

In this paper, I explore Jan Patoĉka’s enigmatic thesis that the the Platonic ideal of care for the soul.constitutes the essence of Europe According to Patoĉka’s reading,

Figure 1.5: Nested active objects would introduce races: the internal active object could access the enclosing active object’s mutable state as the state is its sibling through

This report has presented a model and prototype of a general task scheduler that can be used in many different distributed systems and can handle a variety of tasks.. In the beginning

[r]

In order to answer the research questions, the researchers reviewed extensive amount of literature in four categories. The first category is about the concept of conflict

In the previous section we constructed demand bound functions for mixed-criticality sporadic tasks, where the relative deadlines used by EDF may differ in low- and high-criticality

The four model architectures; Single-Task, Multi-Task, Cross-Stitched and the Shared-Private, first went through a hyper parameter tuning process using one of the two layer options

Grounded in research both on formative assessment but also on motivation in connection to foreign language learning, it is hypothesised that sharing the