简体   繁体   English

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

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

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.此函数在 Factory-agent 类型中的“启动时”字段中调用。

在此处输入图片说明

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.还假设所有 Factory-agents 都包含 Person-agents,我想将 Person-agents 发送到最近的 Shop-agent 连接到 Factory-agent。 What Java code would I need to use to select the most nearby Shop-agent connected to the Factory-agent?我需要使用什么 Java 代码来选择最近连接到工厂代理的商店代理?

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:如果您的 Person 代理存在于 Factory 父代理中,并且您具有如图所示的连接,则这取决于您在 Factory 中的连接设置方式:

(a) If Factory's default connections Link to Agents object contains only Shop agent connections, use (a) 如果 Factory 的默认connections Link to Agents 对象包含 Shop 代理连接,请使用

getNearestAgent(factory.getConnections())

(b) If Factory's shop connections are in a special Link to Agents object (say shopConnections ) then use (b) 如果 Factory 的商店连接在一个特殊的 Link to Agents 对象中(比如shopConnections ),那么使用

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 (c) 如果您在 Factory 的默认connections Link to Agents 中混合了与所有其他代理类型(商店、工厂等)的connections ,您必须首先过滤该列表以仅包含 Shop 代理

(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.) (就为什么 case (c) 在开始时需要(Shop)位而言,Java 有一些微妙之处,这是一个 Java 类型转换,但 (a) 不需要。这与getNearestAgentgetConnections都是通用的事实有关methods ,并且正在使用类型推断。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM