

 This page uses content from Wikipedia. The original article was at Forest-fire model. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



Task

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)

 A burning cell turns into an empty cell
 A tree will burn if at least one neighbor is burning
 A tree ignites with probability   f    even if no neighbor is burning
 An empty space fills with a tree with probability    p 

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.



Related tasks

   See   Conway's Game of Life 
   See   Wireworld.


class Forest_Fire
  Neighborhood = [-1,0,1].product([-1,0,1]) - [0,0]
  States = {empty:" ", tree:"T", fire:"#"}
 
  def initialize(xsize, ysize=xsize, p=0.5, f=0.01)
    @xsize, @ysize, @p, @f = xsize, ysize, p, f
    @field = Array.new(xsize+1) {|i| Array.new(ysize+1, :empty)}
    @generation = 0
  end
 
  def evolve
    @generation += 1
    work = @field.map{|row| row.map{|cell| cell}}
    for i in 0...@xsize
      for j in 0...@ysize
        case cell=@field[i][j]
        when :empty
          cell = :tree  if rand < @p
        when :tree
          cell = :fire  if fire?(i,j)
        else
          cell = :empty
        end
        work[i][j] = cell
      end
    end
    @field = work
  end
 
  def fire?(i,j)
    rand < @f or Neighborhood.any? {|di,dj| @field[i+di][j+dj] == :fire}
  end
 
  def display
    puts "Generation : #@generation"
    puts @xsize.times.map{|i| @ysize.times.map{|j| States[@field[i][j]]}.join}
  end
end
 
forest = Forest_Fire.new(10,30)
10.times do |i|
  forest.evolve
  forest.display
end