<Artificial Intelligence - Volume 1: Wall-Following-Example-2 NetLogo Model

Wall Following Example 2 NetLogo Model

Produced for the book series "Artificial Intelligence";

Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.

powered by NetLogo

view/download model file: Wall-Following-Example-2.nlogo

WHAT IS IT?

The turtles in this example follow walls made out of colored patches. The blue turtles try to keep the wall on their right; the green turtles keep the wall on their left. Hence, the blue turtles end up heading in a clockwise direction, and the blue turtles end up in an anti-clockwise direction. The code is mostly based on Uri Wilensky's original model in NetLogo's Models Library.


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.

The model implements the standard unmodified behaviour as provided by Uri Wilensky's original model, but also provides an alternative modified behaviour where the actions have been split into three independent parts as follows:

1. walk-modified-1: The turtle turns right if necessary.

2. walk-modified-2: The turtle turns left if necessary.

3. walk-modified-3: The turtle moves forward.

These modified sub-behaviours are executed concurrently in no particular order. The purpose is to show a "Sense & Think & Act" type behaviour where sensing, thinking and acting are done concurrently rather then a "Sense - Think - Act" type behaviour where sensing, thinking and acting are done one after the other.


THE INTERFACE

The Interface buttons are defined as follows:

- setup: This initialises the environment with randomly shaped blocks, and places the turtle-agents at random locations in the intervening black spaces.

- go: This runs the model.

- pen-down: This gets the turtle agents to put their pen downs, thereby drawing the paths they take as they move around.

The Interface choosers and sliders are defined as follows:

- behaviour: Setting this to Standard sets the behaviour to that devised in Uri Wilensky's original model. Setting this to Modified sets the behaviour to that explained above in the "How it works" section.

- choose-sub-behaviours: When the behaviour variable is set to Modified, setting this to choose-all-in-random-order, then the three modified behaviours (see above) will be executed together but in no particular order. Otherwise, just one of them will be randomly chosen to be executed.

- number-of-turtles: This is the number of turtle agents that are created at setup.


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.

Changing the behaviour variable from Standard to Modified in the Interface does not seem to change what the turtles seem to be doing. However, when the behaviour is set to Modified, see if you can notice slight variations in behaviour to the Standard behaviour, especially when the turtle agent ends up in a cul-de-sac or there is a narrow alley-way between two buildings. In the latter case, the Modified behaviour results in the turtles having the ability to explore further than their Standard counterparts.

When the behaviour variable is set to Modified, note that the speed of the turtles is much faster when the choose-sub-behaviours variable is set to choose-all-in-random-order rather than choose-one-at-random. Why?


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 the WALK procedure 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 primitives to look at patches around themselves, relative to the direction the turtle is facing.


PROCEDURES

; This code written by Uri Wilensky (see copyright message below)
; has been slightly modified by Bill Teahan for the following
; book:
;
; Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps.
;
turtles-own
[direction                ;;  1 follows right-hand wall, -1 follows left-hand wall
 way-is-clear?            ;; reporter - true if no wall ahead
 checked-following-wall?] ;; reporter - true if checked following 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 number-of-turtles 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
      set way-is-clear? false
      set checked-following-wall? false
      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 ]
    ]
  ]
end

to go
  if-else (behaviour = "Standard")
  [ ask turtles [ walk-standard ]]
  [ ask-concurrent turtles
    [ walk-modified shuffle [1 2 3]]
  ]
  tick
end

to walk-standard  ;; standard turtle walk behaviour
  ;; 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 walk-modified [order] ;; modified turtle walk behaviour
  ifelse (choose-sub-behaviours = "Choose-all-in-random-order")
  [
    foreach order
    [ if (? = 1) [ walk-modified-1 ]
      if (? = 2) [ walk-modified-2 ]
      if (? = 3) [ walk-modified-3 ]]
  ]
  [ 
    let ord one-of order
    if (ord = 1) [ walk-modified-1 ]
    if (ord = 2) [ walk-modified-2 ]
    if (ord = 3) [ walk-modified-3 ]
  ]
end

to walk-modified-1  ;; modified turtle walk sub-behaviour 1
  ;; turn right if necessary
  if not wall? (90 * direction) and wall? (135 * direction)
  [ rt 90 * direction ]
  set checked-following-wall? true
end

to walk-modified-2  ;; modified turtle walk sub-behaviour 2
  ;; turn left if necessary (sometimes more than once)
  ifelse (wall? 0)
  [ lt 90 * direction
    set way-is-clear? false ]
  [ set way-is-clear? true ]
end

to walk-modified-3 ;; modified turtle walk sub-behaviour 3
  ;; move forward
  if way-is-clear? and checked-following-wall?
  [ fd 1
    set way-is-clear? false
    set checked-following-wall? false ]
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


; *** NetLogo 4.0.4 Code Example Copyright Notice ***
;
; (C) 2007 Uri Wilensky.  This code may be freely copied, distributed,
; altered, or otherwise used by anyone for any legal purpose.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
; A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
; OWNERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;
; *** End of NetLogo 4.0.4 Code Example Copyright Notice ***