I have searched through the whole internet, not been able to find any solution yet.
My problem is a convex nonlinear program. For most convex solvers, it is the command
constraint.dual
to get the dual value. Can I get dual values in Gekko?
The dual values are also known as Lagrange multipliers. Set m.options.DIAGLEVEL=2
with a local solve m=GEKKO(remote=False)
to access the dual variables.
lam = np.loadtxt(m.path + '/apm_lam.txt')
Here is a test script.
from gekko import GEKKO
import numpy as np
m = GEKKO(remote=False)
#initialize variables
xi = [1,5,5,1]
x1,x2,x3,x4 = [m.Var(xi[i],lb=1,ub=5) for i in range(4)]
m.Equation(x1*x2*x3*x4>=25)
m.Equation(x1**2+x2**2+x3**2+x4**2==40)
m.Obj(x1*x4*(x1+x2+x3)+x3)
m.options.DIAGLEVEL=2
m.solve(disp=False)
print('')
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('x4: ' + str(x4.value))
print('Lagrange multipliers')
lam = np.loadtxt(m.path + '/apm_lam.txt')
print(lam)
This produces the results:
Results
x1: [1.000000057]
x2: [4.74299963]
x3: [3.8211500283]
x4: [1.3794081795]
Lagrange multipliers
[-0.55227642 0.16143862]
There is a similar question Lagrange multipliers (marginals) in Gekko with additional comments. One of the important observations is that some solvers return the Lagrange multipliers while others don't report them. For example, solvers m.options.SOLVER=2
(BPOPT) and m.options.SOLVER=3
(IPOPT) return the dual variables while others m.options.SOLVER=1
(APOPT) currently do not return them.