# Realistic Multi-Agent Formation Using Discretionary Group Behavior (DGB)
## 3 Algorithm: Discretionary Group Behavior
### Confronting with Obstacles  
The situation in which the measured density expresses the existence of obstacles, makes the agent find the nearest neighbor that is closer to the leader (we will discuss further in more details) and then, changes its direction toward one unit vector of the reverse velocity behind the subleader (Figure 5). The reason for doing this is that it leads the agent in an appropriate distance from its subleader and makes a narrow formation for the group when it is necessary for being able to pass through narrow spaces easier and it can help agents to find the path through tight and challenging obstacles. For agents to find their direction toward the subleader, we simply rotate the relative position between the agent and the subleader by 45 degrees clockwise and vice versa. Except for the calculation of relative position (\(RP=pos_{subleader}-pos_{agent}\)), all other steps are similar to Equation (3)-(6).  
The algorithm that finds the best neighbor as the subleader for each agent is shown in Algorithm 1. \(d_{max}\) (see Section 4 for more details) which is used in Line 9 for deciding whether to skip that neighbor or keep it as one of the subleader candidates, is remarkable. It's a factor that helps to determine the maximum distance a good subleader can have from the main leader based on the distance of the agent from the leader. For understanding the reason, let's consider a situation when the agent's nearest neighbor has more or equal distance from the leader than the agent itself. So, it is obvious that this neighbor cannot be a good choice for being the subleader. In our experiment, \(d_{max}\) makes the agents choose a better neighbor. The function used in Line 1 which is called obstacleDensity, is related to the part discussed in Section 3.3. It takes the position of the agent and its unit velocity to compute the density of obstacles around that area. The purpose of Line 18 and 19 is that if no subleader is found, the agent chooses the main leader as its subleader. It happens for members in the first line of the formation.  
Figure 5: Producing additional half-planes in the direction of the subleader. \(V_{S}^{\prime}\) is the reverse unit velocity of the subleader.  
```
Input: Agent's Position: agentPos, Agent's Unit Velocity: agentUnitVel, Leader's Position: leaderPos, Leader's ID: leaderID Output: Subleader's ID: subLeaderID ifobstacleDensity(agentPos,agentUnitVel)then  lowestDistance \(\leftarrow\) Infinity;  SubLeaderID \(\leftarrow\) -1; forall (a such that a \(\in agentNeighbors\))do if\(a\
eq agentID\)then  dNA \(\leftarrow\) squared length of (\(neighborPos-agentPos\));  dLA \(\leftarrow\) squared length of (\(leaderPos-agentPos\));  dLN \(\leftarrow\) squared length of (\(leaderPos-neighborPos\)); if\(dLA<d_{m}ax*dLN\)then  Skip this neighbor and go to the next neighbor in the loop;  end if if\(dNA<lowestDistance\)then  lowestDistance \(\leftarrow\) dNA;  subLeaderID \(\leftarrow\) a;  end if  end if  end forall  end forall  end forall
```  
**Algorithm 1**Finding the best subleader for each agent in challenging situations.