ropensci / nlrx

nlrx NetLogo R
https://docs.ropensci.org/nlrx
GNU General Public License v3.0
77 stars 12 forks source link

Exception error when running sensitivity analysis with nlrx #22

Closed schellheinz closed 5 years ago

schellheinz commented 5 years ago

Hello,

I want to use an sensitivity analysis on the model I build in NetLogo but I always get the following error message in R:

Exception` in thread "main" Expected a literal value. at position 0 in 
    at org.nlogo.core.Fail$.exception(Fail.scala:27)
    at org.nlogo.core.Fail$.exception(Fail.scala:25)
    at org.nlogo.core.Fail$.exception(Fail.scala:23)
    at org.nlogo.parse.LiteralParser.readLiteralPrefix(LiteralParser.scala:83)
    at org.nlogo.parse.LiteralParser.getLiteralValue(LiteralParser.scala:33)
    at org.nlogo.parse.CompilerUtilities$.$anonfun$readFromString$3(CompilerUtilities.scala:22)
    at org.nlogo.parse.CompilerUtilities$.$anonfun$numberOrElse$1(CompilerUtilities.scala:37)
    at scala.util.Either$RightProjection.getOrElse(Either.scala:666)
    at org.nlogo.parse.CompilerUtilities$.numberOrElse(CompilerUtilities.scala:36)
    at org.nlogo.parse.CompilerUtilities$.readFromString(CompilerUtilities.scala:22)
    at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$4(LabLoader.scala:70)
    at scala.collection.TraversableLike$WithFilter.$anonfun$map$2(TraversableLike.scala:739)
    at scala.collection.Iterator.foreach(Iterator.scala:929)
    at scala.collection.Iterator.foreach$(Iterator.scala:929)
    at scala.collection.AbstractIterator.foreach(Iterator.scala:1417)
    at scala.collection.IterableLike.foreach(IterableLike.scala:71)
    at scala.collection.IterableLike.foreach$(IterableLike.scala:70)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:54)
    at scala.collection.TraversableLike$WithFilter.map(TraversableLike.scala:738)
    at org.nlogo.fileformat.LabLoader.readEnumeratedValueSetElement$1(LabLoader.scala:66)
    at org.nlogo.fileformat.LabLoader.$anonfun$readProtocolElement$5(LabLoader.scala:77)
    at scala.collection.immutable.List.flatMap(List.scala:335)
    at org.nlogo.fileformat.LabLoader.valueSets$1(LabLoader.scala:74)
    at org.nlogo.fileformat.LabLoader.readProtocolElement(LabLoader.scala:94)
    at org.nlogo.fileformat.LabLoader.$anonfun$apply$1(LabLoader.scala:45)
    at scala.collection.immutable.List.map(List.scala:283)
    at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:45)
    at org.nlogo.fileformat.LabLoader.apply(LabLoader.scala:30)
    at org.nlogo.fileformat.LabFormat.load(NLogoLabFormat.scala:37)
    at org.nlogo.fileformat.LabFormat.load$(NLogoLabFormat.scala:35)
    at org.nlogo.fileformat.NLogoLabFormat.load(NLogoLabFormat.scala:40)
    at org.nlogo.headless.BehaviorSpaceCoordinator$.$anonfun$selectProtocol$1(BehaviorSpaceCoordinator.scala:34)
    at scala.Option.map(Option.scala:146)
    at org.nlogo.headless.BehaviorSpaceCoordinator$.selectProtocol(BehaviorSpaceCoordinator.scala:32)
    at org.nlogo.headless.Main$.runExperiment(Main.scala:23)
    at org.nlogo.headless.Main$.$anonfun$main$1(Main.scala:12)
    at org.nlogo.headless.Main$.$anonfun$main$1$adapted(Main.scala:12)
    at scala.Option.foreach(Option.scala:257)
    at org.nlogo.headless.Main$.main(Main.scala:12)
    at org.nlogo.headless.Main.main(Main.scala)
Fehler in util_gather_results(nl, outfile, seed, siminputrow) : 
  Output file is empty - simulation aborted due to a runtime error!
         Make sure that parameter value definitions of the experiment are valid and the model code is running properly!

This even happens with your example of the wolf-sheep model:

#set paths
netlogopath <- file.path("/Applications/NetLogo 6.0.3")
modelpath <- file.path("/Applications/NetLogo 6.0.3/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/Users/christian/Schreibtisch")

#attach paths, set available memory for JAVA, set NetLogo version
nl <- nl(nlversion = "6.0.3",
         nlpath = netlogopath,
         modelpath = modelpath,
         jvmmem = 1024)

nl@experiment <- experiment(expname="wolf-sheep",
                            outpath=outpath,
                            repetition=1,
                            tickmetrics="true",
                            idsetup="setup",
                            idgo="go",
                            idfinal=NA_character_,
                            idrunnum=NA_character_,
                            runtime=50,
                            evalticks=seq(40,50),
                            metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
                            variables = list('initial-number-sheep' = list(min=50, max=150, qfun="qunif"),
                                             'initial-number-wolves' = list(min=50, max=150, qfun="qunif")),
                            constants = list("model-version" = "\"sheep-wolves-grass\"",
                                             "grass-regrowth-time" = 30,
                                             "sheep-gain-from-food" = 4,
                                             "wolf-gain-from-food" = 20,
                                             "sheep-reproduce" = 4,
                                             "wolf-reproduce" = 5,
                                             "show-energy?" = "false"))

nl@simdesign <- simdesign_eFast(nl=nl,
                                samples=10,
                                nseeds=3)

setsim(nl, "simoutput") <- run_nl_all(nl)

I dont really know about programming so maybe the solution to this is very trivial but I would be very thankful for your help!

nldoc commented 5 years ago

Hi and sorry for the delayed response. I checked the code example and discovered the problem. After adding the simdesign, if you check the siminput tibble in your nl object (nl@simdesign@siminput) you will see that there are a lot of NAs in the tibble. This is due to the eFAST algorithm. The number of samples depends on the number of variables. If the number of samples is too low, NA values are created. Thus, to solve your problem you just have to increase the number of samples of your simdesign (for the example above 65 samples seem to be the minimum number of samples). However, I agree that this should be printed on the interface. We will probably add a warning when NAs are created by simdesigns.