CS 420/527 — Biologically Inspired Computation
NetLogo Simulation

SIPD — Spatial Iterated Prisoner's Dilemma


This page was automatically generated by NetLogo 5.0beta2. Questions, problems? Contact feedback@ccl.northwestern.edu.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site. If the display appear cut off with Firefox, then try another browser (Safari works).



powered by NetLogo

view/download model file: SIPD-async.nlogo

WHAT IS IT?

This model simulates the Iterated Prisoner’s Dilemma (IPD) in a spatial environment in which agents can adopt the best strategy of their neighbors. The agents obey one of five different strategies: All-C (always cooperate), All-D (always defect), TFT (Tit-for-tat, i.e., act the same way your opponent acted last time), Pavlov (win-stay, lose-shift, i.e., reverse your strategy when your opponent defects), and Rand (randomly cooperate or defect). TFT and Pav always begin by cooperating. In addition, there can be noise in the environment, which may cause an agent to behave oppositely to what it intended.

The reward for various combinations of cooperation and defection are determined by a payoff matrix.

After a specified number of rounds, an agent adopts the strategy of its most succesful neighbor, unless they all did worse than the agent, in which case it keeps its current strategy. If the mutation rate is non-zero, then an agent may adopt a random strategy.

HOW IT WORKS

Each patch corresponds to an agent which interacts with its neighbors for a specified number of rounds. At the end of this number of rounds (called a “generation”), the agent assesses its performance. If it has done at least as well as its neighbors, it keeps its current strategy, otherwise it adopts the strategy of its best performing neighbor. However, if the mutation probability is nonzero, it adopts a random strategy at the end of the generation.

HOW TO USE IT

SETUP is used to initialize a new simulation.
GO FOR… runs for a specified number of generations
GO runs when clicked, stops when unclicked.
ROUNDS determines the number of rounds of interaction (or game play) per generation.

The second group of sliders controls the payoff matrix (CC-R, CD-S, DC-T, DD-P). You will not need to change them for this project unless you want to investigate effects of the payoff matrix. For example DC-T is your reward when you defect but your opponent cooperates (the “Temptation”).

The next group of sliders controls the initial populations of the five strategies (N-All-C, N-All-D, N-TFT, N-Pav, N-RAN). Only the relative values of these quantities matter, and they are used to probabilistically initialize the starting population.

RANCOOP controls the Rand strategy’s probability of cooperating.
NOISE controls the probability that in a given interaction a cell will act randomly rather than in accord with its strategy.
MUTATION controls the probability that a new agent will adopt a random strategy, rather than the best strategy of itself and its neighbors.

INSERT-STRATEGY allows you to select the strategy to be inserted.
TOGGLE INSERT allows you to use the mouse to paint the chosen strategy anywhere in the space. Click to paint.

The current generation number and the populations of the different strategies are shown by the six monitors. Note that the total population (number of cells) is 2500.

METRIC controls the plot, either the size of the subpopulations or their running average scores.

THINGS TO NOTICE

Watch for the formation of clusters of homogeneous strategies, which then spread.

THINGS TO TRY

This section could give some ideas of things for the user to try to do (move sliders, switches, etc.) with the model.

EXTENDING THE MODEL

Try different neighborhood relations.

NETLOGO FEATURES

This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.

RELATED MODELS

This model is an extension and reimplementation in NetLogo of the sipd program described in ch. 17 of Gary William Flake’s “Computational Beauty of Nature” (MIT Press, 1998).

CREDITS AND REFERENCES

To refer to this model in academic publications, please use: MacLennan, B.J. (2007). NetLogo Spatial Iterated Prisoner’s Dilemma (SIPD) model. http://www.cs.utk.edu/~mclennan. Dept. of Electrical Engineering & Computer Science, Univ. of Tennessee, Knoxville.

In other publications, please use: Copyright 2007, 2012 Bruce MacLennan. All rights reserved. See http://web.eecs.utk.edu/~mclennan/420/NetLogo/SIPD-async-alter.html for terms of use.

CODE

globals [
  All-D-color
  All-C-color
  TFT-color
  Rand-color
  Pav-color
  color-list
  pen-names
  num-strategies
  gen
  pop
  rel-nbrs
  inserting
  pops
]

patches-own [
  age
  strategy
  score
  previous-cooperations
  plan-coop?
]

to startup
  set All-D-color red
  set All-C-color black
  set TFT-color green
  set Rand-color blue
  set Pav-color violet
  set color-list (list
   All-D-color All-C-color TFT-color Rand-color Pav-color)
  set pen-names ["All-D" "All-C" "TFT" "Rand" "Pav"]
  set num-strategies 5
  set rel-nbrs [[0 1] [1 1] [1 0] [1 -1] [0 -1] [-1 -1] [-1 0] [-1 1]]
end

to setup
  ca
  startup
  set pop N-All-D + N-All-C + N-TFT + N-Ran + N-Pav
  set pops map [? / pop] (list N-All-D N-All-C N-TFT N-Ran N-Pav)
  ask patches [ initialize-patch ]
  set gen 0
  reset-ticks
  do-plots
end

to initialize-patch
  let choice random pop
    ifelse choice < N-All-D [ init-patch-strategy 0 ]
  [ ifelse choice < (N-All-D + N-All-C) [ init-patch-strategy 1 ]
  [ ifelse choice < (N-All-D + N-All-C + N-TFT) [ init-patch-strategy 2 ]
  [ ifelse choice < (N-All-D + N-All-C + N-TFT + N-Ran) [ init-patch-strategy 3 ]
  [ init-patch-strategy 4 ]]]]
  set age random rounds
end

to init-patch-strategy [strat]
    ifelse strat = 0 [ initialize-All-D ]
  [ ifelse strat = 1 [ initialize-All-C ]
  [ ifelse strat = 2 [ initialize-TFT ]
  [ ifelse strat = 3 [ initialize-Rand ]
  [ initialize-Pav ]]]]
  set pcolor item strategy color-list
  set score 0
  set previous-cooperations n-values 8 [true]
end

to initialize-All-D
  set strategy 0
  set plan-coop? n-values 8 [false]
end

to initialize-All-C
  set strategy 1
  set plan-coop? n-values 8 [true]
end

to initialize-TFT
  set strategy 2
  set plan-coop? n-values 8 [true]
end

to initialize-Rand
  set strategy 3
  set plan-coop? n-values 8 [ (random-float 1 < RANcoop) ]
end

to initialize-Pav
  set strategy 4
  set plan-coop? n-values 8 [true]
end

to go
  repeat rounds [ do-one-round ]
  set gen gen + 1
  do-plots
end

to do-one-round
  ask patches [ plan-act-with-all-neighbors ]
  ask patches [ do-act-with-all-neighbors ]
end

to plan-act-with-all-neighbors
  (foreach n-values 8 [?] previous-cooperations [
    plan-act-with-one-neighbor ?1 ?2
  ])
end

to plan-act-with-one-neighbor [nbr prev-coop?]
    ifelse strategy = 0 [ All-D-interact nbr prev-coop? ]
  [ ifelse strategy = 1 [ All-C-interact nbr prev-coop? ]
  [ ifelse strategy = 2 [ TFT-interact nbr prev-coop? ]
  [ ifelse strategy = 3 [ Rand-interact nbr prev-coop? ]
  [ Pav-interact nbr prev-coop? ]]]]
  if noise > 0 [
    if random-float 100 < noise [
      set plan-coop? replace-item nbr plan-coop? not (item nbr plan-coop?) ]]
end

to All-D-interact [nbr prev-coop?]
  set plan-coop? replace-item nbr plan-coop? false
end

to All-C-interact [nbr prev-coop?]
  set plan-coop? replace-item nbr plan-coop? true
end

to TFT-interact [nbr prev-coop?]
  ifelse prev-coop?
   [ set plan-coop? replace-item nbr plan-coop? true ]
   [ set plan-coop? replace-item nbr plan-coop? false ]
end

to Rand-interact [nbr prev-coop?]
  set plan-coop? replace-item nbr plan-coop? (random-float 1 < RANcoop)
end

to Pav-interact [nbr prev-coop?]
  if not prev-coop? 
  [ set plan-coop? replace-item nbr plan-coop? not (item nbr plan-coop?) ]
end

to do-act-with-all-neighbors
  (foreach n-values 8 [?] plan-coop? [
    let ndx item 0 (item ?1 rel-nbrs)
    let ndy item 1 (item ?1 rel-nbrs)
    let rev-index position (list (- ndx) (- ndy)) rel-nbrs
    do-act-with-one-neighbor ?1 ?2
      [item rev-index plan-coop?] of patch-at ndx ndy
  ])
  set age age + 1
  if age > rounds [ evolve ]
end

to do-act-with-one-neighbor [nbr coop? nbr-coop?]
    ifelse strategy = 0 [ All-D-act nbr coop? nbr-coop? ]
  [ ifelse strategy = 1 [ All-C-act nbr coop? nbr-coop? ]
  [ ifelse strategy = 2 [ TFT-act nbr coop? nbr-coop? ]
  [ ifelse strategy = 3 [ Rand-act nbr coop? nbr-coop? ]
  [ Pav-act nbr coop? nbr-coop? ]]]]
end

to All-D-act [nbr coop? nbr-coop?]
  ifelse nbr-coop?
   [ set score score + DC-T ]
   [ set score score + DD-P ]
end

to All-C-act [nbr coop? nbr-coop?]
  ifelse nbr-coop?
   [ set score score + CC-R ]
   [ set score score + CD-S ]
end

to TFT-act [nbr coop? nbr-coop?]
  ifelse coop?
    [ ifelse nbr-coop?
      [ set score score + CC-R
        set previous-cooperations replace-item nbr previous-cooperations true ]
      [ set score score + CD-S
        set previous-cooperations replace-item nbr previous-cooperations false ]]
    [ ifelse nbr-coop?
      [ set score score + DC-T
        set previous-cooperations replace-item nbr previous-cooperations true ]
      [ set score score + DD-P
        set previous-cooperations replace-item nbr previous-cooperations false ]]
end

to Rand-act [nbr coop? nbr-coop?]
  ifelse coop?
  [ ifelse nbr-coop?
    [ set score score + CC-R ]
    [ set score score + CD-S ] ]
  [ ifelse nbr-coop?
    [ set score score + DC-T ]
    [ set score score + DD-P ] ]
end

to Pav-act [nbr coop? nbr-coop?]
  ifelse coop?
  [ ifelse nbr-coop?
    [ set score score + CC-R
      set previous-cooperations replace-item nbr previous-cooperations true ]
    [ set score score + CD-S
      set previous-cooperations replace-item nbr previous-cooperations false ] ]
  [ ifelse nbr-coop?
    [ set score score + DC-T
      set previous-cooperations replace-item nbr previous-cooperations true ]
    [ set score score + DD-P
      set previous-cooperations replace-item nbr previous-cooperations false ] ]
end

to evolve
  ifelse mutation > 0 and random-float 1 < mutation [
    set strategy random num-strategies
  ][
  let best-nbr one-of (neighbors with-max [score])
  if score < [score] of best-nbr
    [ set strategy [strategy] of best-nbr
      set pcolor item strategy color-list ]
  ]
  set previous-cooperations n-values 8 [true]
  set score 0
  set age 0
end

to do-plots
  set pops map [count patches with [strategy = ?]] n-values num-strategies [?]
  ifelse metric = "population"
  [
    set-plot-y-range 0 world-width * world-height
    foreach n-values num-strategies [?] [
      set-current-plot-pen item ? pen-names
      plot item ? pops
    ]
  ][
    set-plot-y-range 0 max (list CC-R CD-S DC-T DD-P)
    foreach n-values num-strategies [?] [
      set-current-plot-pen item ? pen-names
      plot (mean lput 0 [score] of patches with [strategy = ?]) / (rounds * 8)
    ]
  ]
end

to deposit-agents
if mouse-down?
 [ask patches
  [if ((abs (pxcor - mouse-xcor)) < 0.5) and ((abs (pycor - mouse-ycor)) < 0.5)
   [ init-patch-strategy position insert-strategy ["All-D" "All-C" "TFT" "Rand" "Pav"]
     set age rounds ]
 ]]
  set pops map [count patches with [strategy = ?]] n-values num-strategies [?]
end

Return to COSC 420/527 home page

Return to MacLennan's home page

Send mail to Bruce MacLennan / MacLennan@utk.edu

Valid HTML 4.01!This page is web.eecs.utk.edu/~mclennan/Classes/420/NetLogo/SIPD-async.html
Last updated: 2012-04-25.