# A Biologically Inspired Self-Organizing Underwater Sensor Network
## 4 Ecological Niche-Inspired Slot Distributor
### Slot Distributor  
An ecological niche describes the ecological role of specie in the ecosystem. Species have different preferences for different resources such as food. Different species living in the same region usually occupy different ecological niches or show different preferences for similar resources. Otherwise, conflicts occur and war among species happens until a winner is generated. This phenomenon inspires us to treat nodes as species and communication slots as resources. Thus, the preferences of nodes for slots can be used to mimic the ecological niche of species. If nodes show different preferences for slots, they are encouraged to occupy slots with different probabilities. As a result, nodes try to occupy more slots, while the chance of collision can be decreased. If the preference is scattered in a way that can guide nodes to occupy slots evenly scattered in the frame, the expected wait can be decreased. With an ecological niche, nodes are encouraged to occupy slots, which introduces collisions into the sensor network. Even though the chance of a collision can be decreased, it cannot be eliminated with an ecological niche. Therefore, we introduce the phenomenon of pheromone markers. In the wild, animals deploy pheromone markers to transmit messages. For example, ants use pheromone clues to tell their mates the path to food, and wolves use urine to mark the ownership of territory. Wounded fish can use a pheromone to mark the existence of danger in the environment, and others avoid visiting these marked territories. Similarly, if a node fails to occupy a slot, it can deploy a pheromone there. Once the density of the pheromone reaches a threshold, the node stops attempting to occupy the slot. As a result, no more communication collisions are introduced into the sensor network. Following the above ideas, we propose the ecologically inspired slot distributor shown in Algorithm 1. ```
1:Set initial values: \(L_{f}\leftarrow[0,0,\ldots,0]\), \(L_{f}(id_{self})\gets 1\), \(L_{pheromone}\leftarrow[0,0,\ldots,0]\), \(S_{tryOccup}\leftarrow\phi\)
2:Wait for several frames to obtain set \(S_{12\text{-}neighbor}\) containing IDs of all first and second-order neighbors
3:Calculate ecological preference of self \(p_{self}\) and all neighbors \(p(i),i\in S_{12\text{-}neighbor}\),
4:\(L_{f}(i)\gets 1\) if \(p_{self}>p(i),\forall i\in S_{12\text{-}neighbor}\). 5:whileNew slot with address=\(id_{slot}\)do
6:if\(id_{slot}\in S_{tryOccupy}\)then
7: delete \(id_{slot}\) from \(S_{tryOccupy}\)
8:if\(L_{f}(id_{slot})=1\)then
9: keep occupying this slot by setting \(L_{pheromone}(id_{slot})\gets 0\)
10:else
11: Increase pheromone density by \(L_{pheromone}(id_{slot})\). 12:endif
13:endif
14:if\(L_{f}(id_{slot})=1\wedge L_{pheromone}(id_{slot})<k_{threshold}\wedge random(p_{ self})<k_{occupy}\)then
15: Try to occupy this slot: \(S_{tryOccupy}\gets S_{tryOccupy}\cup id_{slot}\), \(L_{f}(id_{slot})\gets 1\)
16:endif
17:endwhile
```  
**Algorithm 1** Slot distributor  
As shown in Algorithm 1, initially, all nodes only occupy their identical slots. List \(L_{pheromone}\) shows the pheromone density in each slot. Once a robot fails to occupy a slot, it deploys a pheromone there. Once the density reaches a threshold, the robot stops attempting to occupy the slot again. Initially, the pheromone density in each slot is set to a small value, indicating they can all be occupied, as in Line 1. Then, in Line 2, for the first several frames, nodes do not try to occupy slots other than their identical slots. In this stage, no collision exists in the network. When a node receives a message packet, the node can obtain the ID of the sender and the first-order neighbors of the sender from the message packet. Apparently, the sender is a first-order neighbor of the node, and the sender'sfirst-order neighbors may be the node's first-order neighbors or second-order neighbors. Thus after the first several frames, all nodes can obtain a set \(S_{12\text{-}neighbor}\) containing all their first- and second-order neighbors.