📄 ---antsopt(1).nlogo
字号:
patches-own [ground pheromone visited]turtles-own [cost path pathdir]globals [n-of-turtles latest-cost currentmin currentmax pathlengths]to setupask turtles [die]ask patches [set ground rand-from-to -3 4]repeat 10 [diffuse ground 0.3]ask patches [ set ground crude ground 0 1]clearclearendto clearask patches with [ground = 0] [set pcolor blue - 1]ask patches with [ground = 1] [set pcolor brown]ask patches with [ground = 2] [set pcolor green - 2]ask patches [set pheromone [1 1 1 1]]ask patch-at screen-edge-x ((-1) * screen-edge-y) [set pcolor red]ask patch-at ((-1) * screen-edge-x) screen-edge-y [set pcolor green]ask patches with [pxcor = ((-1) * screen-edge-x)] [set pheromone nullify pheromone 3]ask patches with [pxcor = (screen-edge-x)] [set pheromone nullify pheromone 1]ask patches with [pycor = ((-1) * screen-edge-y)] [set pheromone nullify pheromone 2]ask patches with [pycor = (screen-edge-y)] [set pheromone nullify pheromone 0]ask patches [if (value-from patch-at 0 1 [ground]) = 0 [set pheromone nullify pheromone 0]]ask patches [if (value-from patch-at 1 0 [ground]) = 0 [set pheromone nullify pheromone 1]]ask patches [if (value-from patch-at 0 -1 [ground]) = 0 [set pheromone nullify pheromone 2]]ask patches [if (value-from patch-at -1 0 [ground]) = 0 [set pheromone nullify pheromone 3]]set n-of-turtles 0set latest-cost 1000000set currentmin 1000000set currentmax 0ask turtles [die]set pathlengths []setup-plotdo-plotendto recolorask patches with [ground = 0] [set pcolor blue - 1]ask patches with [ground = 1] [set pcolor brown]ask patches with [ground = 2] [set pcolor green - 2]ask patch-at screen-edge-x ((-1) * screen-edge-y) [set pcolor red]ask patch-at ((-1) * screen-edge-x) screen-edge-y [set pcolor green]endto-report crude [val lo hi]if val < lo [report 0]if val > hi [report 2]report 1endto turtle-steplocals [rdir dirvec];set dirvec value-from patch-here [pheromone];show dirvec;if (value-from patch-at 0 1 [visited]) [set dirvec nullify dirvec 0] ;if (value-from patch-at 1 0 [visited]) [set dirvec nullify dirvec 1] ;if (value-from patch-at 0 -1 [visited]) [set dirvec nullify dirvec 2] ;if (value-from patch-at -1 0 [visited]) [set dirvec nullify dirvec 3] ;set heading random-dir dirvecset heading random-dir value-from patch-here [pheromone]set pathdir fput (int (heading / 90)) pathdirforward 1set cost (cost + value-from patch-here [ground])set path fput (list xcor ycor) path ask patch-here [ifelse ground = 1 [set pcolor turquoise - 1] [set pcolor turquoise - 2]]ask patch-here [set visited true]endto-report arrived?report (xcor >= (screen-edge-x - 0.1)) and (ycor <= (-1) * (screen-edge-y) + 0.1)endto walkset n-of-turtles n-of-turtles + 1set path fput (list ((-1) * screen-edge-x) screen-edge-y) []set pathdir []setxy ((-1) * screen-edge-x) screen-edge-ywhile [not arrived?] [turtle-step]endto single-walkcrt 1 ;ask patches [set visited false]ask turtles [walk]do-plotendto retracelocals [currind currcost localpathdir localpath];show length pathif remove-loops? [ set localpath remove-loops path set localpathdir stepdirs localpath set path localpath set pathdir localpathdir ]set path butfirst pathset currcost costset latest-cost costset currentmin min list latest-cost currentminset currentmax max list latest-cost currentmaxset pathlengths fput cost pathlengths;show length pathwhile [not empty? path] [ setxy first first path last first path set currind first pathdir; set currind first localpathdir ask patch-here [ ifelse ground = 1 [set pcolor yellow - 1] [set pcolor yellow - 2] set pheromone replace-item currind pheromone ((item currind pheromone) + (increment currcost))] set pathdir butfirst pathdir ; set localpathdir butfirst localpathdir set path butfirst path ]endto-report random-dir [weightvec]locals [randnum probvec]set probvec cumsum normalize weightvecset randnum random-int-or-float 1.0if randnum < (item 0 probvec) [report 0]if randnum < (item 1 probvec) [report 90]if randnum < (item 2 probvec) [report 180]report 270endto-report nullify [l index]if empty? l [report l] if index = 0 [report fput 0 butfirst l]report fput first l nullify butfirst l (index - 1)endto-report addlists [l1 l2]if empty? l1 [report l2]if empty? l2 [report l1]report fput ((first l1) + (first l2)) (addlists (butfirst l1) (butfirst l2))endto-report normalize [l]report scalmult (1 / (sum l)) lendto-report scalmult [factor l]if empty? l [report l]output fput (factor * first l) scalmult factor butfirst lendto-report scaladd [scalar l]if empty? l [report l]output fput (scalar + first l) scaladd scalar butfirst lendto-report vecadd [vec1 vec2]if empty? vec1 [report vec2]if empty? vec2 [report vec1]report fput ((first vec1) + (first vec2)) vecadd butfirst vec1 butfirst vec2endto-report cumsum [l]if empty? l [report l]report fput (first l) scaladd (first l) (cumsum butfirst l)endto runsrepeat n-of-runs [ full-turtle-run]endto full-turtle-runsingle-walkwait delay / 10recolorask turtles [retrace]ask turtles [show cost]wait delay / 10ask turtles [die]recolorendto-report rand-from-to [a b]report (random-int-or-float (b - a + 0.0)) + aendto-report increment [x]report (2000 ^ 3) / (x ^ 3)endto-report second [l]report first butfirst lendto-report stepdirs [coordlist]if (length coordlist = 1) [report []]output fput dir-from-step (list ((first first coordlist) - (first second coordlist)) ((second first coordlist) - (second second coordlist))) stepdirs butfirst coordlist endto-report dir-from-step [step]if ((first step) = 0) and ((second step) = 1) [report 0]if ((first step) = 1) and ((second step) = 0) [report 1]if ((first step) = 0) and ((second step) = -1) [report 2]if ((first step) = -1) and ((second step) = 0) [report 3]endto-report remove-loops [p] locals [loopitem initend reststart intermediate] if double-free? p [report p] set loopitem first-double p set initend position loopitem p set reststart lastpos loopitem p set intermediate sentence (segment p 0 initend) (segment p (1 + reststart) length p) report remove-loops intermediateendto-report first-double [p] if empty? p [report []] if member? (first p) (butfirst p) [report first p] report first-double butfirst pendto-report lastpos [el p] report ((length p) - position el reverse p) - 1 endto-report iseq [a b] report n-values ((b - a) + 1) [a + ?]endto-report segment [p start finish] report map [item ? p] iseq (ifelse-value (start < 0) [0] [start]) (ifelse-value (finish >= length p) [(length p) - 1] [finish]) endto-report double-free? [p] if empty? p [report true] if member? first p butfirst p [report false] output double-free? butfirst pendto-report cut-head [p num] if (num < 1) [report p] report cut-head butfirst p (num - 1)endto setup-plot set-current-plot "Path length" set-plot-x-range 0 10 * (screen-edge-x * screen-edge-y) set-plot-y-range 0 10 set-histogram-num-bars 10endto do-plot set-histogram-num-bars 10; set-plot-x-range 0 50 * (screen-edge-x * screen-edge-y) set-plot-x-range 0 histwidth set-plot-y-range 0 max list 10 n-of-turtles set-histogram-num-bars 20 histogram-list pathlengths ; using the default plot penend@#$#@#$#@GRAPHICS-WINDOW3211067222815811.0110111CC-WINDOW329295644415Command CenterBUTTON241010543NILsetupNIL1TOBSERVERTBUTTON2587106120NILrunsNIL1TOBSERVERTSLIDER11187283120n-of-runsn-of-runs1100111NILSWITCH255017583remove-loops?remove-loops?11-1000BUTTON11210193
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -