R extension demo
Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)
WHAT IS IT?
This program implements Conway's "Game of Life" together with a simulation of bacterial chemical-gradient-climbing, in order to demo the R extension for a tutorial at http://scientificgems.wordpress.com/2014/02/11/the-r-extension-for-netlogo-a-tutorial/
HOW TO USE IT
The "average-density" slider sets the initial average fraction of live cells for the "Game of Life."
The "Setup Life" or "Setup Bacteria" buttons initialise one or the other simulation.
The "Run" button runs the selected simulation.
The "Plot Life" button plots the "Game of Life" density over time, using the R extension. An input box allows editing of the default file name.
Comments and Questions
extensions [ r ] globals [ is-life? ;; distinguish between the two demos ;; LIFE cell-count ;; total number of cells total ;; total number of live cells history ;; list of totals ;; BACTERIA window ;; number of steps over which to analyse ] patches-own [ ;; LIFE alive? ;; is cell alive? counter ;; used to pass number of live neighbours between Phase 1 and Phase 2 ;; BACTERIA value ;; simulated chemical concentration ] turtles-own [ ;; BACTERIA hist ;; history of concentration values ] to rplot-graph ;; R interface -- plot the LIFE history if (is-life?) [ r:put "life.hist" history r:put "plotfile" life-plot-filename r:eval "png (filename = plotfile, width = 1600, height = 800, pointsize=18)" r:eval "par (mar=c(5,5,0.8,0.5))" r:eval "xs <- 0:(length(life.hist)-1)" r:eval "plot (life.hist ~ xs, type=\"l\", lwd=4, col=\"red\", xlab=\"Step\", ylab=\"Density\", ylim=c(0,max(life.hist)), cex.lab=2, cex.axis=1.5)" r:eval "dev.off()" ] end to-report calculate-slope [ lst ] ;; R interface -- calculate slope for values in lst r:put "valu.hist" lst r:eval "valu.index <- 1:length(valu.hist)" r:eval "valu.lm <- lm(valu.hist ~ valu.index)" report r:get "valu.lm$coefficients[2]" end ;; code for SETUP button -- Life to setup-life clear-all set is-life? true set life-plot-filename "C:/NetLogo/Life_plot.png" set cell-count (count patches) ask patches [ ifelse (random-float 1 < average-life-density) [birth] [death] ] reset-ticks set history (list (total / cell-count)) end to death ;; this procedure applies to a specific patch set alive? false set pcolor white end to birth ;; this procedure applies to a specific patch set alive? true set pcolor blue set total (total + 1) end ;; code for SETUP button -- Bacteria to setup-bacteria clear-all set is-life? false set life-plot-filename "** NOT USED **" set window 4 ;; create gradient repeat 3 [ let x random-xcor let y random-ycor let v (0.1 + random-float 0.5) let r (1 + v * world-width / 2) ask patches [ let d ((distancexy x y) / r) set value (value + v * exp (- d)) ] ] let min-value (min ([value] of patches)) let max-value (max ([value] of patches)) ask patches [ set pcolor (30 + (9.9 * (value - min-value) / (max-value - min-value))) ] ;; create some bacteria crt 20 [ setxy random-xcor random-ycor set color red set size 6 set hist (list value) ] reset-ticks end to go ;; single simulation step for both demos if-else (is-life?) [ ;; LIFE ;; First phase ask patches [ set counter (count neighbors with [alive?]) ] ;; Second phase set total 0 ask patches [ ifelse (counter = 3 or (counter = 2 and alive?)) [ birth ] [ death ] ] set history (lput (total / cell-count) history) ;; Record current density tick ] [ ;; BACTERIA ask turtles [ fd 1 set hist (lput value hist) if (length(hist) > window) [ set hist (bf hist) ] if (length(hist) = window) [ if (calculate-slope hist <= 0) [ set heading (random-float 360) set hist (list value) ] ] ] tick ] end
There is only one version of this model, created almost 11 years ago by Anthony Dekker.
Attached files
File | Type | Description | Last updated | |
---|---|---|---|---|
BacteriaDemoRview.png | png | Screenshot of "bacteria" mode | almost 11 years ago, by Anthony Dekker | Download |
R extension demo.png | preview | Preview for 'R extension demo' | almost 11 years ago, by Anthony Dekker | Download |
This model does not have any ancestors.
This model does not have any descendants.
Anthony Dekker
Explanation
This model is associated with a tutorial at http://scientificgems.wordpress.com/2014/02/11/the-r-extension-for-netlogo-a-tutorial/
Posted almost 11 years ago