Message Boards Message Boards

Make each agent interact with every possible neighbor?

Posted 4 years ago

I am trying to modify an excellent code by Christopher Wolfram: https://community.wolfram.com/groups/-/m/t/1907703

The code sets that each step of the simulation, each agent will pick some subset of its neighbors to meet with. The code specifies the distribution that determines how many neighbors each agent will meet with at each step. An exponential distribution was chosen in the beginning.

meetingCounts = 
 AssociationThread[Keys[neighbors], ExponentialDistribution[2]]

Later, meetings are determined using this code

meetings = DeleteDuplicatesBy[Flatten[
   Function[a,
     {a, #} & /@
      RandomSample[
       neighbors[a],
       Clip[
        Round[RandomVariate[meetingCounts[a]]], {0, 
         Length[neighbors[a]]}]]
     ] /@ Keys[neighbors],
   1], Sort]

What I want to do is make each agent interact with every possible neighbor, not a random subset of neighbors, as in the original code.

I came up with the following solution:

meetingCounts = 
     AssociationThread[Keys[neighbors], UniformDistribution[{Length[neighbors[1]], Length[neighbors[1]]}]]]

So, since in my code the maximum number of neighbors is achieved in agent 1, the code above can do the job, but it is not an ideal solution.

I guess that a simple modification of the meeting function above would be sufficient for my needs, but I am not very familiar with the Mathematica syntax.

Can anybody have an idea of how to modify the meeting function above so each agent will pick every neighbor to interact, instead of a random sample of possible neighbors?

Thanks

POSTED BY: Rafael Guariento

I figured it out.

Just replace the whole RandomSample for the vector of associations:

meetings = DeleteDuplicatesBy[Flatten[
   Function[a,
     {a, #} & /@
      neighbor[a]
     ] /@ Keys[neighbors],
   1], Sort]
POSTED BY: Rafael Guariento
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract