Search code examples
anylogicagent-based-modeling

Creating bidirectional link between 2 agents with different type using parameter conditions


I have 2 agents of different type. [Person(type) owner(agent) 1(parameter) Carlink(connection)] [Vehicle(type) car(agent) 1(parameter) OwnerLink(connection)]

I want to link them if parameter "1" of the owner is same to parameter "1" of the car.

I'm thinking adding this statement on startup on Vehicle(type)

OwnerLink.connectto("condition") What syntax should I need to add on the "condition" part.

Thanks in advance.


Solution

  • My understanding is that you have two populations: owners and cars. You also have a parameter called 1 in each (which is an int? double?). You also want to create single bidirectional links. If so, your code would be as follows:

    for( Person p : owners) {
      for( Vehicle v : cars) {
        if( p.1 == v.1) {
          p.Carlink.connectTo(v);
          break;
        }
      }
    }
    

    If you may have multiple links, remove break;, also if you parameter is of type String, replace == v.1 with .equals(v.1).

    To place the code in vehicle startup:

     for( Person p : owners) {
        if( p.1 == 1) {
          p.Carlink.connectTo(this);
          break;
          }
        }
      }