2017-03-22 65 views
0

我对Netlogo相当陌生,并且在如何设置一个复杂的if语句时苦苦挣扎。该声明适用于海龟,条件是其他海龟生活在同一地区并拥有房屋。Netlogo if if with statement and other

我已经试过以下的迭代,但至今尚未成功:

if (one-of other turtles with [region = [region] of myself and house? = True]) [] 

if (other turtles with [region = [region] of myself and house? = True]) [] 

谢谢你的任何见解!

回答

2

如果您需要在问题中插入代码,请查看工具栏中的“代码示例”按钮。您可以突出显示您的代码并单击按钮 - 超级方便。

你的第二次尝试是非常接近。快速解决方案是添加any?原语,以便告诉Netlogo您希望在此情况下评估代理集“其他海龟”。事实上,你实际上并没有用if来评估任何东西 - 就好像说“如果我所在地区的乌龟有房子”,而不是“如果我所在地区有任何乌龟有房子”。

to check-region 
    ask one-of turtles [ 
    if any? other turtles with [ region = [region] of myself and house? = true ] [ 
     set color white 
    ] 
    ] 
end 

如果您需要评估更多具体的数字,你可以使用像count设置阈值 - 例如:

to check-region-count 
    ask one-of turtles [ 
    if count other turtles with [ region = [region] of myself and house? = true ] > 3 [ 
     set color white 
    ] 
    ] 
end 
+0

谢谢你,它的工作! – James