0
votes

I am building a discrete event model with AnyLogic. I would like to assign unique ID to each agent which comes into the process. I included a variable, ID and another variable, clientID. When the entity gets "this.clientID = ID++;" on at exit. I noticed that there are multiple agents which are getting the exactly the same ID. Are there any ways to assign unique ID to each agent as they are entering a system?

2

2 Answers

1
votes

Each agent has unique id, it is generated internally, and may be returned with getId() function.

Unique ID may be generated as you describe as well. Then, you should have custom agent type with clientID variable at canvas. Source block should create new agents of this type. Main should have at canvas a variable called ID. On At Exit action code of Source block should be: agent.clientID = ID++;

BTW, getIndex() call is not safe, since it returns agent index within population. If population size changes, agent index within population may change as well. getIndex() may return the same index for different agents in case if they are in different population. At last, getIndex() always returns -1 for standalone agents.

0
votes

You need to understand how agent-based works:

if the agent holds the variable "ID", then each agent has its own value for "ID". Hence they all get the same value when each of them individually executes the "ID++" code.

Each agent already has a unique ID assigned by AnyLogic upon its creation. You can retrieve it using "myAgent.getIndex()".

hope this helps