📄 ---golancefinal9.nlogo
字号:
globals [ ticks ; time steps credFollow ; credit for following (to est) lead ; leader gets credit equal to credFollow * lead i ; dumb place holder j ; dumber place holder k ; yet another place holder force ; counter for #spaces forced back b/c space is occupied numFinish ; number of bikers across the finish line nt ; number of tortoises nh ; number of hares nf ; number of free-riders theWinner ; breed (strategy) of winning biker total_rank_t ;sum of ranks of breed tortoise total_rank_h ;sum of ranks of breed tortoise total_rank_f ;sum of ranks of breed tortoise final_rank_t ;final sum of ranks of breed tortoise final_rank_h ;final sum of ranks of breed tortoise final_rank_f ;final sum of ranks of breed tortoise energy_up_bound ;upper bound for uniformly distributed eave - same for all breeds ;(eave = average available energy per trial/time step) vfMultiplier ; multiplier for agents (all breeds/strategies): vf = vfMultiplier * eave tortVfEndGame ; multiplier of vf to increase visibility for tortoises at endgame ]turtles-own [ eave ; incremental available energy per trial/time step est ; energy stock at time t pt ; position (in unidimensional space) at time t vf ; forward visibility measured in unidimensional space move ; amount to move forward in time step d_move ; amount biker desires to move (may be limited by vf) prevPt ; previous position toFollow ; boolean, 1 if there is someone to follow, 0 else rank ; final rank] breeds [tortoise hare freeRider];;; ;;; SETUP AND HELPERS;;; to setup ca set ticks 0 set credFollow Follower_Energy_Credit set lead Leader_Energy_Credit set tortVfEndGame Multiplier_Tort_Vf_atEndGame set vfMultiplier Forward_Visibility_Multiplier set force 0 set numFinish 0 ; counter for n. of agents that crossed the finish line set nt Number_of_Tortoises set nh Number_of_Hares set nf Number_of_Free_Riders set energy_up_bound Energy_upper_Bound ;upper bound on uniformly distributed eave ask patches [ setup-road ] setup-bikers set total_rank_t 0 set total_rank_h 0 set total_rank_f 0 set final_rank_t 0 set final_rank_h 0 set final_rank_f 0 setup-plots update-plotsendto setup-road if ( pycor < 2 ) and ( pycor > -2 ) [ set pcolor white set plabel pxcor ] if ( pxcor = 0 ) [ set pcolor red set plabel pxcor ] endto setup-bikers locals [n nty nfy nhy] set-default-shape turtles "bike" set n nt + nf + nh set nty nt set nfy nf set nhy nh ;agent creation and move order - agents move in a sequence T FR H T FR H ... while [n != 0 ] [ if nty > 0 [createTortoise set nty nty - 1 set n n - 1] if nfy > 0 [createFreeRider set nfy nfy - 1 set n n - 1] if nhy > 0 [createHare set nhy nhy - 1 set n n - 1] ]endto createHare create-custom-hare 1 [ set color grey set xcor -1 * screen-edge-x set ycor 0 ; start all bikers at 0,0 set heading 90 set eave 1 + random energy_up_bound set est 0 set pt 0 set vf vfMultiplier * eave set move 0 set d_move 0 set prevPt 0 set toFollow 0 set rank 0 ] end to createFreeRider create-custom-freeRider 1 [ set color pink set xcor -1 * screen-edge-x set ycor 0 ; start all bikers at 0,0 set heading 90 set eave 1 + random energy_up_bound set est 0 set pt 0 set vf vfMultiplier * eave set move 0 set d_move 0 set prevPt 0 set toFollow 0 set rank 0 ] end to createTortoise create-custom-tortoise 1 [ set color green set xcor -1 * screen-edge-x set ycor 0 ; start all bikers at 0,0 set heading 90 set eave 1 + random energy_up_bound set est 0 set pt 0 set vf vfMultiplier * eave set move 0 set d_move 0 set prevPt 0 set toFollow 0 set rank 0 ]end;;;;;; GO AND HELPERS;;;to go ;; set ticks ticks + 1 ask turtles [ set label who] ;Note on Space/Coordinates - the race is run on the integer space [- screen-edge-x, ..., 0] ;from left to right, with 0 as the finish line. ;Move methods ask turtles [ if breed = tortoise [ without-interruption [ set prevPt xcor ifelse -1 * prevPt >= tortVfEndGame * vf ;; end game / switch to hare - sprint (use all est) if end is in sight (max of x*vf) [set d_move xcor + eave + est set xcor d_move while [any? other-turtles-here] [ bk 1 set k k + 1] ; move back spaces until you find an empty space if k > eave [set xcor prevPt] ; don't go backward set k 0 ; reset i set move xcor - prevPt set est est - (move - eave) if any? turtles-at 1 0 [set est est + credFollow] if not any? turtles-at 1 0 and any? turtles-at -1 0 [set est est + lead * credFollow] ] ;; tortoise the agent doesn't see the finish line - normal move [set d_move xcor + eave ifelse vf < d_move - prevPt [set xcor prevPt + vf] [set xcor d_move] while [any? other-turtles-here] [ bk 1 set k k + 1] ; move back spaces until you find an empty space ;; the tortoise wastes stored energy earned by following (just don't use it) ;; may want to allow them to sprint in the end using any stored energy ;; update prevPt: if k > eave [set xcor prevPt] ; don't go backward set k 0 ;; accumulate est for the endgame if any? turtles-at 1 0 [set est est + credFollow] if not any? turtles-at 1 0 and any? turtles-at -1 0 [set est est + lead ] ] ;check if crossed finish line if xcor > 0 [set numFinish numFinish + 1 set rank numFinish set final_rank_t final_rank_t + rank ;increment the total breed rank if rank = 1 [set theWinner tortoise] if xcor > 0 [die]] ]]] ask turtles [ if breed = hare [ without-interruption [ set prevPt xcor set d_move xcor + eave + est ifelse vf < d_move - prevPt [set xcor prevPt + vf] [set xcor d_move] while [any? other-turtles-here] [ bk 1 set k k + 1] ; move back spaces until you find an empty space if k > eave [set xcor prevPt] ; don't go backward set k 0 ; reset i set move xcor - prevPt set est est - (move - eave) if any? turtles-at 1 0 [set est est + credFollow] if not any? turtles-at 1 0 and any? turtles-at -1 0 [set est est + lead ] ;check if crossed finish line if xcor > 0 [set numFinish numFinish + 1 set rank numFinish set final_rank_h final_rank_h + rank ;increment the total breed rank if rank = 1 [set theWinner hare] if xcor > 0 [die]] ]]] ask turtles [ if breed = freeRider [ without-interruption [ ;; free-riders want to find the follower position in the furthest pack that they can reach/join ;; if there is no pack they move eave (tortoise/conservative move). set prevPt xcor set d_move prevPt + eave + est if vf < d_move - prevPt [set d_move prevPt + vf] ; can't move entire desired amount b/c out of visibility ;; is there a pack they can join? set toFollow 0 set i d_move + 1 while [toFollow = 0 ] [ ; while haven't found someone to follow if any? turtles-at i 0 [set toFollow 1 ] set i i - 1 ] ;; there is someone to follow (a pack) - move to the back of it as long as it's not behind you. ifelse (toFollow = 1 and i > prevPt) [set xcor d_move] [set xcor (prevPt + eave) ] while [any? other-turtles-here] [ bk 1 set k k + 1] ; move back spaces until you find an empty space if k > eave [set xcor prevPt] ; don't go backward set k 0 ; reset i set move xcor - prevPt set est est - (move - eave) if any? turtles-at 1 0 [set est est + credFollow] if not any? turtles-at 1 0 and any? turtles-at -1 0 [set est est + lead ] ;check if crossed finish line if xcor > 0 [set numFinish numFinish + 1 set rank numFinish set final_rank_f final_rank_f + rank ;increment the total breed rank if rank = 1 [set theWinner freeRider] if xcor > 0 [die]] ]]] if numFinish = nt + nh + nf [stop] ;Update Ranks update-ranks ;Activate Plots setup-plots update-plots ;End of Period Clean-up clean-upendto update-ranks ;Note: ask patches - the patches are scheduled for execution by row: ; left to right within each row, and starting with the top row. ; This means that the search starts at the "start line" of the race. locals [rank_counter] set rank_counter nt + nh + nf; ask patches with [any? tortoise-here] [set rank_counter rank_counter - 1 set total_rank_t total_rank_t + rank_counter] ask patches with [any? hare-here] [set rank_counter rank_counter - 1 set total_rank_h total_rank_h + rank_counter] ask patches with [any? freeRider-here] [set rank_counter rank_counter - 1 set total_rank_f total_rank_f + rank_counter]endto clean-up set total_rank_t 0 set total_rank_h 0 set total_rank_f 0end;;;;;; PLOT;;;to-report Winner_Breed report theWinnerendto-report avg_final_rank_t report (final_rank_t / nt )endto-report avg_final_rank_h report (final_rank_h / nh )endto-report avg_final_rank_f report (final_rank_f / nf )endto setup-plots set-current-plot "Breed Distribution" set-plot-x-range 0 screen-edge-x set-plot-y-range 0 (nt + nh + nf) set-current-plot "Average Rank per Breed" set-plot-x-range 0 screen-edge-x set-plot-y-range 0 (nt + nh + nf) endto update-plots update-breed-plot update-average_rank-plotend;; Plot of the n. of agents with each strategy (breed)to update-breed-plot set-current-plot "Breed Distribution" set-current-plot-pen "T" if nt > 0 [plot count turtles with [breed = tortoise] ] set-current-plot-pen "H" if nh > 0 [plot count turtles with [breed = hare] ] set-current-plot-pen "FR" if nt > 0 [plot count turtles with [breed = freeRider] ]end;Plot of average rank of agents in the race spaceto update-average_rank-plot set-current-plot "Average Rank per Breed" set-current-plot-pen "T" if nt > 0 [ plot ( total_rank_t / nt ) ] set-current-plot-pen "H" if nh > 0 [ plot ( total_rank_h / nh ) ] set-current-plot-pen "FR" if nf > 0 [ plot ( total_rank_f / nf ) ]end;This attempt of a scatter plot of individual ranks isn't workingto update-individual_rank-plot set-current-plot "Individual Ranks" ;set-plot-pen-mode pen-mode ;set-plot-pen-interval bar-width ;set-plot-pen-color breed-a-bar-color Ask turtles [ if breed = tortoise [ set-current-plot-pen "T" set-plot-pen-color green plot rank] if breed = hare [ set-current-plot-pen "H" set-plot-pen-color gray plot rank] if breed = freeRider [ set-current-plot-pen "FR" set-plot-pen-color red plot rank] ]end@#$#@#$#@GRAPHICS-WINDOW4
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -