PD Iterated bdd memory

PD Iterated bdd memory preview image

1 collaborator

Default-person Adam Galambos (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by the author
Model was written in NetLogo 6.0.1 • Viewed 145 times • Downloaded 10 times • Run 0 times
Download the 'PD Iterated bdd memory' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

This model is a multiplayer version of the iterated prisoner's dilemma. It is intended to explore the strategic implications that emerge when the world consists entirely of prisoner's dilemma like interactions. If you are unfamiliar with the basic concepts of the prisoner's dilemma or the iterated prisoner's dilemma, please refer to the PD BASIC and PD TWO PERSON ITERATED models found in the PRISONER'S DILEMMA suite.

HOW IT WORKS

The PD TWO PERSON ITERATED model demonstrates an interesting concept: When interacting with someone over time in a prisoner's dilemma scenario, it is possible to tune your strategy to do well with theirs. Each possible strategy has unique strengths and weaknesses that appear through the course of the game. For instance, always defect does best of any against the random strategy, but poorly against itself. Tit-for-tat does poorly with the random strategy, but well with itself.

This makes it difficult to determine a single "best" strategy. One such approach to doing this is to create a world with multiple agents playing a variety of strategies in repeated prisoner's dilemma situations. This model does just that. The turtles with different strategies wander around randomly until they find another turtle to play with. If a turtle's scor rises above rep-score, the turtle reproduces: another turtle is created with that strategy. This results in a loss of 10 points for the reproducing turtle. If a turtle's score falls below death-score, it dies. (Note that in this version each turtle remembers their past interactions, but not whom they played against. This is an anonymous version of N-person PD.)

Payoffs

When two turtles interact, they display their respective payoffs as labels.

Each turtle's payoff for each round will determined as follows:

             | Partner's Action
  Turtle's   |
   Action    |   C       D
 ------------|-----------------
       C     |   3       �
 ------------|-----------------
       D     |   �       1
 ------------|-----------------
  (C = Cooperate, D = Defect)

(Note: This way of determining payoff is the opposite of how it was done in the PD BASIC model. In PD BASIC, you were awarded something bad- jail time. In this model, something good is awarded- money.)

HOW TO USE IT

Buttons:

SETUP: Setup the world to begin playing the multi-person iterated prisoner's dilemma. The number of turtles and their strategies are determined by the slider values.

GO: Have the turtles walk around the world and interact.

GO ONCE: Same as GO except the turtles only take one step.

Sliders:

N-WINNER: When the population with any particular strategy reaches this number, that strategy is declared the winner.

RESCALE-AFTER: Whenever the total population reaches this number, rescale the population to this number by killing of random agents, maintaining the ratio of each strategy in the population.

N-STRATEGY: Multiple sliders exist with the prefix N- then a strategy name (e.g., n-cooperate). Each of these determines how many turtles will be created that use the STRATEGY. Strategy descriptions are found below:

Strategies:

Turtles have bounded memory, and they remember only their last 500 interactions.

RANDOM - randomly cooperate or defect

COOPERATE - always cooperate

DEFECT - always defect

TIT-FOR-TAT - If an opponent cooperates on this interaction cooperate on the next interaction with them. If an opponent defects on this interaction, defect on the next interaction with them. Initially cooperate.

UNFORGIVING - Cooperate until an opponent defects once, then always defect in each interaction with them.

SNEAKY - Defect if an opponent cooperates; if an opponent defects while SNEAKY cooperates, then defect; if an opponent defects while SNEAKY defects, then cooperate.

NOTSOGRIM (trigger) - Cooperate unless partner and I defected last time we met. If last time partner defected and I cooperated, defect with probability 5/7.

UNFORGIVING2 - Cooperate until an opponent defects twice, then always defect in each interaction with them.

ANTI-TIT-FOR-TAT - If an opponent cooperates on this interaction defect on the next interaction with them. If an opponent defects on this interaction, cooperate on the next interaction with them. Initially cooperate.

ANTI-TIT-FOR-TAT-H - If an opponent cooperates on this interaction defect on the next interaction with them. If an opponent defects on this interaction, cooperate on the next interaction with them. Initially defect.

Plots:

TYPE COUNTS - The number of turtles with each strategy.

THINGS TO NOTICE

Set all the number of player for each strategy to be equal in distribution. For which strategy does the average-payoff seem to be highest? Do you think this strategy is always the best to use or will there be situations where other strategy will yield a higher average-payoff?

Set the number of n-cooperate to be high, n-defects to be equivalent to that of n-cooperate, and all other players to be 0. Which strategy will yield the higher average-payoff?

Set the number of n-tit-for-tat to be high, n-defects to be equivalent to that of n-tit-for-tat, and all other playerst to be 0. Which strategy will yield the higher average-payoff? What do you notice about the average-payoff for tit-for-tat players and defect players as the iterations increase? Why do you suppose this change occurs?

Set the number n-tit-for-tat to be equal to the number of n-cooperate. Set all other players to be 0. Which strategy will yield the higher average-payoff? Why do you suppose that one strategy will lead to higher or equal payoff?

THINGS TO TRY

  1. Observe the results of running the model with a variety of populations and population sizes. For example, can you get cooperate's average payoff to be higher than defect's? Can you get Tit-for-Tat's average payoff higher than cooperate's? What do these experiments suggest about an optimal strategy?

  2. Currently the UNKNOWN strategy defaults to TIT-FOR-TAT. Modify the UNKOWN and UNKNOWN-HISTORY-UPDATE procedures to execute a strategy of your own creation. Test it in a variety of populations. Analyze its strengths and weaknesses. Keep trying to improve it.

  3. Relate your observations from this model to real life events. Where might you find yourself in a similar situation? How might the knowledge obtained from the model influence your actions in such a situation? Why?

EXTENDING THE MODEL

Relative payoff table - Create a table which displays the average payoff of each strategy when interacting with each of the other strategies.

Complex strategies using lists of lists - The strategies defined here are relatively simple, some would even say naive. Create a strategy that uses the PARTNER-HISTORY variable to store a list of history information pertaining to past interactions with each turtle.

Evolution - Create a version of this model that rewards successful strategies by allowing them to reproduce and punishes unsuccessful strategies by allowing them to die off.

Noise - Add noise that changes the action perceived from a partner with some probability, causing misperception.

Spatial Relations - Allow turtles to choose not to interact with a partner. Allow turtles to choose to stay with a partner.

Environmental resources - include an environmental (patch) resource and incorporate it into the interactions.

NETLOGO FEATURES

Note the use of the TO-REPORT primitive in the function CALC-SCORE to return a number

Note the use of lists and turtle ID's to keep a running history of interactions in the PARTNER-HISTORY turtle variable.

Note how agent sets that will be used repeatedly are stored when created and reused to increase speed.

RELATED MODELS

PD Basic

PD Two Person Iterated

PD Basic Evolutionary

HOW TO CITE

If you mention this model in an academic publication, we ask that you include these citations for the model itself and for the NetLogo software:

In other publications, please use:

COPYRIGHT NOTICE

Copyright 2002 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.

This model was created as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  ;;number of turtles with each strategy
  num-random
  num-cooperate
  num-defect
  num-tit-for-tat
  num-unforgiving
  num-sneaky
  num-notsogrim
  num-unforgiving2
  num-anti-tit-for-tat
  num-anti-tit-for-tat-h

;;number of turtles with each strategy that will reproduce
  num-random-rep
  num-cooperate-rep
  num-defect-rep
  num-tit-for-tat-rep
  num-unforgiving-rep
  num-sneaky-rep
  num-notsogrim-rep
  num-unforgiving2-rep
  num-anti-tit-for-tat-rep
  num-anti-tit-for-tat-h-rep
  ;;number of interactions by each strategy
  num-random-games
  num-cooperate-games
  num-defect-games
  num-tit-for-tat-games
  num-unforgiving-games
  num-sneaky-games
  num-notsogrim-games
  num-unforgiving2-games
  num-anti-tit-for-tat-games
  num-anti-tit-for-tat-h-games

  ;;total score of all turtles playing each strategy
  random-score
  cooperate-score
  defect-score
  tit-for-tat-score
  unforgiving-score
  sneaky-score
  notsogrim-score
  unforgiving2-score
  anti-tit-for-tat-score
  anti-tit-for-tat-h-score
  turtlecount
]

turtles-own [
  score
  strategy
  defect-now?
  partner-defected? ;;action of the partner
  i-defected?
  partnered?        ;;am I partnered?
  partner           ;;WHO of my partner (nobody if not partnered)
  memory-partners
  memory-partnermove
  memory-ownmove
]


;;;;;;;;;;;;;;;;;;;;;;
;;;Setup Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;

to setup
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  clear-all
  store-initial-turtle-counts ;;record the number of turtles created for each strategy
  setup-turtles ;;setup the turtles and distribute them randomly
  reset-ticks
end 

;;record the number of turtles created for each strategy
;;The number of turtles of each strategy is used when calculating average payoffs.
;;Slider values might change over time, so we need to record their settings.
;;Counting the turtles would also work, but slows the model.

to store-initial-turtle-counts
  set num-random n-random
  set num-cooperate n-cooperate
  set num-defect n-defect
  set num-tit-for-tat n-tit-for-tat
  set num-unforgiving n-unforgiving
  set num-sneaky n-sneaky
  set num-notsogrim n-not-so-grim
  set num-unforgiving2 n-unforgiving2
  set num-anti-tit-for-tat n-anti-tit-for-tat
  set num-anti-tit-for-tat-h n-anti-tit-for-tat-h
end 

;;setup the turtles and distribute them randomly

to setup-turtles
  make-turtles ;;create the appropriate number of turtles playing each strategy
  setup-common-variables ;;sets the variables that all turtles share
end 

;;create the appropriate number of turtles playing each strategy

to make-turtles
  crt num-random [ set strategy "random" set color gray - 1 ]
  crt num-cooperate [ set strategy "cooperate" set color red ]
  crt num-defect [ set strategy "defect" set color blue ]
  crt num-tit-for-tat [ set strategy "tit-for-tat" set color lime ]
  crt num-unforgiving [ set strategy "unforgiving" set color turquoise - 1 ]
  crt num-sneaky [set strategy "sneaky" set color magenta ]
  crt num-notsogrim [set strategy "notsogrim" set color yellow]
  crt num-unforgiving2 [set strategy "unforgiving2" set color 25]
  crt num-anti-tit-for-tat [set strategy "anti-tit-for-tat" set color 23]
  crt num-anti-tit-for-tat-h [set strategy "anti-tit-for-tat-h" set color 22]
end 

;;set the variables that all turtles share

to setup-common-variables
  ask turtles [
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
  ]
  setup-memory ;;initialize MEMORY and MEMORY-OWNMOVE list in all turtles
end 

;;initialize PARTNER-HISTORY list in all turtles

to setup-memory
    let default-memory [] ;;initialize the DEFAULT-HISTORY variable to be a list
  ;;give each turtle a copy of this list for tracking partner histories
  ask turtles [
    set memory-partners default-memory
    set memory-partnermove default-memory]
  ask turtles [ set memory-ownmove default-memory ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;
;;;Runtime Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to go
  clear-last-round
  ask turtles [ partner-up ]                        ;;have turtles try to find a partner
  let partnered-turtles turtles with [ partnered? ]
  ask partnered-turtles [ select-action ]           ;;all partnered turtles select action
  ask partnered-turtles [ play-a-round ]
  do-bookkeeping
  reproduce-turtles
  if num-random > n-winner [
    output-show "Random wins!"
    stop]
   if num-cooperate > n-winner [
    output-show "Cooperate wins!"
    stop]
    if num-defect > n-winner [
    output-show "Defect wins!"
    stop]
     if num-tit-for-tat > n-winner [
    output-show "Tit-for-tat wins!"
    stop]
      if num-unforgiving > n-winner [
    output-show "Unforgiving wins!"
    stop]
       if num-sneaky > n-winner [
    output-show "Sneaky wins!"
    stop]
       if num-notsogrim > n-winner [
    output-show "Not-so-grim wins!"
    stop]
       if num-unforgiving2 > n-winner [
    output-show "Unforgiving2 wins!"
    stop]
       if num-anti-tit-for-tat > n-winner [
    output-show "Anti-tit-for-tat wins!"
    stop]
       if num-anti-tit-for-tat-h > n-winner [
    output-show "Anti-tit-for-tat-h wins!"
    stop]
  if count turtles > rescale-after [
    rescale]
  tick
end 

to clear-last-round
  let partnered-turtles turtles with [ partnered? ]
  ask partnered-turtles [ release-partners ]
end 

;;release partner and turn around to leave

to release-partners
  set partnered? false
  set partner nobody
  rt 180
  set label ""
end 

;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.

to partner-up ;;turtle procedure
  if (not partnered?) [              ;;make sure still not partnered
    rt (random-float 90 - random-float 90) fd advance     ;;move around randomly
    set partner one-of (turtles-at -1 0) with [ not partnered? ]
    if partner != nobody [              ;;if successful grabbing a partner, partner up
      set partnered? true
      set heading 270                   ;;face partner
      ask partner [
        set partnered? true
        set partner myself
        set heading 90
      ]
    ]
  ]
end 

;;choose an action based upon the strategy being played

to select-action ;;turtle procedure
  if strategy = "random" [ act-randomly ]
  if strategy = "cooperate" [ cooperate ]
  if strategy = "defect" [ defect ]
  if strategy = "tit-for-tat" [ tit-for-tat ]
  if strategy = "unforgiving" [ unforgiving ]
  if strategy = "sneaky" [ sneaky ]
  if strategy = "notsogrim" [ notsogrim ]
  if strategy = "unforgiving2" [ unforgiving2]
  if strategy = "anti-tit-for-tat" [ anti-tit-for-tat]
  if strategy = "anti-tit-for-tat-h" [ anti-tit-for-tat-h]
end 

to play-a-round ;;turtle procedure
  get-payoff     ;;calculate the payoff for this round
  ;;update-history ;;store the results for next time
end 

;;calculate the payoff for this round and
;;display a label with that payoff.

to get-payoff
  set partner-defected? [defect-now?] of partner
  set i-defected? defect-now?
  set memory-partnermove (fput partner-defected? memory-partnermove)
  if length memory-partnermove > 500 [set memory-partnermove (remove-item 500 memory-partnermove)]
  set memory-partners (fput [who] of partner memory-partners)
  if length memory-partners > 500 [set memory-partners (remove-item 500 memory-partners)]
  set memory-ownmove (fput i-defected? memory-ownmove)
  if length memory-ownmove > 500 [set memory-ownmove (remove-item 500 memory-ownmove)]
  ifelse partner-defected? [
    ifelse defect-now? [
      set score (score + 0) set label 0
    ] [
      set score (score + beta) set label beta
    ]
  ] [
    ifelse defect-now? [
      set score (score + mu) set label mu
    ] [
      set score (score + 5) set label 5
    ]
  ]
end 

to reproduce-turtles
  crt num-random-rep [
    set strategy "random"
    set color gray - 1
    set memory-partners []
    set memory-partnermove []
    set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
  ]
  crt num-cooperate-rep [
    set strategy "cooperate"
    set color red
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]
  crt num-defect-rep [
    set strategy "defect"
    set color blue
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
    ]
  crt num-tit-for-tat-rep [
    set strategy "tit-for-tat"
    set color lime
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]
  crt num-unforgiving-rep [
    set strategy "unforgiving"
    set color turquoise - 1
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
    ]
  crt num-sneaky-rep [
    set strategy "sneaky"
    set color magenta
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]
  crt num-notsogrim-rep [
    set strategy "notsogrim"
    set color yellow
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]

  crt num-anti-tit-for-tat-rep [
    set strategy "anti-tit-for-tat"
    set color 23
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]

  crt num-anti-tit-for-tat-h-rep [
    set strategy "anti-tit-for-tat-h"
    set color 22
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]

  crt num-unforgiving2-rep [
    set strategy "unforgiving2"
    set color 25
    set memory-partners []
    set memory-partnermove []
        set memory-ownmove []
    set score 0
    set partnered? false
    set partner nobody
    set partner-defected? false
    set i-defected? false
    setxy random-xcor random-ycor
   ]
  ask turtles with [score > rep-score] [set score score - 10]
  ask turtles with [score < death-score] [die]
end 


;;;;;;;;;;;;;;;;
;;;Strategies;;;
;;;;;;;;;;;;;;;;

;;All the strategies are described in the Information Tab.

to act-randomly
  set num-random-games num-random-games + 1
  ifelse (random-float 1.0 < 0.5) [
    set defect-now? false
  ] [
    set defect-now? true
  ]
end 

to cooperate
  set num-cooperate-games num-cooperate-games + 1
  set defect-now? false
end 

to defect
  set num-defect-games num-defect-games + 1
  set defect-now? true
end 

to tit-for-tat
  set num-tit-for-tat-games num-tit-for-tat-games + 1
  ifelse member? [who] of partner memory-partners [
    set partner-defected? item (position [who] of partner memory-partners) memory-partnermove
  ifelse (partner-defected?) [
    set defect-now? true
  ] [
    set defect-now? false
  ]]
  [set defect-now? false]
end 

to anti-tit-for-tat
  set num-anti-tit-for-tat-games num-anti-tit-for-tat-games + 1
  ifelse member? [who] of partner memory-partners [
    set partner-defected? item (position [who] of partner memory-partners) memory-partnermove
  ifelse (partner-defected?) [
    set defect-now? false
  ] [
    set defect-now? true
  ]]
  [set defect-now? false]
end 

to anti-tit-for-tat-h
  set num-anti-tit-for-tat-h-games num-anti-tit-for-tat-h-games + 1
  ifelse member? [who] of partner memory-partners [
    set partner-defected? item (position [who] of partner memory-partners) memory-partnermove
  ifelse (partner-defected?) [
    set defect-now? false
  ] [
    set defect-now? true
  ]]
  [set defect-now? true]
end 

to unforgiving
  set num-unforgiving-games num-unforgiving-games + 1
  let past-moves filter [ ?1 -> item position ?1 memory-partnermove memory-partners = [who] of partner ] memory-partnermove
    ifelse member? true past-moves [
    set defect-now? true]
    [set defect-now? false]
end 

to sneaky
  set num-sneaky-games num-sneaky-games + 1
  ifelse member? [who] of partner memory-partners [
  set partner-defected? item (position [who] of partner memory-partners) memory-partnermove
  set i-defected? item (position [who] of partner memory-partners) memory-ownmove
  ifelse (partner-defected?) [
    ifelse (i-defected?) [
    set defect-now? false
    ][
    set defect-now? true
    ]
  ] [
    set defect-now? true
  ]]
  [set defect-now? true]
end 

to notsogrim
  set num-notsogrim-games num-notsogrim-games + 1
  ifelse member? [who] of partner memory-partners [
  set partner-defected? item (position [who] of partner memory-partners) memory-partnermove
  set i-defected? item (position [who] of partner memory-partners) memory-ownmove
  ifelse (partner-defected?) [
    ifelse (i-defected?) [
    set defect-now? true
    ][
    let w random-float 70
    ifelse w < 50 [
    set defect-now? true]
    [set defect-now? false]
    ]
  ] [
    set defect-now? false
  ]]
  [set defect-now? false]
end 

to unforgiving2
  set num-unforgiving2-games num-unforgiving2-games + 1
  let past-moves filter [ ?1 -> item position ?1 memory-partnermove memory-partners = [who] of partner ] memory-partnermove
    ifelse length filter [ ?1 -> ?1 = true ] past-moves > 1 [
    set defect-now? true]
    [set defect-now? false]
end 

to rescale
  do-scoring
  set turtlecount (count turtles)
  ask n-of (num-random - (rescale-after * ((num-random) / turtlecount) )) turtles with [ strategy = "random" ] [ die ]
  ask n-of (num-cooperate - (rescale-after * ((num-cooperate) / turtlecount) )) turtles with [ strategy = "cooperate" ] [ die ]
  ask n-of (num-defect - (rescale-after * ((num-defect) / turtlecount) )) turtles with [ strategy = "defect" ] [ die ]
  ask n-of (num-tit-for-tat - (rescale-after * ((num-tit-for-tat) / turtlecount) )) turtles with [ strategy = "tit-for-tat" ] [ die ]
  ask n-of (num-unforgiving - (rescale-after * ((num-unforgiving) / turtlecount) )) turtles with [ strategy = "unforgiving" ] [ die ]
  ask n-of (num-sneaky - (rescale-after * ((num-sneaky) / turtlecount) )) turtles with [ strategy = "sneaky" ] [ die ]
  ask n-of (num-notsogrim - (rescale-after * ((num-notsogrim) / turtlecount) )) turtles with [ strategy = "notsogrim" ] [ die ]
  ask n-of (num-unforgiving2 - (rescale-after * ((num-unforgiving2) / turtlecount) )) turtles with [ strategy = "unforgiving2" ] [ die ]
  ask n-of (num-anti-tit-for-tat - (rescale-after * ((num-anti-tit-for-tat) / turtlecount) )) turtles with [ strategy = "anti-tit-for-tat" ] [ die ]
  ask n-of (num-anti-tit-for-tat-h - (rescale-after * ((num-anti-tit-for-tat-h) / turtlecount) )) turtles with [ strategy = "anti-tit-for-tat-h" ] [ die ]
end 

;;;;;;;;;;;;;;;;;;;;;;;;;
;;;Plotting Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;;

;;procedure called by go that calculates scores and plots

to do-bookkeeping
  do-scoring
  do-counting
  do-plotting
end 

;;calculate the total scores of each strategy

to do-scoring
  set random-score  (calc-score "random" num-random)
  set cooperate-score  (calc-score "cooperate" num-cooperate)
  set defect-score  (calc-score "defect" num-defect)
  set tit-for-tat-score  (calc-score "tit-for-tat" num-tit-for-tat)
  set unforgiving-score  (calc-score "unforgiving" num-unforgiving)
  set sneaky-score  (calc-score "sneaky" num-sneaky)
  set anti-tit-for-tat-score  (calc-score "anti-tit-for-tat" num-anti-tit-for-tat)
  set anti-tit-for-tat-h-score  (calc-score "anti-tit-for-tat-h" num-anti-tit-for-tat-h)
  set num-random-rep count turtles with [(strategy = "random") and (score > rep-score)]
  set num-cooperate-rep count turtles with [(strategy = "cooperate") and (score > rep-score)]
  set num-defect-rep count turtles with [(strategy = "defect") and (score > rep-score)]
  set num-tit-for-tat-rep count turtles with [(strategy = "tit-for-tat") and (score > rep-score)]
  set num-unforgiving-rep count turtles with [(strategy = "unforgiving") and (score > rep-score)]
  set num-sneaky-rep count turtles with [(strategy = "sneaky") and (score > rep-score)]
  set num-notsogrim-rep count turtles with [(strategy = "notsogrim") and (score > rep-score)]
  set num-unforgiving2-rep count turtles with [(strategy = "unforgiving2") and (score > rep-score)]
  set num-anti-tit-for-tat-rep count turtles with [(strategy = "anti-tit-for-tat") and (score > rep-score)]
  set num-anti-tit-for-tat-h-rep count turtles with [(strategy = "anti-tit-for-tat-h") and (score > rep-score)]
end 

;; returns the total score for a strategy if any turtles exist that are playing it

to-report calc-score [strategy-type num-with-strategy]
  ifelse num-with-strategy > 0 [
    report (sum [ score ] of (turtles with [ strategy = strategy-type ]))
  ] [
    report 0
  ]
end 

to do-counting
  set num-random count turtles with [strategy = "random"]
  set num-cooperate count turtles with [strategy = "cooprate"]
  set num-defect count turtles with [strategy = "defect"]
  set num-tit-for-tat count turtles with [strategy = "tit-for-tat"]
  set num-unforgiving count turtles with [strategy = "unforgiving"]
  set num-sneaky count turtles with [strategy = "sneaky"]
  set num-notsogrim count turtles with [strategy = "notsogrim"]
  set num-unforgiving2 count turtles with [strategy = "unforgiving2"]
  set num-anti-tit-for-tat count turtles with [strategy = "anti-tit-for-tat"]
  set num-anti-tit-for-tat-h count turtles with [strategy = "anti-tit-for-tat-h"]
end 


;;if a strategy has had any interactions, plot the average score per interaction

to do-plotting
    ;; plot stacked histogram of link types
  set-current-plot "Type counts"
  let total 0
  set-current-plot-pen "random"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "random"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "defect"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "defect"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "cooperate"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "cooperate"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "tit-for-tat"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "tit-for-tat"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "unforgiving"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "unforgiving"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "sneaky"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "sneaky"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "notsogrim"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "notsogrim"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "unforgiving2"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "unforgiving2"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "anti-tit-for-tat"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "anti-tit-for-tat"]
  plot-pen-down plotxy ticks total
  set-current-plot-pen "anti-tit-for-tat-h"
  plot-pen-up plotxy ticks total
  set total total + count turtles with [strategy = "anti-tit-for-tat-h"]
  plot-pen-down plotxy ticks total
end 


; Copyright 2002 Uri Wilensky. All rights reserved.
; The full copyright notice is in the Information tab.

There is only one version of this model, created over 4 years ago by Adam Galambos.

Attached files

File Type Description Last updated
PD Iterated bdd memory.png preview Preview for 'PD Iterated bdd memory' over 4 years ago, by Adam Galambos Download

This model does not have any ancestors.

This model does not have any descendants.