Hill Climbing Example

Hill Climbing Example preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

code example 

Tagged by Reuven M. Lerner over 12 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 1537 times • Downloaded 144 times • Run 2 times
Download the 'Hill Climbing Example' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


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 Code 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.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

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
  ask n-of 800 patches [
    sprout 1 [
      set peak? false
      set color red
      pen-down
    ]
  ]
  reset-ticks
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 


; 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.

Uploaded by When Description Download
Uri Wilensky over 12 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky almost 13 years ago Updated version tag Download this version
Uri Wilensky over 13 years ago Updated to NetLogo 5.0 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky about 15 years ago Model from NetLogo distribution Download this version
Uri Wilensky about 15 years ago Hill Climbing Example Download this version
Uri Wilensky about 15 years ago Hill Climbing Example Download this version

Attached files

File Type Description Last updated
Hill Climbing Example.png preview Preview for 'Hill Climbing Example' over 12 years ago, by Uri Wilensky Download

This model does not have any ancestors.

This model does not have any descendants.