0
votes

I run a simulation where I have one agent type but they arrive of size, say 100. The agents have a parameter, say color, which changes in a simulation. I would like to see the color of each of these agents after a run.

I saw on the documentation site that I can use charts or the inspect window. But, this option seems to work for a single system variable/parameter. I would like to see the value of each agent's color, i.e., a list of 100 parameters. I will also appreciate having the knowledge of the historical changes of the parameter values. How can I make this?

2

2 Answers

1
votes

This is rather a general question; there are lots of ways to do this depending on exactly what you want (and the exact details will also be specific to how you coded/determined the colours in your model). As some general pointers

  • If you want individual values per agent, you can create charts with a dynamic number of data entries (as in Sahar's answer) or use replicated Text objects (with dynamic contents) to produce tables --- see the AnyLogic Help > 2D and 3D Animation > Shape Replication help topic.

  • If you want totals (say the number of agents of each colour) you can add statistics to agent populations and query those for a chart (or set of Text objects) --- see the AnyLogic Help > Agent Based Modeling > Collecting statistics on agents help topic or Phase 3 (p.54) of the market model in the "AnyLogic 7 in 3 Days" free textbook.

  • If you want the above, but don't want to have to hardcode the set of colours (in terms of a statistic per colour), you would do something like the following:

    • write a function that accepts a Color (as an argument) and loops through the population, returning the number of agents of that colour;
    • define the set of colours in some collection, with contents added dynamically according to the specifics of your model;
    • use a chart (with dynamic number of data entries) or replicated Text / shape objects to display this information (i.e., entries per collection entry and values taken by calling the function).
  • If you want to have longitudinal data (over simulated time), use datasets to store time series data (which can also be plotted using charts) --- see the AnyLogic Help > Collecting Output Data > Data Set help topic. You can create datasets dynamically (at the Java level) if you need to dynamically determine the number of datasets you need.

1
votes

There can be many creative ways to visualize the final results, here is one:

Drag a Chart element to the canvas and remove the default data items that is in it. Create a function with this code:

for(MyAgent myAgent : myAgents){
    DataItem di = new DataItem();
    //set the height of the bars
    di.setValue(5);
    chart.addDataItem(di, "MyAgent " + myAgent.getIndex(), myAgent.color);
}

Call this function on "After simulation run" box of the experiment. This code will draw something like this on your chart:

enter image description here MyAgent agents have a parameter called color which is set to randomColor() on startup.

To store all the colors that an agent had you can create an ArrayList type variable in the agent and whenever you change the value add it to the list. I can't think of any better way right now, maybe someone will answer a more efficient way :)

enter image description here