Dual value Pi and QCPi for specified constraint - python
AnsweredHello,
I am using Gurobi for Python. I am trying to obtain Pi or QCPi for a specific constraint in my model, similarly as it is asked in the following post:
Referencing constraints and retrieving their dual (Pi) values with C++ – Gurobi Help Center
Could you please extend the explanation for Python as well. I have tried searching for constraint by name and then getting the QCPi attribute, without success:
c0 = model.getConstrByName('dem_%s_%s' % (k,n))
sh_pr[k,n] = c0.get(GRB.Attr.QCPi)
Also, is there a way to see which constraint is associated to Pi when doing the general call: model.getAttr(GRB.Attr.QCPi)
-
Official comment
This post is more than three 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. -
I have also tried the following without succes:
fixed = model.fixed()
fixed.Params.QCPDual = 1
fixed.optimize()
constrs = fixed.getConstrs()
shadow_price = fixed.getAttr(GRB.Attr.QCPi) #this works but there is no information about associated constraints to QCPi values.
sh_pr = np.zeros((len(shadow_price),2))
sh_p_i = 0
for ci in range(0,len(constrs)+1):
try:
sh_pr[sh_p_i,0] = constrs[ci].getAttr(GRB.Attr.QCPi) #this does not work
sh_pr[sh_p_i,1] = constrs[ci].index
sh_p_i = sh_p_i + 1
except Exception:
passthanks
0 -
Model.getConstrs() returns a list of all linear constraints in the model. Model.getQConstrs() returns a list of all quadratic constraints in the model.
The code
model.getAttr(GRB.Attr.QCPi)
is equivalent to
model.getAttr(GRB.Attr.QCPi, model.getQConstrs())
0 -
Thanks Eli for the clarification. Now it works.
0 -
Hello,
as I'm writing the above after my initial model , (model=ConcreteModel(name=".......")), has been solved/optimized i get the below message:
'ConcreteModel' object has no attribute 'fixed' .
Could you please advise why I'm getting this? Should i put the code before solving the initial quadtratic MILP model?
0 -
The code included in previous posts applies to models built using \(\texttt{gurobipy}\), Gurobi's native Python interface. Specifically, \(\texttt{model}\) is a Model object, and the user calls the Model.fixed() method.
In contrast, you are building your model using Pyomo. This is a separate, third-party modeling framework. As far as I can tell, the ConcreteModel class has no analogous method to fix every non-continuous variable. However, the Pyomo documentation suggests using the following code snippet to fix all non-continuous variables to their current values:
import pyomo.environ as pyo
for var in instance.component_data_objects(pyo.Var, active=True): if not var.is_continuous(): var.fixed = TrueThe Fixing Variables and Re-solving section of the Pyomo documentation might also be useful.
0
Post is closed for comments.
Comments
6 comments