# A Power Planning Algorithm Based on RPL for AMI Wireless Sensor Networks
## 4 DODAG-Oriented Power Planning Algorithm
A solution is considered suitable if the cardinality of the parent set returned by SelectParents is \(k\) or the node is connected directly to the sink. If this condition is not satisfied, the node may be included in the set **J** to \"jump\" to the next round. Nodes in **J** are re-evaluated after all nodes in **U** are tested. ```
SelectParents (\(j\)) /* \(j\)node in U */ begin \(S\leftarrow\varnothing\)forall the w in W do \(A\leftarrow\) nodes in C whose distance from \(j\leq d_{w}^{Q}\) \(B\leftarrow\) at most \(k\)-lowest cost\((j,i)\) nodes \(i\) in \(A\) \(C\leftarrow\) nodes \(i\) in \(B\) with the minimum rank\((j,i)\) \(S\gets S\cup\{(w,C)\}\)  end for \((w,C^{*})\leftarrow\) element \((w,C)\) in \(S\) with maximum cardinality of \(C\) and smallest \(w\) as tiebreaker return \(C^{*}\)  end for
```  
**Algorithm 3**Select the Parents to Connect a Node to the DODAG  
If a suitable solution is found, the transmission power of the node and of its parents is adjusted using SetPowers (Algorithm 4). This procedure adjusts the transmission power to assure that all links between the node and its parents have \(ETX\leq Q\). To calculate the value of \(w\) in step 3 of SetPowers, first it calculates the outage of the link \(\mathcal{O}\) assuming \(\mathcal{O}=\mathcal{O}_{ab}=\mathcal{O}_{ba}\) and \(ETX=Q\) in Equation (3). Then, it computes \(\overline{\gamma}\) from Equation (2). Finally, it computes \(w\) from \(\overline{\gamma}=\frac{M_{i}\cdot w}{N_{0}\cdot B}\). ```
SetPowers (\(j\), \(p\)); /* \(j\)node in U */ /* \(P\)set of nodes in C */ begin \(S\leftarrow\varnothing\);forall the i in P do \(w\leftarrow\) minimum power in W such that \(etx_{j,i}\leq Q\); \(S\gets S\cup\{i,w\}\);  end for forall the \(\{i,w\}\) in S do \(w_{i}\leftarrow\max{(w_{i},w)}\);  end for \(w_{j}\leftarrow\) maximum \(w\) among all \(\{i,w\}\) in \(S\);  end for
```  
**Algorithm 4**Adjust the Power of the Child and Parent Nodes  
The worst-case time complexity of BuildDODAG is polynomial, and given by \(T(N)=O(N^{2\alpha})\), where \(N\) is the total number of nodes. BuildDODAG must receive the parameter \(n\) as input, corresponding to the initial set of nodes to connect to the root. The value of \(n\) is strongly dependent on the topology. The evaluation of random topologies has indicated that the same value of \(n\) may be inappropriate for networks with the same density. For this reason, \(n\) is defined by an optimization procedure that maximizes the following objective function:  
\[\left\{\text{Floor}\left(\frac{AvgParentSet}{\theta}\right)*\theta,-AvgPower \right\}, \tag{6}\]  
where \(AvgParentSet\) is the average parent set size considering all nodes except the root, and \(AvgPower\) is the average transmission power assigned to all nodes. The \(\theta\) parameter is used to reduce the importance of small increments on the parent set size at the expense of an increase in the average power. The second element on the objective function is used only as a tiebreaker. This optimization problem is very simple because it is mono-variable and \(n\) is an integer.