All posts by Manuel

Pseudo-Hands-On Single Agents

After the outline of theoretic considerations concerning the behavior of single agents its time for implementation. In this post I will develop a pseudo-code to model the centralized version of the single mating behavior.

===START: 1. time step===

In the first time step the agent post their ‘wishes’ to the board. To do so they first have to build their intention and draw a location. Both actions involve a random component which we will simulate using just a simple uniform-RNG. But lets first look at the code on a higher level:

  int = d.calculate_intention(AGE, PID, SEXOR)

  if int == 1:

     loc = d.calculate_location(AGE, SEXOR, PID)

     f.post AGE, loc, SEXOR, INFEC

A note on notation: Upper-case variables indicate memory variables of the agent, the prefix ‘d.’ indicates that this function will be defined later and ‘f.’ marks the function a FLAME-specific.

So the agent builds its intention (conditional on its age, if it is in a partnership or not [indicated by the fact that it finds a valid partner ID] and sexual orientation) if the intention is zero the agent will not engage in any partner seeking activities. When ‘int ==1’ is true the agent draws a location and posts all relevant information to the message board.

How does the agent calculate its intention? It basically calculates the probability of having sex (conditional on its age, …) and takes the square-root as we assume that the agents are symmetric. This probability is then used as the parameter for a bernoulli RV.

  DEFINE caluclate_intention(AGE, PID, SEXOR) => OUTPUT: int = {0,1}

  pid = 0, int = 0 

  if PID != 0

    pid = 1

  p = b0 + b1 * AGE + b2 * pid + b3 * SEXOR

  if rand(1) <= p

    int = 1

 

Now that the agent is able to calculate its intention we turn to the location function. Let’s assume that there are three locations. Further we define the chance for unfaithful behavior of agents within a partnership to be 3 %. Lastly we define two age groups each with the following probabilities to be within a location: loc = 1 0.5/0.2, loc= 2 0.25/0.1, loc = 3 0.25/0.7. The random numbers are again derived by a (simple) inversion method (see for example Bucklew 2004, p. 8ff)

  DEFINE calculate_location(AGE, PID, SEXOR)

  if PID != 0

    x = rand(1)

    if x <= 0.97

      loc = PID * ID

    if  0.97 < x <= 0.98

       loc = 1

    if  0.98 < x <= 0.99

       loc =2

    else

      loc=3

  else

    if AGE < AGE1

x = rand(1)

if  x <= 0.5 

        loc = 1

      if  0.5 < x <= 0.75

       loc =2

      else

       loc=3

else

     …

=== 2. time step ==

Now its time for the

 

Bucklew, JA (2004): Introduction to Rare Event Simulation. Berlin: Springer-Verlag.

 

Functional Structure of the Model – Matching Agents by intentions

After modeling the sexual behavior in partnerships the next challenge was to model the behavior of single agents.

The behavior of agents representing single persons is more complex as there are many potential partners. As the agents should be autonomous there is the need to implement an framework that seperates the intention from the actual sexual action. In a very basic model each agents builds an intention — in this case a simple 0 or 1 decision — and communicates this intention to potential partners. Furthermore for singles there is also a search or screening process involved in the behavior.

To model partner selection an agent posts not only its intention and sexual orientation but also a “matching score” as well as its location. Only if there is a “proper partner” with the same intention and in the same location those two agents engage in sexual activity. The interaction of the matching score and the intention-building process need to ensure that the number of sexual contacts in the model are in line with the empirical estimates.

In our first model we decided to use the ID as a pseudo-matching-score; agents that have IDs close to each other are matched. The random part enters via the location variable. Each agents draw a location at random but depending on its sexual orientation and age. Furthermore the agent calculates its intention. If the intention is positive the agent posts its location, sexual orientation.

Next the agents in a location have to be matched up. There are actually two ways to do this (see attached picture).Matching

First in a no-centralized solution each agent is doing its own calculation. Each agent may decide independently if it is a best match for a potential partner or if it will not realize a sexual contact. To know this the agent needs to rule out the possibility that another agent is a better match.  So the calculation needs have a unique solution realizing all minimum distance pairs. Everyone with some exposure to operations research will realize that after a distance matrix is computed this is a (simple) allocation problem solvable by the simplex method  [see for example the classic Dantzig (1963), Chap 15]. Eventhough there are special purpose algorithms [especially the hungarian method see Carpaneto et al. (1988)] it seems that letting millions of agents solve a linear program ‘everyday’ which even yields the exact same results when the agents are in the same location seems to be a huge waste of computing time.
Because we only want to use the ID as a matching score a second approach is based on random matching. When using a similar seed that is know to all agents within a location each agent is able to realize the exact same random matching. Basically a random sample of the sex with a ‘surplus supply’ is drawn to realize all possible contacts.  A shared seed might be the sum of all IDs in a location because that variable is observable by all agents within the location.

A drawback of the non-centralized solution is that its random. If – later in the model building process – a real matching-score is implemented it may be desirable to treat the matching process as the allocation problem sketched above. However because the solution to this problem is unique its the same for all agents within a location so the calculation may best be done by a centralized authority. In the second approach the StatAgent is this authority. It collects all ‘sex posts’ computes the optimal matches and returns the results to the agents. To model the disease spread the agents either post their infection status or the StatAgent has to keep track of the infections in its memory.

As we are foolish enough to assume that computing power is no problem, we will try the first option and see if it will work. If not we either need to buy a super-computer (preferrably) or switch to the second option.

References

Dantzig, GB (1963): Linear Programming and Extensions. Princeton: Princeton University Press.

Carpaneto, G; Martello, S; Toth, P (1988): Algorithms and Codes for the Assignment Problem. In: Annals of Operations Research (13): 193 -223.