Empty Maze with Wall Following 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: Empty-Maze-with-Wall-Following.nlogo

WHAT IS IT?

This model shows how to program simple wall following behaviour for a turtle agent. The agent is then put into an "empty" maze which does not have any internal walls, and the agent then randomly chooses to follow either the right of left-hand wall in order to get to the exit.


WHAT IS ITS PURPOSE?

The purpose of this model is to show how to program simple wall following behaviour in a NetLogo turtle agent.


HOW IT WORKS

The maze is draw using patch agents. There is a single turtle agent for walking the maze. It maintains a direction variable that determines whether left hand walls are followed or right hand walls. Sensing of the wall ahead is done using the patch-right-and-ahead command.


HOW TO USE IT

Press the setup button, then optionally press the pen-down button before pressing the go-once button repeatedly, or by pressing the go-forever button once.


THE INTERFACE

The model's Interface buttons are defined as follows:

- setup: This re-initialises the environment, sets the background to white and (re)draws the empty maze. It also creats a single turtle agent which thens moves towards the entrance at the bottom and then randomly picks whether to head right or left first.

- setup-empty-maze: This re-initialises the environment, sets the background to white and (re)draws the empty maze.

- go-once: The turtle agent will make a single move.

- go-forever: The turtle agent will make moves until it has reached the exit.

- pen-down: This will instruct the turtle agent ot put its pen down. Hence, the effect is that it draws a trail of where it has been.

The model's Interface sliders are defined as follows:

- left-cols: This the width in patches of the left part of the maze.

- right-cols: This the width in patches of the right part of the maze.

- above-rows: This the height in patches of the top part of the maze (above line y = 0).

- below-rows: This the height in patches of the bottom part of the maze (below line y = 0).

- entrance-cols: This is the width in patches of the entrance and exit.


THINGS TO NOTICE

Notice how the turtles follow the walls. The code is slightly different to the code devised by Uri Wilensky in the Code Examples > Wall Following Example model provided in the NetLogo Models Library. How and why is the code different?


THINGS TO TRY

Try altering the values of the sliders to alter the way the maze looks.


EXTENDING THE MODEL

This model has been substantially extended in the Mazes model.


NETLOGO FEATURES

It uses the patch-right-and-ahead command to sense whether there is a wall ahead.


RELATED MODELS

See Hampton Court Maze with Wall Following and Chevening House with Maze Wall Following models.

For implementation of all 3 mazes (Empty, Hampton Court and Chevening House) plus further reactive behaviours (not just wall following), see the Mazes model.


CREDITS AND REFERENCES

This model was created by William John Teahan. The code for wall following is similar to that devised by Uri Wilensky in the Code Examples > Wall Following Example model provided in the NetLogo Models Library.

To refer to this model in publications, please use:

Empty Maze Wall Following NetLogo model.
Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps.


PROCEDURES

; Empty Maze Demo
; 
; Draws an empty maze with entrance at middle bottom and exit
; at middle top. Uses wall following behaviour to search it.
;
; Copyright 2010 William John Teahan. All Rights Reserved.
;
; The wall following code is similar to that devised by Uri Wilensky
; in the Code Examples > Wall Following Example model provided in the NetLogo Models Library

turtles-own [direction]  ;;  1 follows right-hand wall,
                         ;; -1 follows left-hand wall
                         
to setup-empty-maze
  ca ;; clear everything
  
  ask patches
  [
    set pcolor white ;; make background full of white patches
          
    if (pxcor >= (- left-cols - entrance-cols) and pxcor <= (- entrance-cols) and
        pycor = (- below-rows))
      [set pcolor blue] ;; draws bottom left horizontal wall in blue

    if (pxcor >= (entrance-cols) and pxcor <= (right-cols + entrance-cols) and
        pycor = (- below-rows))
      [set pcolor blue] ;; draws bottom right horizontal wall in blue

    if (pxcor >= (- left-cols - entrance-cols) and pxcor <= (- entrance-cols) and
        pycor = (above-rows))
      [set pcolor blue] ;; draws top left horizontal wall in blue

    if (pxcor >= (entrance-cols) and pxcor <= (right-cols + entrance-cols) and
        pycor = (above-rows))         
      [set pcolor blue] ;; draws top left horizontal wall in blue

    if (pxcor = (- left-cols - entrance-cols) and
        pycor >= (- below-rows) and pycor <= (above-rows))
      [set pcolor blue] ;; draws left vertical wall in blue
      
    if (pxcor = (right-cols + entrance-cols) and
        pycor >= (- below-rows) and pycor <= (above-rows))
      [set pcolor blue] ;; draws right vertical wall in blue
  ]
end

to setup
  ca ;; clear everything
  
  setup-empty-maze

  create-turtles 1 [
      set size 5              ;; bigger turtles are easier to see
      set pen-size 2          ;; thicker lines are easier to see
      set color magenta
      
      setxy 0 (- below-rows - 10)
      set heading 0 ; head north
      repeat 11 [forward 1 wait 0.2]
      
      ifelse random 2 = 0
        [ set heading 90
          set direction 1 ]   ;; follow right hand wall
        [ set heading 270
          set direction -1]   ;; follow left hand wall
    ] 
end

to go
  ask turtles [ walk ]
  tick
end

to walk  ;; turtle procedure
  if count neighbors4 with [pcolor = blue] = 4
     [ user-message "Trapped!"
       stop ]
  if ycor >= above-rows and xcor >= (- entrance-cols) and xcor <= entrance-cols
     [ user-message "Made it! I am free!"
       stop ]
  
  ;; turn right if necessary
  if not wall? (90 * direction) 1 and wall? (135 * direction) (sqrt 2) [ rt 90 * direction ]
  ;; turn left if necessary (sometimes more than once)
  while [wall? 0 1] [ lt 90 * direction ]
  ;; move forward
  fd 1
end

to-report wall? [angle dist]  ;; 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 blue = [pcolor] of patch-right-and-ahead angle dist
end
;
; Copyright 2009 by William John Teahan.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Teahan, W. J. (2010).  Empty Maze Wall with Following NetLogo model.
;   Artificial Intelligence. Ventus Publishing Aps
;