Anylogic Message Animation - anylogic

I am trying to force agents of a population to exchange messages in AnyLogic. I would like each time agent A sends a message to B the icon of the message to move from A to B. How can I implement this?

The code Emile sent you works to move an agent from one place to another one. I understand you don't want to move your two agents, but instead you want to move only a "message icon" from one to the other. For that you can create an agent (let's call it agent "Message"), create it and locate it in the agentA, and tell it (as Emile said) to move to agentB: messageAB.moveTo(agentB.getPosition()); this way you'll get the effect you want.
You could also:
use a timer to move from one place to another, or
use an event and change the position of the icon dinamically depending on how much time you have remaining on that event
use a source/delay/sink for the same as in point 2

There are basically two ways to move an agent:
Jump to agent B: Instantly appears near agent B
Move to agent A at a certain speed
For each one the code is respectively as follows:
agentA.jumpTo( agentB.getXYZ() );
agentA.moveTo( agentB );
Where agentA and agentB refer to the agents which you might call differently depending where you are in the model.

Related

Anylogic detect that agent stopped on conveyor

is there any easy way to detect that agent stopped on a roller conveyor because there are other agents ahead? I tried to use dynamic variable and conditional event but it is consuming too much performence. I donĀ“t want to go for dynamically checking condition (with cyclic event) because I have some bad experience with it.
Thanks!
Try this:
Create a LinkedList<YourAgentType> agentsOnConveyor.
Whenever an agent enters the conveyor, call agentsOnConveyor.addLast(agent). When it leaves, call agentsOnConveyor.removeFirst(agent).
Now you have a linked representation of the agents on the conveyor.
Last, you use the "on stopped" field in the conveyor and pass the message "upstream" through the linked list, i.e. use a for-loop from the first to the last entry and send them a msg "conveyor stopped".
You may still need to check if the individual agent is moving itself or has also stopped, but it might put you on a working path

In Anylogic, is it possible to send an agent from one storage to another directly?

I have 2 storages (called storageA & storageB) and I want to move an agent (pallet) from one to the other via forklifts. I have set up the following.
A pallet is created at a node and is moved to storageA via 'store'. This part works fine. The pallet is then moved to storageB via 'store1' after a delay. This is when the following error occurs:
Exception during discrete event execution:
root.store1.seizeTrans.freeSpaceSendTo:
Path not found! {agent=2, source={level=level, pos=(1673.3333333333333, 3245.0, 0.0)}, target={level=level, pos=(1857.25, 3160.4845, 0.0)}}
It works if I replace 'store1' with a retrieve block and send it to a node first. However I would like to send the pallet directly to another storage rather than via another location. Is this possible?
Please let me know if I have not provided enough information.
Thanks
yeah unfortunately you can't do that as far as I know, the solution I use is the following, which is actually not a super robust solution... but has been ok in applications so far
Place a retrieve block between your delay and your store1
Use the agent you pick up as destination:
on the on seize action of the retrieve block do:agent.transporter=unit;
4.On the store1 block put the highest priority for the task
5. ON the store1 block use resource custom transporter choice: agent.transporter.equals(unit)
6. The dispatching policy should be nearest to the agent in store1, but doing all the above ensures that the resource continues doing the task no matter what... by only using the dispatch policy your model will work 99.999999% of the time... the problem occurs only if another task with higher priority occurs at the exact same time as the transporter is released in the retrieve block, which is rare, but can happen.
I had the same question today so I landed here. But luckily, only after the second step written above, the whole process needed did already work for my case. We can move an agent from one storage to another by simply set the destination of the 'retrieve' block to the coordinate of the agent and the move to independently instead of by fleets or resources. after that we put the 'store' block.
Destination is: (x,y,z)
X: agent.getX()
Y: agent.getY()
Z: agent.getZ()
after agents being retrieved to a specified coordinate, it seems that fleets do not comply paths in the network anymore

is it possible to copy the current state of the statechart from one agent to another?

I am trying to make a model to simulate the contagion of covid in public spaces using a mix between SEIR and pedestrian models.
In another question I asked to use a static population. They suggested that before deleting the agent a copy be saved in a list and after the first X agents have been generated I want the next agent generated by the pedSource to be one of the list.
Currently what I do is take a random agent from the list and if it is infected I send a message to the new agent so that it goes into the infected state. But by doing that I am resetting the timeout to recover every time an agent enters the zone that I am modeling.
this is the code that currently runs in the pedSource on exit:
if (personasEnCasa.size()+personasEnSuper.size() > poblacionMaxima){
Persona p = randomFrom(personasEnCasa);
if (p.statechart.getState() == Persona.Infeccioso){
send("Contagiado", ped);
};
personasEnCasa.remove(p);
};
personasEnSuper is my population of Persona, personasEnCasa is my list of agents outside the zone and and poblacionMaxima is the maximum number of agents in the lista and the population
I would like to be able to copy the current statechart of the agent in the list to the agent that generates my pedSource. Or use something similar to a pedSource.inject () but inserting an agent from the list instead of a new one. But I did not know how to do it.
is there any way to do this?
your ped already exists and you don't need to copy it you can just move it to the flow like this, with pedWait being any pedestrian block that you want, so instead of send("Contagiado", ped); you would do enter.take(ped);
but if you insist in using the send, then you can use branches on your statechart to define where this ped goes:
you will need in this case before the send, use ped.infectious=true; and the condition in the branch would be infectious==true to move to the infectious state.
As a side note, instead of p.statechart.getState() == Persona.Infeccioso you should use p.statechart.getState().equals(Persona.Infeccioso)
use == only with primitives such as boolean, int and double, otherwise you are susceptible to errors that are very difficult to discover

AnyLogic: How to refer to an agent in a delay block in anylogic

I am building a DES-ABM hybrid model in AnyLogic.
The agents go through the DES blocks, among which multiple Delay blocks.
How do I
access an agent which is in a Delay block
or peferrably
acces the specific agent which triggered the 'on enter' action of the delay block?
My ultimate goal is to open or close a valve object on the agent frame
So can I/ how do I
A. open or close the valve on the agent frame directly form the main/root frame (on which the Delay block is located)
or if that is not possible
B. send a message or trigger a statechart within the specific agent which will then open or close the valve from the agent's own frame?
I have tried to use the 'DelayBlockName'.agents() function, but this does not work and returns [] when I check it using traceln.
access an agent which is in a Delay block or peferrably
use the keyword agent. These keywords differ for different library blocks so best start learning about the lightbulb and how it can help, see here.
acces the specific agent which triggered the 'on enter' action of the delay block?
When you write agent. in the "On enter" block, every agent coming through will execute that code, so by definition, it is always the specific agent :)
My ultimate goal is to open or close a valve object on the agent frame So can I/ how do I A. open or close the valve on the agent frame directly form the main/root frame (on which the Delay block is located) or if that is not possible B. send a message or trigger a statechart within the specific agent which will then open or close the valve from the agent's own frame?
this is something completely different to your original question and just... messy. Please limit questions to 1 topic so it is easy for us to answer :) (see this guide for more)

How to add to agent to presentation

I am trying to simulate self-sustaining population of cows, but when i dinamicly add new agents to main agent's population newly created agent does not appear. How to fix it? I create agents from mother agent with
this.main.add_cows();
first generation population
pupulation without presentation
First go to the agent where the animation will exist (which is also where your agent is defined). In most cases this is the main agent, so you probably want to go to "main" and click on the cow agent to see the "initial location" in the properties of the cow agent.
When a new agent is created, the default position is at the agent animation location, which is probably somewhere out of the visible area since we generally position our defined agents out of the canvas, and in your case it's the same position for all cows.
Now you have other options: you can select for instance a random coordinate in the space (assuming you have a 600x600 pixels square):
Or you can select a node (as long as a node is defined in your animation canvas:
So, to summarize, when you create a new agent in your population, you have to tell AnyLogic where you want to locate it... Otherwise, how can the software know what you want?
ensure to set the initial location correct, otherwise it might appear at a default location you do not expect. Something like:
Cow myNewCow = this.main.add_cows();
myNewCow.setXY(uniform(0,600), uniform(0,400));