Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due
COMP1100
Assignment 2: Forest-Fire Simulator
COMP1100 Assignment 2,Semester 2, 2024
In this assignment,you will use Haskell to develop a simple simulator for modelling the spread of forest fires.
This assignment is worth 17%of your final grade.
Due Time: Sunday 20 October 2024,23:00 Canberra time sharp.Late submissions will not be marked without an approved extension.Extensions can only be applied for through the Extension App.Students with Education Access Plans still need to use this App.
Required Knowledge
· Up to Week 6 lab for list-handling and parametric polymorphism.
· Week 7 lab for unit testing and coding style.
· Week 8 lab for higher-order functions.
For help with report writing or assessment planning,seek support from ANU Academic Skills early on.
Marking Scheme
Marked out of 100.
Task Marks
Task 1:Grid data structure 25
Task 2:Helpers of the simulator 15
Task 3:Main part of the simulator 20
Task 4:Functions for density-plotting 10
Task 5:Unit Testing 10
Task 6:Style 10
Task 7:Technical Report 10
TOTAL 100
From this assignment onwards,code that does not compile and run will be penalised.This means that both the commands cabal v2-run forestfire and cabal v2-test must run without errors.If either if those commands fails with an error,a heavy mark deduction will be applied.The deduction will be the same regardless of which of these commands fails.If you have a partial solution that you cannot get working,you should comment it out and write an additional comment directing your tutor's attention to it.
Repository
1.Fork the assignment repository and create a project for it in VSCode,following the same steps as in [Lab 2](/courses/comp1100/labs/02/.The assignment repository is at https://gitlab.cecs.anu.edu.au/comp1100/2024s2/2024s2studentfiles/comp1100-2024s2-asst2.
2.Add our version of the repository as a“remote”called upstream.This allows us to provide additional fixes in the unlikely case that they are required.Do the following:
o Go to the command palette in VSCode by pressing Ctrl +Shift +p
o Type git remote
o Click Git:Add Remote
o Enter upstream into the box for the remote name
o The remote URL:
https://gitlab.cecs.anu.edu.au/comp1100/2024s2/2024s2studentfiles/comp1100- 2024s2-asst2.
Most of your code will be written in src/Grid.hs and src/ForestFire.hs.You will also write your own unit tests in src/ForestFireTest.hs.Below is an overview of all the files in the repository.
Key Files
· src/Grid.hs is where you willdefine the polymorphic data structure “Grid”and its associated functions.
· src/ForestFire.hs is where you will programme the behaviour of the simulator
· src/ForestFireTest.hs is where you will write your own unit tests.Some guiding examples are already contained.
Other Files
The repository contains other files,which you do not need to look at.Outline provided just for your information.
· src/TestPatterns.hs contains some pre-defined scenarios for the simulator to run on.
·src/ForestRenderer.hs contains functions for rendering forests and plotting the tree-densities on the screen.
·src/Testing.hs defines the format of the unit tests.This format is used in src/ForestFireTest.hs.
· src/App.hs is the main program that specifies the interaction with key-presses, etc.
·app/Main.hs launches the main program above.
· test/Main.hs runs the unit tests defined in src/ForestFireTest.hs.
·comp1100-assignment2.cabal instruts cabal how to compile the main program.
· Setup.hs tells cabal that this is a normal package with no unusual build steps. Beyond this course,complex packages need more complex setup.
Compiling and Running Your Program
As before,we use the cabal tool to compile and run the programs of this assignment.The relevant commands are similar to last time:
·cabal v2-build:Compile the main program.
· cabal v2-run:Compile(if necessary),and run the main program.
·cabal v2-repl:Load the main program's functions into GHCi.
· cabal v2-test:Build and run the unit tests.(The unit tests will abort on the first failure or the first call to an undefined function.)
You should execute these cabal v2-commands in the top-level directory of your project:~/comp1100/assignment2(i.e.,the directory you are in when you launch a terminal from VSCodium)
The Main Program
The main program in app/Main.hs uses CodeWorld,just like Assignment 1,and responds to the following keys:
Key Effect
|
1 |
Reset the simulation to the first test pattern. |
|
2 |
Reset the simulation to the second test pattern. |
|
3 |
Reset the simulation to the third test pattern. |
|
p |
Toggle between the density plot and the simulation. |
|
|
Develop the simulation by 1 day. |
|
|
Develop the simulation multiple days. |
|
+ |
Make develop more day. |
|
Make develop fewer days. |
|
You can also click on sites with the mouse to change them,if you want to play around with different patterns.
If you try to use the main program without completing Task 1,or you try to run the simulation before completing Task 2,the test program may crash with the following error:
"Exception in blank-canvas application:"
Prelude.undefined
If this happens,refresh the browser to continue.
Simulator
The simulator runs a discrete-time model of a forest,and is mainly defined in ForestFire.hs. The simulator has two modes:
1.A visual model of a forest,made up of a grid of sites.
2.A plot of the tree density of the forest over time.
(The key ‘p'toggles between the two components,as explained in“Overview of the Test Program").Every day,each site in the forest is updated according to a set of rules,which depend on the site's surrounding and the number of days the site has been in its current condition.
The assumptions are as follows:
·A site can either be barren,have trees,or be on fire.
·If a site is barren at t.
o If more than half of the surrounding sites have trees,the site will have trees at t +1.
o If the site's condition has been barren for more than 20 days,it will have trees at t+1.
o Otherwise,the site will remain barren at t+1.
·If a site has trees at t.
o If any of the sites directly north,south,east or west of it are on fire at t,it will be on fire at t+1.
o Otherwise,if 2 or more of the sites diagonally adjacent to it are on fire at t,it will be on fire at t+1.
o If the site has had trees for more than 50 days,it will be on fire at t+1.
o Otherwise,the site will continue to have trees in t+1.
·If a site is on fire:
o If the site has been on fire for 2 or more days at t,it will become barren at t+1.
o Otherwise,it will remain on fire.
Whenever a site changes condition,we consider it to be at day O of having that condition. When the condition of a site does not change at an update of the model,that site will have been in that condition for one more day.
We have included a function fastForest ::Forest ->Int ->Forest in src/App.hs,which updates the forest a given number of days into the future.You may use this function to test your program once you have finished Task 2.
Tasks
We recommend also reading Task 5“Unit Testing”now.In the programming tasks(Tasks 1-
4),it is useful to work on the programming and the unit testing in parallel.
Task 1:Grid
This task is about the“grid”data structure in src/Grid.hs and its basic functions.
You can load the file src/Grid.hs individually in GHCi to test its functions,without needing to build the whole program with cabal and the other .hs files.
1.Define the datatype Grid a and its associated helper-functions.
A grid should consist of the following elements in this particular order:(a)its number of rows,of type Int;(b)its number of columns,of type Int;(c)its cells,as a list of values of type a.
The helper-functions are the following:
o getNumRows returns the number of rows;
o getNumCols returns the number of columns;
o getCells returns the list of cells;
o makeGrid returns the grid,given (a),(b),(c)above.
2.Define the function rowmaj::Int->Int->[Coord].
This function's consists of the two dimensions of a table:the number of rows followed by the number of columns.
The output is the list of coordinates in row-major order.Note that a coordinate is a pair of integers(x,y),where we start from the top-left of the table,goes xsteps
rightwards and then ysteps downwards.
For example,rowMaj 23 should return [(0,0),(1,0),(2,0),(0,1),(1,1),(2,1)].
If at least one of the two arguments is smaller than or equal to 0,then return the empty list.
3.Define the function allCoords ::Grid a->[Coord].
A grid Grid nr nc cells is well-formed if the number of cells specified by nr and nc is the same as the actual number of cells length cells.
Given a well-formed grid G,this function outputs all coordinates of Glisted in row- major order.If Gis not well-formed,then this function outputs the empty list.
4.Define the function getCellMaybe ::Grid a ->Coord ->Maybe a.
Given a grid Gand a coordinate (x,y),this function returns the value at the cell
referenced by (x,y)in G.If (x,y)is not a possible coordinate in G,or if Gis not well- formed,this function returns Nothing.
5.Define the function getOrths ::Grid a->Coord ->[a].
Given a grid and a coordinate (x,y),this function returns the contents in the orthogonal neighbours (i.e.up,down,left,right)of the cell referenced by(x,y).Note that not every coordinate has four orthogonal cells.
Hint:The built-in function catMaybes may be useful here.It is defined in the package Data.Maybe,and already imported into src/Grid.hs.
Remember to handle potentially problematic inputs.If the grid is not well-formed,or if (x,y)is out of bounds,then return the empty list.
6.Define the function getDiags ::Grid a->Coord ->[a].
This function is similar to get0rths,but returns the diagonal neighbours(i.e.up-left, up-right,down-left,down-right).If the grid is not well-formed,or if (x,y)is out of bounds,then return the empty list.
Task 2:ForestFire(Datatypes and Helpers) This task implements the fundamental datatypes and helper functions in
src/ForestFire.hs.
1.Define the datatype Condition,which represents the three states each site can be in (see the “Simulator”section earlier).
2.Define the function charToCond ::Char ->Condition,following the rules below.
o A 'b'character represents a barren site.
o A't'character represents a site containing trees.
o Any other character represents a site on fire.
Examples of forests are defined in src/TestPatterns.hs,encoded as strings,and these strings need to be converted into values of type Forest for our simulator to run.The strings are converted to forests by the pre-defined function parseForest, which relies on the charToCond function to turn each Char into a Site.
3.Define the function cycleCondition ::Condition ->Condition.
This allows the user to change the condition of a site in a forest by repeatedly clicking the mouse (similar to nextColour from Assignment 1).Namely,a barren site should become tree,a site with tree should become fire,and fire should become barren.
4.Define the function renderArea ::Forest ->Site->Picture.
Pre-defined functions in src/ForestRenderer.hs can draw a grid of sites,using renderArea as a helper to draw each site according to the following rules:
o Each site should be drawn as a solid coloured rectangle,centred at the origin (i.e.not translated).
o The overall width of the grid is given by paneWidth,and the overall height of the grid is given by paneHeight.(Both of these values are pre-defined on the top of the file.)
With these overall dimensions,the size of each site can be calculated using the numbers of rows and columns of the forest,which are contained in the first
argument of renderArea.
o The colour of each site should be determined by its condition.A barren site should be brown,a site on fire should be red,and a site with trees should be green.
5.Define the function count ::[a]->(a->Bool)->Int.
The function-call count xs f returns the number of values x in the list xs such that f x =True.
6.Define the function density ::[a]->(a ->Bool)->Double.
Similar to count,the function-call density xs f returns the proportion of values
x in the list xs such that f x =True.
If xs is empty,make density return zero by default.
At this stage,if you run the command cabal v2-run forestfire,there will be a link at the end of the terminal output.Opening the link in a browser,you should see the following:
You should be able to see different initial states by pressing the number keys 1,2 and 3.
You should also be able to change individual cells by clicking on them.