CA                  package:simecol                  R Documentation

_S_t_o_c_h_a_s_t_i_c _C_e_l_l_u_l_a_r _A_u_t_o_m_a_t_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     'simecol' example: This model simulates a stochastic cellular
     automaton.

_U_s_a_g_e:

     data(conway)

_F_o_r_m_a_t:

     An S4 object according to the 'gridModel' specification. The
     object contains the following slots:


     '_m_a_i_n' functions with the state transition rules of Coway's Game
          of Life.

     '_p_a_r_m_s' a list with two vector elements:

          '_p_b_i_r_t_h' probability of birth,

          '_p_d_e_a_t_h' death probability, dependend on neighbors.


     '_t_i_m_e_s' number of time steps to be simulated.

     '_i_n_i_t' a matrix, giving the initial state of the cellular grid
          (default: rectangle in the middle of the grid).


_D_e_t_a_i_l_s:

     To see all details, please have a look into the implementation
     below.

_S_e_e _A_l_s_o:

     'sim', 'parms', 'init', 'times'.

_E_x_a_m_p_l_e_s:

     ##============================================
     ## Basic Usage:
     ##   work with the example
     ##============================================
     data(CA)
     times(CA)["to"] <- 10
     plot(sim(CA))

     set.seed(345)
     times(CA)["to"] <- 50
     CA <- sim(CA)

     library(lattice)
     tcol <- (terrain.colors(13))[-13]
     x <- out(CA, last=TRUE)
     x <- ifelse(x == 0, NA, x)
     print(levelplot(x,
                  cuts = 11,
                  col.regions = tcol,
                  colorkey=list(at=seq(0,55,5))
     ))

     ##============================================
     ## Implementation:
     ##   The code of the CA model
     ##============================================
     CA <- new("gridModel",
       main = function(time, init, parms) {
         z     <- init
         nb    <- eightneighbors(z)
         pgen  <- 1 - (1 - parms$pbirth)^nb
         zgen  <- ifelse(z == 0 &
                    runif(z) < pgen, 1, 0)
         zsurv <- ifelse(z >= 1 &
                    runif(z) < (1 - parms$pdeath),
                    z + 1, 0)
         zgen + zsurv
       },
       parms = list(pbirth = 0.02, pdeath = 0.01),
       times = c(from = 1, to = 50, by = 1),
       init = matrix(0, nrow = 40, ncol = 40),
       solver = "iteration"
     )
     init(CA)[18:22,18:22] <- 1
     ##============================================

