# Enabling Autonomous Agents for Mobile Wireless Sensor Networks
## 5 Implementation
### Agents: Negotiation and Replacement  
Agents negotiation adds an intelligent negotiation protocol to maximize both coverage and lifetime by replacing robots (agents) that are about to be depleted and thus balancing network load. Each robot estimates the absolute minimum battery level it requires to reach a certain depot point safely (denoted as \(bl_{0}\)). To figure out how close robots are to reach that critical level, two additional heuristically chosen thresholds are introduced, \(bl_{h},bl_{1}\). In the equation below, \(d_{dp}\) represents the distance between the current robot position and the depot point,expressed in meters. \(K_{m}\) is a model of the current consumption due to motor movement, expressed in \(mA\times h/m\), as introduced in the previous subsection. \(B_{0}\) is the battery charge at simulation start expressed in \(mA\times h\) and \(B_{cp}\) represents the battery capacity in \(mA\times h\). \[bl_{0}=100\times(d_{dp}\times K_{m}+5\%\times B_{0})/B_{cp} \tag{4}\]  
\[bl_{h}=10\%\times bl_{0}+bl_{0} \tag{5}\]  
\[bl_{1}=30\%\times bl_{0}+bl_{0} \tag{6}\]  
The relation between \(b_{c}\) (or current battery level) and the above-mentioned parameters indicates a robot's urge to be replaced or, by contrast, its disposition to replace a robot in need. Those thresholds are combined in the _willingness to interact_ or \(w(t)\), as in Equation (7). \[w(t)= \begin{cases}-1,&ifb_{c}\leq bl_{0},\\ \frac{b_{c}-bl_{1}}{b_{c}},&ifb_{c}>bl_{0}\end{cases}  \tag{7}\]  
A negotiation round is triggered when an agent reaches a willingness value of \(-1\), that is, critically close to depletion. This is accomplished by sending a message through the network asking for help (Figure 3a). Robots with positive willingness answer to this request by replying back with their disposition to help (Figure 3b). Other agents with negative willingness, but not higher than 20% on top of \(w_{H}\), stated as \(w_{H20}\), also send their request to be replaced. In this way, not only critical robots are replaced, but also a fraction of them that are becoming close to that level. \[w_{H}=\frac{bl_{h}-bl_{1}}{bl_{h}} \tag{8}\]  
\[bl_{h20}=bl_{h}+0.2\times(bl_{1}-bl_{h}) \tag{9}\]  
\[w_{H20}=\frac{bl_{h20}-bl_{1}}{bl_{h20}} \tag{10}\]  
Agents inclined to help others provide their disposition by sending a combination of their willingness and the utility parameter \(u(t)\) to form \(W(t)\) (Figure 3b). The utility is the mathematical model showing the trade-off between current position and moving to a new one. In \(u(t)\), \(b_{m}\) represents the battery expenditure to move to the new position and \(b_{n}\) is the battery level from the new position to the depot point; similarly, \(b_{p}\) is the battery level required to move from the current point to the depot point. Traffic routing is also considered, where \(p_{A}\) reflects the battery spent on forwarding at the current position and \(p_{B}\) is the battery that is necessary to spend on the updated point. From a physical perspective, the term \(\frac{p_{B}-p_{A}}{p_{B}+p_{A}}\) enables the utility parameter to increase as the difference between the packets routed at the new position is greater than the routed packets at the current position. Similarly, \(\frac{b_{m}+b_{n}-b_{p}}{b_{c}}\) ensures that agents farther away from the depot point are also participating in the negotiations as long as their new location is closer to the depot point than their current location. Thus, agents only provide a \(W(t)\) greater than zero when they only have enough battery to move and operate in the new position, so that movement favors locations with higher traffic demands. In case of competing constraints after computing \(W(t)\), the assignment is determined by the chosen algorithm. More details about the assignment algorithms are given in Section 5.5. \[u(t)= \begin{cases}1-\dfrac{b_{m}+b_{n}}{b_{c}}+\dfrac{b_{p}}{b_{c}}+\dfrac{p_{B}-p_{ A}}{p_{B}+p_{A}},&if\,1-\dfrac{b_{m}+b_{n}-b_{p}}{b_{c}}>\,=\,0.3,\\ -w(t),&otherwise\end{cases}