关于囚徒困境N-Person迭代程序的进化动力学

时间:2018-05-10 20:27:10

标签: netlogo

所以,这是我必须完成的任务的一部分:''创建一个这个模型的版本,通过允许它们通过允许它们消失来重现和惩罚不成功的策略来奖励成功的策略。''

但问题是我无法让它工作,它会显示每个滴答数的错误,这是我到目前为止的代码(我删除了分数,死亡和复制的命令,所以这是纯粹的您可以说的代码)我应该添加什么才能使其正常工作?

非常感谢帮助,谢谢!

这是代码

    globals [
  ;;number of turtles with each strategy
  num-random
  num-cooperate
  num-defect
  num-tit-for-tat
  num-unforgiving
  num-unknown

  ;;number of interactions by each strategy
  num-random-games
  num-cooperate-games
  num-defect-games
  num-tit-for-tat-games
  num-unforgiving-games
  num-unknown-games

  ;;total score of all turtles playing each strategy
  random-score
  cooperate-score
  defect-score
  tit-for-tat-score
  unforgiving-score
  unknown-score

  ;;Noise
  noise-active             ;;turn noise on and off
  noise-prob-defect        ;;probability to flip a cooperate into a defect
  noise-prob-cooperate     ;;probability to flig a defect into a cooperate
  noise-defect-true        ;;Counter of times a defect turned into a cooperate
  noise-cooperate-true     ;;Counter of times a cooperate turned into a defect
]
turtles-own [
  score
  strategy
  defect-now?
  partner-defected? ;;action of the partner
  partnered?        ;;am I partnered?
  partner           ;;WHO of my partner (nobody if not partnered)
  partner-history   ;;a list containing information about past interactions
                    ;;with other turtles (indexed by WHO values)
]


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

to setup
  clear-all
  store-initial-turtle-counts ;;record the number of turtles created for each strategy
  setup-turtles ;;setup the turtles and distribute them randomly
  noise-setup ;; setup of the noise variables
  reset-ticks
end

;;record the number of turtles created for each strategy
;;The number of turtles of each strategy is used when calculating average payoffs.
;;Slider values might change over time, so we need to record their settings.
;;Counting the turtles would also work, but slows the model.
to store-initial-turtle-counts
  set num-random n-random
  set num-cooperate n-cooperate
  set num-defect n-defect
  set num-tit-for-tat n-tit-for-tat
  set num-unforgiving n-unforgiving
  set num-unknown n-unknown
end

;;setup the turtles and distribute them randomly
to setup-turtles
  make-turtles ;;create the appropriate number of turtles playing each strategy
  setup-common-variables ;;sets the variables that all turtles share
end

;;create the appropriate number of turtles playing each strategy
to make-turtles
  create-turtles num-random [ set strategy "random" set color gray - 1 ]
  create-turtles num-cooperate [ set strategy "cooperate" set color red ]
  create-turtles num-defect [ set strategy "defect" set color blue ]
  create-turtles num-tit-for-tat [ set strategy "tit-for-tat" set color lime ]
  create-turtles num-unforgiving [ set strategy "unforgiving" set color turquoise - 1 ]
  create-turtles num-unknown [set strategy "unknown" set color magenta ]
end

;;set the variables that all turtles share
to setup-common-variables
  ask turtles [
    set score 0
    set partnered? false
    set partner nobody
    setxy random-xcor random-ycor
  ]
  setup-history-lists ;;initialize PARTNER-HISTORY list in all turtles
end

;;initialize PARTNER-HISTORY list in all turtles
to setup-history-lists
  let num-turtles count turtles

  let default-history [] ;;initialize the DEFAULT-HISTORY variable to be a list

  ;;create a list with NUM-TURTLE elements for storing partner histories
  repeat num-turtles [ set default-history (fput false default-history) ]

  ;;give each turtle a copy of this list for tracking partner histories
  ask turtles [ set partner-history default-history ]
end


;;;;;;;;;;;;;;;;;;;;;;;;
;;;Runtime Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;

to go
  clear-last-round
  ask turtles [ partner-up ]                        ;;have turtles try to find a partner
  let partnered-turtles turtles with [ partnered? ]
  ask partnered-turtles [ select-action ]           ;;all partnered turtles select action
  ask partnered-turtles [ play-a-round ]
  do-scoring
  tick
end

to clear-last-round
  let partnered-turtles turtles with [ partnered? ]
   ifelse (random 100 > prob-of-staying)
   [ask partnered-turtles [ release-partners ]]
   []
end

;;release partner and turn around to leave
to release-partners
  set partnered? false
  set partner nobody
  rt 180
  set label ""
end

;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.

to partner-up ;;turtle procedure
  if (not partnered?) [              ;;make sure still not partnered
    rt (random-float 90 - random-float 90) fd 1     ;;move around randomly
    set partner one-of (turtles-at -1 0) with [ not partnered? ]
    if partner != nobody [              ;;if successful grabbing a partner, partner up
      set partnered? true
      set heading 270                   ;;face partner
      ask partner [
        set partnered? true
        set partner myself
        set heading 90
      ]
    ]
  ]
end

;;choose an action based upon the strategy being played
to select-action ;;turtle procedure
  if strategy = "random" [ act-randomly ]
  if strategy = "cooperate" [ cooperate ]
  if strategy = "defect" [ defect ]
  if strategy = "tit-for-tat" [ tit-for-tat ]
  if strategy = "unforgiving" [ unforgiving ]
  if strategy = "unknown" [ unknown ]

  add-noise ;;adds noise to the games
end

to play-a-round ;;turtle procedure
  get-payoff     ;;calculate the payoff for this round
  update-history ;;store the results for next time
end

;;calculate the payoff for this round and
;;display a label with that payoff.
to get-payoff
  set partner-defected? [defect-now?] of partner
  ifelse partner-defected? [
    ifelse defect-now? [
      set score (score + 1) set label 1
    ] [
      set score (score + 0) set label 0
    ]
  ] [
    ifelse defect-now? [
      set score (score + 5) set label 5
    ] [
      set score (score + 3) set label 3
    ]
  ]
end

;;update PARTNER-HISTORY based upon the strategy being played
to update-history
  if strategy = "random" [ act-randomly-history-update ]
  if strategy = "cooperate" [ cooperate-history-update ]
  if strategy = "defect" [ defect-history-update ]
  if strategy = "tit-for-tat" [ tit-for-tat-history-update ]
  if strategy = "unforgiving" [ unforgiving-history-update ]
  if strategy = "unknown" [ unknown-history-update ]
end


;;;;;;;;;;;;;;;;
;;;Strategies;;;
;;;;;;;;;;;;;;;;

;;All the strategies are described in the Info tab.

to act-randomly
  set num-random-games num-random-games + 1
  ifelse (random-float 1.0 < 0.5) [
    set defect-now? false
  ] [
    set defect-now? true
  ]
end

to act-randomly-history-update
;;uses no history- this is just for similarity with the other strategies
end

to cooperate
  set num-cooperate-games num-cooperate-games + 1
  set defect-now? false
end

to cooperate-history-update
;;uses no history- this is just for similarity with the other strategies
end

to defect
  set num-defect-games num-defect-games + 1
  set defect-now? true
end

to defect-history-update
;;uses no history- this is just for similarity with the other strategies
end

to tit-for-tat
  set num-tit-for-tat-games num-tit-for-tat-games + 1
  set partner-defected? item ([who] of partner) partner-history
  ifelse (partner-defected?) [
    set defect-now? true
  ] [
    set defect-now? false
  ]
end

to tit-for-tat-history-update
  set partner-history
    (replace-item ([who] of partner) partner-history partner-defected?)
end

to unforgiving
  set num-unforgiving-games num-unforgiving-games + 1
  set partner-defected? item ([who] of partner) partner-history
  ifelse (partner-defected?)
    [set defect-now? true]
    [set defect-now? false]
end

to unforgiving-history-update
  if partner-defected? [
    set partner-history
      (replace-item ([who] of partner) partner-history partner-defected?)
  ]
end

;;defaults to tit-for-tat
;;can you do better?
;;Generous Tit-for-Tat, cooperates 10% of the time that it would otherwise defect.
to unknown
  set num-unknown-games num-unknown-games + 1
  set partner-defected? item ([who] of partner) partner-history
  ifelse (partner-defected?)
  [ifelse (random 100 < 10) ;;be generous in 10% of the cases you would defect
    [set defect-now? false]
    [set defect-now? true]
  ]
  [set defect-now? false]
end

;;defaults to tit-for-tat-history-update
;;can you do better?
to unknown-history-update
  set partner-history
    (replace-item ([who] of partner) partner-history partner-defected?)
end

;;;;;;;;;;;
;;;Noise;;;
;;;;;;;;;;;

;;read values from slider and store them
to noise-setup
  set noise-active noise ;;Set if noise is active or not depending on switch
  set noise-prob-defect prob-def-to-cop  ;;Passes the current probability of defect turning into cooperate
  set noise-prob-cooperate prob-cop-to-def  ;;Passes the current probability of cooperate turning into defect
end

;;changes the decision according to the noise setup
to add-noise
  ;;check if noise is activated
  if (noise-active) [
    ;;check decision
    ifelse (defect-now?)
    [
      if (random 100 < noise-prob-defect)
      [
       ;;flip defect -> cooperate
       set defect-now? false
       set noise-defect-true (noise-defect-true + 1)  ;;Counter of times a flip has been made
      ]
    ]
    [
      if (random 100 < noise-prob-cooperate)
      [
       ;;flip cooperate -> defect
       set defect-now? true
       set noise-cooperate-true (noise-cooperate-true + 1) ;;Counter of times a flip has been made
      ]
    ]
  ]

end

;;;;;;;;;;;;;;;;;;;;;;;;;
;;;Plotting Procedures;;;
;;;;;;;;;;;;;;;;;;;;;;;;;

;;calculate the total scores of each strategy
to do-scoring
  set random-score  (calc-score "random" num-random)
  set cooperate-score  (calc-score "cooperate" num-cooperate)
  set defect-score  (calc-score "defect" num-defect)
  set tit-for-tat-score  (calc-score "tit-for-tat" num-tit-for-tat)
  set unforgiving-score  (calc-score "unforgiving" num-unforgiving)
  set unknown-score  (calc-score "unknown" num-unknown)
end

;; returns the total score for a strategy if any turtles exist that are playing it
to-report calc-score [strategy-type num-with-strategy]
  ifelse num-with-strategy > 0 [
    report (sum [ score ] of (turtles with [ strategy = strategy-type ]))
  ] [
    report 0
  ]
endenter code here

0 个答案:

没有答案