Anylogic:根据最短距离创建到特定代理的单向连接

Anylogic: Creating unidirectional connections to specific agents based on shortest distance

我是 Anylogic 新手,想执行以下任务。我在 GIS 环境中有几种类型的固定代理,并且想通过网络连接它们。连接条件如下:让代理类型 A 有 4 个代理,代理类型 B 有 20 个代理。我想根据最短(直线)距离将 B 与 A 连接起来。也就是说,类型 B 的代理将连接到最近的类型 A 代理。

谢谢。

在您的具体情况下,这就是您在模型开始时想要的:

// For each of your Bs
for (Agent B : populationOfB) {
  // Find the nearest A (using a straight line) and connect.
  B.connections.connectTo(B.getNearestAgent(populationOfAgentA));
}

更一般地说,如果您希望 B 连接到多个 A 符合一组特定条件的代理,并且您需要即时(或在开始时)执行此操作模型),您可以执行以下操作:

// For each B
for (Agent B : populationOfB) {

  // Find all A agents that it could possibly connect with.
  // Every time we take an agent from this collection, it will shrink to ensure we don't keep trying to connect to the same A.
  List<Agent> remainingOptions = filter(

    // We start with the full population of A
    populationOfAgentA, 

    // We don't want B to connect with any A it's already connected to, so filter them out.
    A -> B.connections.isConnectedTo(A) == false

      // You might want other conditions, such as maximum distance, some specific attribute such as affordability, etc.; simply add them here with a "&&"
      /* && <Any other condition you want, if you want it.>*/
  );

  // If B ideally wants N connections total, we then try to get it N connections. We don't start i at zero because it may already have connections.
  for (int i = B.connections.getConnectionsNumber(); i < N; i += 1) {

    // Find the nearest A. (You can sort based on any property here, but you'd need to write that logic yourself.)
    Agent nearestA = B.getNearestAgent(remainingOptions);

    // Connect to the nearest A.
    B.connections.connectTo(nearestA);

    // The A we just connected to is no longer a valid option for filling the network.       
    remainingOptions.remove(nearestA);

    // If there are no other remaining viable options, we need to either quit or do something about it.
    if (remainingOptions.isEmpty()) {
      traceln("Oops! Couldn't find enough As for this B: " + B);
      break;
    }
  }
}

另请注意,我经常使用 connections:如果您需要工作、学校和社交网络等多个集合,您可以将其替换为更具体的网络。