This page was automatically generated by NetLogo 5.0.4.

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 Oracle's Java site.


In order for this to work, this file, your model file (uniform et triangulaireVERSIONmai2013.nlogo), and the files NetLogoLite.jar and NetLogoLite.jar.pack.gz must all be in the same directory. (You can copy NetLogoLite.jar and NetLogoLite.jar.pack.gz 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 the NetLogoLite files 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 the NetLogoLite files 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 triangulaireVERSIONmai2013.nlogo

WHAT IS IT?

A prisoner dilemma game with emotions. The payoff of a player i is given by

                                                     Other player
                                             cooperate       defect
         cooperate   (green)               G - C               -C  
player i   
             defect (red)                      G - X               0

X is uniform in [0 1]. It is i’s idiosyncratic emotion (shame) when she violates the cooperative norm and faces a cooperator. If X= 0 then we have a prisoner’s dilemma.

HOW IT WORKS

Pseudo code

to setup

to go
- choose randomly a number of “moving-turtles”:
- 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 “Tremble” the choice is stochastic.
- once the choice is made an updating procedure follows: each moving-turtle update his choice (color may change)
- All turtles update their fitness given the choice of the player (including the passive player who sticked to their last choice in the current period.
- All global variables are updated (%-cooperators, %-defectors, mean-emotion of cooperators and defectors, mean fitness in the population, segregation ) as well as the The state space of population configuration (colors)
- a statistic of unhappy defectors is computed : unhappy defectors 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.

Defining fitness: Fitness of a turtle over one period is the average payoff in the prisoner’s dilemma given the proportion of cooperators (X is excluded from the calculus). Total fitness 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) ).

HOW TO USE IT

THINGS TO NOTICE

THINGS TO TRY

RELATED MODELS

criminality on my web page http://perso.telecom-bretagne.eu/rogerwaldeck/data/applet/criminality/criminality_prog_18.html .

CREDITS AND REFERENCES

R. Waldeck. Segregated cooperation. in JASSS .
The segregation and interaction procedure has been inspired from Sebastian Grauwin
http://ccl.northwestern.edu/netlogo/models/community/Segreg-vs-coord . All errors my own.

CODE

;;; BY ROGER WALDECK 2013 : In this version: 
; emotions are represented by ligth or darker cells depending on their intensity 
; all individual update their fitness and not only passive ones (cf. Jasss paper); 
;  fitness is updtated according to actual play 
extensions [array]


globals [
 Xmax                         ; max emotion  
 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
 init_coop
 expected_equilibrium 
 equilibrium       ; expected equibrium for triangular distribution
 stable              ; stable expected equibrium for triangular distribution
  unstable          ; unstable expected equibrium for triangular distribution
]

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
 
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  __clear-all-and-reset-ticks
  ifelse random_seed 
        [ use-new-seed ]
        [  use-seed-from-user ]
  set list-segregation []
  set list-unhappy? []
  set list-%-cooperation []
  set G 1
  set Xmax 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
  set init_coop   %-cooperators
  if distribution = "triangular" [compute-stable-and-unstable]
  compute-expected-equilibrium
  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
    
end




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

to go
  if (ticks >= Max-ticks)[stop]
  ifelse neighbourhood = "local"             ; 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 [numberB = 0]   )
        set green-nearby (count other turtles in-radius radius-n  with [numberB = 1] )
        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 < tremble 
           [ let alea2 random 2
               ifelse alea2 = 0 [set numberB 1] [set numberB 0] ] 
           [ let alea random 2   ;  if not tremble 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
            array:set tab who numberB  ; updtate the strategy of the randomly chosen by turtle1
           ]    ] 
    
    ask turtles [
      set red-nearby (count other turtles in-radius radius-n    with [numberB = 0]   )
      set green-nearby (count other turtles in-radius radius-n  with [numberB = 1] )
        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
        
            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-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 other turtles    ; with [who != self] OK
        set red-nearby count sample  with [numberB = 0] ;OK
        set green-nearby count sample with [numberB = 1]
        set red-nearby-pondere red-nearby * C
        set green-nearby-pondere green-nearby * a
        let incert random 100
        ifelse incert < tremble 
            [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
             array:set tab who numberB 
   ]
   
   ask turtles [
      set red-nearby (count other turtles in-radius radius-n    with [numberB = 0]   )
      set green-nearby (count other turtles in-radius radius-n  with [numberB = 1] )
        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-fitness who normalized_fitness
  ]  
   
end


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



to
  update-colors ; set the color of the turtle according to its decision in the current period
  
  ask turtles [
      ifelse (array:item tab who) = 1  [set  color scale-color 65 (emotion   )    1.8 0][set color scale-color 15 (emotion  )   2 0  ]            ;[set color green][set color  red]
              ]
end

to update-globals
  set Cooperators count (turtles) with [numberB = 1]
  set Defectors  count (turtles) with [numberB = 0] 
  set %-cooperators (Cooperators * 100 / population)   
  ifelse (ticks < memory) 
   [set list-%-cooperation fput %-cooperators list-%-cooperation 
    set mean-coop mean list-%-cooperation ]                           ;manque sd dev
   [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 [numberB = 1] != 0 [ set emotions-cooperators [emotion] of turtles with [numberB = 1]  ] ;  creates a list of emotion for green turtles
 
  if count (turtles) with [numberB = 0] != 0 [ set emotions-defectors [emotion] of turtles with [numberB = 0]  ]

  ifelse  Cooperators != 0
       [ set %-emotion-cooperators  ( sum emotions-cooperators  * 100 ) / (Cooperators  ) ]  ;   l'�motion moyenne par cooperateur A VOIR SI IFELSE PERTNENT
       [ set %-emotion-cooperators "na" ]
  ifelse count (turtles) with [numberB = 0] != 0
      [set %-emotion-defectors  ( sum emotions-defectors  * 100 ) / (Defectors  )]
      [set %-emotion-defectors "na" ]
 ifelse count (turtles) with [numberB = 1] != 0 
    [ ifelse count (turtles) with [numberB = 0] != 0 
        [set mean-emotion ( ( %-cooperators * %-emotion-cooperators + (100 -   %-cooperators ) * %-emotion-defectors ) / 100 )]
        [set mean-emotion %-emotion-cooperators]  ][set mean-emotion %-emotion-defectors]
  
  
  if  Cooperators != 0 
     [ set mean-fitness-cooperators  (100 * sum [normalized_fitness] of turtles with [numberB = 1] / cooperators ) ]
  if count (turtles) with [numberB = 0] != 0 
     [ set mean-fitness-defectors (100 *  sum [normalized_fitness] of turtles with [numberB = 0] / defectors )]
  set mean-fitness (100 * sum [normalized_fitness] of turtles / population )

  
end

to update-unhappy
   
      ask turtles    
         [  ifelse ( [emotion] of self ) >  X_c  and  ([numberB ] of self = 0  )
              [ 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 [numberB != [numberB] 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 of cooperators/cooperator"
      if %-emotion-cooperators != "na" [  plot %-emotion-cooperators]
      set-current-plot-pen "emotion of defectors/defector"
     if %-emotion-defectors != "na"   [plot %-emotion-defectors]
      set-current-plot-pen "mean-emotion"
      plot  mean-emotion
  
   set-current-plot "Average Fitness  over memory periods"
  ;  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"
  set-current-plot-pen " %-cooperators"
       plot %-cooperators
  set-current-plot-pen "expected_equilibrium"
       plot expected_equilibrium
   set-current-plot "Average Cooperation over memory periods"
   auto-plot-on 
  if (ticks >= 100) [set-plot-x-range (ticks - 100) ticks]
   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

 
   to compute-expected-equilibrium
     
     if (distribution = "uniform") 
        [ let delta (1 - 4 * C) 
          ifelse delta > 0 [ set unstable 100 * (0.5 - sqrt(delta) / 2 )
          ifelse init_coop <= unstable  
              [set expected_equilibrium 0]
              [set expected_equilibrium  100 * (0.5 + sqrt(delta) / 2) ]]
     [ set unstable 0 set expected_equilibrium 0 ]]
      if (distribution = "triangular")
     [ifelse equilibrium != []
       [ifelse init_coop <= unstable
     [set expected_equilibrium 0]
       [set  expected_equilibrium stable]] 
       [set unstable 0  set expected_equilibrium 0   ] 
       
       ]
     
   end
   
   



;this procedure produce the list of stable and unstable equilibria from the triangular (excluding full defection )
to-report solve [eta ]
  
  let x1 1 
  set eta []
  
  while [x1 >= 0.00001]
  [ let x (1 - (C  / x1 ) ^ 2)
     
     if abs (x - x1) <= 0.001 
     [ 
       let s 0 
       if eta != [] [
       set s item 0 eta ]
       if abs (s - x) > 0.01
       [ set eta fput x eta  ]]
     set x1 x1 - 0.00001
      
    
      ]
  report eta  
end

to compute-stable-and-unstable
  set stable []
  set unstable []
  set equilibrium solve equilibrium 
  
  
  if equilibrium != []
  [
  foreach  equilibrium [ 
    ifelse abs ( (2 * C ^ 2 ) / ( ? ) ^ 3 )  < 1 
   [set stable fput  ?   stable ]
   [set unstable fput  ?   unstable]]
    
    
  set  stable 100 * (item 0 stable) print stable
  set unstable 100 *  (item 0 unstable) print unstable
  ]
end