Hill Climbing Example 2 NetLogo Model

Produced for the book series "Artificial Intelligence";

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

powered by NetLogo

view/download model file: Hill-Climbing-Example-2.nlogo

WHAT IS IT?

This example shows how to make turtles climb hills -- or descend into valleys -- using the UPHILL, UPHILL4, DOWNHILL, and DOWNHILL4 commands. The same technique is useful for modeling any kind of creature that follows a gradient in its environment.


THINGS TO NOTICE

These commands always make the turtle move to the center of a patch.

UPHILL and DOWNHILL make the turtle look at all eight neighboring patches (including diagonal neighbors). UPHILL4 and DOWNHILL4 only look at the four neighboring patches (to the north, south, east, and west).

With the UPHILL and DOWNHILL commands, diagonal moves are longer (1.414...) than vertical or horizontal moves (1.0). If you use the UPHILL4 and DOWNHILL4 commands, all moves are the same length (1.0).

If there is a tie between neighboring patches, NetLogo breaks the tie randomly.


THINGS TO TRY

In the GO procedure in the Procedures tab, change UPHILL to UPHILL4, DOWNHILL, or DOWNHILL4 and observe the results.


NETLOGO FEATURES

If you look at the entry for UPHILL in the NetLogo Dictionary, it shows some code that does the exact same thing as the primitive does. If you need to do something that is similar to the primitive, but different in so way, you could use that code as a starting point.


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. (2009). Artificial Intelligence. Ventus Publishing Aps.
;
turtles-own
[
  peak? ;; indicates whether a turtle has reached a "peak",
        ;; that is, it can no longer go "uphill" from where it stands
]

to setup
  clear-all
  ;; make a landscape with hills and valleys
  ask n-of 100 patches [ set pcolor 120 ]
  ;; slightly smooth out the landscape
  repeat 20 [ diffuse pcolor 1 ]
  ;; put some turtles on patch centers in the landscape
  let max-turtles (2 * max-pxcor + 1) * (2 * max-pycor + 1)
  if (number-of-turtles > max-turtles)
    [ set number-of-turtles max-turtles ]
  ask n-of number-of-turtles patches
  [
    sprout 1
    [
      set peak? false
      set color red
      pen-down
    ]
  ]
end

to go
  ;; stop when all turtles are on peak
  if all? turtles [peak?]
    [ stop ]
  ask turtles [
    ;; remember where we started
    let old-patch patch-here
    ;; to use UPHILL, the turtles specify a patch variable
    uphill pcolor
    ;; are we still where we started? if so, we didn't
    ;; move, so we must be on a peak
    if old-patch = patch-here [ set peak? true ]
  ]
  tick
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 ***