5

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.

Arraval
  • 1,110
  • 9
  • 20

1 Answers1

2

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]
John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • 1
    The Lagrange multipliers are dependent on the python version? I get basically the same values for the x's (the differences are negligible) but the Lagrange multipliers are both Zero. I'm running python 2.7.16 in Ubuntu 14.04. What I get: Results x1: [1.0] x2: [4.742999637] x3: [3.8211499845] x4: [1.3794082931] Lagrange multipliers [0. 0.] – Arraval May 28 '20 at 09:24
  • 1
    Try using IPOPT with `m.options.SOLVER=3` or BPOPT with `m.options.SOLVER=2`. Some of the solvers aren't configured to return the Lagrange multipliers. – John Hedengren May 28 '20 at 16:04
  • 1
    Thanks. I added `m.options.SOLVER=2` to your test script and it returned the multipliers but not with `IPOPT`. Where in the GEKKO's documentation can I read about this topic (the lagrange multipliers) I'm working on a linear optimization problem so far the optimization results are as expected however with both solvers still I get zeros for the multipliers. So it must be something in my implementation. – Arraval May 28 '20 at 16:50
  • 1
    Here is more information on the Lagrange multipliers from the discussion on KKT conditions: https://apmonitor.com/me575/index.php/Main/KuhnTucker A zero Lagrange multiplier indicates that an equation inequality is not at a constraint, although you probably already know this. There is no documentation in Gekko on how the solvers generate the Lagrange multipliers. Maybe there is more information on Lagrange multipliers in the solver documentation. If IPOPT Lagrange multipliers should be reported, I recommend that you create a new bug or feature request on Gekko's GitHub repository. – John Hedengren May 29 '20 at 17:47