Lagrange multipliers (marginals) in Gekko

Viewed 185

Is there a one liner in Gekko to retrieve the Lagrange multipliers (the likes of the marginal in GAMS) or if not a single line another way?

Thanks for the help.

1 Answers

Here is one line to retrieve the Lagrange multipliers.

lam = np.loadtxt(m.path + '/apm_lam.txt')

You will need to set the diagnostic level to m.options.DIAGLEVEL=2 and solve locally with m=GEKKO(remote=False). You can see some of the other files that are generated with DIAGLEVEL=2 when you open the folder with m.open_folder(). 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]
Related