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.
Related
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.
My model has a collection of trucks, where the Source block provides each truck with a unique ID. I also have a database containing information on the vehicles' movements.
No column has unique values, but together the trip_start_date and vehicle_id should be a unique set. I'm trying to structure a moveTo block such that when a date in the trip_start_time column is reached during the model runtime, then the agent with that vehicle_id as a parameter will move to the lat/long in a certain amount of time.
However, I'm getting an "empty database value result." I'm wondering if this is because it's expecting every model date to have an entry? I considered making a function that's called when a date in the database is encountered, but I was told that the moveTo block should be able to handle this activity. Help in resolving this issue would be greatly appreciated!
The way you are trying to use the MoveTo block is not correct. It is hard to explain here but I think you need to go through a few tutorials inside help to get a better fundamental understanding.
However, below is a reproducible example of the behavior I think you want to implement, but using agent-based approach instead of a process-centric or discrete event approach.
I have an agent called Vehicle with a ID called vehicle ID.
There is a Dynamic event has all the parameters found in the DB
Then at the start of the model, we generate all the moveTo events found in the Database using the code below.
List<Tuple> rows = selectFrom(db_table)
.where(db_table.vehicle_id.eq(vehicleID))
.list();
for (Tuple row : rows) {
//What is the start time of the movement
double timeToTripBegin = dateToTime(row.get( db_table.trip_begin_time ));
//Lets create a new movement event to trigger the movement int he future
create_MoveToLocation(timeToTripBegin,
row.get( db_table.latitude ),
row.get( db_table.longitude ),
row.get( db_table.trip_duration_h ));
}
This event will then execute the move to when the time arrives
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();
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...
I'm building an ArrayList that contains object of type "TimeRecord" (a object that I have defined). Each TimeRecord object contains the following fields:
ID -> identifier of the field
StartTime -> time when an object pass in a specific point of my network
StopTime -> time when an object pass in another specific point of my network
Each time field is populated with the AnyLogic time() function and has the aim to collect the time between two point in my network. Is it possible to pass a list with the time differences StopTime - StartTime to a chart object to plot the the time of every request?
Yes, you can use a dataset and update it using an event. For instance:
Drag a dataset to main (probably, to use a histogram dataset is a good idea in your case , it depends on what you want to show)
Drag an event to main to be updated every n times.
In the field action of the event you can add something like this:
dataset.reset();
for (TimeRecord t : YourArrayList) {
dataset.add(t.StopTime - t.StartTime);
}
Another way to do this, without using a loop, it is to create a variable at the agent level to record start time, and then when the agent finishes what is doing add to the dataset the difference between stop and start.
Anyway, add a plot, and specify the corresponding dataset in the data field.