How to resolve the algorithm Forest fire step by step in the Java programming language
How to resolve the algorithm Forest fire step by step in the Java programming language
Table of Contents
Problem Statement
Implement the Drossel and Schwabl definition of the forest-fire model.
It is basically a 2D cellular automaton where each cell can be in three distinct states (empty, tree and burning) and evolves according to the following rules (as given by Wikipedia) Neighborhood is the Moore neighborhood; boundary conditions are so that on the boundary the cells are always empty ("fixed" boundary condition). At the beginning, populate the lattice with empty and tree cells according to a specific probability (e.g. a cell has the probability 0.5 to be a tree). Then, let the system evolve. Task's requirements do not include graphical display or the ability to change parameters (probabilities p and f ) through a graphical or command line interface.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Forest fire step by step in the Java programming language
The provided Java code simulates a fire spreading through a forest. It uses a grid-based model to represent the forest, where each cell can be in one of three states: burning, a tree, or empty. The fire spreads according to a set of probabilistic rules based on the F and P variables and the presence of adjacent burning or empty cells. The TREE_PROB variable controls the initial probability of a cell being a tree.
The Fire class contains the main logic for simulating the fire and generating the forest map. Here's a breakdown of the methods:
-
populate(): This method creates a random forest map with a specified width and height, with each cell initially being either a tree or empty. The TREE_PROB variable determines the probability of a cell being a tree.
-
process(): This method simulates one time step of the fire spreading. It iterates over each row in the forest and applies the processRows method to update the row based on the states of the adjacent cells.
-
processRows(): This method updates a single row in the forest by checking the state of each cell and applying the fire spreading rules. It considers the cells above, below, and to the left and right of the current cell and determines whether the cell should become burning, a tree, or remain empty.
-
processN(): This method simulates the fire spreading for a specified number of time steps (n). It repeatedly calls the process method to update the forest map.
-
processNPrint(): This method is similar to processN but also prints the forest map after each time step.
-
print(): This method prints the current state of the forest map to the console.
In the main method, the code initializes a forest map with a predefined layout and then simulates the fire spreading for 10 time steps using the processNPrint method. Additionally, it demonstrates the generation and simulation of a random forest map.
Overall, this code provides a flexible and customizable way to model and simulate the spread of fire in a forest, taking into account probabilistic factors and the interactions between neighboring cells.
Source code in the java programming language
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
public class Fire {
private static final char BURNING = 'w'; //w looks like fire, right?
private static final char TREE = 'T';
private static final char EMPTY = '.';
private static final double F = 0.2;
private static final double P = 0.4;
private static final double TREE_PROB = 0.5;
private static List<String> process(List<String> land){
List<String> newLand = new LinkedList<String>();
for(int i = 0; i < land.size(); i++){
String rowAbove, thisRow = land.get(i), rowBelow;
if(i == 0){//first row
rowAbove = null;
rowBelow = land.get(i + 1);
}else if(i == land.size() - 1){//last row
rowBelow = null;
rowAbove = land.get(i - 1);
}else{//middle
rowBelow = land.get(i + 1);
rowAbove = land.get(i - 1);
}
newLand.add(processRows(rowAbove, thisRow, rowBelow));
}
return newLand;
}
private static String processRows(String rowAbove, String thisRow,
String rowBelow){
String newRow = "";
for(int i = 0; i < thisRow.length();i++){
switch(thisRow.charAt(i)){
case BURNING:
newRow+= EMPTY;
break;
case EMPTY:
newRow+= Math.random() < P ? TREE : EMPTY;
break;
case TREE:
String neighbors = "";
if(i == 0){//first char
neighbors+= rowAbove == null ? "" : rowAbove.substring(i, i + 2);
neighbors+= thisRow.charAt(i + 1);
neighbors+= rowBelow == null ? "" : rowBelow.substring(i, i + 2);
if(neighbors.contains(Character.toString(BURNING))){
newRow+= BURNING;
break;
}
}else if(i == thisRow.length() - 1){//last char
neighbors+= rowAbove == null ? "" : rowAbove.substring(i - 1, i + 1);
neighbors+= thisRow.charAt(i - 1);
neighbors+= rowBelow == null ? "" : rowBelow.substring(i - 1, i + 1);
if(neighbors.contains(Character.toString(BURNING))){
newRow+= BURNING;
break;
}
}else{//middle
neighbors+= rowAbove == null ? "" : rowAbove.substring(i - 1, i + 2);
neighbors+= thisRow.charAt(i + 1);
neighbors+= thisRow.charAt(i - 1);
neighbors+= rowBelow == null ? "" : rowBelow.substring(i - 1, i + 2);
if(neighbors.contains(Character.toString(BURNING))){
newRow+= BURNING;
break;
}
}
newRow+= Math.random() < F ? BURNING : TREE;
}
}
return newRow;
}
public static List<String> populate(int width, int height){
List<String> land = new LinkedList<String>();
for(;height > 0; height--){//height is just a copy anyway
StringBuilder line = new StringBuilder(width);
for(int i = width; i > 0; i--){
line.append((Math.random() < TREE_PROB) ? TREE : EMPTY);
}
land.add(line.toString());
}
return land;
}
//process the land n times
public static void processN(List<String> land, int n){
for(int i = 0;i < n; i++){
land = process(land);
}
}
//process the land n times and print each step along the way
public static void processNPrint(List<String> land, int n){
for(int i = 0;i < n; i++){
land = process(land);
print(land);
}
}
//print the land
public static void print(List<String> land){
for(String row: land){
System.out.println(row);
}
System.out.println();
}
public static void main(String[] args){
List<String> land = Arrays.asList(".TTT.T.T.TTTT.T",
"T.T.T.TT..T.T..",
"TT.TTTT...T.TT.",
"TTT..TTTTT.T..T",
".T.TTT....TT.TT",
"...T..TTT.TT.T.",
".TT.TT...TT..TT",
".TT.T.T..T.T.T.",
"..TTT.TT.T..T..",
".T....T.....TTT",
"T..TTT..T..T...",
"TTT....TTTTTT.T",
"......TwTTT...T",
"..T....TTTTTTTT",
".T.T.T....TT...");
print(land);
processNPrint(land, 10);
System.out.println("Random land test:");
land = populate(10, 10);
print(land);
processNPrint(land, 10);
}
}
You may also check:How to resolve the algorithm SHA-256 step by step in the Ada programming language
You may also check:How to resolve the algorithm Modular arithmetic step by step in the Mercury programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Dyalect programming language
You may also check:How to resolve the algorithm Church numerals step by step in the Go programming language
You may also check:How to resolve the algorithm Sorting algorithms/Cocktail sort with shifting bounds step by step in the Arturo programming language