Calling oemof from R

Hello community,

the package “reticulate” offers the possibility to call Python from R. I tried to run the basic_example.py (oemof-examples/oemof_examples/oemof.solph/v0.4.x/basic_example at master · oemof/oemof-examples · GitHub) via R. However, I always get the Error "Failed to create solver with name “CBC”.

Thanks in advance for your help.

Br,
Alex

Code: 
library(reticulate)
use_python("C:/Anaconda", required = TRUE)
reticulate::py_config()
source_python(".../basic_example.py")

Console: 
python:         C:/Anaconda/python.exe
libpython:      C:/Anaconda/python38.dll
pythonhome:     C:/Anaconda
version:        3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Anaconda/Lib/site-packages/numpy
numpy_version:  1.20.1

NOTE: Python version was forced by use_python function
> source_python(".../basic_example.py")
15:20:25-INFO-Path for logging: C:\Users\.oemof\log_files\oemof_example.log
15:20:25-INFO-Initialize the energy system
15:20:25-INFO-Create oemof objects
15:20:25-INFO-Optimise the energy system
15:20:26-INFO-Solve the optimization problem
WARNING: Failed to create solver with name 'cbc': Could not execute the
    command: 'C:\Program Files\cbc-win64\cbc.exe -stop'
    	Error message: [WinError 6] Das Handle ist ungültig
15:20:26-WARNING-Failed to create solver with name 'cbc':
Could not execute the command: 'C:\Program Files\cbc-win64\cbc.exe -stop'
	Error message: [WinError 6] Das Handle ist ungültig
Error in py_run_file_impl(file, local, convert) : 
  RuntimeError: Attempting to use an unavailable solver.

The SolverFactory was unable to create the solver "cbc"
and returned an UnknownSolver object.  This error is raised at the point
where the UnknownSolver object was used as if it were valid (by calling
method "solve").

The original solver was created with the following parameters:
	solver_io: lp
	type: cbc
	_args: ()
	options: {}

Have you installed the CBC solver on your system?
Is the CBC solver in the PATH, so that it can be found?

Thanks for answering!
I can run “basic_examples.py” by using spyder without getting any error. Therefore, the installation and the PATH of the cbc solver should be fine. I only have the problem if I execute basic_examples.py by using R.

Br,
Alex

Sorry in that case I cannot help, because I do not use the combination of Python and R. I think you have to tell R or reticulate in particular where to find the cbc solver.

A workaround would be to store the needed results into a csv-file in Python and open it with R to go on there.

R probably runs its client processes — in this case, the oemof call — in subshells. In which case, how much of parent environment gets transferred might be the question. I guess you are on Windows? In which case, I am unable to offer much in the way of suggestions.

Thanks for answering!
It seems like that R can find the solver, which is executed by oemof.

WARNING: Failed to create solver with name 'cbc': Could not execute the
    command: 'C:\Program Files\cbc-win64\cbc.exe -stop'

However, it is not possible to execute the solver.

Could not execute the command: 'C:\Program Files\cbc-win64\cbc.exe -stop'

I also tried it with other solvers like gurobi, but still without any success. I appreciate any ideas, which might solve the problem!

Br,
Alex

Files have permissions as to who might do what. Your exe file will therefore need its execute permissions set appropriately. On Linux, this would be something like:

$ cd "C:\Program Files\cbc-win64"
$ chmod ug+x cbc.exe

Maybe you can also do this with a mouse in file explorer too? :grimacing:

You might also need to precede the chmod with sudo depending on how the software was installed and what your authority is as a regular user.