【问题标题】:agent cannot connect to itself error when trying to connected to a random agent on startup in anylogic尝试在anylogic中启动时连接到随机代理时,代理无法连接到自身错误
【发布时间】:2020-01-30 13:00:06
【问题描述】:

我在一个环境中有大量代理,我正在尝试通过连接到具有相同来源的其他代理来创建一个代理网络来代表家庭。 family 是代理人中双向链接的集合。

我已经在启动时使用了代码

for(int i =0; i<5000; i++){
population.get(i).family.connectTo(randomFrom(population));
}

运行时我收到错误消息

Error during model startup
root.population[4005]:
Cannot connect an agent to itself

如何消除此错误?

【问题讨论】:

    标签: anylogic


    【解决方案1】:

    您只需搜索其他代理,直到该其他代理不是您:

    for(int i =0; i<5000; i++){
        Agent randomOtherAgent = randomFrom(population);
        while (randomOtherAgent.equals(population.get(i)) { 
            randomOtherAgent = randomFrom(population); // try to find someone else
        }
        // now you found someone not equal to yourself
        population.get(i).family.connectTo(randomOtherAgent);
    }
    

    【讨论】:

    • 谢谢,一旦我将第 3 行更改为 while (randomOtherAgent.equals(population.get(i)) {
    • 太棒了。请将其标记为“有帮助”(我的答案旁边的向上箭头),以帮助其他人将来也能找到它。我会修改我的答案(没有测试代码;-))
    猜你喜欢
    • 2013-04-17
    • 2017-01-03
    • 2016-02-06
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    相关资源
    最近更新 更多