1.我想多数人都不知道NetLogo是做什么的。
----------------------------------------------
补充:netlogo是面向Agent编程的一种语言。
2. 我想你目前的意图描述过分简洁了。
------------------------------
提醒得对。
3.如果是用go[]代替turtles = moveTurtles[]语句,那么,我似乎无法重现你的问题。
-------------------------------------
go[]的完整定义在moveTurtles[]前面,共三句。
受你的启发,已找到毛病!用()把go[]的定义括上就行了,MMA把go[]看成两句了(跟不放在Initialization :> {...}里不一样,这点忽略了)。非常感谢你。
好了,大家可以试试。用MMA的强大功能编相应的Netlogo程序方便多了。
正确的程序如下:
Manipulate[If[going, go[]]; visualize[]
, {{maxPxcor, 20}, 1, 21(*51*), 1, ImageSize -> Tiny,
Appearance -> "Labeled"}
, {{maxPycor, 20}, 1, 21(*51*), 1, ImageSize -> Tiny,
Appearance -> "Labeled"}
, {{n, 3, "number of agents"}, 1, 100, 1, ImageSize -> Tiny,
Appearance -> "Labeled"}
, Delimiter
, Row[{Control[{{going, False, "Going"}, {False, True}}],
Button["GoNext", going = False; go[]]}, Spacer[10]]
, SynchronousUpdating -> True,
TrackedSymbols :> {going, patches, turtles}, ControlPlacement -> Left
, Initialization :> {minPxcor = minPycor = 1
, worldWidth = {minPxcor, maxPxcor} =
worldHeight = {minPycor, maxPycor}
, setupPatches[] := patches = Table[Green, {maxPxcor}, {maxPycor}]
, setupTurtles[n_] :=
turtles =
Table[{Hue[RandomReal[]], {RandomReal[worldWidth],
RandomReal[worldHeight]}}, {n}]
, setup[] := setupPatches[]; setupTurtles[n]
, go[] := (turtles = moveTurtles[]; eatGrass[];
patches = regrowGrass[])
, moveTurtles[] := Module[{angle},
Map[{#[[1]],
Clip[#[[2]] +(*noise*)
1 {angle = RandomReal[2 Pi]; Cos[angle], Sin[angle]},
worldWidth]} &, turtles]]
, eatGrass[] :=
Module[{x, y},
Scan[If[patches[[{x, y} = Floor /@ #[[2]]; x, y]] == Green,
patches[[x, y]] = Black, Null] &,
turtles]]
, visualize[] :=
Graphics[
Table[{patches[[x, y]], Rectangle[{x, y}]}, {x, maxPxcor}, {y,
maxPycor}]
~Join~{Arrowheads[.1],
Map[{#[[1]],
Arrow[{#[[2]], #[[2]] + RandomReal[{.01, .01}, 2]}]} &,
turtles]}
, ImageSize -> {350, 350}, AspectRatio -> Automatic,
Axes -> False, PlotRange -> {worldWidth, worldHeight}]
, regrowGrass[] :=
MapIndexed[
If[RandomInteger[{1, 100}] < 3, Green,
patches[[Sequence @@ #2]]] &, patches, {2}]
, setup[]
}]