%%%--ants.nlogo

来自「NETLOGO」· NLOGO 代码 · 共 183 行

NLOGO
183
字号
patches-own [pheromone nest_pheromone is_empty? is_obstacle?]; is_empty? indicates whether the patch is not part of the obstacles, nest, or food sources.; is_obstacle? indicates whether the patch is part of the obstacles.turtles-own [with_food? amount]to setup    ca    crt number    ask turtles [turtle-setup]    ask patches [patch-setup]endto turtle-setup    setxy -30 0     set color yellow    set amount 10    set with_food? false    set shape "ant"endto patch-setup    set pcolor black     set pheromone 0     set nest_pheromone 0    set is_empty? true     set is_obstacle? false ;set up ant nest    if (distancexy -30 0) < 3 [set pcolor blue set is_empty? false];set up food source    if (distancexy 30 0) < 3 [set pcolor green set is_empty? false];set up obstacles    if (pxcor <= 5 and pxcor >= -5 and pycor >= -20 and pycor <=  10)         [set pcolor red set is_empty? false set is_obstacle? true]    if ((abs pxcor) = screen-edge-x)          [set pcolor red set is_empty? false set is_obstacle? true]endto go    ask turtles [move drop-food pickup-food reset-pheromone]    diffuse-pheromone    evaporate    ask patches [if is_empty?  [set pcolor scale-color white pheromone 0 3.0]]endto move    ifelse with_food? [return-nest][search-food]endto drop-food    if (pcolor = blue and with_food?) [set with_food? false set color yellow]endto pickup-food    if (pcolor = green and not with_food?) [set with_food? true set color lime set amount 10]    ; when an ant pick up a food, the intitial pheromone is set to be 10endto reset-pheromone   ifelse with_food?         [set pheromone pheromone + 0.1 * amount]        [set nest_pheromone nest_pheromone + 0.1 * amount]   ifelse (pcolor = blue) [set amount 10][set amount 0.9 * amount]   ; if the ants are in the nest, the nest_pheromone is recharged;    ; otherwise the amount is reduced by 10%.endto return-nest    locals [angle]    if nest_pheromone > 0.001    [        set angle uphill nest_pheromone - heading        ;make the angle to be between -180 and 180.        if (angle > 180) [set angle angle - 360]        if (angle < -180) [set angle angle + 360]        if (angle > 45) [rt 45]        if (angle < -45) [lt 45]        if (angle < 45 and angle > -45) [set heading heading + angle]    ]    wiggleendto search-food    locals [angle]    if pheromone > 0.001    [        set angle uphill pheromone - heading        if (angle > 180) [set angle angle - 360]        if (angle < -180) [set angle angle + 360]        if (angle > 45) [rt 45]        if (angle < -45) [lt 45]        if (angle < 45 and angle > -45) [set heading heading + angle]    ]    wiggleendto wiggle    rt random 50    lt random 50     if (pcolor-of patch-at dx dy) = red  [rt 180]    ;if the patch color in front is red, the ant turns back    fd 1endto diffuse-pheromone    diffuse nest_pheromone 0.5    diffuse pheromone 0.5    ask patches [if is_obstacle? [set pheromone 0 set nest_pheromone 0]]endto evaporate    ask patches [set pheromone pheromone * 0.99]    ask patches [set nest_pheromone nest_pheromone * 0.99]end@#$#@#$#@GRAPHICS-WINDOW2561083659035358.16901408450704211000CC-WINDOW2422250586Command CenterBUTTON7168152101setupsetupNIL1TOBSERVERBUTTON71123152156gogoT1TOBSERVERSLIDER62204234237numbernumber101000500101NIL@#$#@#$#@Here we consider a simulation爄n which ants move around looking for food. Upon collecting food, the ant leaves a trail of pheromone for others to follow. The simulation shows how the ants, collectively, solve the problem of shortest path from the nest to the food source.Notes on the simulation:Color schemes: The nest is labeled in blue, the food source in green, and the obstacles in red. The ants without food are in yellow, while the ants that carry foods are in lime color. The pheromones deposited from the ants carrying foods are in white (the intensity is scaled according to the amount of pheromone available on the patch).Two types of pheromones: The ants that do not carry food deposit nest-pheromone to guide food-carrying ants back to the nest, while the ants that carry foods deposit pheromone to guide other ants to the food source. The amount of pheromone an ant deposits to the patch is decreasing as the ant wanders around. The amount of pheromone is reset to the maximum amount when the ant returns to the nest or when it hits the food source.Basic moves of the ants:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?