📄 antspace34.nlogo
字号:
globals [distanceTraveled screenArea numAnts numDead clock tanhVal homeX root2 maxPher colonyFood numPatches amtFood antsHome amtFoodToPlot rand3 showPhero? ]breeds [food ants ]patches-own [foodRandVar food? foodVar totalFood pheromone]ants-own [ home? leftYet? carrying-food? whenToLeave goingOut? move? C-inv test1 test2 ]food-own [foodQuality];;;;;;;;;;;SETUP PROCEDURES;;;;;;;;;;;;;to Setup ca set-default-shape ants "ant" set screenArea (screen-edge-x * screen-edge-y * 4) set showPhero? true set numDead 0 set root2 1.41421356 ;; This is the distance that ants move along the diagonal of a ;; square (makes the world a diamond shape) set colonyFood 0 ;; the amount of food that ants have stored in their nest set clock 0 ask patch-at nestX nestY [set pcolor pink] SetupFood SetupAnts SetupPlotsendto SetupFood locals [rand] set amtFood (screenArea * (%foodDensity / 100)) ;; %foodDensity is a variable meaning percentage ;; of screenArea that contains food, maximum is 100. create-food (amtFood) ask food [ set foodQuality foodConcentration ; indicate the amount of food that every site contains if the ;; site contains food set amtFood (amtFood + foodQuality) ] ask food [ set rand random-float 100 ifelse rand <= 25 ;; distribute the food agents evenly around the NetLogo world [ set xcor (random-float screen-size-x) set ycor (random-float screen-size-y) ] [ ifelse (rand > 25) and (rand <= 50) [ set xcor (random-float screen-size-x) * -1 set ycor (random-float screen-size-y) ] [ ifelse (rand > 50) and (rand <= 75) [ set xcor (random-float screen-size-x) * -1 set Ycor (random-float screen-size-y) * -1 ] [ set xcor (random-float screen-size-x) set Ycor (random-float screen-size-y) * -1 ] ] ] set color green ;; set food green color ]endto cloneFood ask food [ hatch (foodConcentration - 1) [set color green ] ;; Makes sure all patches are same color ]endto SetupAnts create-custom-ants antsNumber ;; antsPerStep ;; Creates "antsPerStep" new ants at Home position [ set carrying-food? false set heading 0 setxy nestX nestY set move? false set home? True set goingOut? True set leftYet? false set color red ] end;;;;;;;;;;;;;;;;;;;;;;;;;Go Procedures;;;;;;;;;;;;;;;;;;;;;;to Go MoveAnts UpdatePatches set clock (clock + 1) PlotAreaCovered PlotWidth endto UpdatePatches ask patch-at nestX nestY [ set pcolor pink ] ask patches [ if (pheromone < 0) [set pheromone 0] if (pheromone > 0) [ set pheromone (pheromone - pheromone * evaporation-rate) ifelse showPhero? [ set pcolor scale-color yellow pheromone 0 (maxPheroPerSite-Return)] ;;;;;;;Scale color of pheromone ants laid between 0 and maxPheroPerSite-Return, ;;;;;;;when ants return to nest, they don't lay pheromone in a visited site ;;;;;;;if pheromone > = maxPheroPerSite-Return [ set pcolor black ] ] ]endto MoveAnts ;;; Ants Procedures locals [ xPos yPos xHome yHome ] ask ants [ ifelse home? [atHome] [ ifelse (abs(xcor) > screen-edge-x - 1.5) or (abs(ycor) > screen-edge-y - 1.5) ;; Kills ants that fall over the edge [ die print "died!" set numDead (numDead + 1)] [ifelse carrying-food? ;If an ant finds food, it returns to nest. [ set color blue if distancexy-nowrap nestX nestY < 5 ;; Check whether an ant has arrived home [ setxy nestX nestY set home? true ] return-to-nest ;; Else, decide where to move to next ] [ look-for-food ] ] ] ]endto look-for-food ;;Ant Procedure ifelse (count food-here > 0) ;; If ant finds food [ set amtFood amtFood - 1 ask food-here [ set foodQuality (foodQuality - 1) if foodQuality < 1 [ die ] ] set carrying-food? true ;; pick up food set heading 180 ;; and turn around return-to-nest ;;Start heading home ] ;; If ant doesn't find food: [ chooseNextPatch ] ;; End of 'ifelse' statementendto chooseNextPatch ;;Ant decides which grid-point to go to next locals [x sumOfProbs localRand scent-left scent-right tanhValue P_m ltProb rtProb rand2] rt 45 set scent-right pher-conc lt 90 set scent-left pher-conc rt 45 ;; Ant decides whether to move: set x ( ( ( scent-left + scent-right ) / 100 ) - 1 ) if (x > 500) ;; e^>500 gives an error because the number is too big [set x 500] set tanhValue ( (exp(x) - exp(- x)) / (exp(x) - exp(- x) ) ) set P_m (1 + tanhValue) / 2 set localRand ((random-float 100) / 100) ifelse (localRand < P_m) [ set move? true layPheromone ] ;; If an ant decides to move, it lays pheromone. [ set move? false] if (move?) ;; If the ant decides to move, calculate which patch it will move to and move it: [ set C-inv ( 1 / ( (k + scent-left) ^ n + (k + scent-right) ^ n ) ) set ltProb (C-inv * (k + scent-left) ^ n ) set rtProb (C-inv * (k + scent-right) ^ n ) set sumOfProbs ltProb + rtProb set ltProb (ltProb / sumOfProbs) set rtProb (rtProb / sumOfProbs) set rand2 ((random-float 100) / 100) ifelse (rand2 < ltProb) ;; If left is the chosen direction: [ set heading 0 lt 45 ;; Check if square is full: ifelse (count turtles-at dx dy > numAntsPerPatch) [ rt 90 ifelse (count turtles-at dx dy > numAntsPerPatch) [ fd 0] [ jump root2 set heading 0 ] ] [ jump root2 set heading 0] ] ;; Otherwise go to front right patch [ set heading 0 ;; Point ant in the right direction rt 45 ;; Check if square is full: ifelse (count turtles-at dx dy > numAntsPerPatch) [ lt 90 ifelse (count turtles-at dx dy > numAntsPerPatch) [ jump 0] [ jump root2 set heading 0 ] ] [ jump root2 set heading 0] ;; Point ant back 'north' ] ] ;; end of "if Move?" set move? false ;; This assures that the ants have to make the move? decision again on the next loopendto return-to-nest locals [newX oldX scent-left scent-right tanhValue P_m ltProb rtProb frst second whichs rand2 sumOfProbs localRandom ] set heading 180 set heading 225 ;; Turn to Front, Right node (node C) set scent-right pher-conc set heading 135 set scent-left pher-conc set heading 180 set oldX ( ( ( scent-left + scent-right ) / 100 ) - 1 ) if (oldX > 500) ;; e^>500 gives an error because the number is too big; note that this does change the shape of the response curve [set oldX 500] set newX exp(oldX) set tanhValue ( (newX - (1 / newX) ) / (newX + (1 / newX) ) ) set P_m (1 + tanhValue) / 2 set localRandom ((random-float 100) / 100) if (localRandom < P_m) [ set move? true layPheromone ;; If ant decides to move, it lays pheromone. ] if (move?) ;; If the ant decides to move, calculate which direction to choose. [ set C-inv ( 1 / ( (k + scent-left) ^ n + (k + scent-right) ^ n ) ) ;; (This is the inverse of the C term in the Sole equation) set ltProb (C-inv * (k + scent-left) ^ n ) set rtProb (C-inv * (k + scent-right) ^ n ) if scent-left < (amtPheroLay-Forage * .5) ;;make sure returning ants always trail pheromone and don't go to any site without pheromone [ set ltProb 0 ] if scent-right < (amtPheroLay-Forage * .5) [ set rtProb 0 ] set sumOfProbs ( ltProb + rtProb ) ifelse (sumOfProbs = 0) and (xCor < 0) ;; If an ant is to the west of nest, it moves east [ set heading 135 jump root2 set heading 180 set move? false ] ;;'move?' is set to false so ant doesn't move again in regular move loop after this one [ ifelse (sumOfProbs = 0) and (xCor > 0) ;; No pheromone with ant to East of colony, ant moves west [ set heading 225 jump root2 set heading 180 set move? false ] [ ifelse (sumOfProbs = 0) ;; No pheromone and an ant is to the right north of nest, it moves south [ set heading 180 jump 1 set move? false] [ set ltProb (ltProb / sumOfProbs) ;;If there is pheromone somewhere, calculate probabilities set rtProb (rtProb / sumOfProbs)] ] ];; end "ifelse (sumofProbs = 0)" set localRandom ((random-float 100) / 100) if move? ;; This If statement is so the ants that just moved above won't move again [ ifelse (localRandom < ltProb) ;; If left is the chosen direction, move southeast. [ set heading 135 ;; Check if square is full: ifelse (count turtles-at dx dy > numAntsPerPatch) [ ;; If full, check other patch, if that's full, don't move set heading 225 ifelse (count turtles-at dx dy > numAntsPerPatch) [ fd 0 ] [ jump root2 set heading 180 ] ] [ jump root2 set heading 180] ] ;; Otherwise choose rtProb and go to southwest [ set heading 225 ;; Point ant in the right direction ;; Check if square is full: ifelse (count turtles-at dx dy > numAntsPerPatch) [ ;; If full, check other patch, if that's full, don't move set heading 135 ifelse (count turtles-at dx dy > numAntsPerPatch) [ jump 0 ] [ jump root2 set heading 180 ] ] [ jump root2 set heading 180] ] ] ;; end of 'ifMove' (second one) ] ;; end of "if Move?" (first one) set move? false ;; This assures that the ants have to make the "move?" decision again on the next loopendto LayPheromone ifelse (goingOut?) ;; If ant is going out Increase 'pheromone' and move forward [ if (pheromone < maxPheroPerSite-Forage) [set pheromone (pheromone + amtPheroLay-Forage)] ] [ ;; Else, increae Pheromone with 'returning' parameters if (pheromone < maxPheroPerSite-Return) [set pheromone (pheromone + amtPheroLay-Return)] ;; '10' is the amt that Deneubourg et al use for returning ants ]endto atHome set color red set home? false if carrying-Food? [ set carrying-Food? false set colonyFood (colonyFood + 1) ] set heading 0 set leftYet? trueend ;;;;;;;;;;;;;;;;;;;;;;Display Procedures;;;;;;;;;;;;;;;;;;;;;to showFood ask food [showturtle]endto showPhero set showPhero? trueendto clear ask food [hideturtle] set showPhero? falseend;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Plotting;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;to SetupPlots set-current-plot "% area covered" clear-plot set-plot-pen-color blue set-plot-y-range 0 15 set-plot-x-range 0 300 auto-plot-on set-current-plot "Foraging-Width" clear-plot set-plot-pen-color blue set-plot-y-range 0 20 set-plot-x-range 0 350 auto-plot-on endto PlotAreaCovered locals [ amtOfPheromone] set-current-plot "% area covered" ppd set-plot-pen-color blue if pheromone-amt > 0 [ plot (pheromone-amt / screenArea) * 100 ]end to PlotWidth set-current-plot "Foraging-Width" ppd set-plot-pen-color blue plot Foraging-Widthend;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Reporters;;;;;;;;;;;;;;;;;;;;;;;;;;;;;to-report pher-conc ;; reports what the strength of nest-scent right in front of the ant is report pheromone-of patch-at dx dyendto-report pheromone-amt locals [countPher] set countPher 0 ask patches [ if pheromone > 0 [ set countPher countPher + 1 ] ] report countPherendto-report max-distance report abs((max values-from ants [yCor] ) - nestY)endto-report Foraging-Width report (max values-from ants [xCor] + abs(min values-from ants [xCor]) )end@#$#@#$#@GRAPHICS-WINDOW467-3113873010
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -