1
votes

I'm new to Anylogic and I apologize in advance if this is a beginner question. I have an agent that travel in a network between nodes along paths. In a function, I would like to get the name of the path between two nodes - the node that the agent is at and the node that it is going to. As an example, the variables containing the name of the two nodes are n1 and n2.

The network is set up such that there is only one path between two given nodes.

I am using the following to get the name of the node for the the current node:

Node n = (Node)agent.getNetworkNode();
String n1 = n.getName();

n2 is manually assigned. For example:

String n2 = "node2";

What is the best way to get the name of the path? Any help would be much appreciated. Thank you.

1

1 Answers

0
votes

I don't think there is and easy way so I show you this way... Note that there can be many paths connecting both nodes, so this solution gives you the paths names for all the paths that connect n1 and n2

Node n1=findFirst(network.nodes(),n->n.getName().equals("n1"));
Node n2=findFirst(network.nodes(),n->n.getName().equals("n2"));
ArrayList <Path> conn1=new ArrayList(); 
ArrayList <Path> conn2=new ArrayList();
ArrayList <Path> paths=new ArrayList();

for(int i=0;i<n1.getConnectionsCount();i++){
    if(n1.getConnection(i) instanceof Path)
        conn1.add(n1.getConnection(i)); // add the path connected to n1
}
for(int i=0;i<n2.getConnectionsCount();i++){
    if(n2.getConnection(i) instanceof Path)
        conn2.add(n2.getConnection(i)); // add the path connected to n2
}

for(Path p1 : conn1){
    if(conn2.contains(p1)){
        paths.add(p1); // add the path matches
    }  
}

for(int i=0;i<paths.size();i++){
    traceln(paths.get(i).getName()); // print the name of the paths
}