将乌龟移动到有一些相同类型乌龟的邻居的补丁并留在那里

Move turtle to a patch with some neighbors of same type of turtle and stay there

我试图将一只乌龟移动到一块有 2 只与其邻居具有相同类型(例如收入)的乌龟并留在那里。我做了以下代码

 to set-move
let target []

 ask turtles with [income = "low"]
 [ let potential-target1 patches with [value < buymiddle and any? turtles-here = false]
   set target potential-target1 with [length remove-duplicates [any? turtles-here with [income = "low"]] of neighbors = 2]
   set target min-one-of potential-target1 [value]
    if target != nobody and any? turtles-on neighbors 
    [ move-to target ask patch-here [set empty false]]]

不过好像不行。选择补丁后,有些海龟仍然会四处走动。一些海龟不会选择一个有两个邻居的补丁。 如何指定具有某些海龟组的两个邻居的补丁?

breed [agens agen]

patches-own [value
empty]
turtles-own [income
            myHouses
            ]
to setup
ca


 ;;Check inputs
 let total-prob prob-low + prob-mid + prob-high
 if (total-prob != 100 )
 [
     print (word "Adoption types must sum to 100, but instead sum to " total-prob)
     stop
 ]

   ask patches [set value random-normal 10 3]

   ask patches [ifelse (value < 8)[ set pcolor green  ]
 [ifelse (value < 11)[ set pcolor blue]
[if value < 15[ set pcolor gray]]]]



end

to go
 ask patches [
 if random 100 < 3 [sprout-agens 1 [set color red
     set shape "default"
     set size 1
     set-income
     set-move]]]

 end

 to set-move
let target []

 ask turtles with [income = "low"]
 [ let potential-target1 patches with [value < buymiddle and any? turtles-here = false]
   set target potential-target1 with [length remove-duplicates [any? turtles-here with [income = "low"]] of neighbors = 2]
   set target min-one-of potential-target1 [value]
    if target != nobody and any? turtles-on neighbors 
    [ move-to target ask patch-here [set empty false]]]


ask turtles with [income = "middle"]
 [ let potential-target2 patches with [(value > buymiddle and value < buyhigh) and any? turtles-here = false]
   let target2 potential-target2 with [length remove-duplicates [any? turtles-here with [income = "middle"]] of neighbors = 2]
   set target2 min-one-of potential-target2 [value]
    if target2 != nobody and any? turtles-on neighbors 
    [ move-to target2 ask patch-here [set empty false]]]


ask turtles with [income = "high"]
 [ let potential-target3 patches with [(value > buyhigh) and any? turtles-here = false]
   let target3 potential-target3 with [length remove-duplicates [any? turtles-here with [income = "high"]] of neighbors = 2]
   set target3 min-one-of potential-target3 [value]
    if target3 != nobody and any? turtles-on neighbors 
    [ move-to target ask patch-here [set empty false]]]



end

to set-income
 let kind-prob (random 100)
 let cumulative-prob prob-low
 ifelse (kind-prob < cumulative-prob)[set income "low" set color red]
 [set cumulative-prob cumulative-prob + prob-mid
 ifelse (kind-prob < cumulative-prob)[set income "middle" set color pink ]
    [set cumulative-prob cumulative-prob + prob-high
      if income < cumulative-prob [set income "high" set color blue]
]]
end

让我们看看第一个代码段的 ask 块中的第一行。

let potential-target1 patches with [value < buymiddle and any? turtles-here = false]

相同
let potential-target1 patches with [value < buymiddle and not any? turtles-here]

这样你的 potential-target1 补丁集就没有海龟了。这将使后续行变得无关紧要。但是假设我们制作了那条线

let potential-target1 patches with [value < buymiddle and any? turtles-here]

下一行,

set target potential-target1 with [length remove-duplicates [any? turtles-here with [income = "low"]] of neighbors = 2]

[any? turtles-here with [income = "low"]] of neighbors 产生一个包含八个 true/false 值的列表,如果相邻的补丁有任何带有 income = low 的海龟,则为真,否则为假。然后减少该列表中的重复项,最后得到一个 [true](如果全部为真)、一个 [false](如果全部为假)或一个真和一个假 [true false][false true] 如果有些是真的有些是假的。然后,您查看该缩减列表中的条目数,并将其与 2 进行比较。当至少一个邻居有这样的海龟而至少一个没有时,就会发生这种情况。我怀疑那不是你想要的。如果你想让两个相邻的补丁至少有一个带有 income = low 的海龟,那么像

count neighbors with [any? turtles-here with [income = low]] = 2

应该这样做。另一方面,如果你想要邻居正好有两只 income = low 的海龟,那么你会想要

neighbors with [count turtles-here with [income = low] = 2]

我不清楚你在找哪个。

看到下面的问题,我猜嘟嘟在找第一个解释。如果是这样,那么在所有候选者中找到具有最低 value 的补丁将是(就像他们在原始代码中所做的那样)

let potential-targets patches with [count neighbors with [any? turtles-here with [income = low]] = 2]
let target min-one-of potential-targets [value]