Cecil
Cecil

Reputation: 55

Anylogic detecting and iterating through agents placed on another agent (main)

I created a custom agent, call it 'MyAgent' and compiled it into a library. A user now starts a new Anylogic project, drags and places a number of these 'MyAgent' instances (each as a single agent) onto the project window (main form). I now have another agent, also placed on main that executes an algorithm to do stuff with the agents placed on the project. For this it needs to "detect" how many of these agents the user dropped on his project (if at all), and then iterate through each of these "MyAgent" instances, doing stuff with them.

Something like:

for (int i=0;i<="number of 'MyAgent' instances on this"-1;i++) {
MyAgent thisinstance= "collection of 'MyAgent's".get(i);
thisinstance."property_I_would_like_to_modify"="new value";
thisinstance."call_a_function():"; 
}

My problem is how to find:

  1. "number of 'MyAgent' instances on this" - on this form(Agent) that the user has dropped, might be nothing, may be 50, i don't know whether user dropped any, and if he did how many
  2. "collection of 'MyAgent's" - to allow me to iterate through the entire list

Above pretty simple. But may be entirely wrong approach. Could someone please offer some guidance on how to do this?

Upvotes: 1

Views: 637

Answers (2)

Amy Brown Greer
Amy Brown Greer

Reputation: 726

A couple of options come to mind.

  1. List on Main added to on startup. Add a collection to main of type ArrayList. On Startup Action of your custom agent, have them add themselves to this list. main.listMyCustomList.add( this ). You will need at least one of your objects on main to automatically get AnyLogic's link to main. If you have none, you may get a compile error. One way to work around this is to create a parameter of type main. Another work around is to use ((Main)getOwner()).listMyCustomList.add( this ) to account for when there are no objects on main and keep the compiler happy. This would not work if the user puts the object on something other than main. You would need to add some checks to see what the agent type was of getOwner() in those cases.
  2. Use filter & myAgents. You can create a list to use later (listMyAgents.size()) or you can call this as needed. In addition to filter, count is also an option. This will just return a list of objects, which you will have to cast later.
List<Object> listMyAgents = filter( agents(), agent -> agent instanceof MyAgent );

If you want to get a list of your agent types, the casting would look like the following:

List<MyAgent> listMyAgents = (List<MyAgent>)(List<?>)filter( agents(), agent -> agent instanceof MyAgent );

Upvotes: 0

Benjamin
Benjamin

Reputation: 12803

here is the only way to do this. On startup of your MyAgent (or on startup of your user's Main, if possible), run this function:

int counter = 0;
for (Object currObject : ((Agent)getRootAgent()).getEmbeddedObjects()) {
    if (currObject instanceof MyAgent) {
            counter ++;
    }
}

Upvotes: 0

Related Questions