3

I am very new to netlogo so this is probably a very basic question, but I am stuck. I want to use a while loop (so the commands keep occurring throughout the run) and patch color to dictate how the turtle will move.

If the turtle is not on a red patch, I want it to keep moving towards the closest red patch.

If it is on a red patch, I want it to stay on the patch

 while [pcolor] of patch-here != red
     [
       face min-one-of patches with [pcolor = red ] [ distance myself ]
       forward 1
     ]

 while [pcolor] of patch-here = red
     [
        stop
     ]

When I run this, I get an error (with '[pcolor] of patch-here != red" highlighted) that says "While expected this input to be a TRUE/False block, but got a TRUE/FALSE instead."

Can anyone help me out?

unor
  • 92,415
  • 26
  • 211
  • 360
emenyea
  • 31
  • 2
  • 3
    You may also have some conceptual confusion. You state that you are using the while loop 'so the commands keep occurring throughout the run'. The while loop will mean that NetLogo will run this set of commands until the turtle gets onto a red patch. If you have multiple turtles and you have this inside an ask turtles ... then one turtle will move all the way to a red patch and then the next. It is more typical in NetLogo to use the tick counter and have each turtle move once each tick, so they are all moving at the same time. If simultaneous is what you want, say so and we will answer that. – JenB Jul 24 '15 at 15:01
  • @JenB I do have multiple turtles and this is inside an ask turtles, I'm trying to accomplish simultaneous movement like what you said! – emenyea Jul 26 '15 at 23:32

3 Answers3

3

You just need to throw [] around the condition of the while-loop, like so:

 while [[pcolor] of patch-here != red]
     [
       face min-one-of patches with [pcolor = red ] [ distance myself ]
       forward 1
     ]

Also, I don't think your second while-loop is right. First of all, it can only run once (since it just stops) so it may as well be an if. Second, you know you just the first while-loop, so you know the patch is red. Thus, the condition will always be true.

Bryan Head
  • 12,360
  • 5
  • 32
  • 50
  • the throw brackets worked, thank you! I tried implementing an if statement if [[p color] of patch-here = red] [ stop ] within the while loop, but it isn't working correctly – emenyea Jul 26 '15 at 23:35
1

Here is a minimal but complete example that allows simultaneous movement (as requested in the OP's comments). If you create a new NetLogo model and copy this in, then you can see it working. You will need to add setup and go buttons to the interface, or you can type setup (once) into the command centre and then type go (multiple times) to get the turtles moving.

to setup
  clear-all
  ask n-of 20 patches [ set pcolor red ]
  create-turtles 20 [ setxy random-xcor random-ycor ]
  reset-ticks
end

to go
  ask turtles with [ [pcolor] of patch-here != red ]
  [ face min-one-of patches with [pcolor = red ] [ distance myself ]
    forward 1
  ]
  tick
end

The basic concept here is that each tick is a timestep. Your go code contains instructions for everything that happens in the same tick and then has the tick command (at the end) to advance the clock. This is critical to understanding how to think the way NetLogo does and I suggest you have a look at some of the examples in the Model Library.

The actual code to do the move toward the nearest red patch is what Bryan gave you.

JenB
  • 17,620
  • 2
  • 17
  • 45
0

there is the whole code with while condition (for dummy users as I am ;))

to setup
  clear-all
  ask n-of 20 patches [ set pcolor red ]
  create-turtles 20 [ setxy random-xcor random-ycor ]
  reset-ticks
end

to go
  ask turtles 
  [while [[pcolor] of patch-here != red]
     [
       face min-one-of patches with [pcolor = red ] [ distance myself ]
       forward 1
     ]
  ]  
tick
end
maycca
  • 3,848
  • 5
  • 36
  • 67