我是Anylogic的新手,并希望执行以下任务。我在GIS环境中有几种类型的固定代理,并希望通过网络连接它们。连接的条件如下:让代理类型A有4个代理,代理类型B有20个代理。我想基于最短(直线)距离将B连接到A.也就是说,类型B的代理将连接到最近的类型A的代理。
谢谢。
答案 0 :(得分:0)
在您的具体情况下,这是您在模型开头所需的内容:
// 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
:如果您需要多个集合,例如工作,学校和社交网络,则可以将其替换为更具体的网络。