【问题标题】:How to find the name of the path between two nodes如何查找两个节点之间的路径名称
【发布时间】:2020-10-14 07:26:14
【问题描述】:

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

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

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

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

n2 是手动分配的。例如:

String n2 = "node2";

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

【问题讨论】:

    标签: anylogic


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,这很有道理。唯一的问题是我的 n2 变量当前是一个字符串(现在我们称之为 n2Name)。如何根据 n2Name 获得 n2?
    • Node theNode=findFirst(network.nodes(),n->n.getName().equals("你的节点名"));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多