Look Ahead 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: Look-Ahead-Example-2.nlogo
WHAT IS IT?
This code example shows how to have turtles look ahead before they move. By looking ahead, a turtle can determine what is in front of it and take a particular action. Looking ahead is most appropriate in situations where the turtle is not supposed to go "on top of" certain agents. This can be extremely useful for something like barriers or walls, which the turtle shouldn't move through.
In this example, 4 turtles are placed in a world with a checkerboard-like pattern of blue barriers on which they are not allowed to step. Before the turtles step forward they always check ahead to see if they are about to move into a wall using the patch-ahead primitives. "patch-ahead n" reports the patch that the turtle would be on if it were to move forward n steps (e.g., by executing "fd n"). In this example, we want turtles to always move one step at a time, so we always use "patch-ahead 1".
In addition to simulating barriers, this technique can also be used to test whether a turtle is near something important, such as food or another turtle.
To see this in use in real models see Ants, Slime, Gas Chromatography, Simple Kinetics, GasLab and Connected Chemistry models, various games, and Wealth Distribution.
Note that the code in this model only prevents the turtle from landing on a blue patch. It doesn't prevent the turtle from:
- following a path which crosses a blue patch
- visually touching a blue patch
The turtles may cross a blue patch because in NetLogo the "fd 1" command is equivalent to "jump 1" -- in other words, the turtle simply disappears from its old location and reappears in its new location, rather than moving continuously. You could add more code to the model to ensure that the turtle's path never crossed a blue patch, but the math for that gets a bit complicated.
The turtles may visually touch a blue patch because to NetLogo, the turtles are really just their center points. We represent the turtle visually with a certain size and shape, but to NetLogo, the turtle is a point with no extent, and that point is always on one and only one patch. Again, you could add "collision detection" code to the model to ensure that the center point never came within a certain distance of any patch, but the math for that would get complicated. This kind of "collision detection" is not built in to NetLogo.
Next Patch Example
; 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. ; ;; This procedure sets up the patches and turtles to setup ;; Clear everything. clear-all ;; This will create a "checkerboard" of blue patches. Every third patch will be ;; blue (remember modulo gives you the remainder of a division). ask patches with [pxcor mod 3 = 0 and pycor mod 3 = 0] [ set pcolor blue ] ;; This will make the outermost patches blue. This is to prevent the turtles ;; from wrapping around the world. Notice it uses the number of neighbor patches rather than ;; a location. This is better because it will allow you to change the behavior of the turtles ;; by changing the shape of the world (and it is less mistake-prone) ask patches with [count neighbors != 8] [ set pcolor blue ] ;; This will create turtles on number-of-turtles randomly chosen ;; black patches. ask n-of number-of-turtles (patches with [pcolor = black]) [ sprout 1 [ set color red ] ] end ;; This procedure makes the turtles move to go ask turtles [ ;; This important conditional determines if they are about to walk into a blue ;; patch. It lets us make a decision about what to do BEFORE the turtle walks ;; into a blue patch. This is a good way to simulate a wall or barrier that turtles ;; cannot move onto. Notice that we don't use any information on the turtle's ;; heading or position. Remember, patch-ahead 1 is the patch the turtle would be on ;; if it moved forward 1 in its current heading. ifelse [pcolor] of patch-ahead 1 = blue [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount. [ fd 1 ] ;; Otherwise, it is safe to move forward. ] tick end ; *** NetLogo 4.0.4 Code Example Copyright Notice *** ; ; (C) 2004 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 ***