netlogo - 将海龟移动到具有相同类型海龟的一些邻居的补丁并留在那里
问题描述
我试图将一只乌龟移到一个有 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
产生一个包含 8 个真/假值的列表,如果相邻块有任何带有 的海龟,则为真,如果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]
我不清楚你在追求哪个。
看到下面的问题后,我推测 Dudi 正在寻找第一个解释。如果是这样,那么在所有候选者中找到最低的补丁value
(就像他们在原始代码中所做的那样)
let potential-targets patches with [count neighbors with [any? turtles-here with [income = low]] = 2]
let target min-one-of potential-targets [value]
推荐阅读
- ajv - 我如何在 fastify 中使用 ajv-i18n?
- anylogic - 如何在 AnyLogic 中测量系统中的总时间
- sql - Elixir,Ecto 比较 SQL 查询中的日期时间
- reactjs - 如何编辑我的 axios post 请求以便成功发送 put 请求?
- php - 为什么这个 PHP 末尾的尾随空格包括阻止 AJAX http_response_code() 返回正确的值?
- python - 从字典列表中获取字典中键的值
- python - 按其中一个成员变量的值对列表对象进行排序
- r - 使用 apply() 根据 R 中另一列的信息对一列执行操作
- c# - 我可以通过 API 添加到管道触发器“分支过滤器”列表吗
- python - 按列表分组并为每个值创建新列