Access value of parameter of type 'Agent' in AnyLogic - 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();

Related

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

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.

Specific arrival times for agents

I have am model which simulates a distribution network.
I have a population of agents (customers), develop from a database (address -> Gis Location and the order time)
Also I have a order agent, which has a parameter "customer"
I have a source in the customer agent to generate orders and the code at source exit: agent.customer = this;
In the source I've set Arrival deffined by: Arrival table in Database and refer to the customer -> order time. My problem is, that I currently generating in every source of this population every order. How can I set up the source/model, that I generate at a specifc customer only the orders from this one?
Unfortunately, a Source object can only ingest the whole table and there is not function to filter records. One way to go about partitioning Order agents is by creating all of them in a single source somewhere in the model and then using Exit to send them to an Enter object within a specific Customer agent. To summarise:
There is a single Source that reads the 'Orders' table for all customers
Each record has a field with 'customer id' so when agent gets created it has a property of which customer it belongs to
Source object feeds all new Order agents into an Exit object
Each Customer agent has an id and an Enter object
Source's Exit object contains code which finds Customer agent based on 'customer id' property and then calls customer.enter.take(agent) to send the Order agent into the right Customer agent
More information about here: Enter and Exit.

agent population produces error after inserting parameters loaded from database

I would like to load my trucks on specific docks like loading truck A on dock 50. After I insert population loaded from database I get the error that it doesn't find my storages and it cant't calculate the space of my palletracks/ docks. Could it be that whenever I insert a population via database that the program ignore the other parameters of the agent like my storage? Before I insert the database, it works fine.
error
populationloadDocks
resourcepool_dockloading
Make sure to load all parameters from the dbase table in the properties of your agent population under "Agent parameter mapping" (visible in your second screenshot).
The AnyLogic help explains more on how this works but you essentially tell your agents which parameters should be filled from which table column.

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 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.