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

Published on 12 May 2024 09:40 PM

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

Source code in the wren programming language

import "random" for Random
import "io" for Stdin

var rand = Random.new()
var rows = 20
var cols = 30
var p    = 0.01
var f    = 0.001
var rx   = rows + 2
var cx   = cols + 2

var step = Fn.new { |dst, src|
    for (r in 1..rows) {
        for (c in 1..cols) {
            var x = r*cx + c
            dst[x] = src[x]
            if (dst[x] == "#") {
                // rule 1. A burning cell turns into an empty cell
                dst[x] = " "
            } else if(dst[x] == "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.float() < f) {
                    dst[x] = "#"
                }
            } else {
                // rule 4. An empty space fills with a tree with probability p
                if (rand.float() < p) dst[x] = "T"
            }
        }
    }
}

var print = Fn.new { |model|
    System.print("__" * cols)
    System.print()
    for (r in 1..rows) {
        for (c in 1..cols) System.write(" %(model[r*cx+c])")
        System.print()
    }
}

var odd  = List.filled(rx*cx, " ")
var even = List.filled(rx*cx, " ")
for (r in 1 ..rows) {
    for (c in 1..cols) {
        if (rand.int(2) == 1) odd[r*cx+c] = "T"
    }
}
while (true) {
    print.call(odd)
    step.call(even, odd)
    Stdin.readLine()

    print.call(even)
    step.call(odd, even)
    Stdin.readLine()
}


  

You may also check:How to resolve the algorithm Peano curve step by step in the Ruby programming language
You may also check:How to resolve the algorithm GUI component interaction step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Unbias a random generator step by step in the Aime programming language
You may also check:How to resolve the algorithm Pernicious numbers step by step in the Java programming language
You may also check:How to resolve the algorithm Fivenum step by step in the Julia programming language