抓不到目标补丁

Catch nobody for target patch

我想检查目标补丁是否满足条件。如果找到满足条件的补丁, 然后海龟应该移动到那里。如果 'nobody' 满足此条件,则应打印一条错误消息。

条件是一块半径内应该有 10 2 只相同品种的海龟。

我尝试用 ifelsenobody 来实现这一点。但是,目前我总是收到错误消息,即使 目标变量不为空(您可以使用 if 循环检查)。

breed [ breed1s breed1 ] 
breed [ breed2s breed2 ]

globals [target1 target2]

to setup   
  ca   
  create-breed1s 1000 [
    setxy random-xcor random-ycor 
  ]
  create-breed1s 1000 [
    setxy random-xcor random-ycor
  ] 
end

to go
 ask turtles [
    set target1  ( count turtles in-radius 10 with [breed = breed1s] ) >= 2
    set target2  ( count turtles in-radius 10 with [breed = breed2s] ) >= 2 
    
    new-position   
 ]
end

to new-position
 ifelse target1 != nobody
    [ if (breed = breed1s) [ move-to one-of patches with [ target1 ] ] ]
    [ print "Not enough agents in the neighborhood" ]
 ifelse target2 != nobody
     [ if (breed = breed2s) [ move-to one-of patches with [ target2 ] ] ]
     [ print "Not enough agents in the neighborhood" ]
 
 ; if (breed = breed1s)
 ;   [ move-to one-of patches with [ target1 ] ]
end

对模型效率的评论:因为我想在每个滴答中稍后添加海龟,所以必须重新评估目标 在每个滴答声中(因此它处于“开始”而不是“设置”)。

另一个问题:是否有可能做类似 [ breed = myself ] 而不是 [ breed = breed1s ] 的事情,所以 我不必为每个品种都输入品种?

编辑: 移动到目标补丁的海龟应该具有目标补丁中也提到的相同品种。

问题实际上是您如何创建 target1,而不是检查它是否 nobody。你有:

set target1  ( count turtles in-radius 10 with [breed = breed1s] ) >= 2

此行首先识别附近所有具有适当品种的海龟并计算它们。如果计数为 2 或更高,则变量 target1 设置为 true,如果计数为 0 或 1,则变量 target1 设置为 false。因此您正在比较布尔值 truefalsenobody (一种特殊类型的代理)。这将始终是不匹配的,因此打印错误。

关于调试的注意事项 - 当您遇到此类问题时,在进行检查之前为检查的每一侧打印语句总是很有用的。您会立即发现 target1 不是您想象的那样。

既然你要求移动到 one-of 个补丁,你可能真的想要存储 10 个距离(我认为)内的可用补丁并且有足够的正确类型的海龟。所以,你需要这样的东西:

to go
 ask turtles [
    set target1 patches in-radius 10 with [count breed1s-here >= 2]
    set target2 patches in-radius 10 with [count breed2s-here >= 2]
    new-position   
 ]
end

那么你的空虚测试是any?

to new-position
 ifelse any? target1
    [ move-to one-of target1 ]
    [ print "Not enough agents in the neighborhood" ]
 ifelse any? target2
     [ move-to one-of target2 ]
     [ print "Not enough agents in the neighborhood" ]
end

假设我已经正确地解释了你想要在 10 个海龟的范围内的斑块(与 10 个距离内有足够海龟的任何斑块相比)并且你只关心它自己品种的海龟的数量,然后:

to go
 ask turtles [
    let target-breed [breed] of myself
    set targets patches in-radius 10 with [count turtles-here with [breed = target-breed] >= 2]
    new-position   
 ]
end

to new-position
 ifelse any? targets
    [ move-to one-of targets ]
    [ print "Not enough agents in the neighborhood" ]
end

关于效率,这取决于你有多少只海龟。如果你有很多海龟,那么让每只海龟数一数自己的邻居会很昂贵。相反,您可以为每个品种设置补丁集。也就是说,在 go 程序的开头将 target1 设置为 patches with [count breed1s-here >= 2]。那么你可以这样做:

to go
  let targets1 patches with [count breed1s-here >= 2]
  let targets2 patches with [count breed2s-here >= 2]
  ask turtles
  [ set targets targets1 in-radius 10
    new-position   
  ]
end

但是,您不能再使用海龟的品种和 myself 技巧来选择正确的补丁集。有一些方法可以解决这个问题(例如,使用包含两个项目的列表,品种在第一个位置,补丁集作为第二个位置)但是这个答案已经偏离轨道了。

to new-position
 ifelse any? targets
    [ move-to one-of targets ]
    [ print "Not enough agents in the neighborhood" ]
end