---minority game.nlogo
来自「NETLOGO」· NLOGO 代码 · 共 1,430 行 · 第 1/3 页
NLOGO
1,430 行
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Variable and Breed declarations ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;globals[ ;; system variables for program to be functional player-history ;; the history of which number was the minority (encoded into a binary number), seen by the players android-history ;; the history of which number was the minority (encoded into a binary number), seen by the androids minority ;; the current number in the minority ticks ;; keeps track of total run time of the game clock ;; keeps track of the timer for players to make their decision avg-score ;; keeps track of all turtles' average score stdev-score ;; keeps track of the standard deviation of the turtles' scores avg-success ;; keeps track of all turtles' average success winner loser ;; lists used to create the various turtles shape-names ;; shapes available for players colors ;; colors available for players color-names ;; names of colors available for players used-shape-colors ;; shape-color combinations used ;; quick start instructions and variables quick-start ;; current quickstart instruction displayed in the quickstart monitor qs-item ;; index of the current quickstart instruction qs-items ;; list of quickstart instructions]turtles-own[ score ;; each turtle's score choice ;; each turtle's current choice]breeds[ androids players]players-own[ user-id ;; user-id of hubnet player chosen-sides? ;; true/false to tell if player has made current choice choices-made ;; the number of choices each turtle has made]androids-own[ strategies ;; each android's strategies (a list of lists) current-strategy ;; each android's current strategy (index for the above list) strategies-scores ;; the accumulated virtual scores for each of the turtle's strategies];;;;;;;;;;;;;;;;;;;;;;; Setup Functions ;;;;;;;;;;;;;;;;;;;;;;;;; startup of the modelto startup setup setup-quick-start hubnet-set-client-interface "COMPUTER" [ "clients/Minority Game client.nlogo" ] hubnet-resetend;; setup for the overall program, will require clients to re-loginto setup ;; prevent an infinite loop from occurring in assign-strategies if (android-memory = 1 and strategies-per-android > 4 ) [ user-message "You need to increase the memory variable or\n" + "decrease the strategies-per-agent variable" stop ] ca setup-quick-start initialize-system initialize-androidsend;; initializes system variablesto initialize-system clear-output ifelse (player-memory >= android-memory) [ set player-history random (2 ^ player-memory) set android-history full-history player-history player-memory while [ length android-history > android-memory ] [ set android-history but-first android-history ] set android-history (decimal android-history) ] [ set android-history random (2 ^ android-memory) set player-history full-history android-history android-memory while [ length player-history > player-memory ] [ set player-history but-first player-history ] set player-history (decimal player-history) ] set ticks 0 set clock wait-time + 1 set avg-score 0 set stdev-score 0 set winner "N/A" set loser "N/A" set shape-names [ "ant" "bee" "boat" "box" "butterfly" "circle" "person" "spacecraft" "arrow 2" "truck" "turtle" "wolf" ] set colors [ white gray brown yellow lime turquoise cyan sky violet orange magenta pink ] set color-names [ "white" "gray" "brown" "yellow" "lime" "turquoise" "cyan" "sky" "violet" "orange" "magenta" "pink" ] set used-shape-colors []end;; reports the history in binary format (with padding if needed)to-report full-history [ agent-history agent-memory ] let full binary agent-history while [length full < agent-memory] [ set full fput 0 full ] report fullend;; converts a decimal number to a binary number (stored in a list of 0's and 1's)to-report binary [ decimal-num ] let binary-num [] loop [ set binary-num fput (decimal-num mod 2) binary-num set decimal-num int (decimal-num / 2) if (decimal-num = 0) [ report binary-num ] ]end;; converts a binary number (stored in a list of 0's and 1's) to a decimal numberto-report decimal [ binary-num ] report reduce [(2 * ?1) + ?2] binary-numend;; remove existing turtles and create number-of-androids androidsto initialize-androids ask androids [ die ] create-custom-androids number-of-androids [ set color black setxy 0 (screen-size-y * who / (count turtles)) ;; disperse over the y-axis set heading 90 assign-strategies set current-strategy random strategies-per-android set choice item android-history (item current-strategy strategies) ifelse (color-by = "choice") [ recolor ] [ set color green ] ;; we initially set all to green to prevent divide by zero error set score 0 set strategies-scores n-values strategies-per-android [0] ] let num-picked-zero count turtles with [choice = 0] ifelse (num-picked-zero <= (count turtles - 1) / 2) [ set minority 0 ] [ set minority 1 ] setup-plotsend;; gives the androids their allotted number of unique strategiesto assign-strategies ;; android procedure let temp-strategy false set strategies [] repeat strategies-per-android [ set temp-strategy create-strategy while [ member? temp-strategy strategies ] [ set temp-strategy create-strategy ] set strategies fput temp-strategy strategies ]end;; creates a strategy (a binary number stored in a list of;; length 2 ^ android-memory)to-report create-strategy report n-values (2 ^ android-memory) [random 2]end;; color the androids based on the color-by optionto recolor ;; android procedure ifelse (color-by = "choice") [ recolor-by-choice ] [ recolor-by-speed ]end;; colors the androids according to their choiceto recolor-by-choice ;; android procedure ifelse (choice = 0) [ set color red ] [ set color blue ]end;; colors the androids according to their successto recolor-by-speed ;; android procedure ifelse (score > avg-score + stdev-score) [ set color red ] [ ifelse (score < avg-score - stdev-score) [ set color blue ] [ set color green ] ]end;; resets the system, activated by clicking "re-run" buttonto clear-all-data initialize-system initialize-androids ask players [ clear-my-data ] setup-plotsend;; reset a player to some initial valuesto clear-my-data ;; players procedure ht setxy 0 (random-float screen-edge-y - random-float screen-edge-y) set choice (random 2) set score 0 set chosen-sides? false set choices-made 0 send-identity-to-clients clear-my-client stend;; allow HubNet clients to log in without running the modelto log-in every 0.1 [ listen-clients ]end;;;;;;;;;;;;;;;;;;;;;;;;; Runtime Functions ;;;;;;;;;;;;;;;;;;;;;;;;;to go ;; we need at least two turtles to do this experiment and to ensure that there is a minority ;; we increase this number to three. if count turtles < 3 [ user-message "This requires at least three (3) turtles to run. Please create more" + " androids or have more people log in." stop ] no-display ;; turn off the display temporarily to speed up the model every 0.1 [ ;; get commands and data from the clients listen-clients ;; determine if the system should be updated (advanced in time) if any? turtles [ if any? androids [ check-if-odd ] if clock = 0 [ decide-for-them ] if all-players-decided? [ update-system update-scores-and-strategies advance-system update-choices do-plot if any? players [ set winner user-id-of random-one-of players with [ score = max values-from players [score] ] set loser user-id-of random-one-of players with [ score = min values-from players [score] ] ] move show-extremes ] ] ] ;; controls the timer for clients to make a decision every 1 [ set clock clock - 1 hubnet-broadcast "Time to choose:" clock ] displayend;; make sure that there will be a true minorityto check-if-odd if (count turtles mod 2 = 0) [ ask random-one-of androids [ die ] ]end;; if the time limit is up, make a random choice for clientsto decide-for-them ask players with [not chosen-sides?] [ set choice random 2 set chosen-sides? true set choices-made choices-made + 1 hubnet-send user-id "Current choice:" choice hubnet-send user-id "Status:" "Your selection was made for you." ]end;; reports if all the players have made a decisionto-report all-players-decided? report not any? players with [ not chosen-sides? ]end;; updates system variables such as minority, avg-score, and stdev-score globalsto update-system let num-picked-zero count turtles with [choice = 0] ifelse (num-picked-zero <= (count turtles - 1) / 2.0) [ set minority 0 ] [ set minority 1 ] print "Number Picked Zero: " + num-picked-zero + "\tMinority Choice: " + minority ;; plot this here (instead of in do-plot) for speed set-current-plot "Number Picking Zero" plot num-picked-zero set avg-score mean (values-from androids [score] + values-from players [score]) set stdev-score standard-deviation (values-from androids [score] + values-from players [score]) if ticks > 0 [ set avg-success mean (values-from androids [score / ticks] + values-from players with [ choices-made > 0 ] [score / choices-made]) ] set clock wait-time + 1end;; ask all participants to update their strategy and scoresto update-scores-and-strategies ask androids [ update-androids-scores-and-strategies ] ask players [ update-score ]end;; if the turtle is in the minority, increase its scoreto update-score ;; turtle procedure if choice = minority [ set score score + 1 ]end;; updates android's score and their strategies' virtual scoresto update-androids-scores-and-strategies ;; androids procedure let max-score last sort strategies-scores ;; this increases the virtual scores of the strategies if they selected the minority set strategies-scores (map [ ifelse-value (item android-history ?1 = minority) [?2 + 1] [?2] ] strategies strategies-scores) set max-score max strategies-scores let max-strategies [] let counter 0 ;; this picks a strategy with the largest virtual score foreach strategies-scores [ if (? = max-score) [ set max-strategies lput counter max-strategies ] set counter counter + 1 ] set current-strategy random-one-of max-strategies update-scoreend;; advances the system forward in time and updates the historyto advance-system set player-history decimal (lput minority but-first full-history player-history player-memory) set android-history decimal (lput minority but-first full-history android-history android-memory) ask players [ hubnet-send user-id "The past history:" full-history player-history player-memory ] set ticks ticks + 1end;; ask all participants to update their choiceto update-choices update-androids-choices update-players-choicesend;; ask the androids to pick a new choice and then recolor themselvesto update-androids-choices ask androids [ set choice (item android-history (item current-strategy strategies)) recolor ]end;; update the monitors on the clientto update-players-choices ask players [ hubnet-send user-id "Total score:" score hubnet-send user-id "Success rate:" round (score / choices-made * 1000) / 1000 hubnet-send user-id "Your last choice:" choice hubnet-send user-id "Current choice:" "N/A" hubnet-send user-id "Status:" "Please make your selection." set chosen-sides? false ]end;; move turtles according to their success (a visual aid to see their collective behavior)to move if avg-success > 0 and ticks > 0 [ ask androids [ fd (score / ticks) / avg-success ] ask players [ fd (score / choices-made) / avg-success ] ]end;; label androids according to who is performing best and worstto show-extremes let max-success max (values-from androids [score / ticks] + values-from players with [ choices-made > 0 ] [score / choices-made]) let min-success min (values-from androids [score / ticks] + values-from players with [ choices-made > 0 ] [score / choices-made]) ask androids [ set label no-label ] ask androids with [score / ticks = max-success] [ set label "best" ] ask androids with [score / ticks = min-success] [ set label "worst" ]end;;;;;;;;;;;;;;;;;;;;; Plotting Code ;;;;;;;;;;;;;;;;;;;;;;; sets up plots and axis maximato setup-plots clear-all-plots set-current-plot "Number Picking Zero" ifelse any? turtles [ set-plot-y-range 0 (count turtles) ] [ set-plot-y-range 0 1 ] set-current-plot "Success Rates" set-histogram-num-bars 25 do-plot ;; plot initial valueend;; updates three of the four plots. one is updated in the update-system procedure for speedto do-plot let score-list values-from turtles [score] let success-list values-from players with [ choices-made > 0 ] [score / choices-made] if ticks > 0 [ set success-list success-list + (values-from androids [score / ticks ]) ] if length score-list > 0 [ set-current-plot "Scores" set-current-plot-pen "max" plot max score-list set-current-plot-pen "min" plot min score-list set-current-plot-pen "avg" plot mean score-list ] if length success-list > 0 [ set-current-plot "Success rate" set-current-plot-pen "max" plot max success-list set-current-plot-pen "min" plot min success-list set-current-plot-pen "avg" plot mean success-list set-current-plot "Success Rates" histogram-list success-list ]end;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Quick Start functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; instructions to quickly setup the model, and clients to run this activityto setup-quick-start set qs-item 0 set qs-items [ "Teacher: Follow these directions to run the HubNet activity." "Optional: Zoom In (see Tools in the Menu Bar)" "Optional: Change any of the settings...." "If you did change settings, press the SETUP button." "Teacher: Press the LOG-IN button." "Everyone: Open up a HubNet Client on your machine and..." "choose a user-name and..." "connect to this activity." "Teacher: Once everyone has started their client..." "press the LOG-IN button, then press GO." "Everyone: Watch your clock and choose 0 or 1." "Teacher: To rerun the activity with the same group,..." "stop the model by pressing the GO button, if it is on." "Change any of the settings that you would like." "Press the RE-RUN button." "Teacher: Restart the simulation by pressing the GO button again." "Teacher: To start the simulation over with a new group,..." "stop the model by pressing the GO button, if it is on..." "and follow these instructions again from the beginning." ] set quick-start (item qs-item qs-items)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?