3

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.

Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33
Shingston
  • 217
  • 1
  • 8

1 Answers1

1

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

Jaco-Ben Vosloo
  • 3,770
  • 2
  • 16
  • 33
  • Amazing little snippet of code, and how frustrating that I hadn't thought of such a simple solution. All that time wasted on writing redundant functions. Thank you, Jaco-Ben. – Shingston Nov 30 '21 at 14:27
  • 2
    Note that this only works (or rather gives a meaningful value) if the scales of all agents are the same. More generally you should adjust according to the scales so the result is correct (in pixels) given the scale of Main (or whatever your 'target' enclosing agent is). – Stuart Rossiter Nov 30 '21 at 19:12