Aman Gupta
Aman Gupta

Reputation: 13

Gekko Solver does not satisfy Constraints gives wrong solution

I was using Gekko solver for optimizing function, but it gives wrong solution even in simple problems where it also not satisfying the given constraints.

from gekko import GEKKO    
m = GEKKO(remote=False)

a = m.Var(value=0, integer=True)
b = m.Var(value=0, integer=True)

# constraints
m.Equation([a + b > 7, a**2 + b**2 < 40])
# Objective function
m.Maximize(a**3 + b**3)


m.solve(disp=False)
print(a.value[0])
print(b.value[0])
max_value = a.value[0]**3 + b.value[0]**3
print(max_value)

Output:

2.0
6.0
224.0

Upvotes: 1

Views: 368

Answers (1)

John Hedengren
John Hedengren

Reputation: 14376

Adding a check at the end reveals that Gekko finds a correct solution within the requested tolerance although the default solver is IPOPT that finds a continuous solution even when integer=True is requested.

a = a.value[0]; b = b.value[0]
print('a+b>7',a+b)
print('a^2+b^2<40',a**2+b**2)

# 0.71611780666
# 6.2838821838
# 248.50000026418317
# a+b>7 6.99999999046
# a^2+b^2<40 40.00000001289459

Try switching to the MINLP solver APOPT with m.options.SOLVER=1. Inequalities < and <= are equivalent in Gekko because it is a numerical solution.

2.0
6.0
224.0
a+b>7 8.0
a^2+b^2<40 40.0

If it is < or > in the mathematical sense that 7 and 40 are not allowable then shift up or down one integer on the constraints such as to >=8 and <=39.

m.Equation([a + b >= 8, \
            a**2 + b**2 <= 39])

Results are correct:

a=3.0  b=5.0  Objective: 152.0
a+b>=8 with a+b=8.0 (constraint satisfied)
a^2+b^2=<39 with a^2+b^2=34.0 (constraint satisfied)

Is there something else missing here? Why is there a claim of no feasible solution?

from gekko import GEKKO    
m = GEKKO(remote=False)

a = m.Var(value=0, integer=True)
b = m.Var(value=0, integer=True)

# constraints
m.Equation([a + b >= 7, \
            a**2 + b**2 <= 40])
# Objective function
m.Maximize(a**3 + b**3)

m.options.SOLVER =1
m.solve(disp=True)
print(a.value[0])
print(b.value[0])
max_value = a.value[0]**3 + b.value[0]**3
print(max_value)

a = a.value[0]; b = b.value[0]
print('a+b>7',a+b)
print('a^2+b^2<40',a**2+b**2)

Upvotes: 1

Related Questions