Skip to content

Comprehensive example

sbalev edited this page Apr 24, 2012 · 25 revisions

In this tutorial we will work with another NetLogo sample model, AIDS. It simulates the spread of HIV in a population. We are going to extract and analyze three graphs from this model.

  1. Couple graph. The nodes of this graph are the people in our population. An edge appears between two individuals when they are in couple and disappears when they break up. We will display this graph in a viewer. The positions and the colors of the nodes will be the same as in the NetLogo simulation.

  2. Infection graph. This is a "who infected who" graph. At the beginning it will contain only several isolated nodes, the initially infected individuals. When someone is infected, we will add a new node in the graph together with an arc from the person who infected him. We will display this graph with automatic layout. The initial infection sources will be colored in red and the remaining nodes in blue.

  3. Cumulative graph. This one is like the couple graph, but edges do not disappear when people break up. We will color the nodes of this graph as in the NetLogo simulation, but we will use an automatic layout. This graph is very important and we will come back to it in the second part of this tutorial.

TODO: Add screenshots for the three graphs

Sending graph events from NetLogo

We will use three different senders, one for each graph. In the following procedure we clear all previously created senders, create our three senders, send 'graph cleared' events using each of them and setup style sheets for each graph.

to setup-senders
  gs:clear-senders  

  let stylesheet (word "node {size: 10px;} "
    "edge {fill-color: grey;} "
    "node.healthy {fill-color: green;} "
    "node.unknown {fill-color: blue;} "
    "node.infected {fill-color: red;}")

  (foreach (list "couple" "infection" "cumulative") (list 2001 2002 2003) [
    gs:add-sender ?1 "localhost" ?2
    gs:clear ?1
    gs:add-attribute ?1 "ui.antialias" true
    gs:add-attribute ?1 "ui.stylesheet" stylesheet
  ])
  
  gs:add-attribute "infection" "ui.stylesheet" 
    "node {fill-color: blue;} node.source {fill-color: red;}"  
end

We will call this procedure at the beginning of setup

to setup
  setup-senders
  clear-all
  ...
end

Now let us take care of the nodes of our graphs. People are created in a procedure called setup-people. As you already guess, we have to make them send 'node added' events just after their creation:

to setup-people
  crt initial-people [
    gs:add "couple"
    gs:add "cumulative"
    ...
    set infected? (who < initial-people * 0.025)
    if infected? [
      gs:add "infection"
      set infection-length random-float symptoms-show
    ]
    ...
  ]
end

Note that we add all the people to the couple graph and to the cumulative graph, but only the initially infected people to the infection graph. We will add other nodes to the last graph dynamically, when new people are infected. To do this, we have to slightly modify the infect procedure:

to infect
  if coupled? and infected? and not known? and [not infected?] of partner [
    if random-float 11 > condom-use or random-float 11 > ([condom-use] of partner) [
      if random-float 100 < infection-chance [
        ask partner [
          set infected? true
          gs:add "infection"
        ]
      ] 
    ]
  ]
end

Now the nodes are added correctly to the three graphs, but we also have to send some of their attributes. To color them, we will use the "ui.class" attribute and the classes set up in the style sheets. Moreover, we need to send the coordinates of the people to the couple graph because we want its vertices to have the same positions as in the NetLogo simulation. We revisit the setup-people procedure and change it as follows:

to setup-people
  crt initial-people [
    gs:add "couple"
    gs:add "cumulative"
    setxy random-xcor random-ycor
    gs:add-attribute "couple" "xy" list xcor ycor
    ...
    set infected? (who < initial-people * 0.025)
    if infected? [
      gs:add "infection"
      gs:add-attribute "infection" "ui.class" "source"
      set infection-length random-float symptoms-show
    ]
    ...
  ]
end

The colors are assigned in the procedure assign-color which is called on each step. We change it as follows:

to assign-color  ;; turtle procedure
  ifelse not infected? [
    set color green
    gs:add-attribute "couple" "ui.class" "healthy"
    gs:add-attribute "cumulative" "ui.class" "healthy"
  ][
    ifelse known? [
      set color red
      gs:add-attribute "couple" "ui.class" "infected"
      gs:add-attribute "cumulative" "ui.class" "infected"
    ][
      set color blue
      gs:add-attribute "couple" "ui.class" "unknown"
      gs:add-attribute "cumulative" "ui.class" "unknown"
    ]
  ]
end
Clone this wiki locally