Search code examples
simulationanylogic

Is there a way to construct a single function to get the coordinates in main of a sub-agent that is present in multiple upper-level agents?


Is there a straightforward way to to get the coordinate points of an agent in relation to main when that agent is the lower agent of multiple other agents?

For example:

I have a Box agent. There are populations of Box agents in both my Shelf and Pallet agents, and the Pallet agents can be located either in the main or Rack agents.

So I've got:

main > Shelf > Box

main > Pallet > Box

main > Rack > Pallet > Box

So far, I've created individual hard-coded functions that add up the coordinate of the Box with the coordinates of its upper-level agents.

So:

For boxes in pallets in racks: CoordBoxInMain = CoordBox + CoordPallet + CoordRack

For boxes in shelves: CoordBoxInMain = CoordBox + CoordShelf

But now I am wondering, is there a way to construct a single function that directly gets the coordinates of my Box agent without having to create multiple different functions that each refers to a different sequence of upper-level agents?

Thank you.


Solution

  • You can use this little piece of code

    Agent agent = myBox;
    double xCoord = agent.getX();
    while (agent.getOwner() != null) {
        xCoord += agent.getOwner().getX();
        agent = agent.getOwner();
    }
    
    traceln(xCoord);
    
    

    It will keep on finding the owner of the agent until it reaches main (or your root agent) and add the X coordinates and then trace it

    You need to do the same for Y and Z as well

    I tested it on a simple model and it works

    enter image description here