如何在AnyLogic中选择通过另一个代理连接的网络中最近的代理?

huangapple go评论161阅读模式
英文:

How to select the nearest agent connected through another agent in a network in AnyLogic?

问题

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

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

因此,每个工厂代理只与最近的商店和家庭相连接。这个函数在工厂代理类型的“启动时”字段中被调用。

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

英文:

In a model I have connected different agent types in a network through 'link to other agents' objects. I have used a function to create the network:

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

So each Factory-agent is connected to the nearest shop and home only. This function is called at the 'at startup' field within the Factory-agent type.

如何在AnyLogic中选择通过另一个代理连接的网络中最近的代理?

Let's assume the red agent types are factories, yellow represent shops, and green represent homes. Also assume that all Factory-agents contain Person-agents, and I want to send the Person-agents to the most nearby Shop-agent is connected to the Factory-agent. What Java code would I need to use to select the most nearby Shop-agent connected to the Factory-agent?

答案1

得分: 1

如果您的人员代理存在于一个名为Factory的父代理中,并且您拥有如图所示的连接,则取决于您在Factory中如何设置连接:

(a) 如果Factory的默认“connections”链接到代理对象仅包含商店代理的连接,则使用:

getNearestAgent(factory.getConnections())

(b) 如果Factory的商店连接位于一个特殊的“链接到代理”对象中(比如说shopConnections),那么使用:

getNearestAgent(factory.shopConnections.getConnections())

(c) 如果您已经将所有其他类型代理(商店、工厂等)的连接混合在Factory的默认“connections”链接到代理中,您首先必须对该列表进行过滤,仅包括商店代理,因此可以使用:

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

(在Java中,情况(c)为什么需要在开头加上(Shop)这一部分存在一些细微差别,这是一个Java类型转换,但是情况(a)则不需要。这与getNearestAgentgetConnections两者都是通用方法,以及正在使用的类型推断有关。)

英文:

If your Person agents exist inside a Factory parent agent, and you have connections as shown, then it depends how your connections are set up in Factory:

(a) If Factory's default connections Link to Agents object contains only Shop agent connections, use

getNearestAgent(factory.getConnections())

(b) If Factory's shop connections are in a special Link to Agents object (say shopConnections) then use

getNearestAgent(factory.shopConnections.getConnections())

(c) If you've mixed connections to all other agent types (shops, factories, etc.) in Factory's default connections Link to Agents you'll have to filter that list first to include only Shop agents so

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

(There are Java subtleties in terms of why case (c) requires the (Shop) bit at the start, which is a Java cast, but (a) doesn't. It's to do with the fact that both getNearestAgent and getConnections are generic methods, and type inference is being used.)

huangapple
  • 本文由 发表于 2020年10月27日 00:10:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64540890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定