For a description, read below. Note that the size of the simulation is rather large, 1271 x 614 pixels, and I haven't seen a browser that supported a zoomout. So make sure you have a proper resolution screen, or download/run the original model (which you can zoom in in NetLogo 4.1).

powered by NetLogo

view/download model file: SexNet.nlogo

A sexual contact and HIV spreading network model.

The left hand side controls the basic parameters such as the mode of sexual contact and the method by which sexual relations end (time, or probability).

The right hand side can introduce infections. One thing to notice is that the infection model is that of HIV, where infected patients never recover (grey) but do not infect (red) after a short interval (in actual HIV, they may infect again after a long time is passed, when the disease is taking over, but here we assume that patients abandon risky modes of sexual behavior when they are in that phase.)

Experience handling (adding plus one to give nonzero chance for virgins to mate) changes proportions slightly in virgin mode. Perhaps add +1 experience at setup but never more is better.

Copyright 2010 George Kampis using code excerpts from Uri Wilensky. All rights reserved.

Permission to use, modify or redistribute this model is hereby granted, provided that both of the following requirements are followed:

a) this copyright notice is included.

b) this model will not be redistributed for profit without permission
from Uri Wilensky. Contact Uri Wilensky for appropriate licenses for
redistribution for profit.

links-own [age max-age] ;; links represent sexual relations, age tells how old a relation is; max age is when it will be broken turtles-own [ adherence ;; in adherence-based mode, the preferred length of a relationship experience ;; eufemism for how many sexual partners the agent has had, ranges from 0 to... whatever t-experience ;; experience in a time window (to study sampling) infected? ;; if true, the person is infected. It may be infecting or not infecting. infecting? ;; if true, the person is infecting (hence, infected? must also be true). infection-length ;; how long an agent has been infected own-component-size ;; variable for identifying connected components explored? ;; helper variable for the above ] globals [ kamu component-size ;; number of turtles explored so far in the current component giant-component-size ;; number of turtles in the giant component giant-start-node ;; node from where we started exploring the giant component virgins-mate ;; can virgins (ie agents with zero experience) mate with virgins? makes a big difference ] ;;;;;;;;;;;;;;;;;;;;;;;; ;;; Setup Procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;;; to setup

clear-all ;;ask patches [set pcolor white] set-default-shape turtles "circle" ;; make the initial network of two turtles and an edge make-initialnode nobody ;; first node, unattached make-initialnode turtle 0 ;; second node, attached to first node crt num-nodes - 2 [set color sky + 2] ;; lonely agents wait in an outside circle... layout-circle turtles with [not any? link-neighbors] max-pycor - 1 set virgins-mate 0 if virgins-can-mate [set virgins-mate 1]

ask turtles

[set infected? false set infecting? false]

if adherence-matters

[ let id 0 repeat num-nodes - 2

[ask turtle id

[set adherence (max-adherence - (max-adherence - 1) * id / num-nodes)

set id id + 1]

]

]

end ;;;;;;;;;;;;;;;;;;;;;;; ;;; Main Procedures ;;; ;;;;;;;;;;;;;;;;;;;;;;; to go

;; new edge is green, old edges are recolored yellow NOTE> now changed to all is yellow ask links [ set color yellow set age age + 1]

;; this is just housekeeping ask turtles [ if infected? [ set infection-length infection-length + 1 ]]

let num-times multiples-in-a-step ;; this is how many new links are formed in a single time step, ;; useful to have less lonely agents: total edge number is a function of this number repeat num-times ;; this is how the above is done... [ make-node find-partner ;; find first partner & use it as attachment point for connecting (or making in the net) another node ] ;; infection part ask turtles [infect]

ask turtles [check-symptoms]

ask turtles [assign-color]

;; infection part ends ;; so far the real stuff, below this point just link breakup, plotting and more of housekeeping tick ifelse die-by-age [ask links [if (age > max-age) [die]]]

[ask links [if (random-float 100) < (100 * p) [die]]] ;; either links have a max age to die, or die by probability p layout ;; show the net layout-circle turtles with [not any? link-neighbors] max-pycor - 1 ;; the lonely agents go to the outer circle if show-components ;; produce and show the histogram of connected network components [ find-all-components ] do-plotting ;; all the clumsy plotting stuff... end ;; used for creating the first link to make-initialnode [old-node]

crt 1 [ set color lime + 3 if old-node != nobody [ let link-max-age min sentence adherence [adherence] of old-node ;; we must compute this before jumping to link context but only needed in adherence mode create-link-with old-node

[ set color yellow

set age -1 * random initial-age-randomization

ifelse adherence-matters

[ set max-age link-max-age

]

[set max-age age-limit]

] ;; was green.. now use same yellow as old links set experience 1 ;; if you have sex, you are not a virgin, are you... set t-experience 1 move-to old-node

fd 8 ;; position the newly connected node near its partner ] ] end ;; used for creating links between two different nodes where... to make-node [old-node] ;; ...old-node is the "proactive partner" that looks for a new mate via make-node (better called make-contact) let new-node nobody ;; ...and new-node is her new lover, ie. nobody yet ifelse preferential-choice and assortative-mating

;; assortative mating means first of all that the new guy will not be necessarily someone w/o a partner. ;; quite the opposite, agents with greater experience are more likely to be selected as a partner. ;; (BUT this model is NOT double assortative, where like begets like: small with small, great with great). ;; ;; the logic is that first an old-node was selected via a find-partned call in the GO procedure. ;; in preferential mode, this operation picks an agent with a probability proportional to its experience. ;; ;; then, in assortative mode, we dont just randomly pick any lonely agent secondly (anybody can be lonely here, ;; not just those w/o a partner), but we find one, again, with probability proportional to its experience. ;; ;; so, highly probable events are hubs w/ hubs, medium p events are hubs w/ virgins, less probable are virgins w/ virgins [set new-node find-lonely]

[set new-node one-of turtles with [not any? link-neighbors]] ;; the basic case: new-node is a random pick from those w/o a partner if new-node = old-node [stop] ;; self-sex is not real sex... so no link is formed (can see the effect on the plot sometimes...) ask new-node ;; so we have a new guy... [ if old-node != nobody ;; ... and unless the old guy dies or otherwise inexists (it happened...), a link is formed, tadamm. [ let link-max-age min sentence adherence [adherence] of old-node ;; we must compute this before jumping to link context but only needed in adherence mode create-link-with old-node

[ set color yellow

set age -1 * random initial-age-randomization

ifelse adherence-matters

[ set max-age link-max-age

]

[set max-age age-limit]

] ;; was green.. now use same yellow as old links ] ;move-to old-node ;; uncomment this experimentally and then decide fd -1 ;; a drawing trick, dont let newly connected nodes collapse, no matter what set experience experience + 1 ;; experience grows for both nodes of a new link set t-experience t-experience + 1 ask old-node [

set experience experience + 1

set t-experience t-experience + 1]

]

end to-report find-partner ;; dont let the name trick you, this finds a FIRST guy, ie. the proactive one, looking for a mate let partner nobody if serial-monogamy ;; serial monogamy = only agents w/o partner can mate. implies virgins-can-mate or nothing happens at startup [set partner one-of turtles with [count link-neighbors = 0] report partner]

ifelse preferential-choice ;; there are two modes here, preferential, which uses code from a lottery w/ multiple tickets ;; and random uniform, below the preferential, which picks any one guy and finito. ;; [let total random-float sum [experience + virgins-mate] of turtles ;; a bit of a trick here: if virgins can mate, we shift experience by 1 so they have one ticket. ask turtles [ let nc experience + virgins-mate

;; if there's no winner yet... if partner = nobody [ ifelse nc > total

[ set partner self ]

[ set total total - nc ]

]

]

report partner

]

;; below this line, the non-preferential mode, ie. random uniform selection of partner [ ifelse virgins-can-mate [set partner one-of turtles][set partner one-of turtles with [experience > 0]] ;; select any agent, or anyone with experience report partner

]

end to-report find-lonely ;; misleading name again, lonely is the proactive agent that seeks a partner, we call this function in assortative mode ;; which means more experienced, and probably linked, agents have a higher chance to be selected let total random-float sum [experience + 1] of turtles ;; here the plus one is a must, or nothing ever happens at startup, where everybody has zero let partner nobody ask turtles

[

let nc (experience + 1)

;; if there's no winner yet... if partner = nobody [ ifelse nc > total

[ set partner self ]

[ set total total - nc ]

]

]

report partner

end ;;;;;;;;;;;;;;;;;;;; ;; Infection part ;; ;;;;;;;;;;;;;;;;;;;; to random-infection ;; you may get infected only if you are not a virgin and not currently inactive let nums (count turtles with [count link-neighbors > 0]) * actives-to-infect / 100 ask n-of nums turtles with [count link-neighbors > 0] [set infected? true]

ask turtles [

if infected? [ ifelse (experience = 0) or (count link-neighbors = 0) [ set infected? false set infecting? false]

[ set infection-length random-float stop-infecting set infecting? true set color red]]

]

end to experienced-infection ;; as random infection, except that we infect agents with most experience let nums (count turtles with [count link-neighbors > 0]) * actives-to-infect / 100 ask max-n-of nums turtles [experience] [set infected? true]

ask turtles [

if infected? [ ifelse (experience = 0) or (count link-neighbors = 0) [ set infected? false set infecting? false]

[ set infection-length random-float stop-infecting set infecting? true set color red]]

]

end to infect ;; turtle procedure if (count link-neighbors > 0) and infected? and infecting?

[ ask link-neighbors [ if random-float 100 < p-of-infection-per-step

[ set infected? true set infecting? true ] ] ]

end to check-symptoms

if (infection-length > stop-infecting) [set infecting? false]

end to-report %infected

ifelse any? turtles [ report (count turtles with [infected?] / count turtles) * 100 ]

[ report 0 ]

end to-report num-infected

report (count turtles with [infected?])

end to-report num-infecting

report (count turtles with [infecting?])

end to-report %infecting

ifelse any? turtles [ report (count turtles with [infecting?] / count turtles) * 100 ]

[ report 0 ]

end to-report num-active

report (count turtles with [count link-neighbors > 0])

end to-report %infected-active

ifelse any? turtles [ report (count turtles with [infected? and count link-neighbors > 0] / count turtles with [count link-neighbors > 0]) * 100 ]

[ report 0 ]

end ;; to find all the connected components in the network, their sizes and starting turtles to find-all-components

ask turtles [ set explored? false set own-component-size 0]

;; keep exploring till all turtles get explored loop [ ;; pick a node that has not yet been explored let starting-agent one-of turtles with [ not explored? ]

if starting-agent = nobody [ stop ]

;; reset the number of turtles found to 0 ;; this variable is updated each time we explore an ;; unexplored node. set component-size 0 ;; at this stage, we recolor everything to light gray ask starting-agent [ explore ]

ask starting-agent [ set own-component-size component-size]

;; the explore procedure updates the component-size variable. ;; so check, have we found a new giant component? if component-size > giant-component-size

[

set giant-component-size component-size

set giant-start-node starting-agent

]

]

end to assign-color ;; turtle procedure ifelse (experience = 0) [set color sky + 2 set infected? false set infecting? false]

[ifelse not infected?

[ set color lime + 3 ]

[ ifelse infecting?

[ set color red ]

[ set color gray ] ]]

end ;; Finds all turtles reachable from this node (and recolors them) to explore ;; node procedure if explored? [ stop ]

set explored? true set component-size component-size + 1 ;; color the node ;set color new-color ask link-neighbors [ explore ]

end ;;;;;;;;;;;;;;;; ;;; Plotting ;;; ;;;;;;;;;;;;;;;; to do-plotting ;; plotting procedure set-current-plot "Degree Distribution" let max-experience max [experience] of turtles set-plot-x-range 1 (max-experience + 1)

set-current-plot-pen "cumulative-degree" plot-pen-reset histogram [experience] of turtles set-current-plot-pen "degree" plot-pen-reset ;; erase what we plotted before let max-degree max [count link-neighbors] of turtles ; set-plot-x-range 1 (max-degree + 1) ;; + 1 to make room for the width of the last bar histogram [count link-neighbors] of turtles ;; for the next plot, the axes are logarithmic, so we can't ;; use "histogram-from"; we have to plot the points ;; ourselves one at a time set-current-plot "Degree Distribution (log-log)" set-current-plot-pen "degree" plot-pen-reset ;; erase what we plotted before ;; the way we create the network there is never a zero degree node, ;; so start plotting at degree one let degree 1 while [degree <= max-degree]

[

let matches turtles with [count link-neighbors = degree]

if any? matches

[ plotxy log degree 10 log (count matches) 10 ]

set degree degree + 1 ] set-current-plot-pen "degree-dot" plot-pen-reset ;; erase what we plotted before ;; the way we create the network there is never a zero degree node, ;; so start plotting at degree one set degree 1 while [degree <= max-degree]

[

let matches turtles with [count link-neighbors = degree]

if any? matches

[ plotxy log degree 10 log (count matches) 10 ]

set degree degree + 1 ] set-current-plot-pen "cumulative-degree" plot-pen-reset ;; erase what we plotted before ;; the way we create the network there is never a zero degree node, ;; so start plotting at degree one let cumulative-degree 1 while [cumulative-degree <= max-experience]

[

let cumulative-matches turtles with [experience = cumulative-degree]

if any? cumulative-matches

[ plotxy log cumulative-degree 10 log (count cumulative-matches) 10 ]

set cumulative-degree cumulative-degree + 1 ] set-current-plot-pen "timed-degree" plot-pen-reset let max-t-experience max [t-experience] of turtles set cumulative-degree 1 while [cumulative-degree <= max-t-experience]

[

let cumulative-matches turtles with [t-experience = cumulative-degree]

if any? cumulative-matches

[

plotxy log cumulative-degree 10 log (count cumulative-matches) 10 ]

set cumulative-degree cumulative-degree + 1 ] set-current-plot-pen "cumulative-degree-dot" plot-pen-reset ;; erase what we plotted before ;; the way we create the network there is never a zero degree node, ;; so start plotting at degree one set cumulative-degree 1 while [cumulative-degree <= max-experience]

[

let cumulative-matches turtles with [experience = cumulative-degree]

if any? cumulative-matches

[

plotxy log cumulative-degree 10 log (count cumulative-matches) 10 ]

set cumulative-degree cumulative-degree + 1 ] let cumulative-links sum [experience] of turtles ;; set-current-plot "Density" set-current-plot-pen "current" plot count links / (num-nodes * num-nodes)

set-current-plot-pen "summative" plot cumulative-links / (num-nodes * num-nodes * 2) ;; two nodes is one link, divide by 2 ;; set-current-plot "Populations" set-current-plot-pen "HIV-" plot count turtles with [not infected?] ;and count link-neighbors > 0] set-current-plot-pen "HIVall" plot count turtles with [infected?]

set-current-plot-pen "HIV+" plot count turtles with [infecting?]

;; set-current-plot "Experience" histogram [experience] of turtles set-current-plot "Link-age" set kamu [age] of links histogram kamu

;; if show-components

[

set-current-plot "Component Distribution" clear-plot histogram [own-component-size] of turtles with [own-component-size > 0]

]

end to-report num-components

report count turtles with [own-component-size > 1]

end ;;;;;;;;;;;;;; ;;; Layout ;;; ;;;;;;;;;;;;;; ;; resize-nodes, change back and forth from size based on degree to a size of 1 to resize-nodes

ifelse all? turtles [size <= 1]

[

;; a node is a circle with diameter determined by ;; the SIZE variable; using SQRT makes the circle's ;; area proportional to its degree ask turtles [ set size sqrt experience ]

]

[

ask turtles [ set size 1 ]

]

end to layout

;; the number 3 here is arbitrary; more repetitions slows down the ;; model, but too few gives poor layouts repeat 3 [

;; the more turtles we have to fit into the same amount of space, ;; the smaller the inputs to layout-spring we'll need to use let factor sqrt count turtles ;; numbers here are arbitrarily chosen for pleasing appearance layout-spring turtles with [any? link-neighbors] links (1 / factor) (7 / factor) (1 / factor)

display ;; for smooth animation ] ;; don't bump the edges of the world let x-offset max [xcor] of turtles + min [xcor] of turtles let y-offset max [ycor] of turtles + min [ycor] of turtles ;; big jumps look funny, so only adjust a little each time set x-offset limit-magnitude x-offset 0.1 set y-offset limit-magnitude y-offset 0.1 ask turtles [ setxy (xcor - x-offset / 2) (ycor - y-offset / 2) ]

end to-report limit-magnitude [number limit]

if number > limit [ report limit ]

if number < (- limit) [ report (- limit) ]

report number

end ; Copyight 2010 by George Kampis. Using code by Uri Wilensky and others as execution modules.