In AnyLogic I am trying to count and display the current number of agents moving on a single path or rather in a single "moveTo" block. After that, the travel speed of the agents should be dependent on the displayed number.
Is that even possible?
I've tried it with statistics and variable but I guess I don't know enough about it yet.
The "moveTo" blocks are linked with nodes between which the paths are.
Can someone help?
In the following code, I am setting the agents' speed to 10 if the number of agents in the moveTo block is greater than 10.
if( moveTo.size() > 10 ) {
for( Agent a : moveTo ) {
a.setSpeed(10);
}
}
So you can get the number of agents in the moveTo block by using .size() and then you can change agent speeds using .setSpeed().
Related
Two groups of men and women arrive at the same entrance source at the same time to avail a certain service, then how can we get the entire average waiting time of both separately in anylogic .please help me.
You can use these two block provided by AnyLogic.
Or as I do, create two variables for your agents called enterTime and leaveTime and set them to time() when they enter and leave the Queue. Like agent.enterTime = time(). Create a variable in the Main (assuming your blocks are there) called totalTimeInQueueMen and totalTimeInQueueWomen and set it to totalTimeInQueueMen=totalTimeInQueueMen+(agent.leaveTime-agent.enterTime). Also count your number of agents with a variable called count. Every time they enter the queue, increment it by 1; like count+=1;. At the end of the the simulation you can calculate the average as totalTimeInQueueMen/count (the same for women).
I need to create an agent with a parameter "Class" that has 3 possible values. I need to be able to determine the number of agents with each class value at the Simulation screen before the simulation starts, by using a slider that determines the number of Agents.Class=="A", Agents.Class=="B" and so on.
I've tried creating an action chart that uses RandomTrue to assign the class.
The inputs for this action chart are:
A parameter sums the output of the 3 sliders to determine the total n of agents
Another parameter computes the percentage of agents.class=="A" from the total n of agents
A third parameter computes the percentage of the agents.class="B" from the remaining n of agents (n-agents.class=="A")
I'm not allowed to post pictures yet, but see action chart Here .
The issue with this is that when using percentages, it will round here or there and the actual number of agents with desired class may have one more or one less.
How can I make this with an exact number of agents created with each class?
This does not make sense mathematically. If you create 10 agents and the random number splits it using 0.66 and 0.33, then you cannot create 6.66 agents.
So some rounding must happen, unless you create infinite agents.
However, you can decide to round the random number. If you know you will create 100 agents, you could use Math.ceil(uniform(0, randomA)*100.) or similar. If randomA would be 0.66666, you would get 67 as a result.
(Math.floor would give you 66)
It isn't clear to me why you're converting the number to %-ages at all? Are you going to create N agents in total? If so you can just use amounts for class 'A' and class 'B' agents in two separate Source objects (one for each class) to create the agents at the start.
Using the Road Traffic Library, I have created a 4-way intersection that is stop-controlled and I need to measure the average delay for each approach. Currently I am using timeMeasureStart and timeMeasureEnd blocks, showing the time taken as the car enters the road/model, until it exits the intersection.
Instead, I want to measure from the time the car slows to 40km/h, until it exits the intersection. Any suggestions?
The initial speed of all cars entering the model is 60 km/hr.
Sure, there is no pre-defined way but this custom approach should work:
make sure your cars are a custom agent type, not the default cars. Lets name it My Car
add a variable myTimeBelow40 into MyCar of type double
check the car speed regularly (every 0.1 sec?!) in an event in MyCar.checkSpeed. Use the getSpeed() function. If it is below 40 KPH, you log the current time into myTimeBelow40
log the departure time of your car: your time since 40 KPH is the difference
Finally, add a statistic across your car population or log your individual car duration into main
#Benjamin thank you for pointing me in the right direction.
Here is my solution, guided by the suggestion. I'm sure it could be refined but in the end it is what worked for me and my limited knowledge of AnyLogic.
For a 4-way intersection, I wanted the delay for each approach, so I created 4 custom car agents, with each populations starting out empty.
In each agent, I had 2 variable blocks - var_Start and var_Slow, an one event block set to Timeout, Cyclic, first read at time(), and proceeding in intervals of 0.1s. In the event action, i specidied the following:
if(getSpeed(KPH) <= 40) {
var_Slow=time();
var_Slow.suspend();
}
In main I used Histogram Data, labeled as dataDelay, and a chart with the mean showing, to see the results. I had one for each intersection.
Back in the car agent, in actions on startup:
var_Start=time();
and on destroy:
if(var_Slow = 0)
main.dataDelay.add(time()-var_Start);
else
main.dataDelay.add(time()-var_Slow);
At the car source block in main, I kept the initial and prefered speed at 60, however if there were cars backed up then new cars were often initiated at a slower speed, sometimes already below 40kph, hence the if,else code on destroy.
I had everything labeled according to its corresponding approach direction, unlike the simplified version I have here.
I have an AnyLogic simulation model using trucks and forklifts as agents (Transporter type), and among other things would like to identify each time one of them becomes within a certain distance of another one (example within 5 meters). I will record this count as a variable within the main space of the model. I am using path-guided navigation.
I have seen a method "agentsInRange" which will probably be able to do the trick, but am not sure where to call this from. I assume I should be able to use the AL functionality of "Min distance to obstacle" (TransporterFleet) and "Collision detection timeout" (TransporterControl)?
Thanks in advance!
Since there don't seem to be pre-built functions for this, afaik, the easiest way is to:
add an int variable to your transporter agent type counter
add an event to your transporter type checkCollision that triggers every second or so
in the event, loop across the entire population of transporters and count the number that are closer than X meters (use distanceTo(otherTransporter) and write your own custom code)
add that number to counter
Note that this might be very inefficient computationally as it is quite brute-force. But might be good enough :)
I have a question about space between agents. In my model I have agents generated from a source and then they enter a delay, after the delay the agents go into a a queue with a capacity of 1 but I have a preemption option. The agents that go into the preemption are supposed to move along a circled path (I used a delay block for this) but there should always be a certain space between the agents, e.g. 100 meters. How can I incorporate this in my model to make sure my agents are not too close to each other?
One way you can control the distance between your agents is to move them on a path using a dummy transporter instead of the moveTo block. Transporters allow you to define a minimum distance to obstacle that prevents the agents from getting too close to each other.
Two options if you mean the static queue with agents actually waiting:
1) if your queue size is 500 meters, define the maximum amount of agents allowed in that queue to 6 (so you have 100 meters of distance between each agent)
2) Use the PML settings block from the PML palette and define an initial capacity of animation location equal to 6 (if your queues are 500 meters)... but this applies to all the model, so maybe it won't be good enough.
If you want them to have 100 meters space while they are moving towards their objective through the path that represents the queue, then the answer depends heavily on your model and it cannot be answered with the info provided... you need in this case to control the agent movements adding some logic... but I don't know what logic is suitable for you.