Netlogo代码问题,日/夜循环不起作用

时间:2017-09-07 20:19:35

标签: netlogo

我正在尝试创建一个复杂的环境,其中三种类型的树共存,您可以使用滑块修改每种类型的数字。我实施的问题是“白天/黑夜”周期。当它是夜晚时,颜色应该变暗,但是使用代码我会使颜色变暗并且永远不会变回更亮的颜色。我用藻类模型作为基础。

这是我的代码:

to setup
 clear-all
 setup-world
 reset-ticks
end

to setup-world
  ask n-of synchronic-tree-density patches [
    set pcolor blue
  ]
  ask n-of asynchronious-tree-density patches [
    set pcolor yellow
  ]
  ask n-of tree-patches patches [
    set pcolor green
  ]
  recolor-world true
end

to recolor-world
  ask patches [
    if pcolor = blue [
      ifelse setting-up? or daytime? [
        set pcolor blue 
        ] [
        set pcolor blue - 3
        ]
      ]

    if pcolor = yellow [
      ifelse setting-up? or daytime? [
        set pcolor yellow
        ] [
        set pcolor yellow - 3
        ]
      ]

    if pcolor = green [
      ifelse setting-up? or daytime? [
        set pcolor green
        ] [
        set pcolor green - 3
        ]
      ]
    ]
end

to go
  recolor-world false
  tick-advance 1
end

to-report daytime?
  report ticks mod 24 < day-length
end

2 个答案:

答案 0 :(得分:2)

卢克的回答解决了你的问题。但是,您可能还需要稍微简化代码,以便仅在每次滴答时评估daytime?的状态。例如:

to recolor-world
  if-else daytime? [
    ask patches with [ tree-type = 1 ] [ set pcolor blue ]
    ask patches with [ tree-type = 2 ] [ set pcolor yellow ]
    ask patches with [ tree-type = 3 ] [ set pcolor green]
  ] [
    ask patches with [ tree-type = 1 ] [ set pcolor blue - 3 ]
    ask patches with [ tree-type = 2 ] [ set pcolor yellow - 3 ]
    ask patches with [ tree-type = 3 ] [ set pcolor green - 3 ]
  ]
end

答案 1 :(得分:1)

欢迎使用Stack Overflow。有关一些提示提示,请查看MCVE guidelines。理想情况下,您的问题代码可以简化为其他用户运行程序所需的内容 - 目标是他们可以按原样复制代码。现在,如果没有相当多的修改,我就无法运行您的程序 - 我不确定我的解决方案是否适用于您的设置。如果简化代码,则更有可能获得有用的答案。

那就是说,我很确定你的问题来自你recolor-world程序中所有if语句的事实。考虑一下这个:

if pcolor = green [
  ifelse daytime? [
    set pcolor green
  ] [
    set pcolor green - 3
  ]
]

所以在第一次复飞时,你确实有一些绿色补丁,因为它们是在setup程序中设置的。但是,一旦daytime?变为false,这些补丁就会运行命令set pcolor green - 3,因此他们不再将if pcolor = green评估为真 - 他们将永远不再运行该代码块。我认为最简单的解决方法是使用除颜色之外的分组变量进行过滤:

patches-own [ tree-type ]

to setup
  clear-all
  reset-ticks
  setup-world
end

to setup-world
  ask n-of 50 patches [
    set pcolor blue
    set tree-type 1
  ]
  ask n-of 50 patches [
    set pcolor yellow
    set tree-type 2
  ]
  ask n-of 50 patches [
    set pcolor green
    set tree-type 3
  ]

end

to recolor-world
  ask patches with [ tree-type = 1 ] [
    ifelse daytime? [
      set pcolor blue 
    ] [
      set pcolor blue - 3
    ]
  ]

  ask patches with [ tree-type = 2 ] [
    ifelse daytime? [
      set pcolor yellow
    ] [
      set pcolor yellow - 3
    ]
  ]

  ask patches with [ tree-type = 3 ] [
    ifelse daytime? [
      set pcolor green
    ] [
      set pcolor green - 3
    ]
  ]

end

to go
  recolor-world
  tick
end

to-report daytime?
  report ticks mod 24 < 12
end

修改

请参阅@ JenB对她更有效实施的答案的进一步改进。