公告:1)网站程序升级:Q2A升级到1.8.6,Wordpress升级到5.7.2
2)修复了头像加载慢与提交问题反应慢等问题
2021-06-16

欢迎来到 Mathematica 问答社区

提问时请贴上文本代码

语法高亮:在编辑器中点击

被禁止的话题:广告破解

请阅读:《提问的智慧》

备用域名:mma.ooo

支持LaTex数学公式
行内公式标识符:\$ 或“$\backslash ($”+“$\backslash )$”,
行间公式标识符:\$\$ 或 “$\backslash [$”+“$\backslash ]$”

社区建议QQ群:365716997

分类

0 投票
329 浏览

代码是海龟吃草,草也自己以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[]}]

 

分类:动态交互 | 用户: 绿叶海蜗牛 (21 分)
修改于 用户:xzczd
1. 我想多数人都不知道NetLogo是做什么的。2. 我想你目前的意图描述过分简洁了。3. 你是怎么“用go[]”的?如果是简单地去掉目前程序里关于go的注解,那么你对这个函数的使用显然是错的;如果是用go[]代替turtles = moveTurtles[]语句,那么,我似乎无法重现你的问题。
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[]
   }]

登录 或者 注册 后回答这个问题。

...