NETLOGO:只给出变量符合条件的海龟的命令 - 在" foreach"

时间:2015-12-07 07:05:40

标签: list foreach netlogo

用户,

我正在尝试为拥有各种变量的海龟发出命令。我的经纪人叫消费者。每个"消费者"有不同的需要,它的颜色代表(我使用14色基)。当具有特定颜色的消费者的数量达到> 10,我希望他们把颜色变成白色。我使用下面的代码,导致所有消费者将颜色变为白色。虽然我只需要"消费者"满足条件变为白色。

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

有人能告诉我解决方案吗?

谢谢

1 个答案:

答案 0 :(得分:1)

您的问题是您的列表由数字(每种类型的消费者的计数)组成,而不是自己需要的类型。您可以在创建print type-of-need之后看到这一点。让我们说它看起来像[5 12 4 ...]。然后循环遍历这个数字列表,最终得到一个大于10的数字。在这个例子中,你现在有? = 12。然后,为所有消费者创建代理集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