Sunteți pe pagina 1din 8

Department of Computer Science, Australian National University COMP1100 Introduction to Programming and Algorithms Semester 1, 2011

Assignment 1 Autonomous Agents and Emergent Behaviour Langtons Ants


Due: 7 p.m. on Wednesday 6th April, 2011

Outline
An autonomous agent is an entity that interacts with its environment, possibly including other agents, but acts independently in the sense that it is not guided by commands from a supervisor, nor does it work towards some global plan. An autonomous agents behaviour is determined by its immediate local neighbourhood. Despite this independence and simplicity, complex behaviour can emerge, especially for collections of autonomous agents. Among the best known examples are the steering algorithms invented by Craig Reynolds, where boids follow independent rules, but the overall effect is that the agents form a ock that appears to be co-operating in its movement. See http://www.red3d.com/cwr/boids/ For this assignment we will develop a graphical simulation of another autonomous agent: Christopher Langtons Virtual Ant. The rules governing this ants actions are extremely simple, but even for a single ant surprisingly complex behaviour emerges by interaction with the environment that the ant itself has created. Langtons ants are an example of a cellular automaton, a concept developed by John von Neumann before the advent of the modern computer, which he also helped to develop. As is common for cellular automata, the world occupied by a virtual ant consists of a grid of discrete locations. At each discrete time step, the ant is facing either North, South, East or West. All of the locations on the grid are in one of two states: on or off, which we may display as being white and black respectively, for example. At each time step, the ant acts according to the following simple rules: If the ant is standing at an on location, then it switches it off and turns right 90 . Otherwise, if the ant is standing at an off location, then it switches it on and turns left 90 . The ant takes a step forward. The excellent book by Gary Flake, The Computational Beauty of Nature: Computer Explorations of Fractals, Chaos, Complex Systems, and Adaptation (MIT Press 1998) is a good starting point for nding out more about Langtons ants and other cool computational phenomena.

Assignment 1 Langtons Ants

The following diagrams show the rst eight steps that one of our virtual ants would take if initially facing South on a grid where all points are initially off.

As you can see, the ant quickly begins interacting with its own path. If we let it run for a while, say a few thousand steps, the grid looks like a chaotic mess.

Assignment 1 Langtons Ants

However, after about 10000 or so steps something very surprising happens: the ant falls into a regular pattern, building what James Propp, who discovered the phenomenon, called a highway.

The aim of this assignment is to develop a Haskell program which generates a graphical animation of a virtual ant.

These screen shots are from an implementation using a different geometry. In your assignment, the highway should head towards the bottom left of the window.

1 Required Tasks
Since this is your rst assignment, weve already done a lot of the work for you. The design and structure of the overall program is already determined and provided as a set of modules which you can download from the COMP1100 web site. The interface to the graphics package is outside the scope of the course, so that is provided in the module Wrapper.hs. The modules are: Ant.hs. Grid.hs, Point.hs, State.hs, Main.hs, and Wrapper.hs. 1.1 Complete the Ant module This module contains the types for representing virtual ants, and the functions that manipulate them. An ant is determined by its location and its direction. Its location is simply a cartesian

Assignment 1 Langtons Ants

coordinate (type Point). Rather than North, South, East and West, it is more convenient to use the coordinate system to specify directions, so X means along the x-axis, NegX means along the xaxis in a negative direction, and so on. Similarly, the values of type Turn are positive and negative rather than left and right. They are relative to the direction the ant is facing. In computer graphics, it is fairly common for the origin (0, 0) to be the top left corner of the window. That means the x-axis goes across the window and the y-axis goes down the window. That may seem odd, but the advantage is that when we work in 3 dimensions, the z-axis goes into the screen, which may seem more natural. In the Ant module we have supplied the signatures of four functions. You are to provide their implementations. turn :: Ant -> Turn -> Ant This function turns an ant in a positive or negative direction. It does not change its location. forward :: Ant -> Int -> Int -> Ant This function moves an ant forward one step in the direction it is facing. It does not change its direction. The second argument is the width of the grid, and the third argument is the height of the grid. If we had an unbounded universe, we could allow our ant to march off into the distance forever. Unfortunately, we are limited to a nite window, so we need to consider what to do when an ant wants to cross the boundary of the grid. If we do nothing, our program will crash when this occurs. Note: My strong advice is that you rst develop and test a version of forward which takes no account of the borders. In fact you may prefer to complete your entire program (and test it thoroughly up to the point where the ant reaches the border) before returning to modify forward to deal with this situation. A very common approach is to wrap around at the borders. For example, suppose we have a 10 10 grid, in which case the coordinates are (0, 0) to (9, 9). If the forward function would move our ant off the grid to (1, y), then instead we wrap around to (9, y). Similarly, instead of moving to an illegal (10, y) we wrap around to (0, y). The same approach is used in the y dimension too. The following screen shots demonstrate the effect.

Assignment 1 Langtons Ants

If you imagine bending the grid around so that the sides are touching and the top and bottom borders are touching, this is the effect we have created. In 3 dimensions the grid becomes a torus a doughnut shape. So the nal (the correct) version of forward moves the ant forward as above, but it also checks and manages wrapping at the edges. The width and height arguments allow us to check whether the ant has stepped off the grid, and if so, to wrap that position around to the opposite edge. An obvious approach is to use conditionals (i.e. guards) but modular arithmetic is an alternative. advance :: Ant -> State -> Int -> Int -> Ant The advance function turns the ant according to the state of the point it is on in the grid, and takes a step forward in that direction (wrapping at the borders if necessary). You have already written functions turn and forward to do these two actions separately, so advance will be dened in terms of those two functions. Make sure that you develop and carefully test each function individually using ghci before proceeding to the next step. 1.2 Complete the Grid module The Grid module provides a representation, using lists, of the world on which the ant moves. At all times, each grid element is in an On or Off state. Corresponding to the underlying coordinate geometry framework, the grid consists of a list of rows. The number of rows is determined by the height of the grid. Each row is a list of State values. The length of the rows is determined by the width of the grid. In the Grid module we have supplied the signatures of two functions. You are to provide their implementations. initial :: Int -> Int -> Grid This function takes two arguments, respectively specifying the width and the height of the grid. Calling the initial function with arguments w and h produces a value of type Grid, consisting of h Rows, each consisting of w Off state values. For example, initial 3 4 returns: [ [ Off , [ Off , [ Off , [ Off , Off , Off , Off , Off , Off ] , Off ] , Off ] , Off ] ]

update :: Grid -> Point -> Grid When an ant visits a grid point, the state of that point is changed from On to Off, or from Off to On. (This is what the next function in the State module is for.) The update function takes a grid of state values and the coordinates of a point that is to be changed, presumably because an ant has visited. That is, given some grid g and a point (x, y), update returns a grid identical to g except that the state at point (x, y) has been inverted. You may assume that the point is on the grid there is no need for error checking here. Hint: You may nd it simpler to rst dene a related but simpler worker function with type signature updateRow :: Row -> Int -> Row which takes a row r and an x-coordinate and returns a row identical to r except that the xth element has been inverted.

Assignment 1 Langtons Ants

1.3 Complete the Main module The Main module is the driver of the program. It initialises the grid and the ant, then calls the runCA function from the Wrapper module to set the animation going. The GridSizeX and GridSizeY variables specify the width and height of the grid respectively, and squareSize sets the size (in pixels) at which individual grid elements are displayed. You can change these values, but keep in mind that the size of the window is determined by the product of the square size and the grid dimensions, and you want the window to t on the screen. The warp parameter species how many steps the ant should take between each window refresh. During initial testing of your program, you should set warp to 1 so that each individual step is shown. The type World consists of a Grid and an Ant. The initial world (startWorld) consists of a grid initialised to Off everywhere, and an ant located at the centre and facing in the Y direction. startAnt sets the initial location and direction of the ant which you may change if you wish. You can also change the colours representing the states if you wish. advanceWorld :: World -> World Given a particular world conguration, this function returns the state of the world after an ant has taken a single step and the grid has been modied accordingly. The functions you have developed in the Ant and Grid modules are all you need to implement this function. 1.4 The State module The State module is very simple, dening the states in which each grid element may be. The next function simply cycles through On and Off, which is convenient for changing the state of grid elements as an ant visits. There is no need to change this module. However, note that one of the extension exercises considers a world where there are more than two different states grid elements may adopt. Clearly in that case the State module will be changed. 1.5 The Wrapper module This module interfaces to the ANUPlot library to render your virtual ant simulation. We dont expect you to understand this module and you should not modify it. 1.6 Document the modules Ant, Grid, State, Point and Main You will notice that there are almost no comments in any of the module templates. It is your job to add them (which means you have to understand them). We do not expect you to document Wrapper.hs. Every module should have a standard header comment, including at least a description of the purpose of the module, part of what program, programmers names, date and so on. There should be a type signature for each function. Names for every function, every parameter and every value should be informative. Every function should have a comment describing its purpose. These comments should immediately precede the function denition and its type signature. Similarly, every type synonym and algebraic data type denition should have a comment describing its purpose and interpretation.

Assignment 1 Langtons Ants

2 Extension Tasks
By completing the required tasks above you can achieve a mark of up to 80% for this assignment. The other 20% of marks can be obtained by completing some of the following optional extension tasks. Your basic program (i.e. the required tasks above) will be assessed separately from any extensions you attempt, so make sure you keep a copy of the original. 2.1 Several Ants It is easy to imagine that several ants on the same grid may create new and different effects as their paths intersect. Indeed that is the case. For example, one ant may erase anothers highway, or several ants may build more interesting highways together. This can be quite a straightforward extension and only requires modications to the Main module. (A consequence of good modular structure.) Rather than the World consisting of a Grid and a single Ant, there will be a list of Ants. The AdvanceWorld function will advance each of them and affect the grid in turn. Of course, you will not necessarily want the initial positions of all the ants to be the same. 2.2 Several States This is a more challenging and open-ended extension. Rather than the grid points being in one of only two states, On or Off, imagine a grid where there are three, four or more possible states (which we would render in different colours for the simulation). In that case, the rules for the virtual ants behaviour can similarly be more complex, turning left on some states and right on others. Its up to you to program their behaviour. A similar kind of variation is often called Turmites rather than Ants. Im sure you can nd out more and get some ideas from the web. Here are a few starting points. Have fun! http://mathworld.wolfram.com/Turmite.html http://tetrahedral.blogspot.com/2010/09/langtons-ant.html http://en.wikipedia.org/wiki/Turmite

Using the ANU Graphics Library


The ANU Graphics Library is not installed with the GHC libraries, so you will need it to be in the same directory as the scripts you are developing. The code templates for the assignment are provided as a tarball Ant.tgz on the course web site. You should download this archive to a sensible and convenient location in your home directory, then unpack it with a command like tar -zxf Ant.tgz to create an Ant directory. Alternatively, in MS Windows winzip should work. As well as the code template modules, there will be a subdirectory Graphics which contains the ANU Graphics Library. If you are working in MS Windows, you will need to comment out line 26 of Graphics/Plot/Animate.hs. Make no other modications to these les. You can work on most of the modules (Ant, Grid, State) as usual by simply loading them into ghci. However, the ANU Graphics library uses some non-standard extensions to Haskell so when you nally come to test the graphical display of the program you need to tell ghci about it:

Assignment 1 Langtons Ants

ghci - fglasgow - exts Main . hs When youre convinced your program is working properly, you can create an executable version: ghc - fglasgow - exts - O2 -- make Main . hs The -O2 ag tells the compiler to optimise the program. (Otherwise the animation will be rather slow.) The --make ag tells ghc to nd and compile imported scripts. This will leave an executable le called Main in the working directory. You can run it by typing ./Main at the terminal prompt. You can give the executable a better name (like ant) if you wish: ghc - fglasgow - exts - O2 -- make Main . hs -o ant

Assessment
This assignment forms 15% of your total assessment for COMP1100. Marks will be allocated according to the following scheme: Documentation and style: Technique: Completion of required tasks: Extension tasks: 4 marks 4 marks 4 marks 3 marks

Your extensions (3 marks) and the correctness of your program (4 marks) will be assessed by demonstrating them to your tutor during week 8 or 9 prac classes. You must attend your regular prac class to obtain any marks for this component of the assessment.

Submission
Your technique (4 marks) and your documentation and style (4 marks) will be assessed by your tutor inspecting a copy of your program that you will have submitted electronically, as described below. Late submissions will be penalised 10% for each day (or part thereof) past the deadline. Only submit your basic program, that is, your solutions to the required tasks 1.1 1.7. For this part of the assessment, you should not submit any extensions you have attempted. Extensions will be assessed during the demonstration sessions. You should submit your documented (and possibly modied) versions of Ant.hs, Grid.hs, State.hs, Point.hs and Main.hs modules as well as any new modules that you may have created. Do NOT submit Wrapper.hs or any of the ANUPlot modules. Assignment submission will be electronic. The only acceptable way to submit your assignment is with the command: submit comp1100 Asst1 < file names > You must be on the Computer Science Student System to run the submit command. Make sure you practice submitting your les well before the deadline. It is possible and perfectly acceptable to submit updated versions of your les. Our system will collect them all but only your most recent submission will be assessed. You should check that your submissions have been successful by clicking the View Marks button in StReaMS (http://cs.anu.edu.au/streams/). You should see a list of les together with the time and date of submission.

Assignment 1 Langtons Ants

S-ar putea să vă placă și