Prisoner's Dilemma Tournament

No preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 4.0pre8 • Viewed 564 times • Downloaded 38 times • Run 0 times
Download the 'Prisoner's Dilemma Tournament' modelDownload this modelEmbed this model

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


VERSION

$Id: Prisoner's Dilemma Tournament.nlogo 37529 2008-01-03 20:38:02Z craig $

add model documentation here

Comments and Questions

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

Click to Run Model

; add model procedures here
globals
[
  status
  STRATEGIES PAYOFFS COOPERATE DEFECT
]
turtles-own
[
  user-id
  turtle-color
  strategy
  ready?
  total-assets
  plays
]

to startup
  setup
  hubnet-set-client-interface "COMPUTER" []
  hubnet-reset
  set status "Click GO to begin"
end 

to setup
  ca
  set COOPERATE 1
  set DEFECT 0
  set PAYOFFS [
    [ [ 0 0 ] [ 2  2  ] ]
    [ [ 0 1 ] [ 10 0  ] ]
    [ [ 1 0 ] [ 0  10 ] ]
    [ [ 1 1 ] [ 6  6  ] ]
  ]
end 

to play [ p1 p2 ]
  let d1 0
  let d2 0
  let po 0
  let game-number 0
  let moves1 0
  let moves2 0
  if (p1 != p2) [
    hubnet-send [user-id] of p1 "current-opponent" [user-id] of p2
    hubnet-send [user-id] of p2 "current-opponent" [user-id] of p1
    set game-number 1
    set moves1 (list )
    set moves2 (list )
    repeat 1000 [
      set d1 [ make-decision game-number moves1 moves2 ] of p1
      set d2 [ make-decision game-number moves2 moves1 ] of p2
      set po lookup-table PAYOFFS (list d1 d2)
      ask p1 [ collect-payoff item 0 po set plays plays + 1]
      ask p2 [ collect-payoff item 1 po set plays plays + 1]
      set game-number game-number + 1
      set moves1 lput d1 moves1
      set moves2 lput d2 moves2
    ]
    hubnet-send [user-id] of p1 "current-opponent" ""
    hubnet-send [user-id] of p2 "current-opponent" ""
  ]
end 
;; players

to-report make-decision [ game-number my-moves opponent-moves ]
  report runresult strategy
end 

to collect-payoff [ amount ]
  set total-assets total-assets + amount
  set plabel total-assets
  hubnet-send user-id "total-assets" total-assets
end 

;;;;

to-report lookup-table [ table entry ]
  if empty? table [ report "-1" ];
  if item 0 item 0 table = entry [
    report item 1 item 0 table;
  ]
  report lookup-table but-first table entry;
end 

;;;;

to go
  if hubnet-message-waiting?
  [
    hubnet-fetch-message
    ifelse hubnet-enter-message?
    [
      ask turtles with [ user-id = hubnet-message-source ]
      [ die ]
      crt 1
      [
        setxy random-float world-width random-float world-width
        set user-id hubnet-message-source
        hubnet-send hubnet-message-source "rank" 0
        set ready? false
        ;;hubnet-send hubnet-message-source "Your turtle's color is:" (color-string color)
        set label-color black
        set label user-id

          set status (word "" count turtles " clients logged in, "
                      count turtles with [ ready? ] " ready.")
      ]
    ]
    [
      ifelse hubnet-exit-message?
      [
        ask turtles with [ user-id = hubnet-message-source ]
        [ die ]
      ]
      [ exe-cmd ]
    ]
  ]
end 

to exe-cmd
  show hubnet-message-tag
  if hubnet-message-tag = "strategy"
  [
    check-strategy hubnet-message
  ]
end 

to check-strategy [ the-strategy ]
  let strategy-status-message 0
  let opponent-moves 0
  let my-moves 0

  carefully [
    set opponent-moves (list )
    set my-moves (list )
    __ignore runresult the-strategy
    set opponent-moves (list COOPERATE DEFECT COOPERATE)
    set my-moves (list COOPERATE DEFECT COOPERATE )
    __ignore runresult the-strategy
    ask turtles with [ user-id = hubnet-message-source ] [ set strategy the-strategy set ready? true]
    set strategy-status-message "OK"
  ]
  [
    ask turtles with [ user-id = hubnet-message-source ] [ set ready? false]
    set strategy-status-message error-message
  ]
  hubnet-send hubnet-message-source "strategy-status" strategy-status-message
            set status (word "" count turtles " clients logged in, "
  count turtles with [ ready? ] " ready.")
  if count turtles with [ ready?] = count turtles [ set status "Ready to go. Click START to begin tournament" ]
end 

There are 2 versions of this model.

Uploaded by When Description Download
Uri Wilensky over 12 years ago Prisoner's Dilemma Tournament Download this version
Uri Wilensky over 12 years ago Prisoner's Dilemma Tournament Download this version

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.