が、これはあなたがあなたのモデルの最初に何をしたいです:
より一般的に
// 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
をたくさん使うことに注意してくださいyの場合学校やソーシャルネットワークなどの複数のコレクションが必要です。
本当にありがとうございます。私たちがconnectTo(エージェント)メソッドを使うとき、私はそれが双方向接続を作成することに気付きました。しかし、私はAをBに目的 'x'に、BをAに目的 'y'を結ぶ必要がある状況にあります。 connectTo()はここで動作しません。あなたはこれのための任意のソリューションに出くわしましたか?ありがとうございました。 –
AとBにユニークな「エージェントへのリンク」を作成して、オプションが単方向性であることを確認してください。 Aで 'employer'というふりをしましょう。次に、' myAgentA.employer.connectTo(myAgentB) 'のようなことを言うことができます。 「シングルリンク」オプションをチェックすると、接続されているBが1つしかないことが保証されます。これは...これは過度に複雑です。エージェントにAまたはB型の変数を宣言し、それを正しいエージェントに割り当てるだけではどうですか?たとえば、Aで 'B 'という変数の' employer'を作成し、 'myAgentA.employer = someAgentB'と言うことができますか?希望が助けてくれる! –
@SrijithBalakrishnan、これはStackOverflowでの最初の質問の1つです。それがあなたの問題を解決するのに役立つならば、これを答えとして記入してください。 :) –