📄 ---netlogo.nlogo
字号:
breeds [ doves hawks ]turtles-own [ lifetime]hawks-own [ cruise-speed hunt-speed prey-sensing-radius energy metabolism]doves-own [ flockmates ;; agentset of nearby doves nearest-neighbor ;; closest one of our flockmates vision minimum-separation max-align-turn max-cohere-turn max-separate-turn normal-speed danger-sensing-radius escape-angle boost-speed]globals [ deaths ticks ];;;;;; SETUP ;;;;;;;;;;;;;;to setup ca cct-doves population [ if shade-of? red color [ set color white ] set shape "fish" setup-dove-genome ] cct-hawks h-population [ set color red set shape "spider" setup-hawk-genome set energy initial-energy set metabolism 1 ] ask turtles [ setxy (random-float screen-size-x) (random-float screen-size-y) rt random-float 360 set lifetime 0 ] set ticks 0endto setup-dove-genome set vision random-normal 3 .25 set minimum-separation random-normal 10 1 set max-align-turn random-normal 30 7 set max-cohere-turn random-normal 30 7 set max-separate-turn random-normal 30 7 set normal-speed random-normal 1 .2 set danger-sensing-radius random-normal 3 .3 set escape-angle random-normal 30 7 set boost-speed random-normal 1 .25endto setup-hawk-genome set cruise-speed random-normal 1 .2 set hunt-speed random-normal 2 .1 set prey-sensing-radius random-normal 4 .3end ;;;;;; MAIN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;to go set ticks ticks + 1 ask turtles [ set lifetime lifetime + 1 ] ask doves [ flock escape ] ask hawks [ hunt hawk-death ] do-plots endto do-plots set-current-plot "dove-life" set-current-plot-pen "mean" plot mean values-from doves [ lifetime ] set-current-plot-pen "min" plot min values-from doves [ lifetime ] set-current-plot-pen "max" plot max values-from doves [ lifetime ] set-current-plot "hawk-life" set-current-plot-pen "average" plot mean values-from hawks [ lifetime ] set-current-plot-pen "min" plot min values-from hawks [ lifetime ] set-current-plot-pen "max" plot max values-from hawks [ lifetime ]endto hawk-death set energy energy - metabolism if energy <= 0 [ let mean-lifetime mean values-from hawks [lifetime] ;ask random-one-of hawks with [ lifetime >= mean-lifetime] [ ; one of the elder reproduces ;ask max-one-of hawks [ lifetime ] [ ; THE oldest reproduces ask random-one-of hawks [ ; random reproduction hatch 1 [ set energy initial-energy set lifetime 0 lt random 360 mutate-hawk ] ] die ]end to mutate-hawk set cruise-speed abs (cruise-speed + plusminus cruise-speed-mrate) set hunt-speed abs (hunt-speed + plusminus hunt-speed-mrate) set prey-sensing-radius abs (prey-sensing-radius + plusminus prey-sensing-radius-mrate)end to mutate-dove set vision abs (vision + plusminus vision-mrate) set minimum-separation abs (minimum-separation + plusminus minimum-separation-mrate) set max-align-turn abs (max-align-turn + plusminus max-align-turn-mrate) set max-cohere-turn abs (max-cohere-turn + plusminus max-cohere-turn-mrate) set max-separate-turn abs (max-separate-turn + plusminus max-separate-turn-mrate) set normal-speed abs (normal-speed + plusminus normal-speed-mrate) set danger-sensing-radius abs (danger-sensing-radius + plusminus danger-sensing-radius-mrate) set escape-angle abs (escape-angle + plusminus escape-angle-mrate) set boost-speed abs (boost-speed + plusminus boost-speed-mrate)end; reports +/- xto-report plusminus [ x ] report ((random 2) * 2 - 1) * xend; reports a random number { x | lo <= x < hi }to-report random-range-float [ lo hi ] report random-float 1 * (hi - lo) + loend to escape let nearest-hawk min-one-of hawks in-radius danger-sensing-radius [ distance myself ] if (is-agent? nearest-hawk and distance nearest-hawk != 0) [ turn-away towards nearest-hawk escape-angle fd boost-speed ;* (1 - distance nearest-hawk / danger-sensing-radius) ] endto hunt without-interruption [ let nearest-dove min-one-of doves in-radius prey-sensing-radius [ distance myself ] ifelse (nobody != nearest-dove and distance nearest-dove != 0 and energy <= 15) [ set heading towards nearest-dove fd hunt-speed if distance nearest-dove <= .5 [ ; dove in eating range? set energy energy + 20 set deaths deaths + 1 ask nearest-dove [ die ] ask random-one-of doves [ ; spawn a new dove hatch 1 [ lt random 360 mutate-dove set lifetime 0 ] ] ] ][ ; if not hunting or hungry wiggle cruise-speed ] ]end to wiggle [ speed ] lt random 40 * 2 - 40 fd speedendto flock ;; dove procedure find-flockmates if any? flockmates [ find-nearest-neighbor ifelse distance nearest-neighbor < minimum-separation [ separate ] [ align cohere ] ] fd normal-speedendto find-flockmates ;; dove procedure set flockmates (doves in-radius vision) with [self != myself]endto find-nearest-neighbor ;; dove procedure set nearest-neighbor min-one-of flockmates [distance myself]end;;; SEPARATEto separate ;; dove procedure turn-away (heading-of nearest-neighbor) max-separate-turnend;;; ALIGNto align ;; dove procedure turn-towards average-flockmate-heading max-align-turnendto-report average-flockmate-heading ;; dove procedure ;; We can't just average the heading variables here. ;; For example, the average of 1 and 359 should be 0, ;; not 180. So we have to use trigonometry. ;; Theoretically this could fail if both sums are 0 ;; since atan 0 0 is undefined, but in practice that's ;; vanishingly unlikely. report atan sum values-from flockmates [sin heading] sum values-from flockmates [cos heading]end;;; COHEREto cohere ;; dove procedure turn-towards average-heading-towards-flockmates max-cohere-turnendto-report average-heading-towards-flockmates ;; dove procedure ;; "towards myself" gives us the heading from the other dove ;; to me, but we want the heading from me to the other dove, ;; so we add 180 report atan mean values-from flockmates [sin (towards myself + 180)] mean values-from flockmates [cos (towards myself + 180)]end;;; HELPER PROCEDURESto turn-towards [new-heading max-turn] ;; dove procedure turn-at-most (subtract-headings new-heading heading) max-turnendto turn-away [new-heading max-turn] ;; dove procedure turn-at-most (subtract-headings heading new-heading) max-turnend;; turn right by "turn" degrees (or left if "turn" is negative),;; but never turn more than "max-turn" degreesto turn-at-most [turn max-turn] ;; dove procedure ifelse abs turn > max-turn [ ifelse turn > 0 [ rt max-turn ] [ lt max-turn ] ] [ rt turn ]end; *** NetLogo Model Copyright Notice ***;; This model was originally created as part of the project: CONNECTED MATHEMATICS:; MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL; MODELS (OBPML). The project gratefully acknowledges the support of the; National Science Foundation (Applications of Advanced Technologies; Program) -- grant numbers RED #9552950 and REC #9632612.;; Copyright 1998 by Uri Wilensky. All rights reserved.;; Permission to use, modify or redistribute this model is hereby granted,; provided that both of the following requirements are followed:; a) this copyright notice is included.; b) this model will not be redistributed for profit without permission; from Uri Wilensky.; Contact Uri Wilensky for appropriate licenses for redistribution for; profit.;; This model was converted to NetLogo (and was changed in the process to; be an alternate version of the model) as part of the project:; PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN; CLASSROOMS. The project gratefully acknowledges the support of the; National Science Foundation (REPP program) -- grant number REC #9814682.; Converted from StarLogoT to NetLogo, 2002. Updated 2002.;; To refer to this model in academic publications, please use:; Wilensky, U. (1998). NetLogo Flocking model.; http://ccl.northwestern.edu/netlogo/models/Flocking.; Center for Connected Learning and Computer-Based Modeling,; Northwestern University, Evanston, IL.;; In other publications, please use:; Copyright 1998 by Uri Wilensky. All rights reserved. See; http://ccl.northwestern.edu/netlogo/models/Flocking; for terms of use.;; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW2091072354525259.91101110CC-WINDOW5646947741Command CenterBUTTON13149447NILsetupNIL1TOBSERVERTNILBUTTON961317746NILgoT1TOBSERVERTNILSLIDER125719290populationpopulation120010811NILSLIDER7381591348h-populationh-population02002111NILPLOT738414938564dove-lifeNILNIL0.010.00.010.0truefalsePENS"max" 1.0 0 -65536 true"mean" 1.0 0 -16777216 true"min" 1.0 0 -16776961 trueSLIDER1294184127vision-mratevision-mrate021.30.11NILSLIDER12130191163minimum-separation-mrateminimum-separation-mrate051.00.11NILSLIDER11169190202max-align-turn-mratemax-align-turn-mrate030111NILSLIDER11204190237max-cohere-turn-mratemax-cohere-turn-mrate030111NILSLIDER11240193273max-separate-turn-mratemax-separate-turn-mrate040111NILSLIDER11276189309normal-speed-mratenormal-speed-mrate020.00.11NILSLIDER12311185344danger-sensing-radius-mratedanger-sensing-radius-mrate050.30.11NILSLIDER11346183379escape-angle-mrateescape-angle-mrate0100111NILSLIDER11382183415boost-speed-mrateboost-speed-mrate060.00.11NILMONITOR1442467473visionmean values-from doves [ vision ]31MONITOR69424126473min-sep
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -