Search code examples
python-3.xtypeerrornonlinear-optimizationmaximizegekko

TypeError: object of type 'int' has no len() in Objective function nonlinear optimization Gekko


I am to maximize my non-linear function and trying to do that with GEKKO

m=GEKKO()
x=m.Var(value=1,lb=0, ub=50)
y=m.Var(value=1, lb=0, ub=50)
m.Equation(puree*x+cutlet*y==1500)
m.Obj(-min(x,y))
m.solve(disp=False)
x.value
y.value

but I get TypeError: object of type 'int' has no len() in this string m.Obj(-min(x,y)) and I don't know what to change to make it work...


Solution

  • Your x and y are specific Gekko variable types, even though when you display them they display as integers. There is no min function defined on that specific type. So when you call min, the Python builtin min function depends on len, and the Gekko specific len function takes as its argument the value of the variable, so effectively min calls len(x.value), which does not work because x.value is an int (equivalently for y). If you want to set your objective function to be some function of x and y, then you need to do it as such:

    m.Obj(<f(x,y)>)
    

    and Gekko will try to minimize f. So if you just want to minimize x+y, then all you need is m.Obj(x+y).