objects clones and collections
play

Objects, Clones and Collections Implementation and simulation with - PowerPoint PPT Presentation

Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects Objects, Clones and Collections Implementation and simulation with simecol An example


  1. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects Objects, Clones and Collections Implementation and simulation with simecol An example Implementation Simulation Dynamic (Ecological) Models and Scenarios with simecol Cloning Scenarios Equations Observers Outlook Daphnia Thomas Petzoldt 2D Zooplankton 3D Random Walk Conclusions

  2. What are Ecological Models? Two Examples from UseR!2006 Differential Equations Individual-Based (Cellular (e.g. Lotka-Volterra) automata, Random walk, . . . ) X = state.of(cell) dX 1 dt = b · X 1 − e · X 1 · X 2 N = neighbours(cell) if X = 1 and N in {2,3} then X := 1 dX 2 dt = d · X 1 + e · X 1 · X 2 else if X = 0 and N = 3 then X := 1 else X=0 3.0 2.5 2.0 predator prey 1.5 1.0 0.5 0.0 1 2 3 0 20 40 60 80 100 time time

  3. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Why Ecological Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects R is more productive than other environments Implementation and simulation with simecol 1. More flexible than mouse-click environments, An example Implementation 2. Matrix orientation, Packages, Graphics, Sweave ... Simulation Cloning Scenarios 3. More interactive than C++, Equations Observers 4. Modern computers are fast enough. Outlook Daphnia 2D Zooplankton 3D Random Walk Conclusions

  4. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Why Ecological Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects R is more productive than other environments Implementation and simulation with simecol 1. More flexible than mouse-click environments, An example Implementation 2. Matrix orientation, Packages, Graphics, Sweave ... Simulation Cloning Scenarios 3. More interactive than C++, Equations Observers 4. Modern computers are fast enough. Outlook Daphnia 2D Zooplankton R’s Soft Skills 3D Random Walk Conclusions ◮ Scientific development model (books and publications), ◮ GPL makes sharing code easy, ◮ Enthusiastic useR! community.

  5. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Ecological Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects Problems: Implementation and simulation with simecol ◮ 3 Modellers – 7 Programming styles, An example Implementation ◮ Copy & Paste to derive variants and scenarios? Simulation Cloning ◮ Reading code? ⇒ Better write a new program. Scenarios Equations Observers Outlook Daphnia 2D Zooplankton 3D Random Walk Conclusions

  6. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Ecological Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects Problems: Implementation and simulation with simecol ◮ 3 Modellers – 7 Programming styles, An example Implementation ◮ Copy & Paste to derive variants and scenarios? Simulation Cloning ◮ Reading code? ⇒ Better write a new program. Scenarios Equations Observers Outlook Approach: Daphnia 2D Zooplankton ◮ Provide a standard approach, 3D Random Walk Conclusions ◮ Use Object Oriented Programing – OOP, ◮ Package simecol: a model of ecological models.

  7. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? Ecological Models in R? Introductory examples Why R Problems Concepts What is typical? Simecol Objects Problems: Implementation and simulation with simecol ◮ 3 Modellers – 7 Programming styles, An example Implementation ◮ Copy & Paste to derive variants and scenarios? Simulation Cloning ◮ Reading code? ⇒ Better write a new program. Scenarios Equations Observers Outlook Approach: Daphnia 2D Zooplankton ◮ Provide a standard approach, 3D Random Walk Conclusions ◮ Use Object Oriented Programing – OOP, ◮ Package simecol: a model of ecological models. ?? What do ecological / dynamic models have in common?

  8. Objects, Clones and Collections Thomas Petzoldt Dynamic Models in R? State Transition Diagram Introductory examples Why R Problems Concepts What is typical? Simecol Objects Implementation and simulation with simecol An example initial values Implementation state transition observer x(t 0 ) Simulation function function Cloning Scenarios Equations Iterator x(t) u(t) y(t) Observers f(x, u, p, t) g(x, u, p, t) or Outlook inputs Integrator outputs Daphnia (external forcing) 2D Zooplankton 3D Random Walk omo Conclusions t = t + dt time steps

  9. Specification A whole model in one compact object. S4 Classes: odeModel, gridModel, rwalkModel, model: simObject indbasedModel main: function Equations and algorithms equations: list of funct. init: vector, matrix Necessary data parms: vector or list iteration, user defined times: vector or imported from: inputs: list, matrix deSolve, rootSolve or ddeSolve solver: char. or function data storage, screen output, observer: function logfiles, animation out: dataframe or list The results initfunc: function Tasks during initialization Slots for: Data Functions

  10. Example: Stochastic Cellular Automaton Spread of plants, animals, diseases, . . . Survival rules ◮ Number of direct neighbours: n n = n neighbours ∈ { 0 . . . 8 } , 2 ◮ Probability of seedling per adult number of adult neighbours neighbour: p seed , 2 3 ◮ Total probability of seedlings 1 per empty cell: p gen = 1 − (1 − p birth ) n n eight direct neighbours ◮ Probability of death p death , ◮ Time step ∆ t = 1, empty juvenile adult ◮ State: ◮ living cells: Z i , j = t (age), ◮ dead cells: Z i , j = 0.

  11. Stochastic Cellular Automaton in simecol CA <- new(’gridModel’, main = function(time, init, parms) { Z <- init with(parms, { nn <- eightneighbours(Z >= adult) # direct neighbours pgen <- 1 - (1 - pseed)^nn # probability product zgen <- ifelse(Z == 0 & runif(Z) < pgen, 1, 0) zsurv <- ifelse(Z >= 1 & runif(Z) < (1 - pdeath), Z + 1, 0) zgen + zsurv } ) }, parms = list(adult = 2, pseed = 0.2, pdeath = 0.1), times = c(from = 1, to = 50, by = 1), init = matrix(0, nrow = 80, ncol = 80), solver = ’iteration’, initfunc = function(obj) { # obj = ’object in creation’ init(obj)[38:42, 38:42] <- 1 # ... modify it obj # return the final object. } )

  12. Simulate the Model t = 0 library(’simecol’) # (1) define or load the model # source(); data(); ... # (2) simulate the model # Note: pass-back modification CA <- sim(CA) t = 50 # (3) plot the model # ... calls a specific plot-method plot(CA) # (4) Extract outputs o <- out(CA)

  13. Cloning ... Typical for Prototype-based-OOP (object-based, classless) ◮ Object creation: ex-nihilo or cloning and modification. ◮ Cloning: Creation time sharing (simple copy), ◮ Delegation: Run-time sharing (more memory efficient, but can confuse ecologists)

  14. Cloning ... Typical for Prototype-based-OOP (object-based, classless) ◮ Object creation: ex-nihilo or cloning and modification. ◮ Cloning: Creation time sharing (simple copy), ◮ Delegation: Run-time sharing (more memory efficient, but can confuse ecologists) S4-classes not so far away from prototypes ◮ Cloning with assignment operator scenario1 <- scenario2 <- CA ⇒ Independent copies of the whole model object, ◮ Modify slots with replacement functions parms, times, initfunc, . . . , equations, . . .

  15. Creating Scenarios sc0 # Cloning sc0 <- sc1 <- sc2 <- sc3 <- CA # a series of scenarios with different settings sc1 parms(sc1)$adult <- 1 # grow-up faster parms(sc2)$pdeath <- 0.01 # live longer # a scenario with random initialization initfunc(sc3) <- <- function(obj) { sc2 init(obj) <- matrix(round(runif(80*80)), 80, 80) obj } sc3 # (2) simulate, plot ... plot(sim(sc0)) plot(sim(sc1)) plot(sim(sc2)) plot(sim(sc3))

  16. Creation-Time Sharing sc0 # Cloning sc0 <- CA # keep CA as backup parms(sc0)$pseed = 1 # <-- modify sc0 globally sc1 <- sc2 <- sc3 <- sc0 sc1 # a series of scenarios with different settings parms(sc1)$adult <- 1 parms(sc2)$pdeath <- 0.01 sc2 # a scenario with random initialization initfunc(sc3) <- <- function(obj) { init(obj) <- matrix(round(runif(80*80)), 80, 80) obj sc3 } # ... simulate, plot ... plot(sim(sc0)) ...

  17. A Little Bit More Structure The ’equations’-slot ◮ Modularization, functions, submodels, ◮ Implemented as list of functions in ’equations’ ( e.g. neighb, generate, survive ) ◮ main -function should be as general as possible (call equations), ⇒ Possibility to derive scenarios with different functionality, e.g: # 8 direct neighbours (quick and simple) equations(sc_8nb)$neighb = function(Z, adult, ...) eightneighbours(Z >= adult) # neighborhood matrix (more general) equations(sc_wdist)$neighb <- function(Z, adult, wdist) neighbours(Z >= adult , wdist = wdist)

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend