breeds [ user computer ] globals [ human-score computer-score iteration hidden-strategy ] turtles-own [ score ;;my current score defect-now? ;;what will I do this round? partner ;;the who of my partner partner-defected? ;;did my partner defect last round? partner-defected-past? ;;did my partner defect two rounds ago? ] to setup ca ;;place the computer create-custom-computer 1 [ set partner 1 set shape "computer" set heading 90 fd screen-edge-x / 2 ] ;;place the human create-custom-user 1 [ set partner 0 set shape "person" set heading 270 fd screen-edge-x / 2 ] ;;initially assume you and your partner have always cooperated ask turtles [ set defect-now? false set partner-defected? false set partner-defected-past? false set size 10 set label 3.0 ] prepare-next-round ;;choose the secret strategy the computer will play if select-computer-strategy? is off set hidden-strategy random 6 do-plotting end ;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Runtime Procedures;;; ;;;;;;;;;;;;;;;;;;;;;;;;; to play ;;choose strategy ask user [ set-action human-strategy ] play-a-round prepare-next-round end to play-a-round ;ask each turtle to select its strategy ifelse select-computer-strategy? [ask computer [ set-action computer-strategy ]] [ask computer [ set-action hidden-strategy ]] ;based upon the strategy each agent has choosen, determine this round's payoffs ask turtles [ get-payoff ] ;;update the displayed score in the graphics window ask turtles [ set label precision (score / iteration) 3] do-plotting end to prepare-next-round set computer-score score-of turtle 0 set human-score score-of turtle 1 set iteration iteration + 1 ;;display the computer's action in the last round if display-history? [ ask user [ ifelse partner-defected? [show "Last turn your partner defected"] [show "Last turn your partner cooperated"] ] show "Choose your action" ] end to set-action [strategy ] ;;Turtle Procedure ;;call the strategy based on the number passed through if (strategy = "random") [ act-randomly ] if (strategy = "cooperate") [ cooperate ] if (strategy = "defect") [ defect ] if (strategy = "tit-for-tat") [ tit-for-tat ] if (strategy = "tit-for-two-tats") [ tit-for-two-tats ] if (strategy = "unforgiving") [ unforgiving ] if (strategy = "custom-strategy") [ custom-strategy ] end ;;;;;;;;;;;;;;;;;; ;;; Strategies ;;; ;;;;;;;;;;;;;;;;;; to act-randomly ;;Turtle Procedure ifelse (random 2 = 0) [set defect-now? false] [set defect-now? true] end to cooperate ;;Turtle Procedure set defect-now? false end to defect ;;Turtle Procedure set defect-now? true end to tit-for-tat ;;Turtle Procedure ifelse partner-defected? [ set defect-now? true ] [ set defect-now? false ] end to tit-for-two-tats ;;Turtle Procedure ifelse (partner-defected? and partner-defected-past?) [set defect-now? true] [set defect-now? false] end to unforgiving ;;Turtle Procedure ifelse (partner-defected? or defect-now?) [set defect-now? true] [set defect-now? false] end to custom-strategy ;;Turtle Procedure ;;Currently defaults to tit-for-tat. Can you do better? ifelse partner-defected? ;;partner defected stores your partner's action last round [set defect-now? true] [set defect-now? false] end ;;;;;;;;;;;;;;;;;;;;;; ;;; End Strategies ;;; ;;;;;;;;;;;;;;;;;;;;;; to get-payoff ;;Turtle Procedure set partner-defected-past? partner-defected? set partner-defected? defect-now?-of turtle partner ifelse partner-defected? [ifelse defect-now? [set score score + 1] [set score score + 0] ] [ifelse defect-now? [set score score + 5] [set score score + 3] ] end to do-plotting set-current-plot-pen "human" plot label-of turtle 1 set-current-plot-pen "computer" plot label-of turtle 0 end ; *** NetLogo Model Copyright Notice *** ; ; This model was created as part of the project: ; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN ; CLASSROOMS. The project gratefully acknowledges the support of the ; National Science Foundation (REPP program) -- grant number REC #9814682. ; ; Copyright 2002 by Uri Wilensky. Updated 2002. 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. ; ; To refer to this model in academic publications, please use: ; Wilensky, U. (2002). NetLogo PD Two Person Iterated model. ; http://ccl.northwestern.edu/netlogo/models/PDTwoPersonIterated. ; Center for Connected Learning and Computer-Based Modeling, ; Northwestern University, Evanston, IL. ; ; In other publications, please use: ; Copyright 1998 by Uri Wilensky. All rights reserved. See ; http://ccl.northwestern.edu/netlogo/models/PDTwoPersonIterated ; for terms of use. ; ; *** End of NetLogo Model Copyright Notice *** @#$#@#$#@ GRAPHICS-WINDOW 329 10 724 426 17 17 11.0 1 10 1 1 1 CC-WINDOW 329 436 715 538 Command Center BUTTON 6 43 87 76 Setup setup NIL 1 T OBSERVER T MONITOR 22 220 120 269 NIL human-score 3 1 MONITOR 120 220 233 269 NIL computer-score 3 1 MONITOR 233 220 305 269 NIL iteration 3 1 PLOT 22 269 305 459 Average Score Iterations Ave Score 0.0 10.0 0.0 5.0 true true PENS "human" 1.0 0 -16777216 true "computer" 1.0 0 -16776961 true BUTTON 87 43 183 76 Play Once play NIL 1 T OBSERVER T BUTTON 183 43 313 76 Play Repeatedly play T 1 T OBSERVER T SWITCH 59 128 258 161 select-computer-strategy? select-computer-strategy? 0 1 -1000 SWITCH 74 463 246 496 display-history? display-history? 1 1 -1000 CHOICE 59 83 258 128 human-strategy human-strategy "random" "cooperate" "defect" "tit-for-tat" "tit-for-two-tats" "unforgiving" "custom-strategy" 0 CHOICE 59 161 258 206 computer-strategy computer-strategy "random" "cooperate" "defect" "tit-for-tat" "tit-for-two-tats" "unforgiving" "custom-strategy" 3 @#$#@#$#@ WHAT IS IT? ----------- This model is an iterated version of the prisoner's dilemma. If you are unfamiliar with the basic concepts of the prisoner's dilemma, please refer to the PD BASIC model found in the PRISONER'S DILEMMA suite. HOW IT WORKS ------------ The PD BASIC model presents an interesting problem: In order to minimize the overall jail time you would cooperate with your partner and remain silent and not confess. However, the rational choice is to defect against your partner by confessing. 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, you will both be worse off. The dilemma is made more interesting when you know you will interact with the person again. Let us consider the case where you and a friend are chosen for a research study to play the prisoner's dilemma game; only instead of the payoffs being years of jail time, they are money. The researchers separate you and your friend into separate rooms allowing communication to occur only through a computer. They give you a sheet with the rules for the iterated prisoner's dilemma that reads as follows: RULES FOR THE ITERATED PRISONER'S DILEMMA ----------------------------------------- 1. This game will consist of an unspecified number of rounds. At the end of the game, you will receive $1 for each point you have earned. 2. Each round you and your partner will have the opportunity to earn points by choosing to either cooperate (C) or defect (D). Communication will be done only through the computer. The only message you will be able to pass is cooperate or defect. Neither person will see the other's message until both have chosen their action. 3. Your payoff for each round will determined by the actions as follows: | | Partner's Action | Your | | Action | C D | -------|----------------- | C | 3 0 | -------|----------------- | D | 5 1 | -------|----------------- (Note: This way of determining your payoff is the opposite of the PD BASIC model. In PD BASIC, you were awarded something bad- jail time. In this model, you are awarded something good- money.) Your partner has an identical payoff matrix. HOW TO USE IT ------------- Buttons ------- SETUP: Begin playing the iterated prisoner's dilemma. If you choose to turn the SELECT-COMPUTER-STTRATEGY? switch off before pressing this button, the computer's strategy will be randomly chosen at this time. PLAY ONCE: Play a single round of the prisoner's dilemma with the strategy you have selected. PLAY REPEATEDLY: Repeatedly play rounds of the prisoner's dilemma between you and the computer. You can change your strategy at any time. If the SELECT-COMPUTER-STTRATEGY? switch is on you can also change the computer's strategy at any time. With both PLAY buttons, the computer's action each round will be displayed in the command center. Switches -------- SELECT-COMPUTER-STRATEGY?: If on, you may select the computer's strategy using the computer strategy slider. If off, the computer's strategy will be randomly chosen from the strategy list found below, excluding the Custom Strategy. DISPLAY-HISTORY?: Turn on or off messaging in the command center. Sliders ------- HUMAN-STRATEGY - Select your strategy from the list below. COMPUTER STRATEGY - Select the computer's strategy from the list below. STRATEGIES: Random - randomly cooperate or defect Cooperate - cooperate always Defect - defect always Tit-for-Tat - If the opponent cooperates this round cooperate next round. If the opponent defects this round, defect next round. Initially cooperate. 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. Unforgiving - Cooperate always unless the opponent defects once. Upon opponent defection retaliate by defecting always. Custom-Strategy - This strategy is intended to be written by you. It currently defaults to Tit-for-Tat. Monitors -------- HUMAN-SCORE - The total points you have earned COMPUTER-SCORE - The total points the computer has earned ITERATION - The number of rounds that have been played Plots ----- AVERAGE SCORE: The average scores of you and the computer each round vs. the number of iterations. This is a good indicator of how well you are doing relative to the maximum possible average of $5 per round. THINGS TO NOTICE ---------------- Should the computer always plays strategy #1 (cooperate), then which strategy for the user results in the highest score? If the computer always plays strategy #2 (defect), then what is the nature of the average score plot when the user plays strategy #3 - #6 (Tit-for-Tat, Tit-for-Two-Tat, Unforgiving, and Custom Strategy, respectively)? Why does such a nature arise for these combination of strategies? What is the nature of the plot for average score when the computer always plays strategy #3 and the user plays every startegy except strategy #2 (defect) and strategy #0 (random)? Why does such a curve arise? THINGS TO TRY ------------- 1. Turn the SELECT-COMPUTER-STTRATEGY? switch off. Setup the model and play the iterated prisoner's dilemma against the computer. You may choose between selecting your strategy each round using the PLAY ONCE button, or automating your choices each round using the PLAY REPEATEDLY button. What approach wins you the most money? 2. Turn the SELECT-COMPUTER-STTRATEGY? switch on. Experiment with playing different strategies against one another. Which strategies do the best? Which do the worst? Why? 3. Repeat task 1 several times. How does the best strategy vary? Based on you experience in task 2, why might this be so? 4. The researchers now tell you that they will double the amount of money the person with the most points gets at the end, but the other person will get nothing. In the event of a tie, each person still recieves $1 per point. How does this change your strategy? Why? 5. Describe a real life scenario that is similar to the iterated prisoner's dilemma, preferably one you have experienced. How might the strategies examined here relate to actions taken in that scenario? EXTENDING THE MODEL ------------------- Even the most complex strategies in this model are relatively simple. Surely you can do better. Redefine the CUSTOM-STRATEGY procedure attempting to develop a strategy that can earn a higher score than those presented in the model or a human player. Test it against the other strategies and yourself. What are its strengths? What are its weaknesses? Try to keep improving it. Examine the PD N-PERSON ITERATED model NETLOGO FEATURES ---------------- Note the use of the turtle variable LABEL to display each turtle's average score in the GRAPHICS WINDOW. Note that the SET-ACTION [STRATEGY] procedure uses "[]" to define a parameter that must be passed through when it is called. RELATED MODELS -------------- PD Basic, PD N-Person Iterated, PD Basic Evolutionary CREDITS AND REFERENCES ---------------------- To refer to this model in academic publications, please use: Wilensky, U. (2002). NetLogo PD Two Person Iterated model. http://ccl.northwestern.edu/netlogo/models/PDTwoPersonIterated. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL. In other publications, please use: Copyright 2002 by Uri Wilensky. All rights reserved. See http://ccl.northwestern.edu/netlogo/models/PDTwoPersonIterated for terms of use. @#$#@#$#@ default true 0 Polygon -7566196 true true 150 5 40 250 150 205 260 250 ant true 0 Polygon -7566196 true true 136 61 129 46 144 30 119 45 124 60 114 82 97 37 132 10 93 36 111 84 127 105 172 105 189 84 208 35 171 11 202 35 204 37 186 82 177 60 180 44 159 32 170 44 165 60 Polygon -7566196 true true 150 95 135 103 139 117 125 149 137 180 135 196 150 204 166 195 161 180 174 150 158 116 164 102 Polygon -7566196 true true 149 186 128 197 114 232 134 270 149 282 166 270 185 232 171 195 149 186 149 186 Polygon -7566196 true true 225 66 230 107 159 122 161 127 234 111 236 106 Polygon -7566196 true true 78 58 99 116 139 123 137 128 95 119 Polygon -7566196 true true 48 103 90 147 129 147 130 151 86 151 Polygon -7566196 true true 65 224 92 171 134 160 135 164 95 175 Polygon -7566196 true true 235 222 210 170 163 162 161 166 208 174 Polygon -7566196 true true 249 107 211 147 168 147 168 150 213 150 arrow true 0 Polygon -7566196 true true 150 0 0 150 105 150 105 293 195 293 195 150 300 150 bee true 0 Polygon -256 true false 152 149 77 163 67 195 67 211 74 234 85 252 100 264 116 276 134 286 151 300 167 285 182 278 206 260 220 242 226 218 226 195 222 166 Polygon -16777216 true false 150 149 128 151 114 151 98 145 80 122 80 103 81 83 95 67 117 58 141 54 151 53 177 55 195 66 207 82 211 94 211 116 204 139 189 149 171 152 Polygon -7566196 true true 151 54 119 59 96 60 81 50 78 39 87 25 103 18 115 23 121 13 150 1 180 14 189 23 197 17 210 19 222 30 222 44 212 57 192 58 Polygon -16777216 true false 70 185 74 171 223 172 224 186 Polygon -16777216 true false 67 211 71 226 224 226 225 211 67 211 Polygon -16777216 true false 91 257 106 269 195 269 211 255 Line -1 false 144 100 70 87 Line -1 false 70 87 45 87 Line -1 false 45 86 26 97 Line -1 false 26 96 22 115 Line -1 false 22 115 25 130 Line -1 false 26 131 37 141 Line -1 false 37 141 55 144 Line -1 false 55 143 143 101 Line -1 false 141 100 227 138 Line -1 false 227 138 241 137 Line -1 false 241 137 249 129 Line -1 false 249 129 254 110 Line -1 false 253 108 248 97 Line -1 false 249 95 235 82 Line -1 false 235 82 144 100 bird1 false 0 Polygon -7566196 true true 2 6 2 39 270 298 297 298 299 271 187 160 279 75 276 22 100 67 31 0 bird2 false 0 Polygon -7566196 true true 2 4 33 4 298 270 298 298 272 298 155 184 117 289 61 295 61 105 0 43 boat1 false 0 Polygon -1 true false 63 162 90 207 223 207 290 162 Rectangle -6524078 true false 150 32 157 162 Polygon -16776961 true false 150 34 131 49 145 47 147 48 149 49 Polygon -7566196 true true 158 33 230 157 182 150 169 151 157 156 Polygon -7566196 true true 149 55 88 143 103 139 111 136 117 139 126 145 130 147 139 147 146 146 149 55 boat2 false 0 Polygon -1 true false 63 162 90 207 223 207 290 162 Rectangle -6524078 true false 150 32 157 162 Polygon -16776961 true false 150 34 131 49 145 47 147 48 149 49 Polygon -7566196 true true 157 54 175 79 174 96 185 102 178 112 194 124 196 131 190 139 192 146 211 151 216 154 157 154 Polygon -7566196 true true 150 74 146 91 139 99 143 114 141 123 137 126 131 129 132 139 142 136 126 142 119 147 148 147 boat3 false 0 Polygon -1 true false 63 162 90 207 223 207 290 162 Rectangle -6524078 true false 150 32 157 162 Polygon -16776961 true false 150 34 131 49 145 47 147 48 149 49 Polygon -7566196 true true 158 37 172 45 188 59 202 79 217 109 220 130 218 147 204 156 158 156 161 142 170 123 170 102 169 88 165 62 Polygon -7566196 true true 149 66 142 78 139 96 141 111 146 139 148 147 110 147 113 131 118 106 126 71 box true 0 Polygon -7566196 true true 45 255 255 255 255 45 45 45 butterfly1 true 0 Polygon -16777216 true false 151 76 138 91 138 284 150 296 162 286 162 91 Polygon -7566196 true true 164 106 184 79 205 61 236 48 259 53 279 86 287 119 289 158 278 177 256 182 164 181 Polygon -7566196 true true 136 110 119 82 110 71 85 61 59 48 36 56 17 88 6 115 2 147 15 178 134 178 Polygon -7566196 true true 46 181 28 227 50 255 77 273 112 283 135 274 135 180 Polygon -7566196 true true 165 185 254 184 272 224 255 251 236 267 191 283 164 276 Line -7566196 true 167 47 159 82 Line -7566196 true 136 47 145 81 Circle -7566196 true true 165 45 8 Circle -7566196 true true 134 45 6 Circle -7566196 true true 133 44 7 Circle -7566196 true true 133 43 8 circle false 0 Circle -7566196 true true 35 35 230 computer false 0 Rectangle -7566196 true true -55 -45 297 296 Rectangle -16777216 true false 43 27 264 263 Rectangle -11352576 true false 50 46 255 52 Rectangle -11352576 true false 53 238 250 246 Circle -11352576 true false 166 117 9 Rectangle -11352576 true false 153 70 205 76 Rectangle -11352576 true false 84 220 147 228 person false 0 Circle -7566196 true true 155 20 63 Rectangle -7566196 true true 158 79 217 164 Polygon -7566196 true true 158 81 110 129 131 143 158 109 165 110 Polygon -7566196 true true 216 83 267 123 248 143 215 107 Polygon -7566196 true true 167 163 145 234 183 234 183 163 Polygon -7566196 true true 195 163 195 233 227 233 206 159 spacecraft true 0 Polygon -7566196 true true 150 0 180 135 255 255 225 240 150 180 75 240 45 255 120 135 thin-arrow true 0 Polygon -7566196 true true 150 0 0 150 120 150 120 293 180 293 180 150 300 150 truck-down false 0 Polygon -7566196 true true 225 30 225 270 120 270 105 210 60 180 45 30 105 60 105 30 Polygon -8716033 true false 195 75 195 120 240 120 240 75 Polygon -8716033 true false 195 225 195 180 240 180 240 225 truck-left false 0 Polygon -7566196 true true 120 135 225 135 225 210 75 210 75 165 105 165 Polygon -8716033 true false 90 210 105 225 120 210 Polygon -8716033 true false 180 210 195 225 210 210 truck-right false 0 Polygon -7566196 true true 180 135 75 135 75 210 225 210 225 165 195 165 Polygon -8716033 true false 210 210 195 225 180 210 Polygon -8716033 true false 120 210 105 225 90 210 turtle true 0 Polygon -7566196 true true 138 75 162 75 165 105 225 105 225 142 195 135 195 187 225 195 225 225 195 217 195 202 105 202 105 217 75 225 75 195 105 187 105 135 75 142 75 105 135 105 wolf-left false 3 Polygon -6524078 true true 117 97 91 74 66 74 60 85 36 85 38 92 44 97 62 97 81 117 84 134 92 147 109 152 136 144 174 144 174 103 143 103 134 97 Polygon -6524078 true true 87 80 79 55 76 79 Polygon -6524078 true true 81 75 70 58 73 82 Polygon -6524078 true true 99 131 76 152 76 163 96 182 104 182 109 173 102 167 99 173 87 159 104 140 Polygon -6524078 true true 107 138 107 186 98 190 99 196 112 196 115 190 Polygon -6524078 true true 116 140 114 189 105 137 Rectangle -6524078 true true 109 150 114 192 Rectangle -6524078 true true 111 143 116 191 Polygon -6524078 true true 168 106 184 98 205 98 218 115 218 137 186 164 196 176 195 194 178 195 178 183 188 183 169 164 173 144 Polygon -6524078 true true 207 140 200 163 206 175 207 192 193 189 192 177 198 176 185 150 Polygon -6524078 true true 214 134 203 168 192 148 Polygon -6524078 true true 204 151 203 176 193 148 Polygon -6524078 true true 207 103 221 98 236 101 243 115 243 128 256 142 239 143 233 133 225 115 214 114 wolf-right false 3 Polygon -6524078 true true 170 127 200 93 231 93 237 103 262 103 261 113 253 119 231 119 215 143 213 160 208 173 189 187 169 190 154 190 126 180 106 171 72 171 73 126 122 126 144 123 159 123 Polygon -6524078 true true 201 99 214 69 215 99 Polygon -6524078 true true 207 98 223 71 220 101 Polygon -6524078 true true 184 172 189 234 203 238 203 246 187 247 180 239 171 180 Polygon -6524078 true true 197 174 204 220 218 224 219 234 201 232 195 225 179 179 Polygon -6524078 true true 78 167 95 187 95 208 79 220 92 234 98 235 100 249 81 246 76 241 61 212 65 195 52 170 45 150 44 128 55 121 69 121 81 135 Polygon -6524078 true true 48 143 58 141 Polygon -6524078 true true 46 136 68 137 Polygon -6524078 true true 45 129 35 142 37 159 53 192 47 210 62 238 80 237 Line -16777216 false 74 237 59 213 Line -16777216 false 59 213 59 212 Line -16777216 false 58 211 67 192 Polygon -6524078 true true 38 138 66 149 Polygon -6524078 true true 46 128 33 120 21 118 11 123 3 138 5 160 13 178 9 192 0 199 20 196 25 179 24 161 25 148 45 140 Polygon -6524078 true true 67 122 96 126 63 144 @#$#@#$#@ NetLogo 2.0beta4 @#$#@#$#@ setup @#$#@#$#@ @#$#@#$#@