我要实现Netlogo的MMA类似物为何不行?还请高手帮忙指点。
mathematica吧
全部回复
仅看楼主
level 1
代码是海龟吃草,草也自己以3%的比率长。不用go[]就正常,一用go[]草就不更新了。咋办?
Manipulate[
If[going,(*go[*)turtles = moveTurtles[]; eatGrass[];
patches = regrowGrass[](*]*)]
; 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[*)turtles = moveTurtles[];
eatGrass[]; patches = regrowGrass[](*]*)]}, 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[]
}]
2017年01月14日 02点01分 1
level 1
找到毛病了:用()把go[]的定义括上即可。正确代码如下:
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[]
}]
2017年01月15日 01点01分 4
吧务
level 15
2017年02月04日 08点02分 5
1