Anylogic: How to add a variable on agent based on a parameter of its upper agent? - anylogic

EX:
I have a population people with agent type Person with a parameter wrist_size.
In the agent type Person I've created another population =watches, agent type =Watch.
Now I want to create a variable in the lower agent type that will be based on the parameter of the upper agent.
length of the watch should be equal to the wrist_size of the person
What should code should I put to the variable?
Many thanks.

You can simply use people.wrist_size in the "initial value" field of Watch.length.
Each embedded population (here Watch within Person) gets a "link" up to their parent agent (here Person named people).
See https://anylogic.help/advanced/code/access.html#where-am-i-and-how-do-i-get-to for understanding model hierarchies.

Related

How to store agents and its' parameters in a 2 dimensional collection in anylogic?

I have a population of agents (called products) that has multiple parameters of different types. The parameters are "amount" of type int and the other is "location" which is a location on the GIS map. Now I would like to store these agents in a collection when they enter an enter block. Does anyone know how to do this? What type of collection could I use and how do I define the elements?
Drag in a collection, keep the default ArrayList type and set the content type to your agent type.
In the Enter block's "on enter", write myCollection.add(agent)
That is all :)

Access value of parameter of type 'Agent' in AnyLogic

The model I am working on is set up as follows. It contains four agent types:
Patient
SmallHospital
MediumHospital
BigHospital
. All these agents are located in Main. SmallHospital and MediumHospital agents are connected in a network to BigHospitals using link to agent objects e.g.: mediumHospitalLink and bigHospitalLink.
When generated, Patients move, via a statechart in Patient, to nearest hospital agent (regardless of type, using this function in Patient). The hospital agent to which the Patient moved to is stored in a parameter in Patient called p_myFirstHospital. This parameter has type 'Agent' as I do not beforehand to which hospital type the agent will be move to. This results in no error and functions as expected.
However, I want to move the Patient-agent to a second hospital after a while. If the Patient is currently in a smallHospital, it should move to the mediumHospital to which the smallHospital is linked to, and likewise, if the Patient is in a mediumHospital, it should move to a bigHospital to which the mediumHospital is linked to.
I expected that I would be able to call from within Patient the following code to access those agents:
p_myFirstHospital.mediumHospitalLink.getConnectedAgent();
or through a simple function I created. It should retrieve the agent from the Link to agents located in the hospital agent stored in p_myFirstHospital.
Unfortunately, this does not work. The compiler returns the following error message:
Description: smallHospitalLink cannot be resolved or is not a field. Location: model/Patient/f_DetermineSecondHospital - Function
Am I missing some Java or AnyLogic subtleties? Your help and suggestions are very welcome.
That's because p_myFirstHospital is of type Agent
So you need to make java magic as follows:
case1:
p_mySecondHospital=((SmallHospital)p_myFirstHospital).mediumHospitalLink.getConnectedAgent();
case2:
p_mySecondHospital=((MediumHospital)p_myFirstHospital).mediumHospitalLink.getConnectedAgent();

Access paramters of Anylogic agent population created from db

OK... let me retry the question.
I'll just walk through the steps I (wrongly) assumed would work.
Create a data table in excel with passenger info (id, flight_time, type, class, qty...), note that the table is sorted by id... not flight_time.
Import this database into anylogic.
Create a population of agents (Passenger/Passengers) from the data table (one/row).
Create a schedule that addresses the Start Column as flight_time and Value column as qty (for this job I'm only sending one passenger at a time therefore qty = 1 for each row).
Set the pedSource to arrive according to schedule and to use the Passenger Agent as the New pedestrian.
So here is where I'm losing it. When I run this model the new Passengers do not have any parameters associated from the data table. The pedestrian id is some weird number (say 3000 or so). I can click on the Passengers icon during run time and scroll through the created agents (all of the parameter data is there and correctly assigned), but I'm not sure how to associate the new Agents in the run-time model with the population of Passengers agents.
Am I missing a step here? I was thinking that if I import a population of agents from a data table and then have each show up at a particular time in the model that I could then do some calculations with regard to each such as ped.exitTime = time() - ped.flightTime - ped.bufferTime.
I'm just not understanding why the table data is not available for use during run-time through ped? Is there another mapping step that must be performed to push the data to the ped agnets?
I'm at an impasse at this point. I hope this question is described more clearly and your feedback is appreciated.
Several things are wrong here.
You do not create agents in a pop first and then try to reuse them in a PedSource. The latter creates agents itself.
You don't seem to create pedestrians but just agents
you are not mapping the data to parameters
Quick guide to help:
create custom agent type "MyPed" . Make sure its "use in flowchart" property is set to "Pedestrian"
add 1 param into "MyPed" for each dbase table column
set your PedSource to "calls of inject()" function for its arrivals
delete your schedule, you cannot get the data you need
USe a DynamicEvent. Include 1 parameter argument for each dbase column. On Startup of main, loop across all dbase entries and create a dynamic event with the current row-data.
in the action of the DynEvent, call myPedSource.inject(1) and then manually fill that ped with the data from the arguments
This is not straightforward, especially the DynEvent stuff. So do more research in the AL help to understand these and how they work, check example models...

assign a variable to agents' statistics

I have a population of agents as employees. They have a salary which has a formula to be calculated, so I defined it as dynamic variable, "salary". I want to know the average of the salaries, so I went to the statistics and defined:
Name: AvgSalary
Type: Average
Expression : item.Salary
The question is that I may need to assign this number to a variable so that it can be used in other formulas in other stages of my evaluation. How is that possible?
Many thanks in advance,
Your statistic object AvgSalary is already an object you can access. You defined it in the properties of the agent population so you can access it by baccling myAgentPopulation.AvgSalary() and it will always return the current value.
Hope this helps

Assign value to entities - Anylogic

I am just starting with Anylogic & Java. Could you please help me with my simple issue?
I have two windows: Main & Trucks
The trucks are entities generated on my model using a "source" within "Main".
I asked the model to create 10 trucks at the same time. It creates the trucks and then they move to another defined point at different speeds. When they arrive at the final point I want to know which truck arrived first (1, 2, ... or 10).
So... I am assuming that there is a way to assign a number to each truck when they are created. And I am also assuming I can read this number at the final point to know which one arrived first.
How can I do this?
Yes it is possible. Use a parameter. Truck is an agent and it can have multiple parameters. The easiest way to use parameters is to drag them from the Agent pallet into the Truck window. You define its name and type (in your case will be integer). When the truck is generated at the source you can give the parameter the value you want. If you want to have a sequential number, create a variable in your main window called ID with initial value 1 and then, in the source object, on the On Exit code area you type something like:
Agent.parameter = ID++;
Note that "parameter" should be the name of the parameter you created in Truck window.
Hope that helps.