How I can impose social distance in agent based anylogic model? - anylogic

I was trying to model an agent-based anylogic model where pedestrian agents will maintain a distance of 6 feet between them when moving in the continuous space. How I can do that?
I tried to put a circle of 6 feet radius around the agent and iterated whether any other agent is within that "socialDistance" circle. If yes, then the person agent will move (current distance between agents + 6 units) to maintain social distance. I used "moveTo" & I know it's not the right command. Additionally, my distance calculation is also not right. For example, if the agents are in 5 feet distance initially, I command asks to move (5+6=11 feet), which should not be the situation. Moreover, sometimes, my code may push the agent out of the simulation area (seems to me).
So, in summary, can someone help me to model that 6 feet distance between agents? It will be a great help.
Code (wrong!) that I tried:
for (person socialD : get_Main().people)
{
double pedSocialX = socialD.getX() -this.getX();
double pedSocialY = socialD.getY()- this.getY();
if (this.socialDistance.contains(pedSocialX, pedSocialY))
{
double a= socialD.getX()+ 6;
double b= socialD.getY()+ 6;
moveTo( a, b);
//[this is wrong command, what should be a good command?]
traceln ("social distance not mainted");
}
else
traceln (" social Distance maintained");
errors:

Related

How to plot distance travelled on graph by agent(s) in Anylogic in Discrete Event Simulation?

Dear Anylogic Experts,
I am trying to calculate the cost based on distance travelled by the agents in GIS environment. Does anyone know how can I calculate this? The final graph function is supposed to look like this: Distance moved by Agent in km or meter multiplied by the Cost per km or meter.
The idea is to see which route will cost how much. The way I have moved agents in via PML. please see picture attached of the process taking place.
there is no default way for this, several options:
Turn on "Model execution logging" and you will get total travelled distance for each agent.
In your MoveTo blocks, log the departure and arrival location (lat/lon) of your agent and use getDistanceGIS(...) to log the total distance from your agent

Anylogic - line of sight

Is there a way to check if there is a line of sight between two agents assuming some buildings and presentation markup?
(Meaning a function that would check if two agents can see each other assuming buildings and walls)
This is how I did it once in the past. The only problem is that it might be slow if you need to do that calculation thousands of times per second. And it's only in 2D. If you need 3D, the idea is not so different.
1) add all your building nodes and everything that might be an obstacle between the two agents to a collection. You may want to put a rectangular node around your buildings to keep everything in one collection (assuming you are using space markup with nodes)
2) generate a delta distance delta (equal to 1 for example) and find the angle of the line that passes through both agents.
3) Make a loop from the position of agent1 till the position of agent2. It will be something like this:
L=delta;
while(L<LThatReachesSecondAgent){
x1 = agent1.getX() + L*cos(angle);
y1 = agent1.getY() + L*sin(angle);
for(Node n : yourCollectionOfNodes){
If(n.contains(x1,y1))
return false
}
/*This can also work maybe faster
//int numNodesInTheWay=count(yourCollectionOfNodes,n->n.contains(x1,y1));
//if(numNodesInTheWay>0) return false
*/
L+=delta;
}
return true
welcome to SOF.
No, there is no build-in function, afaik. You would have to code something manually, which is possible but not straight forward. If you want help with that, you need to provide more details on the specifics.

How to calcucalate the distance agent moved

guys
Have the population of trucks in model, moving from city to city. How to calculate the distance, every truck has moved?
There are 2 ways... one very accurate that requires your cities to be agents, and the other less accurate that doesn't require that.
Option 1: (accurate, requires agents)
Your truck is moving from city1 to city2, both are agents and you want to know the distance between city1 and the truck (which is equal to the distance moved)
calculate: city1.distanceByRoute(truck) whenever you need to know the distance moved.
Option 2 (less accurate, doesn't requires agents)
Create a variable called initialTime=time(); (it takes the value time() when the truck starts the journey.
Calculate (time()-iniTime)*truck.getSpeed() whenever you want to know the distance moved.

Netlogo - Controlling specific sets of turtles

I am trying to develop a model to demonstrate a field principle I am trying to work with. I am having trouble finding information on how to control specific groups of turtles. Essentially I would like to make a grid of turtles and links that I can distort, relative to an objective coordinate grid, based upon the placement of a different type of turtle, like a particle interacting with a field?
Any suggestions would be greatly appreciated. Is it possible to write a command that only talks to specific turtles by their who value? for instance if i had a coordinate plane that went from -3,3 in both x and y, rendering 49 vertices, could I set up those first 49 turtles, and then spawn my particle turtles (lets say 3), which would be who 49,50, and 51. could I write a command specifically for those 3 turtles based upon their relations to the first 49 turtles?
I hope this question makes sense.

Sphero: How to make it move a given length

I want to make the sphero move a given amount of centimeters ahead but so far i have not managed to get anything to work properly
This is the code i have now:
EditText distanceText = (EditText) findViewById(R.id.distanceText);
int inputMSec = Integer.parseInt(distanceText.getText().toString());
int time = (inputMSec - inputMSec/2);
// The ball is given the RollCommand at half speed.
RollCommand.sendCommand(mRobot, heading,0.5f);
// A handler is created to delay the Stop command.
final Handler handler = new Handler();
handler.postDelayed(new Runnable(){
#Override
public void run() {
// Makes the ball stop
RollCommand.sendStop(mRobot);
}
// The variable 'time' defines how long the ball rolls until it is told to stop.
}, time);
Is there any other command i can send to the ball instead of RollCommand?
Or can anyone find out what to do to the input from the EditText so that the distance turns out correct?
There's no API command that directly provides the ability to drive a given distance. The only way I now how is with an iterative strategy using the locator, which provides position information about the ball.
Here's an overview of the strategy. Let me know if you need more details.
Turn on data streaming and request locator position data. You should now have some callback giving you Sphero position ~20x a sec. The positions are (x, y) coordinates on the floor in centimeters.
Record the starting position of the ball (x0, y0).
At each point in time, compute the distance the ball has traveled so far using the Pythagorean theorem/distance formula.
Use this to figure out how far the ball has left to go. Call this distance D.
Give a roll command where the speed is computed from D.
The big question is: how do you decide what speed to command based on D? I had some success with a strategy something like this: (undoubtedly you can tune it up a lot better).
If the remaining distance D > 100cm, command full speed.
Between D = 10cm and D = 100cm, command power ranging from 30% to 100% (linearly).
If D < 10cm, command zero speed to make Sphero stop.
This worked pretty well. It would drive, slow down, and coast the last few inches to a stop. Problems with this algorithm include:
You have to tune it to prevent overshoot/undershoot.
When you want to command the ball to move only very short distance it doesn't work well, so you might have to tweak it to cover those cases.
Good luck!