Reputation: 1
I have built a simulation model in Netlogo and hope to optimize model parameters (around 30). Since Netlogo does not support automate multiple runs with different parameter sets, I was thinking using another platform (R/python/Java) to call Netlogo, analyze the simulated results, and find the optimal parameters.
However, none of them work so far...In R, I have encountered error when starting Netlogo using RNetLogo. I have tried all the potential solutions I can find online, but still haven't figured out the issue. Would appreciate it if someone can help.
Code:
library(RNetLogo)
nl.path = "C:/Program Files/NetLogo 5.3/app"
NLStart(nl.path, gui=FALSE, nl.jarname = 'NetLogo.jar')
Error message:
java.lang.NoClassDefFoundError: org/nlogo/api/Exceptions$Handler
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
Caused by: java.lang.ClassNotFoundException
at RJavaClassLoader.findClass(RJavaClassLoader.java:383)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Version:
- system: Windows 10
- R: 3.3.3
- Netlogo: 5.3/5.3.1/6.0/6.0.2 (tried all of them, same error message)
- Java: 1.8.0_151-b12 (this is the one called in R, checked by .jcall)
- RNetlogo and rJava are most up-to-date as of 1/9/2018
Upvotes: 0
Views: 687
Reputation: 13
There are a few diagnostics to run, but before that, copy all the .jar files from the Java/ folder in your Netlogo/ folder to the same folder where NetLogo 5.3.0.app
is located.
Then make sure you have both /app
in the dir path and the NL version in the nl.jarname
argument.
# get packages from source
p<-c("rJava", "RNetLogo"); remove.packages(p)
install.packages("rJava", repos = "https://cran.r-project.org/", type="source")
install.packages("RNetLogo", repos = "https://cran.r-project.org/", type="source")
library(rJava);library(RNetLogo)
nl_path = "C:/Program Files/NetLogo 5.3.0/app"
ver_nl <- "5.3.0"
NLStart(nl_path,gui=F,nl.jarname = paste0("netlogo-",ver_nl,".jar")) # open netlogo without gui
# NLLoadModel("path-to-nl-model",nl.obj=NULL) # load model with nl.obj=NULL
Failing this, run these diagnostics in order. You should have no rJava errors. If you do, ensure you have the latest version of Java SE installed: https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
# test 1
install.packages("rJava", repos = "https://cran.r-project.org/", type="source"); library(rJava)
.jinit()
.jnew( "java/awt/Point", 10L, 10L )
f <- .jnew("java/awt/Frame","Hello")
.jcall(f,,"setVisible",TRUE)
t1err <- geterrmessage()
# test 2
component <- .jnull()
component <- .jcast(component, new.class = "java/awt/Component")
message <- .jnew("java/lang/String","This is a JOptionPane test from rJava.")
message <- .jcast(message, new.class = "java/lang/Object")
title <- .jnew("java/lang/String","Test")
type <- .jnew("java/lang/Integer", as.integer(2))
f <- .jnew("javax/swing/JOptionPane")
.jcall(f,"showMessageDialog", component, message, title, .jsimplify(type))
t2err <- geterrmessage()
# # test 3
.jcall("java/lang/System", "S", "getProperty", "java.vm.version")
.jcall("java/lang/System", "S", "getProperty", "java.vm.name")
.jcall("java/lang/System", "S", "getProperty", "java.vm.info")
.jcall("java/lang/System", "S", "getProperty", "java.runtime.version")
.jcall("java/lang/System", "S", "getProperty", "sun.arch.data.model")
t3err <- geterrmessage()
# test 4
.jcall("java/lang/System", "S", "getProperty", "java.awt.headless")
Sys.getenv("NOAWT")
t4err <- geterrmessage()
errorlist <- function(){
if(geterrmessage()==t1err){stop("Failed Test 1 — Headless exception \n \n Wrong awt GUI support for Java/rJava",call.=T)}
if(geterrmessage()==t2err){stop("Failed Test 2 — Invalid method name for RcallMethod (unable to open dialog box)",call.=T)}
if(geterrmessage()==t3err){stop("Failed Test 3 — Old version of Java. \n \n Download latest version \n \n > https://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html.",call.=T)}
if(geterrmessage()==t4err){stop("Failed Test 4 — ",call.=T)}
}
Final alternative is to run RNetLogo from JGR, which works in headless mode for Mac.
install.packages('JGR',,'http://www.rforge.net/') # note the extra comma
library(JGR)
JGR::JGR()
Upvotes: 0
Reputation: 66
Does it solve your problem, if you use the java environment included in the NetLogo installation?
Try this one before you install or load RNetLogo inside R:
Sys.setenv(JAVA_HOME="YOUR-INSTALLATION-PATH\\NetLogo 6.0.1\\runtime")
Upvotes: 1
Reputation: 10291
I'm definitely stuck on that same error with 5.3.1. Out of curiosity, does this work for you with version 6.0.2?
library(RNetLogo)
nl.path <- "C:/Program Files/NetLogo 6.0.2/app"
NLStart(nl.path, gui = FALSE, nl.jarname = "netlogo-6.0.2.jar")
Upvotes: 0