!!!---wolfmoose.nlogo
来自「NETLOGO」· NLOGO 代码 · 共 397 行
NLOGO
397 行
breeds [wolves moose] wolves-own [packmates nearest-wolf]globals [surrounded steps patches-colored]to setup ca set patches-colored false set-default-shape wolves "wolf-face" set-default-shape moose "moose-face" set surrounded 0 set steps 0 create-custom-wolves population [ set color yellow - 2 + random 7 ;; random shades look nice setxy (random-float screen-size-x) (random-float screen-size-y) rt random-float 360 ] ask wolves [set packmates wolves with [self != myself]] create-custom-moose 1 [ set color blue setxy (random-float screen-size-x) (random-float screen-size-y) rt random-float 360 ] set-plot-pen-interval 1 auto-plot-onendto color-patches ;; patch procedureifelse patches-colored [ask patches [set pcolor 0]] [ask patches [ifelse (pxcor + pycor) mod 2 = 1 [set pcolor 0] [set pcolor 4]]] set patches-colored not patches-coloredendto go ask moose [ move-moose ] plot surrounded if surrounded = guards-needed [stop] ask wolves [ move-wolf ] set steps steps + 1 wait delay / 20.endto move-moose ;; turtle procedure set surrounded count neighbors with [count wolves-here > 0 ] if surrounded >= guards-needed [stop] turn-towards towards min-one-of wolves [distance myself] + 180 fd random-float moose-speedendto move-wolflocals [goal-vector] ;; This is a vector of two elements: <length, bearing> set nearest-wolf min-one-of packmates [distance myself] set goal-vector vector-add list distance one-of moose towards one-of moose list (repulsion * distance nearest-wolf) (180 + towards nearest-wolf) turn-towards last goal-vector + random wolf-noise fd random-float wolf-speedend;;; HELPER PROCEDURESto-report vector-add [v1 v2] ;; a vector is a list of two elements: length and angle locals [x-sum y-sum] set x-sum ((first v1) * sin (last v1)) + ((first v2) * sin (last v2)) set y-sum ((first v1) * cos (last v1)) + ((first v2) * cos (last v2)) report list (sqrt ((x-sum * x-sum) + (y-sum * y-sum))) atan x-sum y-sumendto turn-towards [new-heading ] ;; turtle procedure rt (subtract-headings new-heading heading) endto turn-away [new-heading ] ;; turtle procedure rt (subtract-headings heading new-heading) end;; To find the difference between two headings, we can't just;; subtract the numbers, because 0 and 360 are the same heading.;; For example, the difference between a heading of 5 degrees;; and a heading of 355 degrees is 10 degrees, not 350 degrees.to-report subtract-headings [h1 h2] ifelse abs (h1 - h2) <= 180 [ report h1 - h2 ] [ ifelse h1 > h2 [ report h1 - h2 - 360 ] [ report h1 - h2 + 360 ] ]end; *** NetLogo Model Copyright Notice ***;; Portions of this model were borrowed from the "flocking" model copyright ; 1998 by Uri Wilensky. All Rights Reserved. In its present form the model; is Copyright 2004 by H. Van Dyke Parunak. 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 and Van Parunak.; Contact the authors for appropriate licenses for redistribution for; profit.;; To refer to this model in academic publications, please use:; H.V.D. Parunak. Wolf-Moose Capture Model. ; http://www.erim.org/~vparunak/models/WolfMoose.nlogo . Altarum Institute, ; Ann Arbor, MI, 2003.; ; *** End of NetLogo Model Copyright Notice ***@#$#@#$#@GRAPHICS-WINDOW33511975672101030.0110111CC-WINDOW6464250546Command CenterBUTTON6126245NILsetupNIL1TOBSERVERTBUTTON711312646RungoT1TOBSERVERTSLIDER988186121populationpopulation2201011NILSLIDER6218233251repulsionrepulsion0.02.00.70.11NILSLIDER9135232168wolf-speedwolf-speed0103.50.51patchesSLIDER9169232202moose-speedmoose-speed0102.50.51patchesBUTTON1521420747StepgoNIL1TOBSERVERTSLIDER6256178289wolf-noisewolf-noise01801021NILMONITOR254133311182NILsteps01SLIDER19987330120guards-neededguards-needed18611NILPLOT23300311450guardsstepsoccupied0.0100.00.04.0truefalsePENS"nbhd-size" 1.0 0 -65536 falseMONITOR251194329243NILsurrounded01BUTTON2461432647NILcolor-patchesNIL1TOBSERVERTSLIDER95318586delaydelay010211NIL@#$#@#$#@WHAT IS IT?-----------Wolves are able to catch moose, deer, and other animals much larger and more powerful than an individual wolf. They overcome their prey by surrounding them. The wolves in front of the prey keep its attention by menacing it, enabling one of the wolves behind the prey to jump on its back and bring it down. How do the wolves coordinate their actions so as to surround the prey? This problem was introduced to the Distributed AI research community in the late 1980's by Miro Benda, and stimulated a series of studies that assumed wolves could communicate with one another and exchange beliefs and intentions. These unrealistic assumptions were shown to be unnecessary by Korf in 1991 [1] (summarized in [3]). He showed that if the prey always moves away from the nearest wolf, and if each wolf moves toward the prey and away from the wolf nearest to it, the wolves will always catch the prey on a hexagonal lattice, as long as the prey moves slower than the wolves do.This model recreates Korf's solution. Unfortunately, NetLogo does not give an easy way to work with hexagonal places, but the basic dynamics still work in spite of the anisotropies of the square place lattice. The prey is considered surrounded when a specified number (guards-needed) of the cells in its 8-neighborhood are occupied by wolves.HOW TO USE IT-------------Buttons:setup.--Creates wolves and the moose.Run.--Runs the model in a continuous loop.Step.--Runs one step of the model.color-patches.--Toggles coloring of the individual patches so that you can see which patches are occupied by wolves and the moose.Variables:delay.--Slows down the simulation so that you can observe the movement of the wolves and moose more clearly.population.--The number of wolves.guards-needed.--The number of cells in the moose's 8-neighborhood that the wolves must occupy to capture the moose. When this condition is satisfied, the model stops execution.wolf-speed.--The maximum speed, in places per time step, of a wolf. The actual distance the wolf moves is selected randomly with this as the upper limit. Thus a wolf's average speed will be half of this value.moose-speed.--The maximum speed, in places per time step, of the moose. The actual distance the moose moves is selected randomly with this as the upper limit. Thus the moose's average speed will be half of this value.repulsion.--How much a wolf is repelled by its nearest neighboring wolf, compared with its attraction to the moose. wolf-noise.--The upper limit of a random angle to add to each wolf's final direction when it moves, useful to keep the wolves from aligning themselves.Reporters:steps.--The current number of steps through which the model has run.surrounded.--The current number of places in the moose's 8-neighborhood that are occupied by wolves. When surrounded = guards-needed, the model will stop.guards.--A plot of surrounded over time.First, determine the wolf population and the number of guards needed to capture the moose. (It helps if the wolf population is at least as large as the required number of guards.) Press setup.Set the relative speed of wolves and moose, and press Run or Step to watch them move.When the wolves manage to surround the moose, toggle patch coloring on with the "color-patches" button to verify that they have indeed occupied the required number of neighboring patches.THINGS TO NOTICE----------------If the moose is faster than the wolves, it tends to stay on the edge of the wolf population and can evade it. If the moose is slower than the wolves, they quickly surround it. The model then goes through three phases:* The Chase, in which the wolves are getting themselves distributed around the moose;* Herding, in which the mass of wolves with the moose at their center jostles around until the wolves manage to occupy the required number of places;* The Kill, when surrounded = guards-needed.THINGS TO TRY-------------Experiment with different settings of the parameters, and try to correlate them with the number of steps it takes the wolves to surround the moose.Pay particular attention to the effect of the wolf-noise parameter. What is it about the basic rules that makes a little noise helpful? What happens if this parameter is too high? What is the "right amount" of wolf noise?Similarly, what is the "right amount" of repulsion between wolves? Can the wolves dislike one another so much that they would rather disperse than catch dinner?EXTENDING THE MODEL-------------------Add the ability to have more than one moose. How do the wolves' decision rules have to change to enable the pack to harvest the most moose?This simple model does not observe the physical constraint that two things cannot occupy the same space at the same time. In particular, wolves and the moose can pass through each other. Add code to prevent a wolf or moose from moving to or through a place if it is already occupied. How does this exclusion principle affect the behavior of the model? Does the wolves' ability to block the moose's movements shorten the herding time? Or does the restriction on the wolves' ability to move around extend it?Adjust the wolf-noise and repulsion parameters dynamically as the system runs. For example, when wolves are far from the moose, these parameters might be lower to enable the wolves to get near the moose as quickly as possible. As they draw nearer to the moose, it becomes more important for them to spread out in order to surround the moose. Compare the behavior of the adaptive model with the model with static parameters.NETLOGO FEATURES----------------This model uses breeds to distinguish the wolves and the moose.I had to implement my own vector addition function to combine wolf-wolf repulsion with wolf-moose attraction. Did I miss a NetLogo primitive here? Is this a candidate for a new primitive?CREDITS AND REFERENCES----------------------This model is based on the Flocking model in the NetLogo distribution. The algorithm is that proposed in ref. [1]. Ref [2] offers discussion on the limitations of this model.[1] R. E. Korf. A Simple Solution to Pursuit Games. In Proceedings of Eleventh International Workshop on Distributed Artificial Intelligence, pages 183-194, 1992. [2] M. Manela and J. A. Campbell. Designing Good Pursuit Problems as Testbeds for Distributed AI: A Novel Application of Genetic Algorithms. In Proceedings of 5th European Workshop on Modeling Autonomous Agents in a Multi-Agent World, MAAMAW
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?