New To NetLogo, don't really know what I'm doing

36 Views Asked by At

The GO function doesn't want to work. It's trying to spread a fire with different probailities for different colour but i cant get it to work

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

I've tried everything I can think of and nothing works.

1

There are 1 best solutions below

0
lmf0845 On

So multiple things:

  1. if requires to be followed by a logical statement. neighbors4 [pcolor = green] is not a logical statement. neighbors4 creates an agentset. If you want to pick only green patches you could use neighbors4 with [pcolor = green] and to make it a logical statement use any? neighbors4 with [pcolor = green]. This would make a logical statement if TRUE/FALSE as a result and thus usable for the if.

  2. You want to ignite these patches and therefore have to select them. You currently ask the fires to do something. sprout is a command that can only be used by patches. You therefore need to select the patch by using another ask.

  3. You are missing brackets. The NetLogo Dictionary tells you when and how to use them. For if it says if boolean [command]. So the consequence of your logical statement needs to be in square brackets. The logical expression itself does not.

So keeping this in mind, there are multiple ways to achieve what you want. Here is one solution:

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

At least that is what I am guessing you would like to happen. It asks all neighbors and checks their color (using the ifelse with multiple choices by using the round brackets) and ignites them based on probabilities (check the numbers there, it is not quite clear based on your original code!)