Hampton Court Maze with Wall Following NetLogo Model
Produced for the book series "Artificial Intelligence";
Author: W. J. Teahan; Publisher: Ventus Publishing Aps.
powered by NetLogo
view/download model file: Hampton-Court-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 a schematic representation of the real-life Hampton Court maze, 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 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.
- go-once: The turtle agent will make a single move.
- go-forever: The turtle agent will make moves until it has reached the exit.
The model's Interface switch and sliders are defined as follows:
- set-pen-down: If set to On, 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.
- row-patches-width: This the width in patches of each row the maze.
- col-patches-width: This the width in patches of each column the maze.
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 Empty Maze with Wall Following and Chevening House Maze with 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:
Hampton Court Maze with Wall Following NetLogo model.
Teahan, W. J. (2010). Artificial Intelligence. Ventus Publishing Aps.
PROCEDURES
; Hampton Court Maze Demo
;
; Draws a schematic map of the Hampton Court Maze and then gets
; a wall following agent to run around within it.
;
; Copyright 2010 William John Teahan. All Rights Reserved.
;
; 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.
turtles-own [direction] ;; 1 follows right-hand wall,
;; -1 follows left-hand wall
to setup-row [row colour segments]
foreach segments
[
if pycor = row * row-patches-width and
(pxcor >= col-patches-width * (item 0 ?)) and (pxcor <= col-patches-width * (item 1 ?))
[set pcolor colour]
]
end
to setup-col [col colour segments]
foreach segments
[
if pxcor = col * col-patches-width and
(pycor >= row-patches-width * (item 0 ?)) and (pycor <= row-patches-width * (item 1 ?))
[set pcolor colour]
]
end
to setup-hampton-court-maze
ca ;; clear everything
ask patches
[
if (pxcor >= min-pxcor and pxcor <= max-pxcor and
pycor >= min-pycor and pycor <= max-pycor)
[set pcolor white] ;; make background full of white patches
setup-row 5 blue [[-9 10]]
setup-row 4 blue [[-8 -5] [-3 -1] [0 3] [5 9]]
setup-row 3 blue [[-7 -4] [-2 2] [4 8]]
setup-row 2 blue [[-6 -1] [1 4] [5 7]]
setup-row 1 blue [[-3 3] [8 9]]
setup-row 0 blue [[-8 -7] [9 10]]
setup-row -1 blue [[-9 -8]]
setup-row -2 blue [[-8 -7] [-3 0] [1 3]]
setup-row -3 blue [[-4 -1] [2 4] [6 8]]
setup-row -4 blue [[-7 -1] [1 9]]
setup-row -5 blue [[-8 10]]
setup-row -6 blue [[-9 0] [1 10]]
setup-col 10 blue [[-6 5]]
setup-col 9 blue [[-4 -1] [1 4]]
setup-col 8 blue [[-3 1] [2 3]]
setup-col 7 blue [[-2 2]]
setup-col 6 blue [[-4 1]]
setup-col 5 blue [[-3 2]]
setup-col 4 blue [[-3 2] [3 5]]
setup-col 3 blue [[-2 1] [2 4]]
setup-col 1 blue [[-4 -2]]
setup-col 0 blue [[-5 -2] [1 3]]
setup-col -1 blue [[-4 -3] [4 5]]
setup-col -3 blue [[-2 1] [2 4]]
setup-col -4 blue [[-3 2] [3 5]]
setup-col -5 blue [[-4 1]]
setup-col -6 blue [[-3 2]]
setup-col -7 blue [[-4 -3] [-2 0] [1 3]]
setup-col -8 blue [[-5 -2] [0 4]]
setup-col -9 blue [[-6 5]]
]
end
to setup
ca ;; clear everything
setup-hampton-court-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
; move the turtle slowly to the entrance
setxy (col-patches-width / 2) (- row-patches-width * 7)
if set-pen-down
[ pen-down ]
set heading 0 ; head north
repeat row-patches-width + 1 [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
forward 2
]
end
to go
ask turtles [ walk ]
tick
end
to walk ;; turtle procedure
ifelse set-pen-down [ pen-down ] [ pen-up ]
if count neighbors4 with [pcolor = blue] = 4
[ user-message "Trapped!"
stop ]
if ycor > ( - 2 * row-patches-width) and ycor <= row-patches-width and
xcor >= 0 and xcor <= col-patches-width
[ user-message "Made it to the centre of the maze!"
stop ]
;; turn right if necessary
if not wall? (90 * direction) 1 and wall? (135 * direction) (sqrt 2)
[ rt 90 * direction ]
;; wall straight ahead: 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]
;; 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 2010 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). Hampton Court Maze with Wall Following NetLogo model.
; Artificial Intelligence. Ventus Publishing Aps
;
