【问题标题】:How to select the nearest agent connected through another agent in a network in AnyLogic?如何在 AnyLogic 中选择通过网络中另一个代理连接的最近代理?
【发布时间】:2021-02-08 22:22:27
【问题描述】:

在一个模型中,我通过“链接到其他代理”对象连接了网络中的不同代理类型。我使用了一个函数来创建网络:

shopLink.connectTo(this.getNearestAgent(main.shops));
homeLink.connectTo(this.getNearestAgent(main.homes));

所以每个工厂代理只连接到最近的商店和家。此函数在 Factory-agent 类型的“启动时”字段中调用。

假设红色的代理类型是工厂,黄色代表商店,绿色代表家庭。还假设所有 Factory-agents 都包含 Person-agents,并且我想将 Person-agents 发送到最近连接到 Factory-agent 的 Shop-agent。我需要使用什么 Java 代码来选择最近连接到 Factory-agent 的 Shop-agent?

【问题讨论】:

  • 我认为这是一个图形问题。因此,您可以阅读此blog post。否则知道你已经做了什么会有所帮助吗?你写了一些代码吗?
  • 分享更多关于您在工厂代理类型中的实际连接设置的详细信息,我们可以提供帮助;)

标签: java anylogic


【解决方案1】:

如果您的 Person 代理存在于 Factory 父代理中,并且您有如图所示的连接,那么这取决于您在 Factory 中的连接设置:

(a) 如果 Factory 的默认 connections Link to Agents 对象包含 Shop agent 连接,则使用

getNearestAgent(factory.getConnections())

(b) 如果 Factory 的商店连接在一个特殊的 Link to Agents 对象中(比如shopConnections),则使用

getNearestAgent(factory.shopConnections.getConnections())

(c) 如果您在 Factory 的默认 connections 代理链接中混合了与所有其他代理类型(商店、工厂等)的连接,您必须首先过滤该列表以仅包含商店代理,所以

(Shop) getNearestAgent(filter(factory.getConnections(), f -> f instanceof Shop))

(关于为什么 case (c) 需要 (Shop) 开头的位,这是 Java 强制转换,但 (a) 不需要。这与 @987654330 @ 和getConnectionsgeneric methods,而type inference 正在使用。)

【讨论】:

  • 谢谢你的详细回答,斯图尔特。非常感谢。我正在使用方法 (b),并且我设法让它按预期工作。
猜你喜欢
  • 2018-11-16
  • 2018-08-25
  • 2017-01-10
  • 1970-01-01
  • 2021-06-03
  • 2016-02-17
  • 2017-06-20
  • 2022-09-26
  • 2021-04-08
相关资源
最近更新 更多