!!!---moth.nlogo
来自「NETLOGO」· NLOGO 代码 · 共 842 行 · 第 1/2 页
NLOGO
842 行
breeds [strategies players]globals [ticks games matches numplayers payoffmat stratmat population davidRound?]; lastheader]players-own [strategy partner score history coop? stay?]strategies-own [name isNice? num]to startup set stratmat [ ; name color nice? stay? ["Nice" green true true] ["Nasty" black false true] ["Tit4Tat" gray true true] ["Moth" blue true false] ["Hit&Run" yellow false false] ["Santa" red true false] ["NasMoth" violet false false] ["NNHRun" cyan true false] ] clear-output print-stats-headerendto setup ;if seed? [random-seed 1] ;if stratmat = 0 [startup] clear-turtles clear-plot clear-graphics ; ca would also clear stratmat! ask patches [set pcolor white] setup-strategies set population values-from strategies [num] set numplayers sum population if count strategies with [num > 0] = 0 [output-print "No strategies!" stop] if sum population mod 2 = 1 [output-print "Odd number of players!" stop] setup-players set ticks 0 set games 0 set matches 0 set davidRound? true set payoffmat list (list (list DD DD) (list DC CD)) (list (list CD DC) (list CC CC)) set-plot-x-range 0 matchesPerSim set-plot-y-range 0 count playersendto setup-strategies foreach stratmat [ ; Note: Nice is turtle 0, Nasty is turtle 1, etc create-custom-strategies 1 [ set name item 0 ? set color item 1 ? set isNice? item 2 ? set hidden? true set num runresult word "num" name ] ]endto setup-players foreach values-from strategies [self] [ create-custom-players (num-of ?) [ set strategy ? set partner nobody set score 0 set color color-of strategy ] ] let delta 360 / count players ask players [set heading (who - count strategies) * delta forward screen-edge-x] setup-partnersendto setup-partners let l shuffle values-from (players with [partner = nobody]) [self] let len length l ; generally > 0 should work if 0 let len1 len / 2 let l1 sublist l 0 len1 let l2 sublist l len1 len (foreach l1 l2 [ set partner-of ?1 ?2 set partner-of ?2 ?1 set history-of ?1 [] set history-of ?2 [] ask ?1 [set heading towards-nowrap ?2] ask ?2 [set heading towards-nowrap ?1] ])endto-report stats let payoffs (list CC CD DC DD) let initpop values-from strategies [num] let nums (list numplayers gamesPerMatch matchesPerSim) ;let roundoff ifelse-value davidRound? [1][0] let results (sentence payoffs initpop nums population) ; roundoff population) report list-to-string resultsendto print-stats-header let names "AllC AllD T4T Moth HnR Santa NMoth NNHnR" let names0 "AllC0 AllD0 T4T0 Moth0 HnR0 Santa0 NMoth0 NNHnR0" output-print (word "CC CD DC DD " names0 " Players GperMatch MperSim " names); D " names)endto print-stats let s ifelse-value stop? [""][(word "[" matches "]" )] output-print word s statsendto go if numplayers = 0 [stop] if matches = 0 [graph-stats] set matches matches + 1 repeat gamesPerMatch [set games games + 1 play-one-game] evolve if graph? [graph-stats] if stop? [print-stats stop]endto-report stop? report matches = matchesPerSimendto scan repeat scanNum [ setup ;repeat matchesPerSim [go] while [not stop?] [go] ] ;stopendto play-one-game let pairs players with [who < who-of partner] ask pairs [play] setup-partnersendto graph-stats ask strategies with [num > 0] [ set-current-plot-pen name plot count players with [strategy = myself] ]endto evolve ask players [if score < 0 [set score 0]] ; adjust for negative numbers. possibly translate instead? let total score-sum players if total = 0 [stop] ; all nasty w/ 0 DD would cause this set population map [score-sum players with [strategy = ?]] (values-from strategies [self]) set population map [(? * numplayers) / total] population set population rounded-list population let id count strategies (foreach values-from strategies [self] population [ repeat ?2 [ ask turtle id [set strategy ?1] set id id + 1 ] ]) ask players [set color color-of strategy set score 0 set partner nobody] setup-partnersend;;;; Utilitiesto-report precision-list [l places] report map [precision ? places] lendto-report list-to-string [l] report reduce [(word ?1 " " ?2)] lendto-report score-sum [agents] report sum-list (values-from agents [score])endto-report sum-list [l] report ifelse-value (length l = 0) [0] [reduce [?1 + ?2] l]endto-report rand-weighted-index [floats] let rand random-float (sum-list floats) let i 0 let s 0 foreach floats [ set s s + ? if s >= rand [report i] set i i + 1 ] output-show "Oops in rand-weighted-index" report -1endto-report rounded-list [floats] ifelse davidRound? [report floor-whole-adjusted-list floats] [report floor-fraction-adjusted-list floats]end; Historical: Several methods were looked at for resolving fractional agents for a given; strategy during the evolutionary phase. Here are two. The first is the original one used; by David Joyce, the second used during our initial modeling. Choosing between them; did not change the overall statistics. We now default to using David's approach.to-report floor-fraction-adjusted-list [floats] let roundoff map [ ? - int ? ] floats let ints map [ int ? ] floats repeat ((round sum-list floats) - sum-list ints) [ let i rand-weighted-index roundoff set roundoff replace-item i roundoff 0 set ints replace-item i ints (1 + item i ints) ] report intsendto-report floor-whole-adjusted-list [floats] let ints map [ int ? ] floats repeat ((round sum-list floats) - sum-list ints) [ let i rand-weighted-index floats set ints replace-item i ints (1 + item i ints) ] report intsendto-report rand-weighted-list [floats] let ints n-values length floats [0] repeat (round sum-list floats) [ let i rand-weighted-index floats set ints replace-item i ints (1 + item i ints) ] report intsendto-report round-adjusted-list [floats] let n round sum-list floats let ints map [round ?] floats let inum sum-list ints let delta ifelse-value (inum < n) [1][-1] repeat abs (n - inum) [ let i rand-weighted-index floats set ints replace-item i ints (delta + item i ints) ] report intsend;;;; Player turtle procsto play set ticks ticks + 1 next ask partner [next] let payoff item (coop?-of partner) (item coop? payoffmat) let myscore item 0 payoff let itscore item 1 payoff set score score + myscore ask partner [set score score + itscore] ifelse (stay?) * (stay?-of partner) = 0 [ ask partner [set partner nobody] set partner nobody ] [ ask partner [set history fput coop?-of partner history] set history fput coop?-of partner history ]endto next let strat name-of strategy ifelse strat = "Nice" [Nice] [ ifelse strat = "Nasty" [Nasty] [ ifelse strat = "Tit4Tat" [Tit4Tat] [ ifelse strat = "Moth" [Moth] [ ifelse strat = "Hit&Run" [Hit&Run] [ ifelse strat = "Santa" [Santa] [ ifelse strat = "NasMoth" [NasMoth] [ ifelse strat = "NNHRun" [NNHRun] [ output-show (word "Oops: Bad strategy name " ) stop ]]]]]]]]endto Nice set coop? 1 set stay? 1endto Nasty set coop? 0 set stay? 1endto Tit4Tat set coop? ifelse-value (length history = 0) [1] [first history] set stay? 1endto Moth set coop? ifelse-value (length history = 0) [1] [first history] set stay? coop?endto Hit&Run set coop? 0 set stay? 0endto Santa set coop? 1 set stay? 0endto NasMoth set coop? ifelse-value (length history = 0) [0] [first history] set stay? ifelse-value (length history = 0) [1] [first history]endto NNHRun set coop? ifelse-value (length history < 2) [1] [0] set stay? ifelse-value (length history = 0) [1] [first history]end; Not needed for model: run from command center for parameter scansto paramscan let ranges [0 1 3 5] clear-output output-print (word "playoff scan: ranges = " ranges) print-stats-header foreach ranges [ set CC ? foreach ranges [ set CD ? foreach ranges [ set DC ? foreach ranges [ set DD ?; if not (CC = CD and CD = DC and DC = DD); [show (list CC CD DC DD) scan] show (list CC CD DC DD) scan ] ] ] ]endto pdscan let ranges [0 1 2 3 4 5 6 7 8 9 10] clear-output output-print (word "PD scan: ranges = " ranges) print-stats-header foreach ranges [ set CC ? foreach ranges [ set CD ? foreach ranges [ set DC ? foreach ranges [ set DD ? if DC > CC and CC > DD and DD > CD and 2 * CC > DC + CD [show (list CC CD DC DD) scan] ] ] ] ]end@#$#@#$#@GRAPHICS-WINDOW309105392615520.01101110CC-WINDOW5661687756Command CenterSLIDER546369671402gamesPerMatchgamesPerMatch1402011NILBUTTON791913452gogoT1TOBSERVERNILNILSLIDER546401671434matchesPerSimmatchesPerSim5503051NILBUTTON16197152NILsetupNIL1TOBSERVERTNILMONITOR2491030659NILgames01PLOT5265539486PopulationMatchNumber0.010.00.010.0truetruePENS"Nice" 1.0 0 -11352576 true
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?