Skip to main content

Referencing constraints and retrieving their dual (Pi) values with C++

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    Gurobi Staff Gurobi Staff
    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 why not try our AI Gurobot?.
  • Elisabeth Rodríguez Heck
    Gurobi Staff Gurobi Staff

    Hi Ali,

    In order to retrieve the dual values of your constraints in the code above, you could first store these constraints in an array and use this array later to get the dual values. I am sharing a code snippet below printing the retrieved dual variables after optimization.

    GRBConstr* constrs = new GRBConstr[n];
    for (k = 1; k < n; k++) {
    GRBLinExpr u=0;
    for (j = 1; j < n; j++)
    u +=x[k][j];
    constrs[k] = model.addConstr(u == 1);
    }

    model.optimize();

    for (k = 1; k < n; k++) {
    cout << "Pi of constraint " << k << " is " << constrs[k].get(GRB_DoubleAttr_Pi) << endl;
    }
     
    Best regards,
    Elisabeth
    -1
  • Ali Balma
    Gurobi-versary
    First Comment
    First Question

    It works!

    Thanks a lot Elisabeth

    Best regards

    Ali

    0
  • Patricio Burdiles
    Gurobi-versary
    First Comment
    First Question

    Hello

    I am trying to extend the methodology presented here to python, but have not been succesfull.

    Here is a section of my code:

    I have also tried the following without success:

     

    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:
            pass

     

    I appreciate any comments.

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    The discussion about applying this methodology in Python is continued in the post Dual value Pi and QCPi for specified constraint - python.

    0

Post is closed for comments.