How can I send one agent to another agent in Anylogic? - anylogic

I have a reservation object, which I want to send to the car agent so that the car is set with the attributes of the reservation object, when the car arrives. The car will be injected when the arrivaltime is reached. So I tried injecting a car to the car source on the "on exit" section of the delay block of the reservation pipeline and then sending the reservation object to the car statechart with:
send(agent, Car);
but it says: "Car cannot be resolved to a variable".
Can anyone help how to achieve that?

Understand how keywords work in code sections. The "on exit" code block will have specific keywords that can be used, if you hover over the little lightbulb you can learn about them. See my blog post on this.
In your case, however, you have not actually connected a reservation with a car. Your car just appears in the source.
Best read up on agent connections, then connect your reservation to the car agent being created. Hope this helps a little

Related

msg can't reach the stat chart for multiple deliveries

I'm trying to change the existing model in Anylogic (product delivery) and trying to move trucks to various locations. in manufacturing processes I add batch and hold to put 5 orders in the truck, but after I can't communicate with truck state chart and can't make it read the trigger (msg). I tried various methods but couldn't make it work. I'm new in anylogic and your help will be appreciated.
the first thing you should do is go to the truck agent, to the connections object, and verify if your message is being sent
Also to understand better how messages work, i suggest you watch the following video which is part of the course available at noorjax.teachable.com
https://www.youtube.com/watch?v=Fe2U8IAhlHM

AnyLogic Car library: Is there a way of checking if a route exists for a given destination?

I am using AnyLogic car library and have multiple roads and parking lots. However, not every parking slip is accessible from every road. I want to detect this beforehand if the car won't be able to make it to the destination and send them to somewhere else.
Currently when the car enters the carMoveTo37 block, it throws an error if there is no way of reaching the destination.
Is there any way of checking this before entering the carMoveTo block?
Yes, you can simply use the bottom out-port as per the help:
So simply reroute those cars via that port or do something else with them :)

In Anylogic How can I model truck that deliver orders to multiple clients

I'm creating a model based on the product delivery example provided by AnyLogic. In my own model, I want a truck to deliver multiple orders in one trip instead of one. My process diagram is shown below. Here, an order enters via the enter block and several orders are accumulated in the batch block. Every order has a specified destination. How do I model the truck such that it combines two orders and move to the nearest delivery location first and then the second etc?
The main problem is that I don't know the code that accesses the parameter "Delivery location" in each order.
enter image description here
enter image description here
Additional information:
The orders agents are generated and the delivery location is stored in a parameter called "client"
The batch block combines (lets say 2) orders into a batch of the type Order ( advanced settings set to population of agents)
The service block pulls a truck from the resource poule and send the batch of orders to the truck agent using send(batch.unit)
The truck agent stores the order/orders(?) in a variable called "order"
Then, a moveTo function should deliver the order to the first destination
What would be the code to move to the first, second etc., destination?
Here is the conceptual piece you are probably not aware of, and that should help you move in the right direction:
you can have "for loops" as part of your process flows. Below, you see an example where an agent keeps driving to places until it has no more parcels.
Obviously, the details of the blocks depend on your model but in each, you can access the truck's orders if you have them in your Truck agent type (which is needed, obviously).

How do I connect agents by name in AnyLogic?

I am trying to connect different agent types by names. So, for example, I have a dataset where I have a list of patient names and their doctors name. I would first like to create two different populations of patients and doctors where each individual agent is assigned a name from the dataset. Then I need to create connections between the two different populations based on the corresponding connections in the data. Anyone know how to do this? Any help appreciated!
AT
Let's assume your doctor and patient agent populations are created, and the patient having a parameter called doctorName, and the doctor having a parameter called name. You have to figure out how to do this based on where you get the info from. I will also assume that all doctor names are different.
the doctor will have a link to agents object (from the agent palette) called patientLink as a collection of links and bidirectional, but as a single link on the patient side (called doctorLink on the patient side).
now you can use the following function to connect them:
for(Patient p : patients){
Doctor doctor=findFirst(doctors,d->d.name.equals(p.doctorName));
p.doctorLink.connectTo(doctor);
}
You should use the "Agent Link" object. This does exactly what you need.
In your case, you will need to write some code looping across your data and setup the links accordingly. Check the example models using AgentLink objects to learn about it and read in the help, there is a lot of stuff on it.
Here are just a few quick thoughts on this that I hope are helpful in your process:
1) If you want to keep doctors and patients linked, you can craft an agent as a doctor-patient dyad--this could make a lot of sense depending on what your research question is; or
2) If one doctor handles more than one patient, you could also consider forming an agent that is actually a network-type arrangement with the doctor as the central node--again, this depends what your research question is and what your data looks like; or
3) If you want to link doctors with patients based on some rule, consider using a discrete event approach by using a "Match" function from the "Process Modeling Library" palette.
Best wishes,
LCG

AnyLogic how to link one agent to another

Im a relative noob regarding anylogic but i got a task to do in my homework so here comes my question:
I created a population of agents who are all patients... these patients get ill with a probabilty of lets say 30%... i alrdy implemented this one but now my task is to add a medicine using a new agent for this problem to heal them... but how do I link this new agent with the already existing one? My first agent gets triggered by a message.. therefore we have to use an event sending this message to a first person who gets infected etc..
Can anyone help me how I can create a new agent and link it to the old one to heal the people?
Many thanks in advance!
ok so since this is what you need I will post it as an answer
medicine has a state chart called SC with an initial state which is used and a second state notUsed that you get through a message transition from one to the other.
when the patient gets to the state sick, it will need to find an agent Medicine which is in the state notUsed to be able to get healed, so you find it with the following code:
Medicine med=findFirst(main.medicines,m->m.inState(m.notUsed));
if(med!=null){//meaning that there is at least one not used medicine
med.SC.fireEvent("use medicine");
send("get better",this);
}
You will have to do the same probably when a new medicine is created you use the same method to find a guy in the state "sick"..
I assume you are calling this function in the patient agent, but it may differ depending on when you are calling it.