首页 > 解决方案 > 如何查找两个节点之间的路径名称

问题描述

我是 Anylogic 的新手,如果这是一个初学者问题,我提前道歉。我有一个代理沿着路径在节点之间的网络中旅行。在一个函数中,我想获取两个节点之间的路径名称——代理所在的节点和它要去的节点。例如,包含两个节点名称的变量是 n1 和 n2。

网络的设置使得两个给定节点之间只有一条路径。

我正在使用以下内容获取当前节点的节点名称:

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

n2 是手动分配的。例如:

String n2 = "node2";

获取路径名称的最佳方法是什么?任何帮助将非常感激。谢谢你。

标签: anylogic

解决方案


我认为没有简单的方法,所以我以这种方式向您展示...请注意,连接两个节点的路径可能很多,因此此解决方案为您提供连接 n1 和 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
}

推荐阅读