Altuwariki
Altuwariki

Reputation: 51

Manual casting of agents and accessing its parameter of type SelectOutputOut in AnyLogic?

I understood that I can insert different types of agents in the same block by changing the agent type in the whole process to the generic Agent (works well thanks to Amy). snapshot

But, I am stuck on how to get them out using a manual casting for the process selectOutputIn1. Each Agent type has a parameter called p_new_location with type SelectOutputOut.

What I need help in

passing what is equivalent to agent.p_new_location if properly casted through selectOutputIn1.

What I have tried

creating a function with manual casting:

if( agent instanceof Wife){
    return ((Wife)agent).p_new_location ;
} else if(agent instanceof Child_M){
    return ((Child_M)agent).p_new_location ;
} else if(agent instanceof Child_F){
    return ((Child_F)agent).p_new_location ;}

how it looks with the error

Unfortunately, as you can see there is an error saying the method must return a result of type SelectOutputOut although it is already defined.

The parameter type in each class looks like exactly like this. And from inside the simulation, it looks like this before passing the value and like this after passing its value. Furthermore, I noticed that the value of an uncommon parameter type like SelectOutputOut, is not showing during the simulation as shown here. As you can see parameters (age, countDb and taken) are all there with their values but, not p_new_location.

The Solution

Thanks to Amy Again :)

The compiler is seeing if / else if / else if. What if none of those are true? If your last else if is the only option, just change that to an else or put in appropriate code for what you want to do.

This is how the code looks like now

if( agent instanceof Wife){
    return ((Wife)agent).p_new_location ;
    } else if(agent instanceof Child_M){
        return ((Child_M)agent).p_new_location ;
    } else{
        return ((Child_F)agent).p_new_location ;}

Thanks inAdvance;

Upvotes: 1

Views: 803

Answers (1)

Amy Brown Greer
Amy Brown Greer

Reputation: 726

Yes, AnyLogic can easily handle multiple agent types in a single process block. A few things to keep in mind:

  • Make sure the process block is set to handle a generic type "Agent" or the parent class of mother, father, child.
  • Since you have multiple agent types flowing through the same blocks, you should be prepared to do some casting to get any class specific information.
  • AnyLogic agents cannot be in more than one flowchart block at a time, even though they can be in many collections. They can also be in NO flowchart blocks. Before you send an agent to an enter block, you must first remove it from any other block it is in (if it is in one). For example, if all you wives were in a queue, you would need to remove the wife agent from the queue before calling your enter.take( wife ) line of code.

Upvotes: 1

Related Questions