By tips in a related question for using dictionaries as source input to gekko, I did as below;
import pandas as pd
from gekko import GEKKO
my_vars = ['x1','x2','x3']
# stored as dictionaries
Cost = {'x1':100,'x2':125,'x3':80}
Min = {'x1':0,'x2':0,'x3':0}
Max = {'x1':70,'x2':40,'x3':15}
LP = GEKKO(remote=False)
va = LP.Array(LP.Var, (len(my_vars))) # array
vd = {} # dictionary
for i,xi in enumerate(my_vars):
vd[xi] = va[i]
vd[xi].lower = Min[xi]
vd[xi].upper = Max[xi]
# Cost function
LP.Minimize(LP.sum([Cost[xi]*vd[xi] for xi in my_vars]))
# This also works as a dictionary
LP.Equation(LP.sum([vd[xi] for xi in my_vars])==100)
LP.solve(disp=True)
for xi in my_vars:
print(xi,vd[xi].value[0])
print ('Cost: ' + str(LP.options.OBJFCNVAL))
This was successful.
But suppose I have variables coefficients as LHS and the criteria data as RHS, in data frame format,
for example;
Coef = [['x1', 33, 8.8, 0.2, 0.1], ['x2', 24, 4.5, 2, 0.5], ['x3', 82, '', '', '']]
LHS = pd.DataFrame(Coef, columns=['Name', 'p1', 'p2', 'p3', 'p4'])
LHS.set_index('Name')
and
goals = [['p1', 30], ['p2', 5], ['p3', 0.7], ['p4', 0.2]]
RHS = pd.DataFrame(goals, columns=['property', 'goal'])
I want to construct constraints dynamically using LHS and RHS data in for loop. I did as below,
for i in range(len(RHS)):
LP.Equation(LP.sum([LHS.iloc[list(my_vars).index(xi), i]*vd[xi] for xi in my_vars]) >= RHS.iloc[i,1])
but there were errors.
APMonitor, Version 0.9.2
APMonitor Optimization Suite
--------- APM Model Size ------------Each time step contains
Objects : 6
Constants : 0
Variables : 28
Intermediates: 0
Connections : 24
Equations : 22
Residuals : 22
@error: Model Expression *** Error in syntax of function string: Invalid element: x1Position: 7 v9-(((x1)*(v1))) ?
Traceback (most recent call last):
File "", line 1, in LP.solve(disp=True)
File "/opt/anaconda3/envs/py37/lib/python3.7/site-packages/gekko/gekko.py", line 2130, in solve raise Exception(apm_error)
Exception: @error: Model Expression *** Error in syntax of function string: Invalid element: x1
Position: 7 v9-(((x1)*(v1))) ?
What is wrong with my code?