I'm trying to solve a non-linear optimization problem. I've duplicated my issue by creating the code below. Python returns TypeError: object of type 'int' has no len()
. How can I include an IF statement in my constraint functions?
Console prints the following:
File "<ipython-input-196-8d29d410dcea>", line 1, in <module>runfile('C:/Users/***/Documents/***/Project/untitled.py', wdir='C:/Users/***/Documents/***/***/Project')File "C:\Users\***\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfileexecfile(filename, namespace)File "C:\Users\***\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfileexec(compile(f.read(), filename, 'exec'), namespace)File "C:/Users/***/Documents/***/***/Project/untitled.py", line 27, in <module>m.Equation(Cx(x1,x2,x3,x4) < 0)File "C:/Users/***/Documents/***/***/Project/untitled.py", line 17, in Cxif K > 15:File "C:\Users\***\Anaconda3\lib\site-packages\gekko\gk_operators.py", line 25, in __len__return len(self.value)File "C:\Users\***\Anaconda3\lib\site-packages\gekko\gk_operators.py", line 134, in __len__return len(self.value)TypeError: object of type 'int' has no len()
-
from gekko import GEKKO
m = GEKKO()def Cr(x1,x2,x3,x4):return (x1*x4*(x1+x2+x3)+x3**2)def Cw(x1,x2,x3,x4):return x1*x2*x3*x4def Ck(x1,x2,x3,x4):return x1*x2*x3*x4+1def Cx(x1,x2,x3,x4):K = Ck(x1,x2,x3,x4)if K > 15: #Issue hereK = 15return x1**2+x2**2+x3**2+x4**2 - Kx1 = m.Var(value=1,lb=-5000,ub=5000)
x2 = m.Var(value=1,lb=-5000,ub=5000)
x3 = m.Var(value=1,lb=-5000,ub=5000)
x4 = m.Var(value=1,lb=-5000,ub=5000)m.Equation(Cw(x1,x2,x3,x4) >= 14)
m.Equation(Cx(x1,x2,x3,x4) < 0)m.Obj(Cr(x1,x2,x3,x4))m.solve(disp=False)
print(x1.value)
print(x2.value)
print(x3.value)
print(x4.value)
-
I am looking to have GEKKO run with the IF statement in the constraint, I'm not concerned if the optimization problem in the code has a solution. Thank you in advance.