APSIMInitiative / ApsimX

ApsimX is the next generation of APSIM
http://www.apsim.info
Other
133 stars 162 forks source link

How to run CroptimizR from APSIMX GUI? #7697

Closed 12devsilva12 closed 1 year ago

12devsilva12 commented 1 year ago

I am trying to work through the Optimization Example included in APSIMX. I have Docker running locally, but not sure what setup is required. I have installed CroptimizR from RStudio. I am getting the following error when I try to run the example:

System.AggregateException: One or more errors occurred. (Container exited with non-zero exit code. Container log:

Error in loadNamespace(x) : there is no package called 'CroptimizR'
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted

)
 ---> System.Exception: Container exited with non-zero exit code. Container log:

Error in loadNamespace(x) : there is no package called 'CroptimizR'
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted

   at APSIM.Shared.Containers.Docker.RunContainerAsync(String image, String entrypoint, IEnumerable`1 args, IReadOnlyList`1 volumes, Dictionary`2 environment, String workingDir, CancellationToken cancelToken) in /tmp/ApsimX/APSIM.Shared/Containers/Docker.cs:line 134
   at APSIM.Shared.Containers.Docker.RunContainerAsync(String image, String entrypoint, IEnumerable`1 args, IReadOnlyList`1 volumes, Dictionary`2 environment, String workingDir, CancellationToken cancelToken) in /tmp/ApsimX/APSIM.Shared/Containers/Docker.cs:line 149
   at APSIM.Shared.Containers.RDocker.RunScriptAsync(String scriptPath, IEnumerable`1 arguments, CancellationToken cancelToken) in /tmp/ApsimX/APSIM.Shared/Containers/RDocker.cs:line 83
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Models.Optimisation.CroptimizR.Run(CancellationTokenSource cancelToken) in /tmp/ApsimX/Models/Optimisation/CroptimizR.cs:line 450
   at APSIM.Shared.JobRunning.JobRunner.Run(IRunnable job) in /tmp/ApsimX/APSIM.Shared/JobRunning/JobRunner.cs:line 224
   at APSIM.Shared.JobRunning.JobRunner.RunActualJob(IRunnable job, IJobManager jobManager) in /tmp/ApsimX/APSIM.Shared/JobRunning/JobRunner.cs:line 189

Any advice?

john12soil commented 1 year ago

Bumping this question, running into the same issue despite having docker installed? Can anyone please help!

hol353 commented 1 year ago

@hol430 wrote the Croptimizr module but he has now left us to work elsewhere. I'm not familiar with it at all. I could probably figure it out by looking at the source code but it would take time that I don't have at the moment. Sorry.

peter-devoil commented 1 year ago

Same errors occur under linux. Looks to be that the container doesnt contain the stics package. It is being updated (last was 7 days ago) but I dont know where to look for the container build logs, sorry

peter-devoil commented 1 year ago

The apsim docker image uses an old version of R (4.1.2), which will no longer build the Stics package:

* building 'CroptimizR_0.5.1.tar.gz'
...
ERROR: dependencies 'BayesianTools', 'nloptr' are not available for package 'CroptimizR'

I don't get this error on 4.2.2

This appears to be because the apsimng-r image (which is used as a layer in apsimng-complete) hasnt been rebuilt for some time. @hol353 - I suspect the jenkins trigger needs to be changed/pulled/whatever here.

peter-devoil commented 1 year ago

As a workaround, if you set the environment variable "APSIM_NO_DOCKER" to 1, (eg set the variable in a command window, and start ApsimNG.exe within that window) it will attempt to use your native (4.22?) installation of R instead of the docker image

hol353 commented 1 year ago

Hmm I don't know if it is related but our build system is giving this error about R and croptimizr:

System.Exception: Error from R:
Script path: '/tmp/fdc73a03-c879-4c40-ab1f-9ab08d64a7e9-getPackage.R'
StdErr:
Warning in install.packages(pkg, repos = "https://cran.csiro.au/", lib = pkgpath,  :
  'lib = "ApsimInitiative/ApsimX/rpackages"' is not writable
Error in install.packages(pkg, repos = "https://cran.csiro.au/", lib = pkgpath,  : 
  unable to install packages
Calls: getPackage -> install.packages
In addition: Warning message:
In dir.create(pkgpath) :
  cannot create dir 'ApsimInitiative/ApsimX/rpackages', reason 'No such file or directory'
Execution halted

StdOut:

Script contents:
#' Loads a package, installing it if necessary.
#'
#' @param pkg Name of the package to be installed/loaded.
#' @param pkgpath Path where package is to be installed.
#' @return Nothing.
getPackage <- function(pkg, pkgpath) {
    if (!pkg %in% rownames(installed.packages())) {
        if (!dir.exists(pkgpath)) {
            dir.create(pkgpath)
        }
        install.packages(pkg, repos = "https://cran.csiro.au/", lib = pkgpath, dependencies = TRUE)
    } else {
        print(paste('Package', pkg, 'is already installed.'))
    }
}

args = commandArgs(TRUE)
getPackage(args[1], args[2])

   at Models.Utilities.R.Run(String fileName, Boolean throwOnError, String[] arguments) in /tmp/ApsimX/Models/Utilities/R.cs:line 194
   at Models.Utilities.R.InstallPackage(String package) in /tmp/ApsimX/Models/Utilities/R.cs:line 265
   at Models.Utilities.R.InstallPackages(String[] packages) in /tmp/ApsimX/Models/Utilities/R.cs:line 307
   at Models.Optimisation.CroptimizR.Run(CancellationTokenSource cancelToken) in /tmp/ApsimX/Models/Optimisation/CroptimizR.cs:line 497
   at APSIM.Shared.JobRunning.JobRunner.Run(IRunnable job) in /tmp/ApsimX/APSIM.Shared/JobRunning/JobRunner.cs:line 225
   at APSIM.Shared.JobRunning.JobRunner.RunActualJob(IRunnable job, IJobManager jobManager) in /tmp/ApsimX/APSIM.Shared/JobRunning/JobRunner.cs:line 189
CroptimizR has finished. Elapsed time was 3.7 seconds.
hol353 commented 1 year ago

I've updated the APSIM R docker container to use R 4.2.2 and @hol430 added a missing bayesean R package. Does it work now?

peter-devoil commented 1 year ago

The example works on linux now. @12devsilva12 can you please check on your windows system, and close the issue if done

ric394 commented 1 year ago

Not sure if this helps but there is related documentation page located here: https://apsimnextgeneration.netlify.app/usage/croptimizr/

par456 commented 1 year ago

Using the instructions above, I was able to run the comptimizr example with no errors. Closing this.