Printing Lagrange multipliers
AnsweredDear Comunity,
After solving a LP problem, I would like to print in Python the Lagrange multipliers for specific equality constraints. Now, I am printing all "pi" values using model.getConstrs() function.
I look forward to hearing your comments and support.
Regards,
Victor
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Victor,
You can query the Pi attribute on each linear constraint. The script below returns the Pi attribute for a specific constraint named \(\texttt{c0}\).
# Access the constraint via its name
c = model.getConstrByName("c0")
# Query the Pi attribute on the linear constraint c
pi = c.PiBest regards,
Maliheh
0 -
Dear Maliheh,
Thank you so much for your message. However, I have problems getting these values for a matrix LP formulation. The optimization problem is modeled using model.addMVar() function.
Best Regards,
Victor
0 -
The Model.getConstrByName() method can be used regardless of how the variables are defined. Since you are using the Gurobi Matrix API, you can retrieve the constraint attributes more efficiently. Please see the example script below.
import numpy as np
A, b = np.ones((5, 10)), np.ones((5, 5))
model = gp.Model()
x = model.addMVar(shape=(10, 5), name="x")
# Add constraints Ax <= b. This will add 25 constraints to the model. The constraints
# are named as c0[0,0], c0[0,1], ..., c0[4,4]. In other words, the scalar name given
# is subscripted by the index of the constraints in the matrix
c = model.addConstr(A @ x <= b, name="c0")
model.optimize()
# Since c is an <MConstr (5, 5)> object, calling c.Pi returns
# a numpy array with shape (5, 5) where pi[i,j] equals the Pi attribute for
# constraint named as c0[i,j]
pi = c.Pi
# Alternatively, you can do the following to query the Pi attribute for a specific
# constraint named "c0[0,4]", for example:
c0_04 = model.getConstrByName("c0[0,4]")
pi_04 = c0_04.PiBest regards,
Maliheh
0 -
Dear Maliheh,
Thank you so much for your message and support. I got the Lagrange multipliers.
Best Regards,
Victor
0
Post is closed for comments.
Comments
5 comments