NetLogo 中嵌套 ifelse 的问题
Trouble with nested ifelse in NetLogo
我是 NetLogo 的新手。一般来说,编程也很新。我在这里读了很多有用的东西。现在,我的知识跌至谷底,并决定提出我的第一个问题。不管我听起来多么愚蠢:
我尝试让一些海龟移动并避开 NetLogo 中的某些补丁。我尝试使用以下嵌套的 ifelse 将其存档:
loop [
ifelse any? Movers with [steps > 0]
[
ask Movers with [steps > 0]
[
ifelse patch-ahead 1 is-Patch? [
ifelse not any? turtles-on patch-ahead 1 [
ifelse [pcolor] of patch-ahead 1 != white [
ifelse [pcolor] of patch-ahead 1 != brown [
fd 1 set steps steps - 1][turnTurtle]
]
[turnTurtle]
]
[turnTurtle]
]
[turnTurtle]
]
]
[stop]
]
循环应该运行直到每个 Mover 移动一次。这就是我使用 "steps" 变量跟踪的内容。如果任何条件为假,它将调用一个名为 "turnTurtle" 的过程。转动的海龟将在循环的下一个 运行 期间尝试移动或再次转动。
当我尝试 运行 代码时出现 "Expected keyword" 错误。为什么?
提问:有没有更好的方法来检查补丁和转动海龟?这种非常嵌套的设置可能不是一个好的选择?
I get an "Expected keyword" error when i try to run the code. Why?
我没有从您 post 编辑的代码片段中得到该错误。 (一般来说,您应该尝试 post 简化但 "complete" 您的代码示例,其他人可以将其粘贴到 NetLogo 中并使用。通常,仅尝试创建这样的示例将帮助您解决问题。 )
我得到的是 "Expected a constant" 编译错误,因为 patch-ahead 1 is-Patch?
被反转了。应该是:
is-patch? patch-ahead 1
也许这就是您问题的根源?
Is there a better way to check patches and turn turtles? This very nested setup might be a poor choice?
如您所见,深度嵌套的代码是不可取的,通常可以避免。在你的情况下,这可以通过使用 and
:
将你的条件组合在一个表达式中来实现
ifelse is-patch? patch-ahead 1
and not any? turtles-on patch-ahead 1
and [ pcolor ] of patch-ahead 1 != white
and [ pcolor ] of patch-ahead 1 != brown
[
fd 1
set steps steps - 1
] [ turnTurtle ]
一些补充说明:
您可以使用 member?
:
组合它们,而不是在两个单独的表达式中检查 != white
和 != brown
[ not member? pcolor [ white brown ] ] of patch-ahead 1
而不是组合 loop
with an if
expression and a stop
command, you could just use while
:
while [ any? movers with [ steps > 0 ] ] [
ask movers with [ steps > 0 ] [
; rest of your code here
]
]
最后,我认为你在这里不需要它,但是对于复杂的控制流代码,你可以使用 cf
extension 向 NetLogo 添加 "switch" 类语句。
我是 NetLogo 的新手。一般来说,编程也很新。我在这里读了很多有用的东西。现在,我的知识跌至谷底,并决定提出我的第一个问题。不管我听起来多么愚蠢:
我尝试让一些海龟移动并避开 NetLogo 中的某些补丁。我尝试使用以下嵌套的 ifelse 将其存档:
loop [
ifelse any? Movers with [steps > 0]
[
ask Movers with [steps > 0]
[
ifelse patch-ahead 1 is-Patch? [
ifelse not any? turtles-on patch-ahead 1 [
ifelse [pcolor] of patch-ahead 1 != white [
ifelse [pcolor] of patch-ahead 1 != brown [
fd 1 set steps steps - 1][turnTurtle]
]
[turnTurtle]
]
[turnTurtle]
]
[turnTurtle]
]
]
[stop]
]
循环应该运行直到每个 Mover 移动一次。这就是我使用 "steps" 变量跟踪的内容。如果任何条件为假,它将调用一个名为 "turnTurtle" 的过程。转动的海龟将在循环的下一个 运行 期间尝试移动或再次转动。
当我尝试 运行 代码时出现 "Expected keyword" 错误。为什么?
提问:有没有更好的方法来检查补丁和转动海龟?这种非常嵌套的设置可能不是一个好的选择?
I get an "Expected keyword" error when i try to run the code. Why?
我没有从您 post 编辑的代码片段中得到该错误。 (一般来说,您应该尝试 post 简化但 "complete" 您的代码示例,其他人可以将其粘贴到 NetLogo 中并使用。通常,仅尝试创建这样的示例将帮助您解决问题。 )
我得到的是 "Expected a constant" 编译错误,因为 patch-ahead 1 is-Patch?
被反转了。应该是:
is-patch? patch-ahead 1
也许这就是您问题的根源?
Is there a better way to check patches and turn turtles? This very nested setup might be a poor choice?
如您所见,深度嵌套的代码是不可取的,通常可以避免。在你的情况下,这可以通过使用 and
:
ifelse is-patch? patch-ahead 1
and not any? turtles-on patch-ahead 1
and [ pcolor ] of patch-ahead 1 != white
and [ pcolor ] of patch-ahead 1 != brown
[
fd 1
set steps steps - 1
] [ turnTurtle ]
一些补充说明:
您可以使用 member?
:
!= white
和 != brown
[ not member? pcolor [ white brown ] ] of patch-ahead 1
而不是组合 loop
with an if
expression and a stop
command, you could just use while
:
while [ any? movers with [ steps > 0 ] ] [
ask movers with [ steps > 0 ] [
; rest of your code here
]
]
最后,我认为你在这里不需要它,但是对于复杂的控制流代码,你可以使用 cf
extension 向 NetLogo 添加 "switch" 类语句。