This page was automatically generated by NetLogo 4.1.2.

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 (uniform et triangulaire for web.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.)

The applet will also need access to the array extension. Copy the entire directory for each required extension into the same directory as the applet.

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: uniform et triangulaire for web.nlogo

WHAT IS IT?

A prisonner dilemma game with emotions (the game is best reponse normalized ). In a framework compatible with reciprocal altruism, it aims at simulating the effect of local interaction on emergence of cooperation and cooperative segregative zones.


HOW IT WORKS

Each player has a cost of cooperating C and a random emotion between 0 and Xmax.
We use a framework similar to stochastic games (Young 1998). Each player may have two actions "cooperate" (color green) and defect (color red); At each period of time a number of moving turtles are chosen. They draws a sample of k other players (depending on the slider "chosen size") and chose a best response to the current proportion of cooperators in their current sample. However with a probability epsilon the strategy chosen is random. Players play a stochastic adaptive game in the sense of Young (1998).

The neighbourhood of the player can be local (neighbourhood button = on) with (k = chosen-size) nearest neighbours or random (neighbourhood button = off) with (k = chosen-size) randomly chosen neighbours.

We use two different concepts for payoffs : fitness corresponds to what is effectively earned by turtles in the game. It is the prisonner dilemma without emotions. Payoffs correspond to Fitness with emotion. Best responses are computed with the use of payoffs. Emotions, interpreted as shame, are only supported by defectors and are proportional to the number of cooperator they face.

#### Pseudo code #####

to setup

- Create randomly a population of green (cooperators) and red (defectors) turtles. Affect a parameter emotion to each turtle.
- Choose the environment:
- interaction environment : random or local
- size of the sample of chosen turtles (4 , 8 24 , 44).
- affect an emotions to each turtle from a uniform distribution on [0, Xmax]

to go
- Given the chosen environment
- choose randomly a number of "moving-turtles" typically a number small with respect to the population number
- the moving-turtles inspect their sample (number of reds and greens in the sample) and decide to cooperate or defect in the next period by choosing a best response to the sample configuration. With a probability epsilon the choice is stochastic.
- once the choice is made an updtating procedure follows: each moving-turtle update his choice (color may change) and fitness level.
- The state space of population configuration is updated (colors)
- news statistics for the population are computed (update globals) : %-cooperators, %-defectors, mean-emotion of cooperators and defectors (mean fitness in the population)
- a statistic of unhappy red is computed : unhappy reds are those turtles who would like to cooperate given the proportion of cooperators in the population but who are forced to defect given the chosen environment configuration.
- a statistic of segregation : a schelling index normalized by given the proportion of greens and reds in the population.

Defining fitness: Fitness of a turtle over one period is defined by
--> if the turtle cooperates, the fitness is given by G - C if all other turtles in the sample cooperate and - C if all defects
--> if the turtle defects, the fitness is given by G if all cooperate and 0 if all defect. It is a prisonner dilemma.
If a proportion of eta*k cooperates then fitness is simply the average of the respective fitnesses of all turtles.
Total fitness of a turtle equals to the sum of the turtle's fitness over a number of period of length "memory".
Normalized_fitness of a turtle equals to (total_fitness + ( C * memory ) ) / ( memory * (G + C) ). And if C equals 1 to (total_fitness + memory ) / ( memory * 2 ).
We Fix G= 1.
Actions of turtles are not based on fitness but on payoffs: the only change is when the turtle defects, where payoffs are given by G-X if all cooperate and 0 if all defect where X is the idiosyncratic emotion of the turtle. If the turtle cooperates payoff corresponds to fitness. Given these payoffs, each moving turtle choose a stochastic best response to the proportion of cooperators in the given sample (that is with probability epsilon/2 the action of the player may not be a best response.


HOW TO USE IT

- Choose your random seed: if seed procedure is on , you may choose your own seed.
- Choose the type of interaction local versus global: if switch "neighbourhood" is on the interaction is local.
- Choose the sample size with the chooser "chosen-size" = 4 or 8 or 24 or 44.
- Choose your random noise "epsilon" corresponding to the size of error in choosing a best response ( if epsilon = 1 then choice is random and if epsilon = 0 then choice is optimal for the chosen sample).

- run simulations and compare


THINGS TO NOTICE

The rate of cooperation

Segregation emerges with local interaction and small sample size.


THINGS TO TRY


EXTENDING THE MODEL


NETLOGO FEATURES

This section could point out any especially interesting or unusual features of NetLogo that the model makes use of, particularly in the Procedures tab. It might also point out places where workarounds were needed because of missing features.


RELATED MODELS

This section could give the names of models in the NetLogo Models Library or elsewhere which are of related interest.


CREDITS AND REFERENCES

Model : R. Waldeck. See paper Segregation in social dilemma for a discussion of results.

The segregation and interaction procedure has been inspired from Sebastian Grauwin
http://ccl.northwestern.edu/netlogo/models/community/Segreg-vs-coord .

Uri Wilensky for random seed example in Code examples.
All errors my own.


PROCEDURES

extensions [array]


globals [
 radius-n                     ; radius of a neighborhood
 %-cooperators                ; proportion of green players : scale 100
 list-%-cooperation           ; list of length "memory " of %-cooperators  over the last memory periods
 mean-coop                    ;  mean of the above list
 sd-coop                      ; standard deviation of the above list
 %-emotion-cooperators        ; mean emotion of cooperators   scale 100
 %-emotion-defectors ;
 cooperators                  ; number of greens = cooperator
 defectors                    ; number of reds
 emotions-cooperators         ; list of emotions of cooperators
 emotions-defectors
 mean-emotion                 ; mean emotion in the population on scale 100
 tab                          ; array by who number of actions (cooperate, defect) of turtles
 tab-emotion                  ; array by who population of emotions of players
 population                   ; number of turtles
 G                            ; payoff from cooperation 
 tab-fitness                  ; array by who number of normalized fitness. 
                              
 mean-fitness-cooperators     ; for each period computes the average normalized fitness of cooperative turtles with Normalized_fitness of a turtle is equalt to (total_fitness  + ( C * memory  ) ) / ( memory * (G + C)  ) 
 mean-fitness-defectors       ; 
 mean-fitness  
 N-size                       ; the  size of the sample chosen by turtles given the move in the period (moving-turtles)
 X_c                          ; threshold level of emotion for a turtle to cooperate ; if emotion of turtle is greater than X_c  his best response is to cooperate. If matching was population wide the turtle with emotion >  X_c (= C * 100  / %-cooperators) would cooperate. 
 unhappy-turtles ;            ; given the prevailing % of cooperators, these turtles would like to cooperate given their emotion (emotion > X_c) but are prisonner of to much  defection in their sample
 %-unhappy-turtles            ; % of unhappy-turtles   on scale 100    
 list-unhappy?                ;  a list of length "memory " of %-unhappy-turtles  over the last memory periods 
 mean-unhappy?                ; mean of the above list
 sd-unhappy?                  ; standard deviation of the above list
 random-mixed-pairs           ; the mean of the  mixed pairs  under a random configuration with %-cooperators
 mixed-pairs                  ; number of pairs of green-red neighbors   (turtles procedure i.e. ask over all turtles) 
 segregation                  ; index of segregation
 list-segregation             ; creates a list of length "memory of segregation
 mean-segreg                  ; mean of the above list
 sd-segreg                    ; standard deviation of the above list
 my-seed1
 
]

turtles-own [
 
  emotion                     ; emotion of one turtle
  a                           ; emotion minus cost of cooperation C
 
  red-nearby-pondere          ; total payoff for a red turtle resulting from  green and red turtles in the sample of N-size of the turtle. If Neighborhood is on the sample is with neighbors  if not the sample is random. 
  green-nearby-pondere
  fitness_list                ; list of lenght "memory" of past and current fitness of each turtle
  total_fitness               ; sum of fitness-list.   - C * memory <= Total fitness <= G * memory
  normalized_fitness          ; normalized_fitness = (total_fitness + ( C * memory  ) ) / ( memory * (G + C)  ). If C = 1, normalized_fitness= (total_fitness + C* memory) /2C*memory
  numberB                     ; if numberB = 1 the turtle cooperates if numberB = 0 , the turtle defects
  fitness                     ; Fitness for one period is defined by 
                              ; if the turtle cooperates the fitness is given by G - C if all other turtles in the sample cooperate and - C if all defects
                              ; if defect the fitness is given by G if all cooperate and 0 if all defect. It is a prisonner dilemma.
  similar-nearby              ; how many neighboring patches have a turtle with my color?
  other-nearby                ; how many  turtles of a different  color in the sample of the calling turtle
  red-nearby                  ; count of red turtles in the sample
  green-nearby                ; count of green turtles in the sample
  unhappy?                     ; if = 1 turtle is unhappy and if equal to 0 the turtle is happy    
]




;;;;;;;;;;;;;; SETUP PROCEDURE ;;;;;;;;;;;

to setup
 
  clear-all
  ifelse seed-procedure 
      [  use-seed-from-user ]
      [ use-new-seed ]
  set list-segregation []
  set list-unhappy? []
  set list-%-cooperation []
  set G 1
  set population ((max-pxcor + 1) * (max-pycor + 1)) 
  set-default-shape turtles "circle"  
  let populationA  random population
  ask n-of population patches
    [sprout 1
       [set color green  
         if (distribution = "triangular")  [ set emotion  (Xmax * (random-float 1)^ 0.5  )   ]
         if (distribution = "uniform") [set emotion  random-float Xmax  ]  ; chooses a value for emotion less than Xmax
          set a (emotion - C)
         ] ] ; turtle specific parameter: a represents emotion - C of a turtle where C represents the cost of cooperation  
  ask n-of populationA turtles
    [ set color red ]
  ask turtles [ set fitness_list [] ]
  choose-size
  setup-array
  update-variables
  do-plots
  
 
 end

to choose-size
  ;set cooperators count (turtles) with [color = green]
 ; set %-cooperators (Cooperators * 100 / population) 
  ;; choice of sample size
  if (chosen-size = "4-cells") [set radius-n 1 set  N-size 4 ]
     
  if (chosen-size = "8-cells") [set radius-n 1.5 set  N-size 8]
     
  if (chosen-size = "24-cells") [set radius-n 2.9 set  N-size 24]
    
  if (chosen-size = "44-cells") [set radius-n 3.9  set  N-size 44]
  
  
   
end

to use-seed-from-user
 ; set my-seed read-from-string user-input "Enter a random seed (an integer):"
  ; output-print word "User-entered seed: " my-seed  ;; print it out
  random-seed my-seed             ;; use the new seed
end

to use-new-seed
  set my-seed1 new-seed            ;; generate a new seed
  output-print word "Generated seed: " my-seed1  ;; print it out
  random-seed my-seed1             ;; use the new seed
end


to  setup-array
  
  set tab array:from-list n-values ((max-pxcor + 1) * (max-pycor + 1))  [0] ; turtles list of 1 (cooperator) and 0 (defectors)
  set tab-emotion array:from-list n-values ((max-pxcor + 1) * (max-pycor + 1))  [0] ; turtles list of emotions
  set tab-fitness array:from-list n-values ((max-pxcor + 1) * (max-pycor + 1))  [0]
  ask turtles [
     ifelse [color] of self = green [set numberB 1][set numberB 0] ; for counts the number of green B= 1 and number of red B=0
     array:set tab who numberB ;  the array are ranged according to who numbers and value = numberB for all turtles
     array:set tab-emotion who emotion ]; creates the array of turtles emotions ordered by who numbers
     
     
     ;set fitness_list n-values memory [0] ; for each turtle creates a list of length "memory with pas fitness.
     ;set total_fitness sum fitness_list
     ;set normalized_fitness ( (total_fitness + ( C * (memory + 1 ) ) )/ ( memory + 1 ) )   ; addition of C * (memory + 1 ) for strict positivity and division by memory +1 for normalizaion
    ;set normalized_fitness ( (total_fitness + ( C * memory  ) )/ ( memory ) ) 
     ;array:set tab-fitness who normalized_fitness  ]
  
    
end




;;;;; GO PROCEDURE ;;;;;;

to go
  if (ticks >= Number-ticks)[stop]
  ifelse neighbourhood             ; if booleab true reports a local  neighbourhood if not a random one.
    [update-tab1]
    [update-tab2]
  update-variables
  tick  
  do-plots
end

to update-tab1
   
       
    ask-concurrent n-of moving-turtles turtles [ 
        set red-nearby (count other turtles in-radius radius-n    with [color = red]   )
        set green-nearby (count other turtles in-radius radius-n  with [color = green] )
        set red-nearby-pondere ( red-nearby * C ) ; payoff from defecting from a best reponse equivalent game
        set green-nearby-pondere (green-nearby * a ) ;   payoff from cooperating from a best reponse equivalent game
        let incert random 100
        ifelse incert < epsilon 
           [ let alea2 random 2
               ifelse alea2 = 0 [set numberB 1] [set numberB 0] ] 
           [ let alea random 2   ;  if not epsilon then chose strategy optimally 
               if red-nearby-pondere = green-nearby-pondere
                    [ifelse alea = 0 [set numberB 1] [set numberB 0]] ; if payoff are equal then choose randomly
               if red-nearby-pondere > green-nearby-pondere [set numberB 0] ; if payoff fom greens < payoffs from reds  then choose 0  defect
               if red-nearby-pondere < green-nearby-pondere [set numberB 1]  ; ; if payoff fom green > payoofs from red  then choose 1 cooperate
           ]    
        ifelse numberB = 1 
            [set fitness  (green-nearby / N-size  ) * G  - C ] 
            [set fitness  (green-nearby /  N-size ) * G   ] 
        ifelse ( length fitness_list < memory) 
            [set fitness_list lput fitness  fitness_list ]
            [ set  fitness_list but-first  fitness_list  set fitness_list lput fitness  fitness_list]
        set total_fitness sum fitness_list  ;      computes the total fitness earned by a turtle over a number of period of length memory
        ifelse C != 1 
            [set normalized_fitness  (total_fitness + ( C * memory  ) ) / ( memory * (G + C)  )] ; computes the normalized fitness earned by a turtle over a number of period of length memory
            [set normalized_fitness  (total_fitness +   memory   ) / ( memory * 2  ) ]
        array:set tab who numberB  ; updtate the strategy of the randomly chosen by turtle1
        array:set tab-fitness who normalized_fitness 
                                                ] 
end

; the next is the procedure with a  random sample of size N-size

to   update-tab2 
   
   ask-concurrent n-of moving-turtles turtles  [
        let sample  n-of N-size turtles with [who != self] 
        set red-nearby count sample  with [color = red]
        set green-nearby count sample with [color = green]
        set red-nearby-pondere red-nearby * C
        set green-nearby-pondere green-nearby * a
        let incert random 100
        ifelse incert < epsilon 
            [let alea2 random 2 
                ifelse alea2 = 0 [set numberB 1] [set numberB 0] ]
            [ let alea random 2
                if red-nearby-pondere = green-nearby-pondere
                  [ifelse alea = 0 [set numberB 1] [set numberB 0] ]
                if red-nearby-pondere > green-nearby-pondere [set numberB 0] ; the turtle defect
                if red-nearby-pondere < green-nearby-pondere [set numberB 1]] ; cooperate
        ifelse numberB =  1 
            [set fitness  (green-nearby /  N-size ) * G  - C ] 
            [set fitness  (green-nearby /  N-size ) * G   ] 
        ifelse (length fitness_list < memory)
            [set fitness_list lput fitness  fitness_list ]
            [set  fitness_list but-first  fitness_list  set fitness_list lput fitness  fitness_list]
        set total_fitness sum fitness_list
        ifelse C != 1 
            [set normalized_fitness  (total_fitness + ( C * memory  ) ) / ( memory * (G + C)  )] 
            [set normalized_fitness  (total_fitness +   memory   ) / ( memory * 2  ) ]
    array:set tab who numberB
    array:set tab-fitness who normalized_fitness 
  ]
  
  
   
   
end


to update-variables
 
  update-colors
  update-globals
  update-unhappy
  report-segreg 
   
end



to
  update-colors
  
  ask turtles [
      ifelse array:item tab who = 1 [set color green][set color red]
              ]
end

to update-globals
  set Cooperators count (turtles) with [color = green]
  set Defectors  count (turtles) with [color = red] 
  set %-cooperators (Cooperators * 100 / population)   
  ifelse (ticks < memory) 
   [set list-%-cooperation fput %-cooperators list-%-cooperation 
    set mean-coop mean list-%-cooperation ]
   [set list-%-cooperation fput %-cooperators list-%-cooperation set list-%-cooperation but-last list-%-cooperation 
   set mean-coop mean list-%-cooperation  
   set sd-coop standard-deviation list-%-cooperation ]
  
  ifelse Cooperators != 0 
   [ set X_c  ( C * 100  / %-cooperators) ] 
   [ set X_c  (Xmax + 1) ] ; fix a number  bigger than X max 
  if count (turtles) with [color = green] != 0 [ set emotions-cooperators [emotion] of turtles with [color = green]  ] ;  creates a list of emotion for green turtles
  if count (turtles) with [color = red] != 0 [ set emotions-defectors [emotion] of turtles with [color = red]  ]

  ifelse count (turtles) with [color = green] != 0
       [ set %-emotion-cooperators  ( sum emotions-cooperators  * 100 ) / (Cooperators  ) ] 
       [ set %-emotion-cooperators 0 ]
  ifelse count (turtles) with [color = red] != 0
      [set %-emotion-defectors  ( sum emotions-defectors  * 100 ) / (Defectors  )]
      [set %-emotion-defectors  0]
  set mean-emotion ( ( %-cooperators * %-emotion-cooperators + (100 -   %-cooperators ) * %-emotion-defectors ) / 100 )
  if count (turtles) with [color = green] != 0 [ set mean-fitness-cooperators  (100 * sum [normalized_fitness] of turtles with [color = green] / cooperators ) ]
  if count (turtles) with [color = red] != 0 [ set mean-fitness-defectors (100 *  sum [normalized_fitness] of turtles with [color = red] / defectors )]
  set mean-fitness (100 * sum [normalized_fitness] of turtles / population )

  
end

to update-unhappy
   
      ask turtles    
         [  ifelse ( [emotion] of self ) >  X_c  and  ([color] of self = red  )
              [ set unhappy? 1 ]
              [ set unhappy? 0 ]
       ]
   
  set unhappy-turtles count turtles with [unhappy? = 1 ]
 ifelse defectors != 0 [ set %-unhappy-turtles (unhappy-turtles * 100  /  Defectors  )]
                       [set %-unhappy-turtles 0 ]
  
   ifelse (ticks < memory) 
  [ set list-unhappy? fput %-unhappy-turtles list-unhappy? 
    set mean-unhappy? mean list-unhappy? ]
  [ set list-unhappy? fput %-unhappy-turtles list-unhappy? set list-unhappy? but-last list-unhappy? 
    set mean-unhappy? mean list-unhappy?  
    set sd-unhappy? standard-deviation list-unhappy? ]

  
end
 
to report-segreg
   
 
ifelse (cooperators != 0)   and (defectors != 0)  
   [ set random-mixed-pairs ( N-size * (%-cooperators / 100) * (1 - (%-cooperators / 100)) * population    )  ; scale 0 to 100
       ask turtles 
           [ set other-nearby (count other turtles in-radius radius-n with [color != [color] of myself]) ]
            set mixed-pairs  (sum [other-nearby] of turtles) / 2
    set segregation   100 * (1 - (mixed-pairs / random-mixed-pairs)) ]
   [set segregation 0 ]
     
ifelse (ticks < memory) 
   [set list-segregation fput segregation list-segregation 
    set mean-segreg mean list-segregation ]
   [set list-segregation fput segregation list-segregation set list-segregation but-last list-segregation 
   set mean-segreg mean list-segregation  
   set sd-segreg standard-deviation list-segregation ]
  end 
       
   



to do-plots
  
   ;set-current-plot "Emotions"
    ;  set-current-plot-pen "%-cooperators"
     ;   plot %-cooperators
      ;set-current-plot-pen "%-emotion-cooperators"
      ; plot %-emotion-cooperators
      ;set-current-plot-pen "%-emotion-defectors"
      ;  plot %-emotion-defectors
      ;set-current-plot-pen "mean-emotion"
      ;plot  mean-emotion
  
   set-current-plot "Average fitness"
  ;  set-current-plot-pen "%-cooperators"
   ;     plot %-cooperators  
      set-current-plot-pen "mean-fitness-cooperators"
        plot mean-fitness-cooperators
      set-current-plot-pen "mean-fitness-defectors"
        plot mean-fitness-defectors
      set-current-plot-pen "mean-fitness"
        plot mean-fitness       
  
  
  set-current-plot "unhappy-turtles"
  ;set-plot-x-range 0   number-ticks
   set-current-plot-pen "%-unhappy-turtles"
   plot %-unhappy-turtles
  ; set-current-plot-pen "unhappy-turtles"
  ; plot unhappy-turtles
  
   set-current-plot "Segregation"
  ;set-plot-x-range 0   number-ticks
 set-current-plot-pen "segregation"
  plot segregation
   set-current-plot-pen "mean-segreg"
  plot mean-segreg
  
  set-current-plot "cooperation"
   plot %-cooperators
   
   ;set-current-plot "Average cooperation"
   ;set-current-plot-pen "mean-coop"
  ; plot mean-coop
  ; set-current-plot-pen "mean-coop + 2 sd-coop"
   ;plot (mean-coop + 2 * sd-coop)
   ; set-current-plot-pen "mean-coop - 2 sd-coop"
  ; plot (mean-coop -   2 * sd-coop)
   
  ; set-current-plot "histogram emotions"
   ;  set-histogram-num-bars 10
   ;  histogram [emotion] of turtles
  
  
  ;set-current-plot "cooperators emotions"
     ;set-histogram-num-bars 20
    ; histogram emotions-cooperators  
     
  
 ; set-current-plot "defectors emotions"
   ;  set-histogram-num-bars 20
   ;  histogram emotions-defectors 
   
end