yufree
2015年1月4日
install.package('RNetLogo')
library("RNetLogo")
# 设定你NetLogo的位置
nl.path <- "/Applications/NetLogo 5.1.0"
# 启动界面
# 如果出现java.lang.UnsupportedClassVersionError: nlcon/Preprocess : Unsupported major.minor version 51.0 建议试用JGR作为IDE rJava与RStudio目前有点不配合
NLStart(nl.path)
model.path <- file.path("models", "Sample Models", "Earth Science","Fire.nlogo")
NLLoadModel(file.path(nl.path, model.path))
NLCommand("set density 70") # 设定森林密度
NLCommand("setup") # 初始化
NLCommand("go") # 运行模型一次
# 模拟火灾的覆盖面积
library(ggplot2)
NLCommand("set density 70")
NLCommand("setup")
burned <- NLDoReportWhile("any? turtles", "go",
c("ticks", "(burned-trees / initial-trees) * 100"),
as.data.frame = TRUE, df.col.names = c("tick", "percent.burned"))
# 用ggplot2对运行时间与模型覆盖度作图
p <- ggplot(burned,aes(x=tick,y=percent.burned))
p + geom_line() + ggtitle("Non-linear forest fire progression with density = 60")
# 与控制台交互
NLCommand("print \"Hello NetLogo, I called you from R.\"")
# 退出
NLQuit()
Thiele, Jan C. “R Marries NetLogo: Introduction to the RNetLogo Package.” Journal of Statistical, 2014. http://www.jstatsoft.org/v58/i02/paper.