[我试图将一只乌龟移到一个斑块,在那里有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
产生八个真/假值的列表,如果相邻面片中有带有income = low
的乌龟,则为true,否则为false。然后,您可以减少该列表中的重复项,并以单个[true]
(如果全部为真),单个[false]
(如果全部为假)或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]
我不清楚你在追捕什么。
我希望这对您入门很有帮助,查尔斯