公告:本站正式转型为非交互式静态网站!
转型:本站将通过笔记和博客的形式继续为大家服务,关于 Mathematica 问答服务请移步至QQ群:365716997
联系:如有问题请联系QQ群管理员,或发送邮件至:lixuan.xyz@qq.com。
感谢:最后非常感谢大家多年来的支持与帮助!
参考《互联网跟帖评论服务管理规定》《中华人民共和国网络安全法》《网络信息内容生态治理规定》《互联网用户账号信息管理规定》

—— 2022-11-27

欢迎来到 Mathematica 问答社区

提问时请贴上文本代码

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

被禁止的话题:广告破解

请阅读:《提问的智慧》

备用域名:mma.ooo

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

社区建议QQ群:365716997

分类

0 投票
1.2k 浏览

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

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

...