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 promptclose all; clear;% Solve linear equations% Initialize Modelm = py.gekko.GEKKO();% Initialize Variablesx = m.Var(); % define new variabley = m.Var(); % default=0% Define Equationsm.Equation(3*x+2*y==1);m.Equation(x+2*y==0);% Solvem.solve();% Extract values from Python lists using curly bracketsdisp(['x: ' num2str(x.VALUE{1})]);disp(['y: ' num2str(y.VALUE{1})]);推荐答案一种方法是使用 py_run_string() 命令在 R 中运行 Python 代码.我使用多个字符串分解了代码以显示步骤.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.Sys.setenv(RETICULATE_PYTHON = "set the path for python")library(reticulate)#Load the packagestr1 = "from gekko import GEKKOm = GEKKO()"py_run_string(str1)#Assign values with lower and upper bound along with equationsstr2 = "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)"py_run_string(str2)#Print the resultsstr3="print('')print('Results')print('x1: ' + str(x1.value))print('x2: ' + str(x2.value))print('x3: ' + str(x3.value))print('x4: ' + str(x4.value))"py_run_string(str3)#To access an objectpy$x1$VALUE 这篇关于尝试通过网状包在R中使用Python Gekko时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 10:48