Why is GEKKO giving a different solution to the same LINGO NLP?

Viewed 46

I am trying to work on an optimization problem using python and I started working on GEKKO since it solves nonlinear programs. I have written a simple model in LINGO to check the answer which didn't have the same value as the answer I got from GEKKO (same model).

Python Code:-

from gekko import GEKKO

# Initialize Model

smplmdl = GEKKO()


# Create Variables
x = smplmdl.Array(smplmdl.Var, 3, lb = 0)
a = smplmdl.Array(smplmdl.Var, 3, lb = 0)

Constant_Val = [10, 15, 20]

for i in range(3):
    smplmdl.Equation(x[i]*(sum(a[j] for j in range(3))) == Constant_Val[i])

# Objective Function
smplmdl.Obj(sum(x[i] for i in range(3)))

smplmdl.options.IMODE = 3
smplmdl.solve()
smplmdl.options.OBJFCNVAL

print('x:', x)
print('a:', a)

print(smplmdl.options.OBJFCNVAL)

LINGO Code:-

Min = x1 + x2 + x3;

x1*(a1 + a2 + a3) = 10;

x2*(a1 + a2 + a3) = 15;

x3*(a1 + a2 + a3) = 20;
1 Answers

It is possible to simply the model.

from gekko import GEKKO

# Initialize Model
smplmdl = GEKKO(remote=False)

# Create Variables
x = smplmdl.Array(smplmdl.Var, 3, lb = 0)
a = smplmdl.Array(smplmdl.Var, 3, lb = 0)

Constant_Val = [10, 15, 20]

for i in range(3):
    smplmdl.Equation(x[i]*sum(a) == Constant_Val[i])

# Objective Function
smplmdl.Minimize(sum(x))

# Solve and print solution
smplmdl.solve()
print('x:', x)
print('a:', a)
print(smplmdl.options.OBJFCNVAL)

This gives the solution:

x: [[5.7995291433e-05] [8.699293715e-05] [0.00011599058287]]
a: [[57475.93038] [57475.930412] [57475.930377]]
Objective:  0.00026097881145

The objective is to minimize the summation of x and the objective function obtained by IPOPT is 2.6e-4. If LINGO gave a different solution, there are likely solver tolerances that can be adjusted to achieve better agreement. Try adjusting m.options.RTOL and m.options.OTOL for the residual and objective function tolerances. For non-convex problems, a multi-start method or a global solver may be better suited to this problem.

If the purpose is to compare LINGO and GEKKO, perhaps try a simple convex optimization problem such as:

from gekko import GEKKO    
import numpy as np
m = GEKKO()
x = m.Array(m.Var,4,value=1,lb=1,ub=5)
x1,x2,x3,x4 = x
# change initial values
x2.value = 5; x3.value = 5
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==40)
m.Minimize(x1*x4*(x1+x2+x3)+x3)
m.solve()
print(x,m.options.OBJFCNVAL)
Related