Anylogic is assigning the value to all entities instead of only one - anylogic

My source has the following code in "On at exit" field:
TRUCK.ID_number = parameter;
parameter = parameter + 1;
Then, each truck will receive an ID: 1, 2, 3, ...
The problem is that All the trucks are circulating at the same time and it looks like everytime a new truck is generated and the code runs it resets the ID for all trucks.
For example: When truck 4 (ID = 4) is created at the source all the other trucks receive ID 4 so I cannot know the correct ID of each truck.
What am I doing wrong?

a few things. First, you need to use the keyword agent in the onExit code box. So in your case, agent.ID_number=parameter. Please read up on these specific keywords that pop up everywhere in AnyLogic code boxes. I call it the "magic lightbulb", also see my blog on that topic:
The magic lightbulb
Second, newly created agents automatically get a unique index (if they belong to the same population). You can access that using the getIndex() method inside your truck agent.

The answer is: Use entity.ID_number=parameter

Related

anylogic selectblock on 'true' returns same agent

I am simulating last-mile delivery and I want to iteratively evaluate the co2 emissions between stops. The numbers of the first route evaluation add up i.e. it correctly counts/removes each stop embedded within the route. The problem is that when the first route is finished, the model should consider the next route embedded in a new agent of type 'Order', in 'toConsumer' but it does not. From what I can see, is that the order agent is not updated after the condition in the select block has been met. I am not sure however, why it does this. When the condition is satisfied the agent does continue to the sink. Does anybody know how i can ensure this updating of the agent?
The anylogic model looks as follows:
In the source block I create agents following optimization results via:
int r = 0;
agent.routeVeh = (int) parVehicle.get(r);
agent.route = (List) parRoute.get(r);
agent.routeDep = (int) parDepot.get(r);
r++;
And in the select block i have:
The to consumer block:
If you are doing this operation in the Main agent, you need to define routeCount and consumerCount inside your Order agent (or if you have a Vehicle agent defined, inside that). Because they are defined for each order. Then in the routeFinished you need update the variable as agent.consumerCount++;

AnyLogic - pickup (container size)

[Screenshot]
I'm sorry it's in german..
1I have a problem with a pickup element in AnyLogic. I have two agents: pallets and pieces.
If an order arrives I combine the two with a pickup element (pallet in inPort) with "at least 4 pieces" and I also set up a parameter on the Agent pallet with the name amountofpieces.
I now want to know exactly how many pieces there are in the container pallet, so that I can add the amount of pieces to my stock variable.
With: Agent.amountofpieces = container.contents().size() I get a error note.
Also Agent.amountofpieces = pallet.contents().size() is not working.
I later want to add it to a variable (name: stock). My code now is: stock += + Agent.amountofpieces.
Do you maybe have an idea how to solve it?
Or is there another posibility to get the amount of agents that went out as "one" new agent?
I found a solution :)
agent.contents().size() works without any parameters or anything else.

Condition For Select Output based on distance between two different GIS Nodes

I am trying to find a condition to put in select output that is based on distance between two GIS points. My network includes a tram with people and I want my person to get off at tram stop nearest to its house. The tram moves in a counter loop and I want to place condition so that the person exits drops off at the right tram stop. Could you please help me with it?
I have already tried distanceByRoute but I don't know how to form a condition for it via statechart or in selectOutput.enter image description here
Assumptions
You have a population of Station-Agents named stations
You have a population of Passenger-Agents named passengers
You have a population of Tram-Agents named trams
You have a population of Home-Agents named homes
Step by step
Add toTram-Agent a variable nextStation of type Station in which the moveTo-Block always saves the current arrival station
Add to Passenger-Agent a parameter of type Home named home where at model startup a Home-Agent is filled in
Create a function named getNearestStation, with a input parameter of type Passenger and output of type Station:
//initialize with first station
Station nearestStation = stations.get(0);
double distanceToNearestStation = passenger.home.distanceByRoute(nearestStation);
double currentDistance;
for(Station station:stations){
currentDistance = passenger.home.distanceByRoute(station);
//save it if you find a closer station
if( currentDistance < distanceToNearestStation){
nearestStation = station;
distanceToNearestStation = passenger.home.distanceByRoute(station);
}
}
return nearestStation;
Instead of having a Station Agent, you could also use GISPoint for both your Variable nextStation inside tram and in the getNearestStation code.
Add a dropoff-Block to your flowchart and set the dropoff condition to the following:
getNearestStation(agent).equals(container.nextStation)
Additional note
In order to make the distance calculation less often, execute the getNearestStation only once at initialization of each passenger and save it in a variable, instead of calculating it each time it passes the dropoff block.
enter image description here
tramCollection here is my collection of tram stops (in GIS/INode) located on my map in Main.

Using context variables to write the user's car registration

In watson-conversation, I have reached a point where I ask the user their car number (registration), which follows this format: 0000BBB (4 numbers, 3 letters).
I want to type that to the user like:
User: "My car id is 0123asd"
Watson: "Okay so your car id is this one: 0123asd"!
I have tried defining an entity #carId with some examples, but every time I input something with that format (0000BBB), it shows "irrelevant".
If the chat detects #carId, respond with Okay so this is your carId! #exampleCarId (I have some examples like 5487qwe, or 8521rty, I thought the machine learning below that would learn the "pattern").
And my #carId has these examples:
I know I am missing something! Do I need to code anything? I think it's not necessary. I tried to save it on a sys-number but it does not work as it is not a "number".
Most developers would consider he car ID an entity (denoted by #). IBM Watson Assistant allows to define so-called dictionary-based entities. One form of such an entity is pattern-based. Thus, you would define a pattern of 4 digits and 3 letters.
You could have an intent (denoted by #) that identifies that a user inputs the car ID. In the dialog node you could match against the intent and then assign the matched entity for the actual ID to a variable.
The linked documentation has examples.

using Joblets in talend with tMemorize and tJavaFlex

I am trying to create some joblets in Talend that will speed up some processes.
I have an input from a MSSQLInput, the results are then sorted and filtered a little. Then I have a tMemorizeRows and a tJavaFlex, the purpose of this is to memorize the rows in a column to preform a count. The count is based on a customer ID, once the the id changes the count starts back to 1 and the proccess begine again and continues to the end. I have refactored this as a joblet but it does not work, the error is:
ID_tMemorizeRows_1 cannot be resolved to a variable
I have a tJavaFlex which starts with
int counte = 1;
The Main code is
if(ID_tMemorizeRows_1[0].equals(ID_tMemorizeRows_1[1]))
{
counte = counte + 1;
}
else
{
counte = 1;
}
context.Enqnum = counte;
The Enqnum variable and is created correctly and added into a tMaps component.
Does anyone know why this is happening, one person told me it is because when you move something to a joblet it gets a new/different name so it has to be specifically called in the Java, if this is the case how do I find the name out?
Thank you
Rich
I do have a resolution. I have tried to add images however my reputation is not high enough.
When using joblets we know that Talend essentially recycles the code used in the joblet by inserting it into the code for the main job.
This is the joblet I have created, i know it works because I have refactored it to a joblet instead of building it from sctatch. What its doing is simply memorises row 0 and row 1 in an ordered data set, the java performs a count and the tMap appends the result to the job (as Mentioned above).
(I will try it inser image in my question, I do not have enough reputation point to insert it into a question).
When the job is run it runs fine. But problems occur when I want to reuse the same joblet in another part of the job. What Talend does is it assigns names within the source code to each component depending on the name of the joblet.
For example, if the Joblet was called ThisJob, then tMemorizeRows_1 would be called ThisJob_1_tMemorizeRows_1.
The row within the component (in this example ReferenceID) would renamed as:
ReferenceID_ThisJob_1_tMemorizeRows_1.
But when you add a second joblet to your job it gives it a new name, eg ThisJob_2. This name will be different depending on how much you have been altering your job before you add the second joblet. Therefore the number within the name will depend on this activity.
If you add the joblet into your job immediately then the joblet would be called ThisJob_2, if you have added 5 other components before you add it in then the joblet is likely to be called ThisJob_6 etc. (I'm not 100% sure how talend renames components)
When you add a joblet, You can see the name of the joblet on the joblet component, this then reverts back the the original joblet name when you create any links/joins to other components.
Its also important that each component within the code is assigned to a variable called currentComponent.
Resolution
What I did was used the Java code to split the name using the code below. This way I can get the current name of the of the joblet and use this name in my Java.
String string = currentComponent;
String[] parts = string.split("_");
String part1 = parts[0];
String part2 = parts[1];
String joblet = part1+'_'+part2;
String newrow = "ReferenceID_"+joblet+"_tMemorizeRows_1";
I hope this makes sense.
Thanks