VSRPD162

VSRPD162 preview image

1 collaborator

Default-person Filip Vesely (Author)

Tags

(This model has yet to be categorized with any tags)
Visible to everyone | Changeable by everyone
Model was written in NetLogo 5.0.5 • Viewed 52 times • Downloaded 7 times • Run 0 times
Download the 'VSRPD162' 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

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; GNU GENERAL PUBLIC LICENSE ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; LeLeLe
;; LeLeLe is a model designed to analyse
;; the effect of conditional dissociation
;; in the evolutionary emergence of cooperation.
;; Copyright (C) 2013 Segismundo S. Izquierdo & Luis R. Izquierdo
;; 
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see .
;;
;; Contact information:
;; Luis R. Izquierdo 
;;   University of Burgos, Spain. 
;;   e-mail: luis@izquierdo.name


;;;;;;;;;;;;;;;;;
;;; Variables ;;;
;;;;;;;;;;;;;;;;;


globals [
  
  ;; Variables for the agent-based model
 strategy-mode
 strategy-mode-%
  
  pool-of-singles ;; players without mate at the beginning of the period
  n-of-singles-at-the-beginning
  n-of-singles-selected-to-be-paired
  paired-players
  


  num-strategies                                  ;; 162
  strategy-frequencies
  strategy-names  

;  strategy-numbers

;  strategy-frequencies-pool-of-singles
;  strategy-frequencies-pool-of-singles-norm
;  strategy-avg-payoffs
;  strategy-mod-payoffs



  num-plots ;; 12
  plot-names
  plot-frequencies
  plot-numbers
  plot-avg-payoffs
  

  
  total-payoff
  avg-payoff
  paired
  
  
  Z  ;; temp variable

  
  
  numSS numSL numLL       ;; these variables store how many times each of the leave outcomes has been observed in one match
  %-SS %-SL %-LL
  num-outcomesS

  numCC numCD numDD       ;; these variables store how many times each of the outcomes has been observed in one match
  %-CC %-CD %-DD
  num-outcomes  
  
  
  ;; to be aded later
  %-A    ;; percentage of strategy
  %p-A   ;; percentage of strategy in the pool
  
  
  
  
]

breed [agents agent] 

agents-own [ 
  strategy-number  ;; strategies are numbered from 0,1,2,3,4,6,7...160,161
                   ;; floor(# /81)       is action for new    0=C, 1= D
                   ;; floor(# /27) mod 3 is action for CC     0=C, 1= D, 2= out
                   ;; floor(# / 9) mod 3 is action for CD     0=C, 1= D, 2= out
                   ;; floor(# / 3) mod 3 is action for DC     0=C, 1= D, 2= out
                   ;; floor(#    ) mod 3 is action for DD     0=C, 1= D, 2= out
  past          ;; LAST PD GAME 0=New, 1=CC, 2=CD, 3=DC, 4=DD
  action        ;; the action is either 0 (C) or 1 (D)
  break-if-C    ;; 0 if the player does not split up after the other player plays C. 1 otherwise.
  break-if-D    ;; 0 if the player does not split up after the other player plays D. 1 otherwise.
  
  name          ;; plot name on graph, A, B, C ...
  mate
  
  new-partnership?
  
  payoff

  plot-number       ;; strategies are numbered from 0 to num-plots-1
  
]

;; The following breeds are the variables for the mean dynamics


breed [singles single]

singles-own [
  value
  cooperate?    
  break-if-C?   
  break-if-D?   
  plot-number      ;; strategies are numbered from 0 to num-plots-1
  strategy-number  ;; strategies are numbered from 0 to num-strategies-1
]

;;;;;;;;;;;;;;;;;;;;;;;;
;;; Setup Procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

;; the following procedure is called when the model is first loaded

to startup
  clear-all
  setup-variables
  reset-ticks
  setup-initial-conditions
  no-display
end 

to setup-variables
  set num-strategies 162  
  set num-plots 12
  set plot-names ["TfTat" "OfTat" "C-Prt" "AllC" "OthC" "TfTit" "OfTit" "WSLS" "FGOF" "D-Prt" "AllD" "OthD"]
  set plot-numbers n-values num-plots [?]
end 

to setup-initial-conditions
  create-agents num-players [
    set past 0
    set mate nobody
    set payoff 0    
    set hidden? true
  ]
  set pool-of-singles (agents with [mate = nobody])
  set n-of-singles-at-the-beginning (count pool-of-singles)
 
  ask agents [
    ifelse ((main-strategy < 0) or (main-strategy > 161)) [set strategy-number (floor(random-float 162.0))] 
                                                          [set strategy-number (floor(main-strategy))]
    update-strategy-variables 
             ]
  ask singles [
         let my-st-number strategy-number
         set value (count agents with [strategy-number = my-st-number]) / num-players
       ]
  
  ;; here we plot the initial conditions, before any matching occurs
  gather-data  
  
  update-graphs   
end 


;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Run-time procedures ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go  
  
  make-couples

  play
    
  gather-data  

  tick  
  
  update-graphs   

  split-up 
 
  ;; new generation
  
  kill-and-breed
  
  if (count agents != num-players) [adjust-num-players]      
end 

to make-couples
  ask agents [set new-partnership? false]
  set pool-of-singles (agents with [mate = nobody])
  set n-of-singles-at-the-beginning (count pool-of-singles)
  let players-to-be-paired (pool-of-singles with [random-float 1.0 < prob-rematch])
  set n-of-singles-selected-to-be-paired (count players-to-be-paired)
   
  if (n-of-singles-selected-to-be-paired mod 2 = 1) [
    set n-of-singles-selected-to-be-paired (n-of-singles-selected-to-be-paired - 1)
    set players-to-be-paired (n-of n-of-singles-selected-to-be-paired players-to-be-paired)
  ]  
  
  ask players-to-be-paired [set new-partnership? true]
  ask players-to-be-paired [
    if (mate = nobody) [  
      set mate one-of (players-to-be-paired with [mate = nobody and self != myself])
      ask mate [set mate myself]
    ]
  ] 
end 

to play
  
  set total-payoff 0
  set paired 0
  set avg-payoff 0

  ask agents [
    set past ifelse-value (mate = nobody)
      [0]
      [past-for action ([action] of mate)] 
      
    set payoff ifelse-value (mate = nobody)
      [out-payoff]
      [payoff-for action ([action] of mate)]
      
    set total-payoff (total-payoff + payoff)
    set paired (paired + 1)
  ]
    set avg-payoff (total-payoff / paired )
end 

to-report past-for [my-action her-action]
  ;; my-action is 0 if C, 1 if D
  report 
    ifelse-value (my-action = 0)
        [ifelse-value (her-action = 0)
          [1][2]]
        [ifelse-value (her-action = 0)
          [3][4]]
end 

to-report payoff-for [my-action her-action]
  ;; my-action is 0 if C, 1 if D
  report 
    (1 - my-action) * (1 - her-action) * CC-payoff +
    (1 - my-action) * her-action * CD-payoff +
    my-action * (1 - her-action) * DC-payoff +
    my-action * her-action * DD-payoff
end 

to split-up
  set numLL 0
  set numSL 0
  set numSS 0
  ask agents[
    if (mate != nobody) [ 
      ifelse ((([action] of mate = 0) and (break-if-C = 1))or(([action] of mate = 1) and (break-if-D = 1)))
        [ifelse (((action = 0) and ([break-if-C] of mate = 1))or((action = 1) and ([break-if-D] of mate = 1)))
          [  set past 0   set numLL (numLL + 0.5) ]
          [  set past 0   set numSL (numSL + 0.5) ] ]
        [ifelse (((action = 0) and ([break-if-C] of mate = 1))or((action = 1) and ([break-if-D] of mate = 1)))
          [  set past 0   set numSL (numSL + 0.5) ]
          [               set numSS (numSS + 0.5) ] ]
          ;; if programmed to split up after your mate's selected action
        ]
    ]
  set num-outcomesS (numLL + numSL + numSS)
  set %-LL ifelse-value (num-outcomesS = 0) [0] [(numLL / num-outcomesS)]
  set %-SL ifelse-value (num-outcomesS = 0) [0] [(numSL / num-outcomesS)]
  set %-SS ifelse-value (num-outcomesS = 0) [0] [(numSS / num-outcomesS)]
end   

to kill-and-breed
  ask agents [  
    ifelse (random-float 1.0 < ifelse-value (die-together) [ ((expected-life -  (sqrt (expected-life) * sqrt (expected-life - 1 ))) / expected-life ) ] [( 1.0 / expected-life )])
     [ if (mate != nobody)  [ ifelse (die-together)  [ask mate [die]] [ask mate [set past 0]     ask mate [set mate nobody]  ask mate[update-strategy-variables] ]]
       die ]
     [ if (past = 0) [set mate nobody]
       update-strategy-variables ] ]
end         

to adjust-num-players 
  let adjustment (num-players - (count agents))
  if adjustment != 0 [  
  ifelse adjustment > 0 
    [
      
    ;;;;;  ^ 1 is original
     let list-fitness n-values 162 [ifelse-value (count agents with [strategy-number = ?] > 0) [(((sum [payoff] of agents with [strategy-number = ?]) / (count agents with [strategy-number = ?])) ^ 5) * (count agents with [strategy-number = ?])][0]]
     
     

     if (sum list-fitness = 0) [set list-fitness n-values 162 [1]]        ;; Applies when all players have zero fitness 
     
     let cum-fitness [0]                                                  ;; cum-fitness last value is 0 and is 9 items long
     foreach list-fitness [set cum-fitness fput (? + first cum-fitness) cum-fitness]      
      
     create-agents adjustment [
        set past 0   
        set mate nobody

        if-else (random-float 1.0 < prob-mutation)
          [set strategy-number (floor(random 162.0))]
          [set strategy-number 161 
           let tmp random-float first cum-fitness    ;; select the new strategy with probability proportional to fitness 
           foreach butfirst cum-fitness [ if ( tmp < ?) [set strategy-number (strategy-number - 1)] ]
          ]
        update-strategy-variables
     ]
    ]
    [
      ask n-of (0 - adjustment) agents [
        if (mate != nobody)  [ask mate [set past 0]   ask mate [set mate nobody]   ]
        die
      ]
    ]
  ]
end 

to update-strategy-variables
  ;; update strategy variables from strategy-number
  
  if-else (past = 0)            [set action            (floor ( strategy-number / 81)        ) ] [
   if-else (past = 1)           [set action     floor ((floor ( strategy-number / 27)) mod  3) ] [
    if-else (past = 2)          [set action     floor ((floor ( strategy-number /  9)) mod  3) ] [
     if-else (past = 3)         [set action     floor ((floor ( strategy-number /  3)) mod  3) ]
                                [set action     floor ((floor ( strategy-number     )) mod  3) ] ]]] 
  if-else (action = 0)          [set break-if-C ifelse-value (floor((floor ( strategy-number / 27)) mod  3) = 2) [1] [0] 
                                 set break-if-D ifelse-value (floor((floor ( strategy-number /  9)) mod  3) = 2) [1] [0]  ] 
                                [set break-if-C ifelse-value (floor((floor ( strategy-number /  3)) mod  3) = 2) [1] [0] 
                                 set break-if-D ifelse-value (floor((floor ( strategy-number     )) mod  3) = 2) [1] [0]  ] 
end 

;;;;;;;;;;;;;;;;;;;;;;;;
;;;    Statistics    ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to gather-data
  
  ;; Agent-based model
  set paired-players (agents with [mate != nobody])
  set numCC (count paired-players with [action = 0 and [action] of mate = 0]) / 2
  set numDD (count paired-players with [action = 1 and [action] of mate = 1]) / 2
  set numCD (count paired-players with [action = 0 and [action] of mate = 1]) 
  set num-outcomes (numCC + numDD + numCD)
  set %-CC ifelse-value (num-outcomes = 0) [0] [(numCC / num-outcomes)]
  set %-CD ifelse-value (num-outcomes = 0) [0] [(numCD / num-outcomes)]
  set %-DD ifelse-value (num-outcomes = 0) [0] [(numDD / num-outcomes)]
  let n-players (count agents)


 
  
  
  
  set strategy-mode first (modes [strategy-number] of agents)
  set strategy-mode-% (count agents with [strategy-number = strategy-mode] / count agents * 100)
  
  
  set strategy-frequencies n-values 162 [count agents with [strategy-number = ?]]
  set strategy-names n-values 162 [?]
  


    
  
    set Z                                            (count           agents with [(floor(strategy-number /  9) mod 27) =  1])                           ;; TfTat
    set plot-avg-payoffs fput (ifelse-value (Z > 0) [(sum [payoff] of agents with [(floor(strategy-number /  9) mod 27) =  1]) /  Z] [0]) []
    set plot-frequencies fput Z []
  
    set Z                                            (count           agents with [(floor(strategy-number /  9) mod 27) =  2])                             ;; OfTat
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [(floor(strategy-number /  9) mod 27) =  2]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies
  
    set Z                                            (count           agents with [(floor(strategy-number / 27) mod 27) =  1])                             ;; C-prt
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [(floor(strategy-number / 27) mod 27) =  1]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies

    set Z                                            (count           agents with [((floor(strategy-number /  9) mod 27) =  0) or ((floor(strategy-number /  9) mod 27) =  6) or ((floor(strategy-number /  9) mod 27) =  8)])                           ;; AllC
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((floor(strategy-number /  9) mod 27) =  0) or ((floor(strategy-number /  9) mod 27) =  6) or ((floor(strategy-number /  9) mod 27) =  8)]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies

    set Z                                            (count           agents with [(floor(strategy-number /  9) mod 27) =  7])                             ;; OthC
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [(floor(strategy-number /  9) mod 27) =  7]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies


    set Z                                            (count           agents with [((strategy-number > 80) and ((floor(strategy-number /  1) mod 9) =  1))])                             ;; TfTit
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number > 80) and ((floor(strategy-number /  1) mod 9) =  1))]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies

    set Z                                            (count           agents with [((strategy-number > 80) and ((floor(strategy-number /  1) mod 9) =  2))])                             ;; OfTit
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number > 80) and ((floor(strategy-number /  1) mod 9) =  2))]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies

    set Z                                            (count           agents with [((strategy-number = 81) or (strategy-number = 84) or (strategy-number = 90) or (strategy-number = 93))])                             ;; WSLS
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number = 81) or (strategy-number = 84) or (strategy-number = 90) or (strategy-number = 93))]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies

    set Z                                            (count           agents with [((strategy-number = 87) or (strategy-number = 96) or (strategy-number = 99) or (strategy-number = 102) or (strategy-number = 105))])                             ;; FGOF
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number = 87) or (strategy-number = 96) or (strategy-number = 99) or (strategy-number = 102) or (strategy-number = 105))]) / Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies
     
    set Z                                            (count           agents with [((strategy-number > 107) and (strategy-number < 135) and ((floor(strategy-number /  1) mod 3) =  0))])   ;; D-prt
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number > 107) and (strategy-number < 135) and ((floor(strategy-number /  1) mod 3) =  0))]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies
     
    set Z                                            (count           agents with [((strategy-number > 80) and(((floor(strategy-number /  1) mod 9) =  4) or ((floor(strategy-number /  1) mod 9) =  5) or ((floor(strategy-number /  1) mod 9) =  7) or ((floor(strategy-number /  1) mod 9) =  8)))])   ;; AllD
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number > 80) and(((floor(strategy-number /  1) mod 9) =  4) or ((floor(strategy-number /  1) mod 9) =  5) or ((floor(strategy-number /  1) mod 9) =  7) or ((floor(strategy-number /  1) mod 9) =  8)))]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies     
  

    set Z                                            (count           agents with [((strategy-number > 134) and ((floor(strategy-number /  1) mod 3) =  0))])   ;; AllD
    set plot-avg-payoffs lput (ifelse-value (Z > 0) [(sum [payoff] of agents with [((strategy-number > 134) and ((floor(strategy-number /  1) mod 3) =  0))]) /  Z] [0]) plot-avg-payoffs
    set plot-frequencies lput Z plot-frequencies     
end 



;;;;;;;;;;;;;;;;;;;;;;;;
;;;      Plots       ;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to update-graphs   
  ;; all graphs refer to the situation before the new breed comes in.
  let current-num-agents (count agents)
   
  set-current-plot "Players C/D"
    set-current-plot-pen "no play"  plotxy ticks current-num-agents    
    set-current-plot-pen "in DD"    plotxy ticks (2 * num-outcomes)
    set-current-plot-pen "in CD/DC" plotxy ticks (2 * (num-outcomes - numDD))
    set-current-plot-pen "in CC"    plotxy ticks (2 * numCC)
    set-plot-y-range 0 current-num-agents
    
  set-current-plot "Players S/L"
    set-current-plot-pen "no play"  plotxy ticks current-num-agents    
    set-current-plot-pen "in LL"    plotxy ticks (2 * num-outcomesS)
    set-current-plot-pen "in SL/LS" plotxy ticks (2 * (num-outcomesS - numLL))
    set-current-plot-pen "in SS"    plotxy ticks (2 * numSS)
    set-plot-y-range 0 current-num-agents
  
  set-current-plot "Players' pairs"
    set-current-plot-pen "singles before matching"  
    plot n-of-singles-at-the-beginning
    set-current-plot-pen "singles after matching"
    plot (n-of-singles-at-the-beginning - n-of-singles-selected-to-be-paired)
    set-current-plot-pen "in pairs after matching"
    plot count paired-players
    set-plot-y-range 0 current-num-agents
    
  set-current-plot "Average Payoff"
    set-current-plot-pen "payoff"  
    plot avg-payoff
    set-plot-y-range DD-payoff CC-payoff

  set-current-plot "Strategy Distribution"
    let total (sum plot-frequencies)
    let bar 1
    foreach (n-values num-plots [?]) [
      set-current-plot-pen item ? plot-names 
      set-plot-pen-mode 1
      plotxy ticks bar 
      set bar (bar - ((item ? plot-frequencies) / total))
    ]    
    set-plot-y-range 0 1 
    
  set-current-plot "Strategy Distribution 2"
    let total2 (sum plot-frequencies)

    foreach (n-values num-plots [?]) [
      set-current-plot-pen item ? plot-names 
      set-plot-pen-mode 0
      plotxy ticks ((item ? plot-frequencies) / total2)   ]    
    set-plot-y-range 0 1   
    
  set-current-plot "Strategy Payoffs"


    
    foreach (n-values num-plots [?]) [
      set-current-plot-pen item ? plot-names 
      set-plot-pen-mode 0
      if (ticks > 0) [plotxy ticks (item ? plot-avg-payoffs) ]
    ]    
    set-plot-y-range CD-payoff  DC-payoff 
end 

There is only one version of this model, created about 5 years ago by Filip Vesely.

Attached files

File Type Description Last updated
VSRPD162.png preview Preview for 'VSRPD162' about 5 years ago, by Filip Vesely Download

This model does not have any ancestors.

This model does not have any descendants.