How to plot data using custom objects in AnyLogic? - anylogic

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.

Related

Moving AnyLogic agents according to database table

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

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.

How To Save A Value from TextBox In MS Access 2010

Good day,
In my Ms Access 2010 Form, I have 4 textboxes, namely GrossAmt, WTax1, WTax2, and NetAmt.
GrossAmt, WTax1, and WTax2 are bounded. And so, their values are stored in the table.
NetAmt is unbounded as I have a function running. In its Control Source I have this:
=[GrossAmount]-([WTax1]+[WTax2]).
which is running properly. My problem is, how can I save the value(answer) in the table?
I have tried adding another textbox (named NetAmount) which is bounded to the table and was supposed to copy the values from the unbounded textbox. I have tried putting Me.NetAmount = Me.NetAmt in the "After Update" Event in Property but its not saving.
What should I do, I don't have any clue on what to do anymore. Thank you for your help.
with the equals sign, the value is not stored anywhere, it is calculated on the fly.
If GrosssAmount, WTax1 & WTax2 are stored in the database you never need to store that data, instead you can run the calculation based on the other three. In fact, I would recommend that you do it this way, unless there is a particular need to retrieve a historic net amount.
If you wanted to save the data to the database, you're going to have to use code on Update.
the easiest would be to write a function, something like
Private Function saveNetAmount()
Me.[NetAmount] = [GrossAmount]-([WTax1]+[WTax2])
End Function
and then add that into the AfterUpdate or OnChange event for the Gross Amount, WTax1 and WTax2 text boxes.
Something like:
Private Sub GrossAmt_AfterUpdate()
Call saveNetAmount
End Sub

Calculate Coredata result approach

Lets say I'm trying to RECORD as well as calculate results based on values stored in core data.
Lets take Enery equation : E=mc(square)
I have three entities, Parent/Child/RecentRecords
A Category's entity
Category
-Name : Einstein's equation
-E = mc2
Child Entity:
- Name: Mass
another entry:
- Name:LightSpeed (for the sake of example, lets say the speed of light is not constant.
Recent Entity: The purpose of this entity is to store and track the Child Entities and so it has:
-NSDate TimeStamp
-NSNumber: Value
-Relationship <---->>ChildEntity
I'll explain from a User's point of view,
If I tap "Einstain's equation" (Category/Parent Entity), the detail view will bring out 2 textfields by iterating through the Child entity. When I record my entries of mass, light speed, it should be saved in the Recent entity. SO that overtime i can have a bunch of recordings.
Now image a similar setup for another formula, v=U+at. where Velocity would be parent. U, a, t would be in CHild entity. And in one recording a user would add three Recent Entity objects.
My question is I want to find out if theres an efficient way to calculate the results/entries at the Parent/Category entity level.
Because, EinstainsEquation stores the formula of its child entities, E=mc2 and V=u+at. The thing thats making this complicated is fetching the entries from the recent Entity based on timestamp (recording) and calculating it and showing the result based on the most recent entries.
Parent<-------->>Child<-------->>Recent
-Einstein <---->>Mass <----------->>Multiple Mass recordings based on time
-mc2 LightSpeed <------>>Multiple Lighspeed records based on time
-Velocity<------>>U <--------->>Multiple entries based on time of all 3.
-u+at Aceleration
TimeTaken
Based on the graph above, I need to get einstein result FROM the most recent entries of mass/lightspeed and calculate each of those entries with its formula mc2.
I thought of having a transient Property in Parent entity, how would i go about accomplishing this? How can i use the formula and calculate the child values of recent entity.
I've read multiple tutorials but most of them have to do with basic setup.. I hope ill learn more each day