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

359 views Asked by At

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?

2

There are 2 answers

2
Benjamin On BEST ANSWER

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.

1
Leo On

The answer is: Use entity.ID_number=parameter