0

I'm trying to create individual agents with a set of parameters, have them choose a population/collection of other agents to join, then use the sortAscending function to rank the agents based on a composite score of characteristics. I'm stuck with how to dynamically create the agents and then add them to a population.

I've tried a process flow of agents and also using the statechart to have agents make the selection but can't get them to add so I can run the code to sort them.

Felipe
  • 8,311
  • 2
  • 15
  • 31
  • Can you shared what you've tried? – Akin Okegbile Aug 12 '19 at 21:25
  • Just as an addition information: It is not possible to dynamically create an Agent and THEN adding it to a population. The only way is to use the function that Felipe already put as an answer (add_populationName()) or using the population settings of the Source block. – Florian Aug 13 '19 at 07:27

2 Answers2

1

It is not possible to dynamically create an Agent and THEN adding it to a population. The only way is to use the function that Felipe already put as an answer (add_populationName()) or using the population settings of the Source block.

However: Since your ultimate goal is to sort the Agents, why dont you just use a Collection/List instead? An Agent population is in fact a list as well, just with some AnyLogic specific extras. To dynamically create Agents and sort them, do the following:

  1. Create your Agent dynamically: MyAgentType agent1 = new MyAgentType();
  2. Fill parameter of your agent (if you didnt do it yet during the creation): agent1.myParameter = 10;
  3. Add it to an existing Collection of type MyAgentType: myCollection.add(agent1);

To sort the Agents in the Collection based on multiple parameters, use a custom comparator, as pointed out in this answer:

Collections.sort(myCollection, new Comparator<MyAgentType>(){
     public int compare(MyAgentType a1, MyAgentType a2){
         if(a1.myParameter == a2.myParameter)
             return 0;
         return a1.myParameter < a2.myParameter ? -1 : 1;
     }
});

In this example just the one parameter is compared, but you can extend this to your needs.

Florian
  • 962
  • 1
  • 5
  • 17
0

If your agent class is Agent and your population is called agents and the parameters are a,b and c you would create an agent of the population with the following code:

Agent agent=add_agents(2,3,4); //where a=2, b=3, c=4
Felipe
  • 8,311
  • 2
  • 15
  • 31