'Is Transporter able to detect another agent?

I am modelling utility strikes by excavators. I modelled excavator as a transporter with free-space movement. I have another agent called Utility. The population of Utility is called utilities.

I want to count utility strikes by transporters. Transporters have detection capability, for example if |Z(transporter) - Z(utility)| <= 3 there will not be any accident! But if |Z(transporter) - Z(utility)| > 3 there will be a strike and transporter needs to stop working for a while, for example 2 mins, and restart working.

I created a variable called v_utilityStrikeNumber and an event called e_checkUtilitystrike. This is something that @Benjamin suggested for someone else. I wrote the following code for the event Action.

for (Utility u: main.utilities){
dist = distanceTo(u);
 return dist; 
}

if (dist <= 3){
main.utilities_remove(u);
}
else{
v_utilityStrikeNumber += 1; 
u.v_isUtilityStrike = true; 
state_excavator.receiveMessage("interrupt"); 
}

I received the following errors:

  1. u cannot be resolved to a variable
  2. dist cannot be resolved to a variable
  3. the type u is not visible

Can you please tell me how to resolve them? Thank you.



Solution 1:[1]

The errors are mostly coding errors. Let's address them one by one:

  1. u can only be accessed within the first for loop, you cannot use it again outside the for loop. u is a different utility in each loop iteration. So, when you get out of the loop, which utility are you referring to? If you mean to proceed within the for loop, as in checking if dist is less than 3 for each utility, then do not close the for loop until the end.

  2. It seems you don't have a variable called dist in your model? As such, just add double before the first time you introduce dist.

  3. 1 should resolve this I believe.

So your code would look like this:

for (Utility u: main.utilities){
double dist = 0;
dist = distanceTo(u);


if (dist <= 3){
main.remove_utilities(u);
}
else{
v_utilityStrikeNumber += 1; 
u.v_isUtilityStrike = true; 
state_excavator.receiveMessage("interrupt"); 
}
}

Try this and let me know if you are still having errors.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1