This page was automatically generated by NetLogo 4.1. 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.
In order for this to work, this file, your model file (ipd.nlogo), and the file NetLogoLite.jar must all be in the same directory. (You can copy NetLogoLite.jar from the directory where you installed NetLogo.)
On some systems, you can test the applet locally on your computer before uploading it to a web server. It doesn't work on all systems, though, so if it doesn't work from your hard drive, please try uploading it to a web server.
You don't need to include everything in this file in your page. If you want, you can just take the HTML code beginning with <applet> and ending with </applet>, and paste it into any HTML file you want. It's even OK to put multiple <applet> tags on a single page.
If NetLogoLite.jar and your model are in different directories, you must modify the archive= and value= lines in the HTML code to point to their actual locations. (For example, if you have multiple applets in different directories on the same web server, you may want to put a single copy of NetLogoLite.jar in one central place and change the archive= lines of all the HTML files to point to that one central copy. This will save disk space for you and download time for your users.)
powered by NetLogo
view/download model file: ipd.nlogo
IPD is a framework for experimenting with strategies in
iterated prisoner's dilemma tournaments.
When a turtle is updated he:
1. plays one prisoner's dilemma (PD) game with a randomly selected neighbor
2. takes several steps in a random direction
In a PD game each turtle decides to cooperate or defect according to the turtle's strategy. If both turtles cooperate, each receives 3 points. If both defect, each receives 1 point. If one cooperates while the other defects, then the defector receives 5 points and the cooperator receives 0 points. The points are added to the turtle's overall score.
Different breeds of turtles use different PD strategies. Defectors always defect, cooperators always cooperate, and impulsives randomly cooperate or defect. Reciprocators defect if their partner defected the last time they met, and cooperate otherwise. This is called the tit-for-tat strategy. To implement tit-for-tat, reciprocators are equipped with a memory. The memory is a list of Booleans. If the item at position i in the memory of turtle j is false, then turtle i defected the last time it played turtle j.
If the control loop is running, pressing the UPDATE button pauses it. Otherwise the control-loop starts or resumes. Users can specify the initial population sizes of each breed of turtle. Setting a population size to 0 allows users to pit one strategy against another. Users can also set the search radius a turtle uses to look for a PD partner. This is also how many steps a turtle takes when it moves.
On the other side of the view the number of PD games played as well as the average scores for each breed of turtle are displayed.
Notice that in the default setting (10 of each breed) after 50,000 games are played, impulsive turtles have the highest average score (2.485), reciprocators are close behind (2.439), defectors have a noticably lower average (2.24), and cooperators have a much lower average (2.1).
One is tempted to conclude that cheaters do prosper over non-cheaters. While reciprocators can beat cheaters, the best strategy is to act like you're nuts and do whatever you want.
There are two types of interesting worlds users can create. In a homogeneous world all turtles belong to the same breed. In a bipolar world there are two breeds. Users can compare averages between homogeneous worlds and they can pit one strategy against another in a bipolar world.
A fifth breed of turtles are the customized turtles. They base their decision to cooperate or not on the previous N moves of their partner. To implement such strategies customized turtles are equipped with a memory consisting of a list of length-N lists of booleans. For example, if N = 3 turtle j's memory might look like this:
[[true false true] [false false false] ... [false true true]]
Note that if N = 3, then there are 2^3 = 8 possible histories turtle j could have with turtle i. If there are 50 turtles, then there are 50^8 possible memories turtle j could have.
Users are invited to redefine a function that takes a parnter history as input and returns a decision to cooperate or not based on this history. Here are some examples:
; cooperate if partner cooperated at least once in last N moves
to-report cooperation-strategy [partner-history]
report reduce [?1 or ?2] partner-history
end
; cooperate if partner cooperated in all of the last N moves
to-report cooperation-strategy [partner-history]
report reduce [?1 and ?2] partner-history
end
; cooperate if partner cooperated in last 2 moves
to-report cooperation-strategy [partner-history]
report item 0 partner-history and item 1 partner-history
end
===============
Genetic Programming Project
Equip each customized turtle with a strategy attribute. A strategy consists of a list of length N lists of random Booleans, where N = history-length. There shouldn't be any repeats in this list. Since there are 2^N such lists, the length of a strategy will be less than or equal to 2^N.
The cooperation-strategy is simply:
to-report cooperation-strategy [partner-history]
report member? partner-history strategy
end
In other words, if the partner's history matches one of the patterns in the strategy list, then the turtle cooperates, otherwise it defects.
Using the evo framework, we can allow fit customized turtles to mate after a fixed number of ticks. All of these turtles then die and their offspring then compete. Plot the average score for each generation. A turtle is fit if its average is above some threshold, the population average for example.
Customized turtles mate by recombination and mutation of their strategies. Recombination consists of taking some random prefix of one turtle's strategy and prepending it to some random suffix of the other turtle's strategy. The length of the result must be less than or equal to 2^N. After recombination, a random position in the strategy is selected and set to a random Boolean.
===============
Strong Reciprocation Project
Equip the customized turtles with two attributes: boldness and vindictiveness. Each is a random integer between 0 and K for some K.
A turtle defects if its boldness is larger than some random number and the number of nearby turtles that might witness the defection is smaller than some random number. Otherwise he cooperates.
When updating a turtle, compute the points as before, but if a turtle defects, he also receives a stiff punishment if there is any nearby turtles with vindictiveness higher than some random number. The score of the punishing turtle is decremented by some fixed small amount.
See Sample Models/Social Sciences/Unverified/Prisoner's Dilemma for related PD models.
Created by Jon Pearce (http://www.cs.sjsu.edu/faculty/pearce/pearce.html)
Iterated PD models, genetic strategies, and strong reciprocation were studied by Robert Axelrod in his book "The Complexity of Cooperation".
;===========================================================
; IPD: A framework for iterated prisoner's dilemma (PD)
; by Jon Pearce (www.cs.sjsu.edu/faculty/pearce/pearce.html)
;===========================================================
;==================================
;===== Declarations =====
;==================================
; turtle breeds:
breed [defectors defector] ; these guys always defect
breed [reciprocators reciprocator] ; these guys use tit-for-tat
breed [cooperators cooperator] ; these guys always cooperate
breed [impulsives impulsive] ; these guys behave randomly
breed [customized a-customized] ; these guys use a user-defined strategy
; these attributes will be inherited by all breeds:
turtles-own [
score ; sum of points in all games
partner ; nobody or current PD partner
cooperated? ; current move of self
num-games ; # games played by self
]
globals [
total-num-games ; total number of PD games played
both-cooperate-points ; points each player receives if both cooperate
both-defect-points ; points each player receives if both defect
defect-reward ; points defector receives if partner cooperates
cooperate-penalty ; points cooperator receives if partner defects
reciprocator-average ; average score of all reciprocators
customized-average ; average score of all customized agents
defector-average ; average score of all defectors
cooperator-average ; average score of all cooperators
impulsive-average ; average score of all impulsives
]
reciprocators-own [
memory ; list of last moves of every turtle
]
customized-own [
memory ; list of lists of last N moves of every turtle
history-length ; N
]
;==================================
;===== Initializing the Model =====
;==================================
to init-model
ca ; clear all
random-seed new-seed ; randomly seed random number generator
init-globals
init-patches
init-turtles
set-current-plot "averages"
end
to init-patches
ask patches [init-patch]
end
to init-turtles
create-defectors num-defectors
create-cooperators num-cooperators
create-reciprocators num-reciprocators
create-impulsives num-impulsives
create-customized num-customized
ask turtles [init-turtle]
ask defectors [init-defector]
ask cooperators [init-cooperator]
ask reciprocators [init-reciprocator]
ask impulsives [init-impulsive]
ask customized [init-customized]
end
;=====Initialization overridables =====
to init-globals
set total-num-games 0
set both-cooperate-points 3
set both-defect-points 1
set defect-reward 5
set cooperate-penalty 0
set reciprocator-average 0
set customized-average 0
set defector-average 0
set cooperator-average 0
set impulsive-average 0
end
to init-patch
; To do: set initial values of patch attributes.
end
to init-turtle
setxy random-xcor random-ycor
set score 0
set num-games 0
set partner nobody
end
to init-defector
set color red
end
to init-cooperator
set color gray
end
to init-impulsive
set color yellow
end
to init-reciprocator
set color pink
set memory []
let num-turtles count turtles
; memory = [true true ... true] = all cooperated in "previous" game with self
repeat num-turtles [ set memory (lput true memory) ]
end
to init-customized
set color green
set memory []
set history-length 3
let num-turtles count turtles
; memory = [[true ... true] ... [true ... true]] = all cooperated in "previous" N games with self
repeat num-turtles [
let init-history []
repeat history-length [
set init-history lput true init-history
]
set memory (lput init-history memory)
]
end
;==================================
;===== Updating the Model =====
;==================================
to update-model
if finished?
[
print "Simulation halted"
stop
]
tick ; increment the tick counter
update-globals
update-patches
update-turtles
update-plot
ask links [die] ; done here so links will be briefly displayed
end
to update-patches
ask patches [update-patch]
end
to update-turtles
ask turtles [update-turtle]
end
; ===== Update overridables =====
to update-globals
; To do: update values of globals.
end
to-report finished?
; To do: report model halting condition.
report false ; for now
end
to update-patch
; To do: update patch attributes.
end
to update-turtle
if partner = nobody [
; find a nearby partner
let candidates other turtles in-radius radius with [partner = nobody]
set partner one-of candidates
if partner != nobody [
set total-num-games total-num-games + 1
ask partner [ set partner myself ]
create-link-with partner ; for aesthetics only
; cooperate or defect?
set-cooperated?
ask partner [ set-cooperated? ]
; calculate points
let partner-cooperated? [cooperated?] of partner
let my-points 0
let partners-points 0
ifelse cooperated? [
ifelse partner-cooperated? [
set my-points both-cooperate-points
set partners-points both-cooperate-points
]
[ ; my partner tricked me :-(
set my-points cooperate-penalty
set partners-points defect-reward
]
]
[ ; I defected! ;-)
ifelse partner-cooperated? [
set my-points defect-reward
set partners-points cooperate-penalty
]
[ ; my partner defected too :-(
set partners-points both-defect-points
set my-points both-defect-points
]
]
; update memories
update-memory
ask partner [update-memory]
; update scores
set score score + my-points
set num-games num-games + 1
ask partner [
set score score + partners-points
set num-games num-games + 1
]
; disengage
ask partner [ set partner nobody ]
set partner nobody
]
]
; look for another partner
move-randomly
end
to move-randomly
set heading random 360
fd radius
end
;=============================
; Deciding to cooperate or defect
;=============================
to set-cooperated?
if breed = defectors [
set cooperated? false
stop
]
if breed = cooperators [
set cooperated? true
stop
]
if breed = impulsives [
let random-bool (random 2) = 1
set cooperated? random-bool
stop
]
if breed = reciprocators [
set-reciprocator-cooperated?
stop
]
if breed = customized [
set-customized-cooperated?
stop
]
end
to set-reciprocator-cooperated?
let partner-index [who] of partner
let last-move item partner-index memory
set cooperated? last-move
end
to set-customized-cooperated?
let partner-index [who] of partner
let previous-moves item partner-index memory
set cooperated? cooperation-strategy previous-moves
end
;=====================
; Updating memories of reciprocators and customized
;=====================
to update-memory
let partner-index [who] of partner
let partner-cooperated? [cooperated?] of partner
if breed = reciprocators [
set memory replace-item partner-index memory partner-cooperated?
stop
]
if breed = customized [
let history item partner-index memory
set history fput partner-cooperated? but-last history
set memory replace-item partner-index memory history
stop
]
end
;====================
; Updating the plotter
;====================
to update-plot
let total-score sum [score] of defectors
let total-games sum [num-games] of defectors
if total-games > 0 [
set defector-average total-score / total-games
set-current-plot-pen "defector-pen"
plot defector-average
]
set total-score sum [score] of impulsives
set total-games sum [num-games] of impulsives
if total-games > 0 [
set impulsive-average total-score / total-games
set-current-plot-pen "impulsive-pen"
plot impulsive-average
]
set total-score sum [score] of reciprocators
set total-games sum [num-games] of reciprocators
if total-games > 0 [
set reciprocator-average total-score / total-games
set-current-plot-pen "reciprocator-pen"
plot reciprocator-average
]
set total-score sum [score] of cooperators
set total-games sum [num-games] of cooperators
if total-games > 0 [
set cooperator-average total-score / total-games
set-current-plot-pen "cooperator-pen"
plot cooperator-average
]
set total-score sum [score] of customized
set total-games sum [num-games] of customized
if total-games > 0 [
set customized-average total-score / total-games
set-current-plot-pen "custom-pen"
plot customized-average
]
end
;==========================
; Customized strategy: redefine this reporter as you wish
; In this example an agent cooperates if its partner has
; cooperated in any of the last N games
;==========================
to-report cooperation-strategy [partner-history]
report reduce [?1 or ?2] partner-history
end