NetLogo 新手,不太知道我在做什么

问题描述 投票:0回答:1

GO 功能不想工作。它试图以不同的概率传播不同颜色的火,但我无法让它工作

globals [
  initial-trees
  burned-trees
]

breed [fires fire]

to setup
  clear-all
  set-default-shape turtles "square"
  ask patches with [(random-float 100) < density] [ 
  set pcolor green 
      if random 100 < 50 [
  set pcolor blue ]
      if random 100 < 25 [
  set pcolor yellow ] ]
  ask patches with [pxcor = min-pxcor] [ 
    ignite 
  ]
  set initial-trees count patches with [pcolor = green]
  set burned-trees 0
  reset-ticks
end

to ignite
  sprout-fires 1
  [ set color red ]
  set pcolor black
  set burned-trees burned-trees + 1
end


to go
 print count patches with [ pcolor = yellow ] 
 print count patches with [ pcolor = blue ] 
 print count patches with [ pcolor = green ] 
 if not any? turtles
   [ stop ]
 ask fires
   [ if neighbors4 [pcolor = green]
     if random 100 < 50 [ignite] ]
      [ if neighbors4 [pcolor = blue]
          [ if neighbors4 [pcolor = yellow]
            if random 100 < 25 [ignite] ]
 tick
end

我已经尝试了所有我能想到的方法,但没有任何效果。如果有人能解决我的问题那就太棒了

netlogo new-operator
1个回答
0
投票

如此多的事情:

  1. if
    需要后面跟一个逻辑语句。
    neighbors4 [pcolor = green]
    不是一个逻辑陈述。
    neighbors4
    创建一个主体集。如果您只想选择绿色补丁,您可以使用
    neighbors4 with [pcolor = green]
    并使用
    any? neighbors4 with [pcolor = green]
    使其成为逻辑语句。如果结果为 TRUE/FALSE,这将构成一个逻辑陈述,因此可用于
    if

  2. 您想要

    ignite
    这些补丁,因此必须选择它们。您目前
    ask
    正忙着做某事。
    sprout
    是一个只能由补丁使用的命令。因此,您需要使用另一个
    ask
    来选择补丁。

  3. 您缺少括号。 NetLogo Dictionary 告诉您何时以及如何使用它们。因为如果它说

    if boolean [command]
    。因此,逻辑语句的结果需要放在方括号中。逻辑表达式本身没有。

因此请记住这一点,有多种方法可以实现您想要的目标。这是一种解决方案:

to go
 print count patches with [ pcolor = yellow ] 
 print count patches with [ pcolor = blue ] 
 print count patches with [ pcolor = green ] 
 if not any? turtles
   [ stop ]
  
  
 ask fires
  [ ask neighbors4 [
    (ifelse
      pcolor = green [if random 100 < 80 [ignite]]
      pcolor = blue [if random 100 < 50 [ignite]]
      pcolor = yellow [if random 100 < 25 [ignite]]
    )]
  ]
  
 tick
end

至少我猜您希望发生这种情况。它会询问所有邻居并检查它们的颜色(通过使用圆括号使用具有多种选择的

ifelse
)并根据概率点燃它们(检查那里的数字,根据您的原始代码不太清楚!)

© www.soinside.com 2019 - 2024. All rights reserved.