Prisoners Dilemma HubNet
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 HubNet version of the Prisoner's Dilemma. The Prisoner's Dilemma is a famous game-theory situation that models the costs and benefits of collaboration or treason between free agents where there is a struggle over some capital.
The game has implications for a variety of social situations that involve negotiations in the absence of absolute trust between agents, such as in politics and economics. The game is a succession of interactions between agents, with each agent trying to maximize personal gains over all interactions. In this game, aggressive behavior is not necessarily the best strategy for maximizing personal gains. The rules of the game are such that agents are penalized when behaving aggressively in interacting with another aggressive agent, At each interaction between agents, each agent decides whether to 'cooperate' or 'defect.' These household terms in economics come from the following hypothetical situation.
The Prisoner's Dilemma presents an interesting problem: if you and your accomplice in crime are imprisoned, then in order to minimize the overall jail time you should cooperate with your partner by remaining silent and not confessing. However, you may choose to defect the unsaid "contract" with your partner by confessing.Let's assume you have confessed. If your partner does not confess you will go free. If your partner confesses, you will go to jail for three years, much better than the five you would have earned had you refused to confess. Unfortunately, your partner is in the same position. Acting rationally in the short term, you will both be worse off. For more introductory information, please refer in the NetLogo models library to the PD BASIC model found in the PRISONER'S DILEMMA suite. Note also that in this model, the traditional year values, such as '3 years' or '5 years,' are given for modification.
HOW IT WORKS
This model allows for many students within a classroom to challenge each other over a network. Students will keep switching partners by moving their agents around on the screen and they can switch strategies. Students compete to see who has gained the maximum points over all interactions.
Moreover, this model allows students to gain a grasp of computer programming. An input box on the screen allows students to edit and author strategies. Following are the strategies that students can choose from (see the STRATEGY-CHOICE choice button):
Strategies:
Random - randomly cooperate or defect
Cooperate - cooperate always
Defect - defect always
Go-by-Majority - Totals the number of times that the partner has defected. {against you or regardless of whom the partner had been playing against? Andrei, please choose and phrase} If the majority of the time, up to that point, the partner has defected, defect. Otherwise, cooperate.
Tit-for-Tat - If the opponent cooperates this round cooperate next round. If the opponent defects this round, defect next round. Initially cooperate. {again-- against me or anyone?}
Suspicious-Tit-for-Tat - The Tit-for-Tat strategy, except initially defects
Tit-for-Two-Tats - If the opponent cooperates this round cooperate next round. If the opponent defects two rounds in a row, defect the next round. Initially cooperate.
Pavlov - If the previous tactic (cooperate or defect) resulted in good {Andrei, what does this mean? Any positive point? The maximum possible?} points, stick with that tactic. Otherwise, switch strategies. Currently, "Success" happens when the partner cooperates, so this will keep switching if the opponent is always defecting. Initially random.
Unforgiving - Cooperate always unless the opponent defects once. Upon opponent defection retaliate by defecting always.
Rules for the Iterated Prisoner's Dilemma
This game will consist of matches in which each student competes against one opponent at a time. Students may search for partners on their own. Once paired, students will play 10 rounds, and then separate and look for other partners.
Each round the student and his/her partner will earn or lose points by either cooperating (C) or defecting (D) based on the rules set in their individual client. Each students' strategy is either selected (the STRATEGY-CHOICE ) or edited on the HubNet client. {Andrei, can players change their strat while in a given interaction?}
The point system is determined by the administrator, using the 4 sliders under "PAYOFF." That is, the administrator does not change the basic game but the value of the prizes and penalties.
HOW TO USE IT
Buttons:
SETUP: Begin playing the iterated prisoner's dilemma.
RERUN: All players set their total back to zero, and are ready to replay
PLAY: When students pair up, they will play a user-defined number of turns before separating
CREATE ANDROID: Creates a computer player to compete with students
Switches:
ALLOW-STRATEGY-CHANGE?: If on, students will be able to change their strategies. If off, they will be locked into the last working strategy in their client until it is turned on again.
Sliders:
COMPUTER STRATEGY - Select the computer's strategy from the drop-down list below.
Plots:
STRATEGY PLOTS: Shows the total points accumulated for each given strategy. Once a strategy is modified by a student, the results of that student's success/failure will not be plotted on this.
C-D PLOT: Plots the total number of times players have cooperated, and the total number of times players have defected.
THINGS TO NOTICE
Watch the plots for the different strategies. Is there a serious difference between tit-for-tat and suspicious-tit-fot-tat? Does one strategy really beat out the rest?
See how often students cooperate and defect. In the long run, do students learn to switch to more trusting strategies? Or do students all too often defect?
THINGS TO TRY
Experiment with playing different strategies one against the other. Which strategies do the best? Which do the worst? Why?
Let it run for a while with hundreds of computer players to see which strategies win under these conditions.
There is a mode in which students cannot change their strategies. Encourage them to code their own strategies, and then make them stick to that. Which strategy seemed to work the best?
EXTENDING THE MODEL
Students know the history of their last round, but not any of the previous rounds. Think of a way to have clients be able to store the history of previous plays, and know what their current partner has done in other rounds.
NETLOGO FEATURES
Note the use of the turtle variable label
to display each turtle's average score in the view.
RELATED MODELS
PD Basic, PD N-Person Iterated, PD Basic Evolutionary, PD 2-Person Iterated
HOW TO CITE
If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:
- Wilensky, U. (2003). NetLogo HubNet Prisoners Dilemma HubNet model. http://ccl.northwestern.edu/netlogo/models/HubNetPrisonersDilemmaHubNet. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
- Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
COPYRIGHT AND LICENSE
Copyright 2003 Uri Wilensky.
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at uri@northwestern.edu.
This activity and associated models and materials were 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Variable and Breed declarations ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; breed [ students student ] breed [ androids android ] globals [ COOPERATE DEFECT number-of-plays strategy-list strategy-totals ;; list of totals for selected strategies, for plotting strategy-totals-count ;; number of times that strategy has been used defect-total ;; total number of times turtles have defected cooperate-total ;; total number of times turtles have cooperated ;; Shapes shape-names ;; list of names of the non-sick shapes a client's turtle can have colors ;; list of colors used for clients' turtles color-names ;; list of names of colors used for students' turtles used-shape-colors ;; list of shape-color pairs that are in use max-possible-codes ;; total number of unique shape/color combinations ] turtles-own [ score ;; my current score defect-now? ;; what will I do this round? partner ;; the who of my partner total-defects ;; counts how many times the partner has defected, used for certain strategies selected-strategy ;; string that contains the user's selected strategy total ;; the total score of the turtle, following all plays play-history ;; stores all of your moves in this play play-partner-history ;; stores all of your partners moves in this play base-shape ;; original shape of a turtle user-code ;; students create custom strategies, which are stored here code-changed? ;; is true when the user changes given strategies ] students-own [ user-id ;; unique id, input by the client when they log in, to identify each student turtle ] ;;;;;;;;;;;;;;;;;;;;; ;; Setup Functions ;; ;;;;;;;;;;;;;;;;;;;;; to startup hubnet-reset setup end to setup clear-all setup-shapes setup-vars reset-ticks end to setup-shapes ;; most of these are to handle shapes and colors for uniqueness of students set shape-names [ "airplane" "android" "box" "butterfly" "cactus" "car" "cat" "cow skull" "dog" "ghost" "heart" "key" "leaf" "monster" "moon" "star" "target" "wheel" ] set colors [ white gray brown yellow green lime turquoise cyan sky blue violet ] ;; adjust a few colors so they don't blend in with the red infection dot too much set colors lput (orange + 1) colors set colors lput (magenta + 0.5) colors set colors lput (pink + 2.5) colors set color-names [ "white" "gray" "brown" "yellow" "green" "lime" "turquoise" "cyan" "sky" "blue" "violet" "orange" "magenta" "pink" ] set max-possible-codes (length colors * length shape-names) set used-shape-colors [] end to setup-vars ;; these are constant, for use when students code their own strategies set COOPERATE false set DEFECT true set number-of-plays 10 ;; this is used for the androids, in interpreting their strategy set strategy-list [ "random" "cooperate" "defect" "go-by-majority" "tit-for-tat" "suspicious-tit-for-tat" "tit-for-two-tats" "pavlov" "unforgiving" "custom-strategy" ] set strategy-totals [] set strategy-totals-count [] foreach strategy-list [ set strategy-totals fput 0 strategy-totals set strategy-totals-count fput 0 strategy-totals-count ] set defect-total 0 set cooperate-total 0 end to create-android-player create-androids 1 [ set total 0 set partner nobody set base-shape "android" set shape base-shape set color blue setup-turtle-vars set selected-strategy one-of but-last strategy-list set label selected-strategy set-code ] end ;; Places turtles in random locations again, ready to find new partners. to rerun ask turtles [ setup-turtle-vars set total 0 set score 3 ] end to setup-turtle-vars ;; turtle procedure set partner nobody set defect-now? false set play-history [] set play-partner-history [] set total-defects 0 set size 1 set total (total + score) set score 3 setxy random-xcor random-ycor end ;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Runtime Procedures;;; ;;;;;;;;;;;;;;;;;;;;;;;;; to play-n-times listen-clients if (any? turtles) [ do-plots find-partners every 0.3 [ play-a-round ] ask turtles with [ length play-history = number-of-plays ] [ ifelse breed = students [ ;; students may have changed strategy during the round, and this change can only be made afterwards if not code-changed? [ let strategy-index position selected-strategy strategy-list set strategy-totals (replace-item strategy-index strategy-totals ((item strategy-index strategy-totals) + score)) set strategy-totals-count (replace-item strategy-index strategy-totals-count ((item strategy-index strategy-totals-count) + 1)) ] ] [ let strategy-index position selected-strategy strategy-list set strategy-totals (replace-item strategy-index strategy-totals ((item strategy-index strategy-totals) + score)) set strategy-totals-count (replace-item strategy-index strategy-totals-count ((item strategy-index strategy-totals-count) + 1)) ] setup-turtle-vars set shape base-shape ] display ] end to play-a-round ;; determines the actions of turtles each turn ask turtles with [ partner != nobody ] [ custom-strategy get-payoff ;; after the strategies are determined, the results of the round are determined if breed = students [ send-info-to-clients ] ] end ;; test user strategy. to custom-strategy ;; turtle procedure carefully [ set defect-now? (run-result user-code) ] [ ifelse (breed = students) [ hubnet-send user-id "Errors:" (error-message) output-show "bad strategy survived!" ] [ output-show (word "Problem on android " who ": " error-message) ] ] end to find-partners ;; In this example, only turtles that haven't found a partner can move around. listen-clients every 0.1 [ ask turtles with [partner = nobody] [ ;; randomly move about the view if breed != students [ rt random-normal 0 20 ] fd 0.5 ] ] ;; Ask unpartnered turtles to check if they are on a patch with a turtle that ;; also don't have a partner. ask turtles [ if (partner = nobody) and (any? other turtles-here with [partner = nobody]) [ set partner one-of other turtles-here with [partner = nobody] ask partner [ set partner myself ] ] ] end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;; Some Helpful Functions ;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to set-defect-shape ifelse defect-now? [ set shape word "sick " base-shape ] [ set shape word "good " base-shape ] end to-report test-strategy [ snippet ] carefully [ let tester (run-result snippet) ifelse not (tester = false or tester = true) [ hubnet-send user-id "Errors:" ("the output must be either true, or false") report false ] [ report true ] ] [ hubnet-send user-id "Errors:" (error-message) report false ] end to set-code ;; outputs the code to the input box, for students to see and modify if selected-strategy = "random" [ set user-code ("ifelse-value (random 2 = 0)\n [DEFECT] \n[COOPERATE]") stop ] if selected-strategy = "cooperate" [ set user-code ("COOPERATE") stop ] if selected-strategy = "defect" [ set user-code ("DEFECT") stop ] if selected-strategy = "go-by-majority" [ set user-code ("ifelse-value (empty? play-history)\n [COOPERATE] \n[ \n ifelse-value (total-defects / (length play-history) > 0.5)\n [DEFECT] \n [COOPERATE] \n]") stop ] if selected-strategy = "tit-for-tat" [ set user-code ("ifelse-value (empty? play-history)\n [COOPERATE] \n[ \n ifelse-value (last play-partner-history = DEFECT)\n [DEFECT]\n [COOPERATE] \n]") stop ] if selected-strategy = "suspicious-tit-for-tat" [ set user-code ("ifelse-value (empty? play-history)\n [DEFECT] \n[ \n ifelse-value (last play-partner-history = DEFECT)\n [DEFECT]\n [COOPERATE] \n]") stop ] if selected-strategy = "tit-for-two-tats" [ set user-code ("ifelse-value (length play-history < 2 )\n [COOPERATE] \n[ \n ifelse-value ((last play-partner-history = DEFECT) and item (length play-partner-history - 2) play-partner-history = DEFECT)\n [DEFECT] \n [COOPERATE] \n]") stop ] if selected-strategy = "pavlov" [ set user-code ("ifelse-value (empty? play-history) \n[ \n ifelse-value (random 2 = 0) [DEFECT] [COOPERATE] \n] \n[ \n ifelse-value (last play-partner-history = DEFECT) \n [\n ifelse-value (last play-history = DEFECT)\n [COOPERATE]\n [DEFECT]\n ]\n [\n ifelse-value (last play-history = DEFECT)\n [DEFECT]\n [COOPERATE]\n ]\n]") stop ] if selected-strategy = "unforgiving" [ set user-code ("ifelse-value (empty? play-history)\n [COOPERATE] \n[ \n ifelse-value ((last play-partner-history = DEFECT) or (last play-history = DEFECT))\n [DEFECT] \n [COOPERATE] \n]") stop ] end ;;;;;;;;;;;;;;;;;;;;;; ;;; End Strategies ;;; ;;;;;;;;;;;;;;;;;;;;;; to get-payoff ;;Turtle Procedure ifelse [defect-now?] of partner ;; if the partner has defected [ set play-partner-history lput true play-partner-history ;; it is recorded in the history of his partner ifelse defect-now? ;; if this player has defected [ set score score + D-D set play-history lput true play-history ;; it is recorded in this player's history ] [ set score score + C-D set play-history lput false play-history ] set total-defects total-defects + 1 ;; used for go-by-majority strategy set defect-total defect-total + 1 ;; when detecting a partner's defecting, it is totaled for plotting ] [ set play-partner-history lput false play-partner-history ifelse defect-now? [ set score score + D-C set play-history lput true play-history ] [ set score score + C-C set play-history lput false play-history ] set cooperate-total cooperate-total + 1 ;;cooperates are totaled for plotting ] set-defect-shape end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Plotting ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to do-plots ;;firsts plots the average turnout for provided strategies, then the rest plot-strategies plot-cooperate-defect end to plot-strategies ;;plots the average scores for each of the given strategies set-current-plot "Strategies" let i 0 foreach (but-last strategy-list) [ set-current-plot-pen ? if ((item i strategy-totals-count) != 0) [ plot-pen-reset set-plot-pen-mode 1 plotxy i ( (item i strategy-totals) / (item i strategy-totals-count) ) ] set i ( i + 1 ) ] end to plot-cooperate-defect ;;plots the total number of times that turtles have cooperated or defected set-current-plot "C-D Plot" set-current-plot-pen "cooperate" plot cooperate-total set-current-plot-pen "defect" plot defect-total end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Code for interacting with the clients ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; determines which client sent a command, and what the command was to listen-clients while [ hubnet-message-waiting? ] [ hubnet-fetch-message ifelse hubnet-enter-message? [ create-new-student ] [ ifelse hubnet-exit-message? [ remove-student ] [ execute-command hubnet-message-tag ] ] ] end ;; NetLogo knows what each student turtle is supposed to be ;; doing based on the tag sent by the node: to execute-command [command] if command = "strategy-choice" ;; the user wants to change his/her strategy, by selecting a pre-made strategy [ ask students with [ user-id = hubnet-message-source ] [ ifelse (allow-strategy-change?) ;; if this is permitted under the current game-mode [ hubnet-send user-id "Errors:" ("") set selected-strategy hubnet-message set-code hubnet-send user-id "strategy" user-code send-info-to-clients set code-changed? false ] [ hubnet-send user-id "Errors:" ("You cannot change your strategy while playing") hubnet-send user-id "strategy-choice" (selected-strategy) ] ] ] if command = "strategy" ;; the user has modified the code of the current strategy [ ask students with [ user-id = hubnet-message-source ] [ if (hubnet-message = user-code or not allow-strategy-change?) [ stop ] hubnet-send user-id "Errors:" ("") ;; code is taken, and tested for accuracy, so students can make more changes before playing another round if ( test-strategy hubnet-message ) [ set user-code hubnet-message set code-changed? true ] ] ] if command = "Up" [ execute-move 0 ] if command = "Down" [ execute-move 180 ] if command = "Left" [ execute-move 270 ] if command = "Right" [ execute-move 90 ] if command = "Up-Left" [ execute-move 315 ] if command = "Up-Right" [ execute-move 45 ] if command = "Down-Left" [ execute-move 225 ] if command = "Down-Right" [ execute-move 135 ] if command = "Change Appearance" [ execute-change-turtle ] end ;; Create a turtle, set its shape, color, and position ;; and tell the node what its turtle looks like and where it is to create-new-student create-students 1 [ setup-student-vars hubnet-send user-id "strategy" user-code send-info-to-clients ] end to execute-move [angle] ask students with [user-id = hubnet-message-source ] [ set heading angle ] end ;; sets the turtle variables to appropriate initial values to setup-student-vars ;; turtle procedure set user-id hubnet-message-source set total 0 setup-turtle-vars set selected-strategy "random" set code-changed? false set-code set-unique-shape-and-color end ;; pick a base-shape and color for the turtle to set-unique-shape-and-color let code random max-possible-codes while [member? code used-shape-colors and count students < max-possible-codes] [ set code random max-possible-codes ] set used-shape-colors (lput code used-shape-colors) set base-shape item (code mod length shape-names) shape-names set shape base-shape set color item (code / length shape-names) colors end ;; report the string version of the turtle's color to-report color-string [color-value] report item (position color-value colors) color-names end ;; sends the appropriate monitor information back to the client to send-info-to-clients hubnet-send user-id "You are a:" (word (color-string color) " " base-shape) hubnet-send user-id "Your Score:" (score) hubnet-send user-id "Your Total:" (total) ifelse partner != nobody [ hubnet-send user-id "Partner's Score:" ([score] of partner) hubnet-send user-id "Partner's History:" (map [ ifelse-value (? = true) ["D "] ["C "] ] play-partner-history) hubnet-send user-id "Your History:" ( map [ ifelse-value (? = true) ["D "] ["C "] ] play-history) hubnet-send user-id "Points:" (map [ifelse-value ((?1 = false) and (?2 = false)) [C-C] [ifelse-value ((?1 = false) and (?2 = true)) [C-D] [ ifelse-value ((?1 = true) and (?2 = false)) [D-C] [D-D]]]] play-history play-partner-history) ] [ hubnet-send user-id "Partner's Score:" ("") hubnet-send user-id "Partner's History:" ("") hubnet-send user-id "Your History:" ("") ] end ;; Kill the turtle, set its shape, color, and position ;; and tell the node what its turtle looks like and where it is to remove-student ask students with [user-id = hubnet-message-source] [ set used-shape-colors remove my-code used-shape-colors die ] end ;; translates a student turtle's shape and color into a code to-report my-code report (position base-shape shape-names) + (length shape-names) * (position color colors) end ;; users might want to change their shape and color, so that they can find themselves more easily to execute-change-turtle ask students with [user-id = hubnet-message-source] [ set used-shape-colors remove my-code used-shape-colors show-turtle set-unique-shape-and-color hubnet-send user-id "You are a:" (word (color-string color) " " base-shape) ] end ; Copyright 2003 Uri Wilensky. ; See Info tab for full copyright and license.
There are 7 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Prisoners Dilemma HubNet.png | preview | Preview for 'Prisoners Dilemma HubNet' | over 11 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.