# Centralized Energy Prediction in Wireless Sensor Networks
## 4 Centralized Energy Consumption Prediction  
According to the literature review, previous works implementing centralized energy consumption prediction in WSN maintain the prediction processing overhead on the sensor nodes. The WSN nodes monitor their behavior, execute the prediction algorithm, estimate an energy consumption rate, and send this information to a sink or controller. We propose to move most of the processing required for the prediction to the SDN controller, assuming that the SDN controller does not have energy, processing power, or memory space constraints. In this manner, the WSN nodes monitor their behavior and the controller executes the prediction algorithm.  
In detail, the WSN nodes execute a training period to calculate their probability matrix. Each node has to register all the transitions and order them in a matrix, just as explained in Section 3.3. When the training process ends, the node calculates the probability matrix and the energy spent up to this moment; then, it sends this information to the SDN controller. The SDN controller creates an energy profile for each node. This profile contains the probability matrix and an energy consumption time series, constructed from the information sent by the WSN nodes and the prediction results.  
The SDN controller executes the prediction algorithm (Algorithm 1) to calculate the energy consumption for the next \(n\) steps. Then, the SDN controller informs the prediction value obtained to the respective WSN node. In this manner, after \(n\) steps the node can calculate the prediction error by comparing the prediction and the energy consumption obtained through the energy model. If the error is above a threshold previously defined, the WSN node initiates a new training period to update the energy information in the energy profile in the SDN controller. On the other hand, if the error is below the threshold, the SDN controller continues the time series construction using the value predicted.  
\begin{table}
\begin{tabular}{c c c c} \hline \hline \multirow{2}{*}{**State**} & \multicolumn{3}{c}{**Domain**} \\ \cline{2-4}  & **Sensing** & **Processing** & **Communication** \\ \hline Sensing & active & active & sleeping \\ \hline Processing & sleeping & active & sleeping \\ \hline LPM1 & sleeping & LPM1 & sleeping \\ \hline Transmitting & sleeping & active & transmitting \\ \hline Receiving & sleeping & LPM1 & receiving \\ \hline \hline \end{tabular}
\end{table}
Table 1: Set of states of the Markov chain.  
```
1:\(n\): number of steps
2:prediction: energy consumption prediction for \(n\) steps
3:error: prediction error
4:threshold: maximum error accepted
5:p_matrix: transitions matrix
6:s_consumption(\(n\)): energy consumption prediction during \(n\) steps for states
7:t_consumption(\(n\)): energy consumption prediction during \(n\) steps for transitions
8:
9:Running on WSN nodes
10: launch the training
11: wait the training period ends
12: calculate p_matrix
13: send p_matrix to the controller
14:while true do
15: wait \(n\) steps
16: calculate energy consumption
17: calculate error(energy consumption, last prediction)
18:if\(error>threshold\)then
19: obtain a new p_matrix
20: send new p_matrix to controller
21:endif
22:endwhile
23:
24:Running on the SDN controller:
25:wait for WSN information
26:functionprediction(p_matrix,\(n\))
27: calculate s_consumption(\(n\)) and t_consumption(\(n\))
28: prediction=s_consumption+t_consumption
29: update time series
30: send prediction to sensor nodes
31:endfunction
```  
**Algorithm 1** Markov chain running on the controller