Physical State - adolgert/CTDE.jl GitHub Wiki

The physical state is composed of a set of disjoint substates. Each substate can be read and written with a key.

##Chemical Species Example For instance, a vector of counts of chemical species can be simply chemicals=zeros(Int, 20). In this case, the key is the integer index of the chemical and the value is an integer species count.

##Rabbits Running Example If there are interchangeable rabbits running on a grid, and the rabbits can get sick, one possible representation specifies a grid of rabbits and whether they are sick. The key is a tuple (i, j) which is a location on the board if j>0, but if j==0, then it's the disease state of rabbit i.

type PhysicalState
    board::Array{Int, 2}
    disease::Array{Int, 1}
end

function getindex(state::PhysicalState, i, j)
    if j>0
        return state.board[i, j]
    else
        return state.disease[i]
    end
end
function setindex!(state::PhysicalState, v, i, j)
    if j>0
        state.board[i, j]=v
    else
        state.disease[i]=v
    end
end

##Susceptible-Infected-Recovered Example Want to be fancy? The key doesn't have to be a key. It can be an object that is, itself, the state. This is trickier. When defining the transitions, the transitions remember the keys they need, so they keep a pointer to the state through the keys.

type Count
    v::Int
end
type SIR
    s::Count
    i::Count
    r::Count
end
getindex(state::SIR, key::Count)
    return key.v
end
setindex!(state::SIR, v::Int, key::Count)
    key.v=v
end

This might be slower, but it does away with the notion of a key.