# Energy-Saving Traffic Scheduling in Hybrid Software Defined Wireless Reschargeable Sensor Networks
## 5 Energy-Aware Traffic Scheduling Algorithm
In order to take the energy consumed by wireless chargers into account, the actual transmitting energy \(E_{c-Tuv}\) should be calculated with the formula below:  
\[E_{c-Tuv}=A\times(E_{Tuv}\times d_{cv}^{2}+E_{Ruv}\times d_{cv}^{2})\quad c,c^ {\prime}\in C. \tag{20}\]  
```
1:The set of nodes \(V\), the set of wireless chargers \(C\), the set of flows \(F\)
2:The set of nodes in working state \(A\)
3:Initialize: \(A\leftarrow\emptyset\);
4:Find the MTE routes for each \(f\in F\);
5:for each \(f\in F\)do
6:if\(f\) is an SDN flow then
7:\(A\gets A+n\), where \(n\) is the nodes on the MTE route from the source node to the first SDN node in flow \(f\);
8:Run optional paths search function to find the optional paths \(P_{f}\) for flow \(f\);
9:else
10:\(A\gets A+n\), where \(n\) is the nodes on the MTE route in flow \(f\);
11:\(F\gets F-f\);
12:endif
13:endfor
14:while\(F\
eq\emptyset\)do
15:for each \(f\in F\)do
16:if\(|P_{f}|=1\)then
17:\(A\gets A+n\), where n is the nodes on the unique optional route of flow \(f\);
18:\(F\gets F-f\);
19:endif
20:endfor
21:Run optional path selection function to find an optional path \(P_{f}\) for the corresponding flow \(f\);
22:\(A\gets A+n\), where n is the nodes on \(P_{f}\);
23: Update the size of flow \(f\);
24:if the size of flow \(f\) equals to 0 then
25:\(F\gets F-f\);
26:endif
27:endwhile
28:return\(A\). ```  
**Algorithm 1** Energy-Saving Traffic SchedulingWhen the amount of SDN nodes increases, there may be multiple SDN nodes in an optional path. If we search the optional paths on every SDN node in the path, it will be considerably complicated to find all the optional paths with a high computational complexity. Consequently, to simplify the calculation, except the first SDN node, the rest SDN nodes on this route do not try to optimize the route again. Therefore, the optional paths for an SDN flow is the acyclic MTE routes from the direct neighbours of the first SDN node to the destination. ```
0: A flow \(f\), the first SDN node \(s\) on the MTE route of \(f\), the set of neighbours of \(s\)\(V_{s}\)
0: The set of optional paths \(P_{f}\)
1:for each \(v\in V_{s}\)do
2: Find the MTE route \(p_{v}\) from node \(v\) to the destination of flow \(f\) with the actual energy consumption calculated by formula (20);
3:\(p_{v}=p_{v}+s\);
4: Delete the loop in \(p_{v}\);
5:\(P_{f}\gets P_{f}+p_{v}\);
6:endfor
7:return\(P_{f}\). ```  
**Algorithm 2** Optional Paths Search Function  
**Definition 1** (Optional paths). : _The optional paths are specifically defined for an SDN flow. When an SDN flow arrives the first SDN node on its MTE route, the optional paths are all the possible routes from this SDN node to the sink node. The next hop of an SDN node on the optional path can be any neighbour of it, and the next hop of a normal node is chosen according to MTE._  
Each SDN flow has one or more optional paths. For the SDN flows with only one optional path, ETS can directly allocate these flows on their unique optional paths. The nodes on the optional paths should also be in a working state. For the SDN flows with multiple optional paths, Algorithm 3 is designed for them to select an optimal path. Some related parameters are as follows:  
* the amount of nodes that are currently in sleeping state in a possibly used optional path. \(r\): the largest amount of flows that may go through the node. If a node is on the optional path for a flow, then \(r=r+1\). \(\overline{r}\): the average amount of flows that may go through the nodes on the optional path, and \(\overline{r}=\frac{(\sum r)}{n}\), where \(n\) is the number of nodes on the optional path. \(cap\): the channel capacity for an optional path can be used, which is equal to the minimum channel capacity of the channels on the path. _restsize:_ the rest channel capacity for an optional path can be used after allocating the flow. \(restsize=cap-flowsize\). When \(restsize\geq 0\), it means that the corresponding flow can be totally allocated on this path; otherwise, it means that only a part of the flow can be allocated on this path.