NETLOGO:仅向变量满足条件的海龟发出命令 - 在 "foreach" 之后
NETLOGO : Give command to ONLY turtles which variable fulfill the condition - after "foreach"
用户,
我正在尝试为具有各种变量的海龟发出命令。我的代理人打电话给消费者。每个 "consumers" 都有不同的需求,用它的颜色来表示(我使用 14 种颜色)。当特定颜色的消费者数量达到 > 10 时,我希望他们将颜色更改为白色。我使用下面的代码,导致所有消费者的颜色都变成了白色。虽然我只需要满足条件的"consumers"就可以变成白色。
to cocreate-value
let a count consumers with [ color = blue]
let b count consumers with [ color = gray]
let c count consumers with [ color = red]
let d count consumers with [ color = orange]
let e' count consumers with [ color = brown]
let f count consumers with [ color = yellow]
let g count consumers with [ color = green]
let h count consumers with [ color = lime]
let i count consumers with [ color = turquoise]
let j count consumers with [ color = cyan]
let k count consumers with [ color = sky]
let l count consumers with [ color = violet]
let m count consumers with [ color = magenta]
let n count consumers with [ color = pink]
ask consumers [set type-of-need ( list a b c d e' f g h i j k l m n ) ]
foreach type-of-need [
if ? > 10 [
let z consumers with [ ? > 10]
ask z [ set color white ]
ask consumers with [color = white] [set need? false]
]]
end
有人可以告诉我解决方案吗?
谢谢
你的问题是你的列表是由数字组成的(每种类型消费者的数量)而不是需求类型本身。您可以在创建后通过执行 print type-of-need
来查看。假设它看起来像 [5 12 4 ...]。然后你遍历这个数字列表,最终得到一个大于 10 的数字。在这个例子中,你现在有 ? = 12
。那么创建agentset z的条件对所有消费者都成立。
以下代码未经测试,但您需要列出类型而不是计数。尝试这样的事情:
to cocreate-value
let a count consumers with [ color = blue]
let b count consumers with [ color = gray]
ask consumers [set type-of-need ( list blue gray ) ]
foreach type-of-need [
if count consumers with [ color = ? ] > 10 [
let z consumers with [ color = ? ]
ask z [ set color white ]
ask consumers with [color = white] [set need? false]
]]
end
用户,
我正在尝试为具有各种变量的海龟发出命令。我的代理人打电话给消费者。每个 "consumers" 都有不同的需求,用它的颜色来表示(我使用 14 种颜色)。当特定颜色的消费者数量达到 > 10 时,我希望他们将颜色更改为白色。我使用下面的代码,导致所有消费者的颜色都变成了白色。虽然我只需要满足条件的"consumers"就可以变成白色。
to cocreate-value
let a count consumers with [ color = blue]
let b count consumers with [ color = gray]
let c count consumers with [ color = red]
let d count consumers with [ color = orange]
let e' count consumers with [ color = brown]
let f count consumers with [ color = yellow]
let g count consumers with [ color = green]
let h count consumers with [ color = lime]
let i count consumers with [ color = turquoise]
let j count consumers with [ color = cyan]
let k count consumers with [ color = sky]
let l count consumers with [ color = violet]
let m count consumers with [ color = magenta]
let n count consumers with [ color = pink]
ask consumers [set type-of-need ( list a b c d e' f g h i j k l m n ) ]
foreach type-of-need [
if ? > 10 [
let z consumers with [ ? > 10]
ask z [ set color white ]
ask consumers with [color = white] [set need? false]
]]
end
有人可以告诉我解决方案吗?
谢谢
你的问题是你的列表是由数字组成的(每种类型消费者的数量)而不是需求类型本身。您可以在创建后通过执行 print type-of-need
来查看。假设它看起来像 [5 12 4 ...]。然后你遍历这个数字列表,最终得到一个大于 10 的数字。在这个例子中,你现在有 ? = 12
。那么创建agentset z的条件对所有消费者都成立。
以下代码未经测试,但您需要列出类型而不是计数。尝试这样的事情:
to cocreate-value
let a count consumers with [ color = blue]
let b count consumers with [ color = gray]
ask consumers [set type-of-need ( list blue gray ) ]
foreach type-of-need [
if count consumers with [ color = ? ] > 10 [
let z consumers with [ color = ? ]
ask z [ set color white ]
ask consumers with [color = white] [set need? false]
]]
end