Python modules are accessible in R via reticulate
package. Unfortunately, it is not working for GEKKO, the formulas are not being recognized as such.
I am receiving these kind of error messages when trying to pass equations like in m$Equation(x1 + x2 == 7)
: Error in x1 + x2 : non-numeric argument to binary operator
.
library(reticulate)
gekko <- reticulate::import("gekko")
m <- gekko$GEKKO(remote=TRUE)
x1 = m$Var(value=1, lb=1, ub=5)
x2 = m$Var(value=5, lb=1, ub=5)
x3 = m$Var(value=5, lb=1, ub=5)
x4 = m$Var(value=1, lb=1, ub=5)
m$Equation(x1 + x2 == 7)
m$Equation(x1 * x2 * x3 * x4 >= 25)
m$Obj(x1 * x4 * (x1 + x2 + x3) + x3)
m$solve(disp=TRUE)
I would really like to use GEKKO in R, is there some tweak we can do to make that possible?
Reposting the question from a GitHub issue in Gekko repository to see if additional help can be found here. A Python Gekko interface in MATLAB is also possible, if this helps.
% start Matlab from Anaconda prompt
close all; clear;
% Solve linear equations
% Initialize Model
m = py.gekko.GEKKO();
% Initialize Variables
x = m.Var(); % define new variable
y = m.Var(); % default=0
% Define Equations
m.Equation(3*x+2*y==1);
m.Equation(x+2*y==0);
% Solve
m.solve();
% Extract values from Python lists using curly brackets
disp(['x: ' num2str(x.VALUE{1})]);
disp(['y: ' num2str(y.VALUE{1})]);
One way would be to run the Python code in R using the py_run_string() command. I have broken up the code using multiple strings to show the steps.