How to resolve the algorithm Forest fire step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Forest fire step by step in the Go 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 Go programming language

This code simulates the spread of fire in a forest represented as a grid of cells, some of which contain trees and others which are empty. The simulation follows four basic rules:

  1. Burning cells turn into empty cells.
  2. Trees burn if at least one of their eight neighbors is burning.
  3. Trees can spontaneously ignite with a small probability.
  4. Empty spaces fill with trees with a small probability.

The source code is written in the Go programming language and makes use of the following libraries:

  • fmt: for printing output to the console.
  • math/rand: for generating random numbers.
  • strings: for string manipulation.

The code starts by defining some constants that control the size of the forest and the probabilities of various events happening. The rows and cols constants specify the number of rows and columns in the forest, respectively. The p and f constants specify the probabilities of an empty space filling with a tree and a tree spontaneously igniting, respectively.

The main function of the program initializes two arrays, odd and even, each of which represents the state of the forest at a given time step. The odd array represents the state of the forest at odd time steps, while the even array represents the state of the forest at even time steps.

The main loop of the program alternates between printing the state of the forest at odd and even time steps, and then updating the state of the forest using the step function. The step function takes two arrays as input: the source array, which represents the state of the forest at the previous time step, and the destination array, which represents the state of the forest at the current time step. The step function updates the state of each cell in the destination array based on the state of the corresponding cell in the source array and the four rules described above.

The print function is used to print the state of the forest to the console. The print function takes an array representing the state of the forest as input and prints a grid of characters representing the state of each cell in the forest. Cells that are empty are represented by two spaces, while cells that contain trees are represented by a single character, either '#' (burning) or 'T' (alive).

The step function is the core of the simulation. The step function takes two arrays as input: the source array, which represents the state of the forest at the previous time step, and the destination array, which represents the state of the forest at the current time step. The step function updates the state of each cell in the destination array based on the state of the corresponding cell in the source array and the four rules described above.

The first rule is that burning cells turn into empty cells. This is implemented by setting the state of any cell in the destination array that is burning in the source array to empty.

The second rule is that trees burn if at least one of their eight neighbors is burning. This is implemented by checking the state of the eight cells surrounding the current cell in the source array. If any of the eight neighbors is burning, the state of the current cell in the destination array is set to burning.

The third rule is that trees can spontaneously ignite with a small probability. This is implemented by generating a random number between 0 and 1. If the random number is less than the probability of spontaneous ignition, the state of the current cell in the destination array is set to burning.

The fourth rule is that empty spaces fill with trees with a small probability. This is implemented by generating a random number between 0 and 1. If the random number is less than the probability of a tree growing, the state of the current cell in the destination array is set to a tree.

Source code in the go programming language

package main

import (
    "fmt"
    "math/rand"
    "strings"
)

const (
    rows = 20
    cols = 30
    p    = .01
    f    = .001
)

const rx = rows + 2
const cx = cols + 2

func main() {
    odd := make([]byte, rx*cx)
    even := make([]byte, rx*cx)
    for r := 1; r <= rows; r++ {
        for c := 1; c <= cols; c++ {
            if rand.Intn(2) == 1 {
                odd[r*cx+c] = 'T'
            }
        }
    }
    for {
        print(odd)
        step(even, odd)
        fmt.Scanln()

        print(even)
        step(odd, even)
        fmt.Scanln()
    }
}

func print(model []byte) {
    fmt.Println(strings.Repeat("__", cols))
    fmt.Println()
    for r := 1; r <= rows; r++ {
        for c := 1; c <= cols; c++ {
            if model[r*cx+c] == 0 {
                fmt.Print("  ")
            } else {
                fmt.Printf(" %c", model[r*cx+c])
            }
        }
        fmt.Println()
    }
}

func step(dst, src []byte) {
    for r := 1; r <= rows; r++ {
        for c := 1; c <= cols; c++ {
            x := r*cx + c
            dst[x] = src[x]
            switch dst[x] {
            case '#':
                // rule 1. A burning cell turns into an empty cell
                dst[x] = 0
            case 'T':
                // rule 2. A tree will burn if at least one neighbor is burning
                if src[x-cx-1]=='#'  || src[x-cx]=='#' || src[x-cx+1]=='#' ||
                    src[x-1] == '#'  ||                   src[x+1] == '#'  ||
                    src[x+cx-1]=='#' || src[x+cx]=='#' || src[x+cx+1] == '#' {
                    dst[x] = '#'

                    // rule 3. A tree ignites with probability f
                    // even if no neighbor is burning
                } else if rand.Float64() < f {
                    dst[x] = '#'
                }
            default:
                // rule 4. An empty space fills with a tree with probability p
                if rand.Float64() < p {
                    dst[x] = 'T'
                }
            }
        }
    }
}


  

You may also check:How to resolve the algorithm Steffensen's method step by step in the ATS programming language
You may also check:How to resolve the algorithm Bitwise IO step by step in the Wren programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the C programming language
You may also check:How to resolve the algorithm HTTP step by step in the C programming language
You may also check:How to resolve the algorithm World Cup group stage step by step in the Racket programming language