Wall Following Example
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
The turtles in this example follow walls made out of colored patches. Some turtles try to keep the wall on their right; others keep the wall on their left.
HOW IT WORKS
Consider a turtle that wants to keep a wall on its right. If there is no wall immediately to its right, but there is a wall behind it on the right, it must turn right in order not to lose the wall. If there's a wall directly in front of the turtle, it keeps turning left until there's free space in front of it. Then it can move forward.
THINGS TO NOTICE
The turtles always stay on patch centers and never move diagonally.
If there isn't a wall next to a turtle when it is born, it just moves forward until it finds one.
EXTENDING THE MODEL
The walk
procedure will get stuck in an infinite loop if a turtle finds itself surrounded by walls on all four sides. The setup
procedure detects such turtles and kills them off, so the model won't get stuck. How would you change walk
not to have this problem? (You might need to do so in a model where the walls could move and grow.)
NETLOGO FEATURES
Turtles use the patch-right-and-ahead
primitive to look at patches around themselves, relative to the direction the turtle is facing.
Comments and Questions
turtles-own [direction] ;; 1 follows right-hand wall, ;; -1 follows left-hand wall to setup clear-all ;; make some random walls for the turtles to follow. ;; the details aren't important. ask patches [ if random-float 1.0 < 0.04 [ set pcolor brown ] ] ask patches with [pcolor = brown] [ ask patches in-radius random-float 3 [ set pcolor brown ] ] ask patches with [count neighbors4 with [pcolor = brown] = 4] [ set pcolor brown ] ;; now make some turtles. SPROUT puts ;; the turtles on patch centers ask n-of 40 patches with [pcolor = black] [ sprout 1 [ if count neighbors4 with [pcolor = brown] = 4 [ die ] ;; trapped! set size 2 ;; bigger turtles are easier to see set pen-size 2 ;; thicker lines are easier to see face one-of neighbors4 ;; face north, south, east, or west ifelse random 2 = 0 [ set direction 1 ;; follow right hand wall set color blue ] [ set direction -1 ;; follow left hand wall set color green ] ] ] reset-ticks end to go ask turtles [ walk ] tick end to walk ;; turtle procedure ;; turn right if necessary if not wall? (90 * direction) and wall? (135 * direction) [ rt 90 * direction ] ;; turn left if necessary (sometimes more than once) while [wall? 0] [ lt 90 * direction ] ;; move forward fd 1 end to-report wall? [angle] ;; turtle procedure ;; note that angle may be positive or negative. if angle is ;; positive, the turtle looks right. if angle is negative, ;; the turtle looks left. report brown = [pcolor] of patch-right-and-ahead angle 1 end ; Public Domain: ; To the extent possible under law, Uri Wilensky has waived all ; copyright and related or neighboring rights to this model.
There are 10 versions of this model.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
Wall Following Example.png | preview | Preview for 'Wall Following Example' | over 12 years ago, by Uri Wilensky | Download |
This model does not have any ancestors.
This model does not have any descendants.