Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Anylogic:基于最短距离创建到特定代理的单向连接_Java_Anylogic_Agent Based Modeling - Fatal编程技术网

Java Anylogic:基于最短距离创建到特定代理的单向连接

Java Anylogic:基于最短距离创建到特定代理的单向连接,java,anylogic,agent-based-modeling,Java,Anylogic,Agent Based Modeling,我对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

我对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));
}
更一般地说,如果希望a
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;
    }
  }
}
//对于每个B
对于(代理B:填充B){
//查找它可能连接的所有代理。
//每次我们从该集合中获取代理时,它都会收缩,以确保我们不会继续尝试连接到同一个代理。
列表剩余选项=过滤器(
//我们从一个国家的全部人口开始
阿根塔的人口,
//我们不希望B连接到它已经连接到的任何A,所以将它们过滤掉。
A->B.connections.isConnectedTo(A)=false
//您可能需要其他条件,如最大距离、某些特定属性,如可承受性等;只需在此处添加一个“&&”
/* && */
);
//如果理想情况下B需要总共N个连接,那么我们试着得到N个连接。我们不从零开始i,因为它可能已经有连接了。
对于(int i=B.connections.getConnectionsNumber();i

还请注意,我经常使用
连接
:如果您需要多个收藏,如工作、学校和社交网络,您可以用它来代替更具体的网络。

我非常感谢您的回复。当我们使用connectTo(agent)方法时,我意识到它可以创建双向连接。但是,我的情况是,我需要将a连接到B以实现“x”,将B连接到a以实现“y”。connectTo()在这里不起作用。你有没有找到解决这个问题的办法?谢谢。请尝试在a和B中创建一个唯一的“指向代理的链接”,然后选中该选项以确保它是单向的。让我们假设它在A中被称为
employer
。然后你可以说
myAgentA.employer.connectTo(myAgentB)
。如果您选中“单链接”选项,您可以保证只有一个连接的B。这表示。。。这太复杂了。为什么不在代理中声明类型为a或B的变量,然后将其分配给正确的代理?例如,您可以在A中创建类型为
B
的变量
employer
,然后说
myAgentA.employer=someAgentB
?希望有帮助@SrijithBalakrishnan,我知道这是你关于StackOverflow的第一个问题。如果这有助于解决您的问题,请记住将其标记为答案。:)