Opening the book…
1. Goal
2. Motivation. Robustness of cooperation in spatial settings
3. Description of the model
CODE 4. Interface design
CODE 5. Code
5.1. Skeleton of the code
5.2. Extension I. Implementation of different neighborhoods
patches at-points [[-1 0] [0 -1] [0 0] [0 1] [1 0]]
patches at-points [[-1 -1] [-1 0] [-1 1] [0 -1] [0 0] [0 1] [1 -1] [1 0] [1 1]]
Implementation of relative coordinates for Moore neighborhoods
to-report moore-offsets [r include-center?] ;; code to be written end
let l (range (- r) (r + 1))
map [el2 -> list el1 el2] l
map [ el1 -> map [el2 -> list el1 el2] l] l
[[[-1 -1] [-1 0] [-1 1]] [[0 -1] [0 0] [0 1]] [[1 -1] [1 0] [1 1]]]
let result reduce sentence map [ el1 -> map [el2 -> list el1 el2] l] l
to-report moore-offsets [r include-center?] let l (range (- r) (r + 1)) let result reduce sentence map [ el1 -> map [el2 -> list el1 el2] l] l report ifelse-value include-center? [ result ] [ remove [0 0] result ] end
Implementation of relative coordinates for Von Neumann neighborhoods
to-report von-neumann-offsets [r include-center?] ;; code to be written endImplementation of procedure to-report von-neumann-offsets
to-report von-neumann-offsets [r include-center?] let moore-list (moore-offsets r include-center?) report filter [l -> abs first l + abs last l <= r] moore-list end
Putting everything together
to setup-players let initial-distribution read-from-string n-of-players-for-each-strategy if length initial-distribution != length payoff-matrix [ user-message (word "The number of items in\n" "n-of-players-for-each-strategy (i.e. " length initial-distribution "):\n" n-of-players-for-each-strategy "\nshould be equal to the number of rows\n" "in the payoff matrix (i.e. " length payoff-matrix "):\n" payoffs ) ]
if sum initial-distribution != count patches [ user-message (word "The total number of agents in\n" "n-of-agents-for-each-strategy (i.e. " sum initial-distribution "):\n" n-of-players-for-each-strategy "\nshould be equal to the number of patches (i.e. " count patches ")" ) ]
ask patches [set strategy false] let i 0 let offsets ifelse-value (neighborhood-type = "Von Neumann") [ von-neumann-offsets neighborhood-range self-matching? ] [ moore-offsets neighborhood-range self-matching? ]
foreach initial-distribution [ j -> ask n-of j (patches with [strategy = false]) [ set payoff 0 set strategy i set my-coplayers patches at-points offsets set n-of-my-coplayers (count my-coplayers) set my-nbrs-and-me (patch-set my-coplayers self) ] set i (i + 1) ]
set n-of-players count patches end
A nice final touch
to show-neighborhood if mouse-down? [ ask patch mouse-xcor mouse-ycor [ ask my-coplayers [set pcolor white] ] ] display end
with-local-randomness [show-neighborhood]
with-local-randomness [ ask patches [update-color] ]
5.3. Extension II. Implementation of different revision protocols
to update-strategy-after-revision ifelse (random-float 1 < noise) [ set strategy-after-revision random n-of-strategies ] [ ;; code to set strategy-after-revision ;; using the revision protocol indicated by the user ;; through parameter protocol ] end
to update-strategy-after-revision ifelse (random-float 1 < noise) [ set strategy-after-revision random n-of-strategies ] [ (ifelse protocol = "best-neighbor" [ best-neighbor-protocol ] protocol = "imitative-pairwise-difference" [ imitative-pairwise-difference-protocol ] protocol = "imitative-positive-proportional-m" [ imitative-positive-proportional-m-protocol ] protocol = "imitative-logit-m" [ imitative-logit-m-protocol ] ) ] endImplementation of procedure to best-neighbor-protocol
to best-neighbor-protocol set strategy-after-revision [strategy] of one-of my-nbrs-and-me with-max [payoff] endImplementation of procedure to imitative-pairwise-difference-protocol
to imitative-pairwise-difference-protocol let observed-player one-of other my-coplayers
;; compute difference in average payoffs let payoff-diff ([payoff / n-of-my-coplayers] of observed-player - (payoff / n-of-my-coplayers))
set strategy-after-revision ifelse-value (random-float 1 < (payoff-diff / max-payoff-difference)) [ [strategy] of observed-player ] [ strategy ] ;; If your strategy is the better, payoff-diff is negative, ;; so you are going to stick with it. ;; If it's not, you switch with probability ;; (payoff-diff / max-payoff-difference) end
globals [ payoff-matrix n-of-strategies n-of-players max-payoff-difference ;; <== New line ]
to setup-payoffs set payoff-matrix read-from-string payoffs set n-of-strategies length payoff-matrix
;;New lines below set max-payoff-difference (max-of-matrix payoff-matrix) - (min-of-matrix payoff-matrix) end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; SUPPORTING PROCEDURES ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;; ;;; Matrices ;;; ;;;;;;;;;;;;;;;;
to-report max-of-matrix [m] report max reduce sentence m end
to-report min-of-matrix [m] report min reduce sentence m end
extensions [rnd]Implementation of procedure to imitative-positive-proportional-m-protocol
to imitative-positive-proportional-m-protocol let chosen-nbr rnd:weighted-one-of my-nbrs-and-me [ payoff ^ m ] set strategy-after-revision [strategy] of chosen-nbr end
to check-payoffs-are-non-negative if min reduce sentence payoff-matrix < 0 [ user-message (word "Since you are using protocol = imitative-positive-proportional-m, all elements in the payoff matrix\n" payoffs "\nshould be non-negative numbers.") ] end
to setup-payoffs set payoff-matrix read-from-string payoffs set n-of-strategies length payoff-matrix
;; New lines below set max-payoff-difference (max-of-matrix payoff-matrix) - (min-of-matrix payoff-matrix) if protocol = "imitative-positive-proportional-m" [ check-payoffs-are-non-negative ] endImplementation of procedure to imitative-logit-m-protocol
to imitative-logit-m-protocol let observed-player one-of other my-coplayers
;; compute difference in average payoffs
let payoff-diff ([payoff / n-of-my-coplayers] of observed-player
- (payoff / n-of-my-coplayers))
set strategy-after-revision ifelse-value (random-float 1 < (1 / (1 + exp (- m * payoff-diff)))) [ [strategy] of observed-player ] [ strategy ] end
5.5. Complete code in the Code tab
extensions [rnd] ;; <== New line globals [ payoff-matrix n-of-strategies n-of-players max-payoff-difference ;; <== New line ] patches-own [ strategy strategy-after-revision payoff my-nbrs-and-me my-coplayers n-of-my-coplayers ] to setup clear-all setup-payoffs setup-players setup-graph reset-ticks update-graph ask patches [update-color] end to setup-payoffs set payoff-matrix read-from-string payoffs set n-of-strategies length payoff-matrix
;; New lines below set max-payoff-difference (max-of-matrix payoff-matrix) - (min-of-matrix payoff-matrix) if protocol = "imitative-positive-proportional-m" [ check-payoffs-are-non-negative ] end to setup-players let initial-distribution read-from-string n-of-players-for-each-strategy if length initial-distribution != length payoff-matrix [ user-message (word "The number of items in\n" "n-of-players-for-each-strategy (i.e. " length initial-distribution "):\n" n-of-players-for-each-strategy "\nshould be equal to the number of rows\n" "in the payoff matrix (i.e. " length payoff-matrix "):\n" payoffs ) ]
if sum initial-distribution != count patches [ user-message (word "The total number of agents in\n" "n-of-agents-for-each-strategy (i.e. " sum initial-distribution "):\n" n-of-players-for-each-strategy "\nshould be equal to the number of patches (i.e. " count patches ")" ) ]
ask patches [set strategy false] let i 0 let offsets ifelse-value (neighborhood-type = "Von Neumann") [ von-neumann-offsets neighborhood-range self-matching? ] [ moore-offsets neighborhood-range self-matching? ]
foreach initial-distribution [ j -> ask n-of j (patches with [strategy = false]) [ set payoff 0 set strategy i set my-coplayers patches at-points offsets set n-of-my-coplayers (count my-coplayers) set my-nbrs-and-me (patch-set my-coplayers self) ] set i (i + 1) ]
set n-of-players count patches end to setup-graph set-current-plot "Strategy Distribution" foreach (range n-of-strategies) [ i -> create-temporary-plot-pen (word i) set-plot-pen-mode 1 set-plot-pen-color 25 + 40 * i ] end to go ifelse synchronous-updating? [ ask patches [ play ] ask patches [ update-strategy-after-revision ] ;; here we are not updating the agent's strategy yet ask patches [ update-strategy ] ;; now we update every agent's strategy at the same time ] [ ask patches [ play ask my-coplayers [ play ] ;; since your coplayers' strategies or ;; your coplayers' coplayers' strategies ;; could have changed since the last time ;; your coplayers played update-strategy-after-revision update-strategy ] ] tick update-graph ask patches [update-color] end to play let n-of-coplayers-with-strategy-? n-values n-of-strategies [ i -> count my-coplayers with [strategy = i] ] let my-payoffs (item strategy payoff-matrix) set payoff sum (map * my-payoffs n-of-coplayers-with-strategy-?) end to update-strategy-after-revision ifelse (random-float 1 < noise) [ set strategy-after-revision random n-of-strategies ] [ (ifelse protocol = "best-neighbor" [ best-neighbor-protocol ] protocol = "imitative-pairwise-difference" [ imitative-pairwise-difference-protocol ] protocol = "imitative-positive-proportional-m" [ imitative-positive-proportional-m-protocol ] protocol = "imitative-logit-m" [ imitative-logit-m-protocol ] ) ] end to best-neighbor-protocol set strategy-after-revision [strategy] of one-of my-nbrs-and-me with-max [payoff] end to imitative-pairwise-difference-protocol let observed-player one-of other my-coplayers
;; compute difference in average payoffs let payoff-diff ([payoff / n-of-my-coplayers] of observed-player - (payoff / n-of-my-coplayers))
set strategy-after-revision ifelse-value (random-float 1 < (payoff-diff / max-payoff-difference)) [ [strategy] of observed-player ] [ strategy ] ;; If your strategy is the better, payoff-diff is negative, ;; so you are going to stick with it. ;; If it's not, you switch with probability ;; (payoff-diff / max-payoff-difference) end to imitative-positive-proportional-m-protocol let chosen-nbr rnd:weighted-one-of my-nbrs-and-me [ payoff ^ m ] ;; https://ccl.northwestern.edu/netlogo/docs/rnd.html#rnd:weighted-one-of set strategy-after-revision [strategy] of chosen-nbr end to imitative-logit-m-protocol let observed-player one-of other my-coplayers
;; compute difference in average payoffs let payoff-diff ([payoff / n-of-my-coplayers] of observed-player - (payoff / n-of-my-coplayers))
set strategy-after-revision ifelse-value (random-float 1 < (1 / (1 + exp (- m * payoff-diff)))) [ [strategy] of observed-player ] [ strategy ] end to update-strategy set strategy strategy-after-revision end to update-graph let strategy-numbers (range n-of-strategies) let strategy-frequencies map [ n -> count patches with [strategy = n] / n-of-players ] strategy-numbers
set-current-plot "Strategy Distribution" let bar 1 foreach strategy-numbers [ n -> set-current-plot-pen (word n) plotxy ticks bar set bar (bar - (item n strategy-frequencies)) ] set-plot-y-range 0 1 end to update-color set pcolor 25 + 40 * strategy end ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; SUPPORTING PROCEDURES ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; to-report moore-offsets [r include-center?] let l (range (- r) (r + 1)) let result reduce sentence map [ el1 -> map [el2 -> list el1 el2] l] l report ifelse-value include-center? [ result ] [ remove [0 0] result ] end to-report von-neumann-offsets [r include-center?] let moore-list (moore-offsets r include-center?) report filter [l -> abs first l + abs last l <= r] moore-list end to show-neighborhood if mouse-down? [ ask patch mouse-xcor mouse-ycor [ ask my-coplayers [set pcolor white] ] ] display end to check-payoffs-are-non-negative if min reduce sentence payoff-matrix < 0 [ user-message (word "Since you are using protocol = imitative-positive-proportional-m, all elements in the payoff matrix\n" payoffs "\nshould be non-negative numbers.") ] end ;;;;;;;;;;;;;;;; ;;; Matrices ;;; ;;;;;;;;;;;;;;;; to-report max-of-matrix [matrix] report max reduce sentence matrix end to-report min-of-matrix [matrix] report min reduce sentence matrix end
6. Sample runs
Revision protocols
Neighborhoods
Critical neighborhood size at which cooperation does not emerge in the simulation parameterized as in figure 1.Discussion
7. Exercises
[ (ifelse protocol = "best-neighbor" [ best-neighbor-protocol ] protocol = "imitative-pairwise-difference" [ imitative-pairwise-difference-protocol ] protocol = "imitative-positive-proportional-m" [ imitative-positive-proportional-m-protocol ] protocol = "imitative-logit-m" [ imitative-logit-m-protocol ] ) ]
This page titled 3.4: Other types of neighborhoods and other revision protocols was last modified on Mon, 13 Mar 2023 04:09:47 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Luis R. Izquierdo, Segismundo S. Izquierdo, William H. Sandholm, & William H. Sandholm via source content that was edited to the style and standards of the LibreTexts platform.
The book could not be loaded just now.