# A Lifetime Maximization Relay Selection Scheme in Wireless Body Area Networks
## 5 Heuristic Iterative Solution  
The fundamental thought of our solution is to iteratively increase the minimum lifetime in the network through adjusting the value of relay selection binary variables in the matrix \(\mathbf{X}\) trying to enhance the network lifetime calculated in Equation (7). The detailed procedure is presented in Algorithm 1. The main steps of the solution in one iteration can be summarized into three major steps:  
1. Find the sensor node with the minimum lifetime as the target node to improve its lifetime. 2. Make relay selection action to improve target node's lifetime according to the condition of the node. 3. Compare the improved minimum lifetime with previous values and decide whether the iteration should be ended or not. After the iteration has been terminated, the values of each binary variable in the relay selection matrix \(\mathbf{X}\) represent the final relay selection results for the networks. More specifically, the initialization is to set each node in the WBAN using direct transmission mode and \(Flag=0\), which represents the indicator to terminate the algorithm when it is set to 1. In addition, each node's lifetime will be calculated for the first time under strategy of all nodes using direct transmission (Line 2-4). Then, the iterative process will begin (Line 6-37): first of all, the target node will be found based on the the value of lifetime (Line 7). Next, different action will be taken depending on whether the target node is a relay node or not, which can be listed as follows:  
1. If the target node is a relay node and does not help any other node in cooperative transmission, the algorithm will be terminated due to the fact that the relay node can not use cooperative transmission to save energy. Therefore, there is no possibility for target node to improve its lifetime as well as the whole network (Lines 9-10). 2. If the target node is a relay node and helps some other nodes in cooperative transmission as a relay node, the node with the longest lifetime in the target node's relayed node list will be removed and set to use direct transmission to reduce the burden of the target node and consequently to increase the lifetime of it (Lines 11-14). 3. If the target node is not a relay node and uses cooperative transmission with the help of a relay node, there will be two conditions: 1. If the relay node of the target node is the nearest relay node from the target node, the algorithm will be terminated because it is not possible to select another relay node that can reduce the transmission costs of the target node (Lines 18-19). If the relay node of the target node is not the nearest relay node, the nearest relay node will replace current relay node as the target node's new relay node (Lines 20-23). 4. If the target node is not a relay node and uses direct transmission, the relay node with the longest lifetime will be the new relay node of the target node (Lines 24-27). ```
1: Assume that there are \(l\) sensor nodes in total in the network including \(m\) relay nodes. 2: Initialization: Node ID from 1 to \(l\), wherein 1 to \(m\) to relay nodes, \(x_{i,i}=1,x_{i,j}=0\), \(\forall i,j=1,2,\cdots,l,i\
eq j\), and \(Flag=0\). 3:for each \(i\in[1,l]\)do
4: Calculate lifetime \(L_{i}\), using Equations (3)-(6). 5:endfor
6:while\(Flag=0\)do
7:\(n=\underset{i\in[1,l]}{arg\ min_{i}(L_{i})}\)
8:if\(n\leq m\)then
9:if\(R_{n}==0\)then
10: Set \(Flag=1\). 11:else
12:\(k=\underset{i\in[1,l],i\
eq n}{arg\ max_{i}(L_{i})}\)
13: Set \(x_{k,k}=1,x_{k,i}=0,\forall i\in[1,l],i\
eq k\). 14:endif
15:else
16:if\(x_{n,n}\) == 0 then
17: Assuming Node ID \(r\) is the target node's relay node,
18:if\(r==\underset{i\in[1,l],i\
eq n}{arg\ min_{i}(d_{n,i})}\),then
19: set \(Flag=1\). 20:else
21:\(k=\underset{i\in[1,l],i\
eq n}{arg\ min_{i}(d_{n,i})}\). 22: set \(x_{n,k}=1,x_{n,i}=0,\forall i\in[1,l],i\
eq k\). 23:endif
24:else
25:\(k=\underset{i\in[1,m]}{arg\ max_{i}(L_{i})}\)
26: set \(x_{n,k}=1,x_{n,i}=0,\forall i\in[1,l],i\
eq k\).