[R] identify connected cells in a cellular automaton
matpops
mathiaspops at hotmail.fr
Wed Aug 24 11:17:31 CEST 2011
Hello,
I’m working on a simulation using a cellular automata. The scenario is:
Each cell represent a land plot. Each plot can have 3 different states:
-The owner is a private individual (P)
-The owner is a company ( C)
-The land is abandoned/not used (A)
The first year the cellular automaton is composed with one cell C in the
middle. The rest of the cells are in P state.
Each year a random number of cells P become A. The company buys the land A
connected to C cells. The expansion process occurs only around the C cells.
When the company buy a new cell its budget decrease of 10 cu (currency
unit). When the amount of connected cells is bigger than 3 the each new
cells connected start to generate earnings. The process occurs until the
budget is equal to 0.
I wish now introduce a new rule: each year the company start to buy
available land connected to it’s own land then purchase land (cells) not
connected according to its budget.
The problem is now to calculate the total earning of the company since with
the new rule some disconnected plot will be created. I need to calculate the
earning of each disconnected land ( since plot with less than three cells
connected will not generate earnings).
With the first simulation calculate the total earnings was easy since every
C cells was connected ( I only had to calculate the area own by the
company).
I’m looking for a way to identify the number of connected cells for each
parcel of company land. Any suggestion?
This is the code of the first simulation:
# starting population
m <- 10 # nbr of rows
n <- 10 # nbr of cols
x <- rep(0, m*n) # cells
dim(x) <- c(m,n) # m rows n cols
# company land is in the middle
x[floor((m+1)/2),floor((n+1)/2)] <- company
# image(x, col=c("white","green"))
span <- 10 # time span in periods
p_avail <- 1/10 # probability of land becoming available
company_area <- sum(x==company) # initial company area
newly_owned <- function(t){ # newly owned area in year t
company_area[[t]] - company_area[[t-1]]}
earning <- function(t){ # earning in year t
ifelse(critical_size < company_area[[t]],
cells_earn*(company_area[[t-1]] - critical_size),0)}
revenue <- 0 # for t=1
net_earned <- 0 # for t=1
for(t in 2:span){
# some cells become available
r01 <- ifelse(x == private, matrix((runif(x,0,1)),nrow=m,ncol=n), x)
x <- ifelse(r01 <= p_avail, available, x)
# buy available lands around the company land
x <- ifelse(
1 == neighbors(x,state=company,wdist=wdist) & x==available,
company, x)
company_area[[t]] <- sum(x==company)
revenue[[t]] <- earning(t)
expansion_cost <- newly_owned(t)*land_cost # the cost of buying new land
net_earned[[t]] <- revenue[[t]] - expansion_cost
budget[[t]] <- budget[[t-1]] + net_earned[[t]]
if (budget[[t]]<=0) break
# white = private owned land, blue = available, green = company
image(x, col=c("white","blue","green"))
}
--
View this message in context: http://r.789695.n4.nabble.com/identify-connected-cells-in-a-cellular-automaton-tp3764851p3764851.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list