A Cooperative Species

A Cooperative Species preview image

2 collaborators

Jbradford_web3 John Bradford (Author)
11jazy ijaz ahmad (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 6.0.2 • Viewed 737 times • Downloaded 59 times • Run 0 times
Download the 'A Cooperative Species' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals [
  combo
  replacement_rate ; set at 5%
  mutation_rate ;; 2%
  groups  ; just a list of numbers from 1 to N_group
  p_links ;; total possible links given n nodes
  p_networks ;; total possible networks from n nodes
  leaders ;; list of "leaders" in round, to serve as anchors for the layout-spring algorithm
  followers ;; everyone who isn't a leader

  avwithingroupvar ;; average within group variance
  betweenvar ;between-group variance

  variance_ratio ;; Fst = var(pj) / [Avvar(pij) + var(pj)]  = population-wide measure of the degree of non-randomness in who interacts with whom; aka *inbreeding coefficient*
  ;; = differences in the probability of being paired with an altruist conditional on being an altruist, and the probability of being paired with an altruist conditional on
  ;; being a non-altruist (defector).
  ;; one expects cooperation to prevail when Fst > c / b.

  pop_change ;; expected change in fraction of altruists

  ]
breed [cooperators cooperator]
breed [defectors defector]

undirected-link-breed [wlinks wlink]   ;within-group links
undirected-link-breed [blinks blink]  ;between-group links


;links-own [memories]
turtles-own [
  earnings ;; accumulated payoffs
  payoff
  N_Neighbors
  mycosts
  mybenefits
  t_threshold
  groupid ;; groups 1 --> N_groups
  group_coop ;; previous number of contributors/cooperators in the previous round, withint he group;
  ;;should be the same for turtles of the same group
  sorted
  contrite  ;; number = 0 originally, if accidentalyl makes a mistake and defects, then set to 2, which means agent will cooperate next 2 rounds automatically.

  wingroupvar ;; within group variance of altruism

  p_i ;; probabilistic interaction; likelihood thta other turtles will interact with this turtle.. test
]

to setup
   clear-all
   reset-ticks

   set-default-shape turtles "face happy"
   set groups []
   set leaders []
   set followers []

   let g 1
   repeat N_groups [
     set groups lput g groups
     set g g + 1]

create-turtles (N_groups * size_n) [
   while [any? other turtles-here] [ let empty_patch one-of patches with [any? turtles-here = false] move-to empty_patch ]
   set sorted false
   set groupid 0
]

  setup-neighbors

   ask turtles [
let cnt size_n ;; (i.e. n)
let t random cnt + 1  ;; i.e. between 0 and n, n = # in group.  Interesting to test differences using n and n-1 as
;; used by Bowles and Gintis.  When using n, turtles with t = n will only cooperate if everybody cooperated in the previous
;; round, including oneself!  A turtle with t = n + 1 is a DEFECTOR, set below.
set t_threshold t
set contrite 0
set breed cooperators set color yellow set size 1
set group_coop size_n ;; turtles act initially as if everybody in group cooperated last round
   ]

let pop count turtles
let num_d (Percent_Defectors / 100) * pop
let new_defectors n-of num_d turtles
ask new_defectors [set breed defectors set shape "face sad" set size 1.5 set color red set t_threshold size_n + 1 set group_coop size_n]

;if GAME = "Pairwise Prisoners Dilemma Game" [ask turtles [create-links-with other turtles [set hidden? true]]]
;layout
end 

to start
if count turtles > 0
[
  if GAME = "Public Goods Game" [Public_goods_game]
  if GAME = "Pairwise Prisoners Dilemma Game" [PD_pairing]

if Replicator_Dynamics? = true AND count cooperators > 0 AND count defectors > 0 [replicator_dynamics]

if count turtles > 0 [
if reassortment? = true [setup-neighbors]
  if starvation? = true [dying-turtles]
  if kill_defectors? = true [kill-d]

ask turtles [if contrite? = true[ ; cooperate if defected in error from previous 1-2 rounds
  if contrite > 0 [set contrite contrite - 1]]]

update-plots
;layout
tick
]
]
end 

to setup-neighbors
    if GAME = "Public Goods Game" [assign_groups]
    if GAME = "Pairwise Prisoners Dilemma Game" [
      create-pairs
      ;layout
      ]
end 

to create-pairs
  ask turtles[
    if PD_assortment = "Random" [create-pairs-random]
    if PD_assortment = "Fixed" [create-pairs-fixed]]
end 

to create-pairs-random
  set n_neighbors other turtles  ;; This will end up being proportional to the population distribution
end 

to create-pairs-fixed

    ifelse [breed] of self = cooperators [
    let p Probability_of_Altruist_meeting_Altruist
    let r random 100
    ifelse r < p [set n_neighbors other cooperators][set n_neighbors defectors]]

    [let p Probability_of_Defector_meeting_Altruist
      let r random 100
      ifelse r < p [set n_neighbors cooperators] [set n_neighbors other defectors]]
end 

to-report find-partner
let partner one-of N_Neighbors
if partner = nobody [set partner one-of turtles]
  report partner
end 

to assign_groups
   ask turtles [setxy random-pxcor random-pycor
     while [any? other turtles-here] [ let empty_patch one-of patches with [any? turtles-here = false] move-to empty_patch ]
     set groupid 0 ]
  let unassigned turtles
    ;; start with group 1 and loop to build each group
  let current 1
  while [any? unassigned]
  [
    ;; place a randomly chosen set of group-size turtles into the current
    ;; group. or, if there are less than group-size turtles left, place the
    ;; rest of the turtles in the current group.
    ask n-of (min (list size_n (count unassigned))) unassigned
      [ set groupid current
        set n_neighbors other turtles with [groupid = current]
        ]
    ;; consider the next group.
    set current current + 1
    ;; remove grouped turtles from the pool of turtles to assign
    set unassigned unassigned with [groupid = 0]
  ]

ask turtles
  [
    ;; if i'm in a group, move towards "home" for my group
    if groupid != 0
      [ face get-home
        let p [neighbors] of get-home
        let area (patch-set get-home p)
        let my_patch one-of area
    move-to my_patch
         ]
    ;; wiggle a little and always move forward, to make sure turtles don't all
    ;; pile up
    lt random 5
    rt random 5
    fd 1
  ]
end 


;; Courtesy of Uri Wilensky:
;; figures out the home patch for a group. this looks complicated, but the
;; idea is simple. we just want to lay the groups out in a regular grid,
;; evenly spaced throughout the world. we want the grid to be square, so in
;; some cases not all the positions are filled.

to-report get-home ;; turtle procedure
  ;; calculate the minimum length of each side of our grid
  let side ceiling (sqrt (max [groupid] of turtles + 1))

  report patch
           ;; compute the x coordinate
           (round ((world-width / side) * (groupid mod side)
             + min-pxcor + int (world-width / (side * 2))))
           ;; compute the y coordinate
           (round ((world-height / side) * int (groupid / side)
             + min-pycor + int (world-height / (side * 2))))
end 

to PD_pairing ;; Pairwise Prisoner's Dilemma Game
ask turtles [
let partner find-partner
;if partner = nobody [die] ;; dies if isolated!

let utility 0
let total_cost 0
let total_benefit 0
let personal_cost 0
ifelse member? self cooperators [set personal_cost cost] [set personal_cost 0]

   set total_cost total_cost + personal_cost
   ifelse member? partner cooperators [set total_benefit total_benefit + benefit ;; if partner is a cooperator, add benefit to 'totalbenefit' recorder.
     set utility utility + Benefit - personal_cost] ;; if neighbor is a cooperator, then add benefit...
   [set utility utility - personal_cost]  ;;if neighbor is a defector, then no benefit and subtract personal cost, if any...



 set payoff utility
 set earnings earnings + payoff
 set mycosts total_cost
 set mybenefits total_benefit
]
end 

To Public_goods_game

   foreach groups [ ?1 ->
     let group_share 0
     let thisgroup turtles with [groupid = ?1]
    ask thisgroup [
      set mycosts 0
      let t group_coop
      let r random-float 1 ;; ERROR

      ifelse contrite > 0 [ ;; if contrite > 0, then cooperate, unconditionally, otherwise...
       set breed cooperators set shape "face happy" set size 1 set color yellow ;; then cooperate
        set group_share group_share + Benefit
        set mycosts cost]
      ;;ERROR IS BOTH ERROR TOWARD COOPERATING AND ERROR TOWARD DEFECTING.

     [ifelse t >= t_threshold  ;;if enough other group members contributed last round then COOPERATE.

      [ifelse r <= error_rate[   ;;  HERE, ERROR MEANS DEFECTING INSTEAD OF COOPERATING
        set breed defectors set shape "face sad" set size 1.5 set color red
        if contrite? = true [if r <= error_rate AND t >= t_threshold [set contrite 2 ]]
        ]

       [set breed cooperators set shape "face happy" set size 1 set color yellow ;; then cooperate
        set group_share group_share + Benefit
        set mycosts cost]]

     [ifelse r <= error_rate[  ;; HERE, ERROR MEANS COOPERATING INSTEAD OF DEFECTING
        set breed cooperators set shape "face happy" set size 1 set color yellow
        set group_share group_share + Benefit
        set mycosts cost]
     [set breed defectors set shape "face sad" set size 1.5 set color red]]]

    ]

 ask thisgroup [
     set payoff (group_share / (size_n - 1)) - mycosts  ;; payoff is b/n or b/(n-1) ??
     set earnings earnings + payoff
     set group_coop count cooperators with [groupid = ?1]
 ]
  ]
end 

to replicator_dynamics

if Replicator_options = "Relative Payoff" [Relative_Payoff]
if Replicator_options = "Variance Ratio" [Variance_Replicator]
if Replicator_options = "Replicator Equation" [Replicator_equation]
if Replicator_options = "Imitation" [Imitate]
end 


;; probability of changing to another strategy is proportional to the difference between the *mean* payoffs for defectors and cooperators.
;; turtle only can switch if the payoffs are larger for the other strategy.

to Relative_payoff
 ifelse mean [payoff] of cooperators > mean [payoff] of defectors [
   ;; if cooperators making more payoff, then select the defectors to change
   ask defectors [let pr random-float 1
     if pr <= RD1 [delete_defectors]]]

 [  ;; if defectors making more, then ask cooperators to change
      ask cooperators [let pr random-float 1
if pr <= RD1 [delete_cooperators]]]
end 

to dying-turtles ;; turtles die if their earnings (or possibly their payoffs) get below zero.
  let consuming ((benefit - cost) / size_n) / 2
    ask turtles [
    set earnings earnings - consuming
    if earnings < 0 [die]]
;ask turtles [setup-neighbors] ;; must reset potential partners to avoid calling on dead turtles!
end 

to kill-d
;;RULE  This just means that half the cost is deducted from earnings each round a turtle has no cooperators to cooperate with
  let consuming ((benefit - cost) / size_n) / 2
ask turtles [
  let g 0
  ask N_neighbors [if member? self cooperators [set g g + 1]]
  if g = 0 [set earnings earnings - consuming]
  ]
end 

to Variance_replicator
  ;; based on variable 'popchange'
  ;; According to Bowles and Gintis, the ratio of between-group variation (of altruists) to the total variation (which is the weighted-average within-group variation + the between-
  ;; group variation) must be greater than the ratio c/b for evolution to favor altruism.
  ;; This ratio is also the probability of being paired with an altruist minus the probability of being paired with an altruist conditional on being an altruist or non-altruist,
  ;; respectively, or P(A|A) - P(A|N).  This seems more of a predictive tool than an algorithm to change the population.

variances
let c count turtles
let new_agents pop_change * c ;;
let c_r round new_agents
ifelse c_r > 0 [;; add more cooperators, kill defectors
  let c_d count defectors
  let c_min min (list c_r c_d)
  let deleted_defectors min-n-of c_min defectors [payoff]
  ask deleted_defectors [delete_defectors]]
;;add more defectors, kill cooperators
[let p_cr  c_r * -1  ;; convert to a positive number
  let c_c count cooperators
  let c_min min (list p_cr c_c)
    let deleted_cooperators min-n-of c_min cooperators [payoff]
          ask deleted_cooperators [delete_cooperators]
  ]
end 

to Replicator_equation    ;; let Pr(i) = the proportion of strategy i
  ;; let $i = the payoff of strategy i, since I can't write the pi symbol here.
  ;; the new proportion of strategy i in the population at time t+1 is given by:
  ;;  Pr(i)t+1 = Pr(i)$(i) / Sum of Weights
  ;; the weight for each strategy is given by the numerator
let expected_coop_change coop_pay - (count cooperators / count turtles)
let expected_defect_change coop_def - (count defectors / count turtles)
let c expected_coop_change * count turtles ;; gives the number of turtles that will be changed
let c_r round c ;rounded

ifelse c_r > 0 [ ;; add more cooperators, kill defectors
  let c_d count defectors
  let c_min min (list c_r c_d)
  let deleted_defectors min-n-of c_min defectors [payoff]
  ask deleted_defectors [delete_defectors]]

;;add more defectors, kill cooperators
[let p_cr  c_r * -1  ;; convert to a positive number
  let c_c count cooperators
  let c_min min (list p_cr c_c)
    let deleted_cooperators min-n-of c_min cooperators [payoff]
          ask deleted_cooperators [delete_cooperators]
  ]
end 

to imitate
  ;; this probably won't work, because its not clear how turtles will decide to imitate..
  ;; if all agents imitate most successful agent in their group, then it creates immediate within-group homogeneity
  ;setting it initially to 4 closest agents, von Neuman, or Moore neighborhood, can't remember which.
  ask turtles [
    let other_a min-n-of 4 other turtles [distance self]
    let max_a max-one-of other_a [payoff]
    if [payoff] of max_a > [payoff] of self [
      ifelse [breed] of max_a = cooperators [delete_defectors] [delete_cooperators]
      set t_threshold [t_threshold] of max_a  ;; copying the threshold (for public goods games), not just the strategy!
      set group_coop t_threshold
    ]

  ]
end 

to delete_defectors
 ;; hatch and die
let i [groupid] of self
hatch-cooperators 1 [
  set groupid i
  let mygroup other turtles with [groupid = i]
  ;create-wlinks-with mygroup
let cnt size_n ;; (i.e. n)
let t random cnt ;; t will be automatically between 0 and n and therefore not a defector
set t_threshold t
set color yellow set size .5
set group_coop t_threshold ;; will initially act as if just enough turtles have cooperated in previous round
       ]
  die
end 

to delete_cooperators
let i [groupid] of self
hatch-defectors 1 [
 set groupid i
 let mygroup other turtles with [groupid = i]
 ;create-wlinks-with mygroup
set t_threshold size_n + 1 ;; requires more turtles to cooperate than actually exist, therefore a defector
set shape "face sad" set size 1 set color red
set group_coop 0  ;; will initially act as if just enough turtles have cooperated in previous round
       ]
die
end 

to variances
  let jmin min [groupid] of turtles
  let jmax max [groupid] of turtles
  let j jmin
  let avgrouplist []
  let bgrouplist []

  repeat jmax [
    let grouplist []
    ask turtles with [groupid = j] [
      ifelse [breed] of self = cooperators [set grouplist fput 1 grouplist] [set grouplist fput 0 grouplist] ;; set 1 if altruist, 0 otherwise
    ]
    ask turtles with [groupid = j] [
      set wingroupvar variance grouplist
    ]
    set j j + 1
  ]

  let j2 min [groupid] of turtles

  repeat jmax [

    let num count turtles with [groupid = j2]
    let numi count turtles with [groupid = j2 AND breed = cooperators] ;; counts number of cooperators
    let pj numi / num  ;; frequency of altruists in the group
    let f num / count turtles
    let gvar mean [wingroupvar] of turtles with [groupid = j2]  ;; every turtle in the group should have the same within group variance, but just in case, i take the average here.

    set avgrouplist fput (f * gvar) avgrouplist
    set bgrouplist fput pj bgrouplist
      set j2 j2 + 1
  ]


  set avwithingroupvar variance avgrouplist ;; reports the weighted-average within-group variance of altruists
  set betweenvar variance bgrouplist

  set variance_ratio betweenvar / (avwithingroupvar + betweenvar)

  p_change
end 

to p_change ;; change in the fraction of altruists population in total population
  let b Benefit
  let c Cost
  let var_pj betweenvar
  let var_pij avwithingroupvar

  let p ((b - c) * var_pj) - (c * var_pij)
  set pop_change p
end 

to-report coop_pay;; proportion of cooperators*payoff of cooperators divided by sum of weighted payoffs
  let expected_coop (count cooperators / count turtles) * mean [payoff] of cooperators
  let expected_def (count defectors / count turtles) * mean [payoff] of defectors
  let total_payoff_c expected_coop / (expected_def + expected_coop)
  report total_payoff_c
end 

to-report coop_def
  let expected_coop (count cooperators / count turtles) * mean [payoff] of cooperators
  let expected_def (count defectors / count turtles) * mean [payoff] of defectors
  let total_payoff_d expected_def / (expected_def + expected_coop)
  report total_payoff_d
end 

to-report RD1 ;; veresion 3.  Qij = B($j - $i)
  ;; probability that agent will switch from less profitable strategy to more profitable strategy
  ;; B has to be sufficiently small so that Qij is always <= 1 !

  let B .1  ;; just trying random numbers
  let payoff_c mean [payoff] of cooperators
  let payoff_d mean [payoff] of defectors

  ifelse payoff_c > payoff_d [
    ;; probability that defectors will switch to cooperation...
    let Qij B * (payoff_c - payoff_d)
    report Qij]
  [ ;; probability that cooperators will switch to defection...
    let Qij B * (payoff_d - payoff_c)
    report Qij]
end 

to-report RD2
  ;; Replicator Dynamics Version #2 for Cooperators
  ;;  Pr(i)t+1 = Pr(i) - a * Pr(i)(1-P)B($j - $i)

  let B .1  ;; randomly assigned

  let p_c (count cooperators / count turtles)  ;; proportion of turtles that are cooperators
  let p_d (count defectors / count turtles)  ;; proportion defectors
  let payoff_c mean [payoff] of cooperators
  let payoff_d mean [payoff] of defectors

  let expected_p p_c - ( p_c * (1 - p_d) * B * (payoff_d - payoff_c))
  report expected_p
end 













There are 2 versions of this model.

Uploaded by When Description Download
ijaz ahmad over 6 years ago Updated vesrion Download this version
John Bradford about 7 years ago Initial upload Download this version

Attached files

File Type Description Last updated
A Cooperative Species.png preview Preview for 'A Cooperative Species' about 7 years ago, by John Bradford Download

This model does not have any ancestors.

This model does not have any descendants.