sensitivity analysis

721 views
Skip to first unread message

Jorge von Atzingen dos Reis

unread,
Sep 11, 2015, 9:40:48 AM9/11/15
to Gurobi Optimization
Hi
How do I see the sensitivity analysis ( shadow price and constraint slack ) using the GUROBI and C ++

I found it but did not understand how to use:

"objval: The objective value of the computed solution.
runtime: The elapsed wall-clock time (in seconds) for the optimization.
x: The computed solution. This array contains one entry for each column of A.
slack: The constraint slack for the computed solution. This array contains one entry for each row of A.
qcslack: The quadratic constraint slack in the current solution. This array contains one entry for second-order cone constraint and one entry for each quadratic constraint. The slacks for the second-order cone constraints appear before the slacks for the quadratic constraints.
pi: Dual values for the computed solution (also known as shadow prices). This array contains one entry for each row of A." (Gurobi Optimizer Reference Manual, page 432)

My source code is:

//------------------------------------------------------------------------------ 
#include "gurobi_c++.h"
using namespace std;
//------------------------------------------------------------------------------ 
int main()
{
try
{
GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env);

//Criando as variaveis
GRBVar x1 = model.addVar(0, GRB_INFINITY, 0, GRB_CONTINUOUS, "Soja");//GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER
GRBVar x2 = model.addVar(0, GRB_INFINITY, 0, GRB_CONTINUOUS, "Milho");//GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER
GRBVar x3 = model.addVar(0, 15000, 0, GRB_CONTINUOUS, "Serragem");//GRB_CONTINUOUS, GRB_BINARY, GRB_INTEGER

//Adicionando novas variaveis
model.update();

//Funcao Objetivo: min= 0.50*x1+0.30*x2+0.02*x3
model.setObjective(0.50*x1+0.30*x2+0.02*x3, GRB_MINIMIZE);

//primeira restricao: 15*x1+12*x2+2*x3>=10000
model.addConstr(15*x1+12*x2+2*x3>=10000, "Calcio");

//segunda restricao: 40*x1+10*x2+1*x3>=20000
model.addConstr(40*x1+10*x2+1*x3>=20000, "Proteina");

//terceira restricao: 290*x1+340*x2+80*x3>=200000000
model.addConstr(290*x1+340*x2+80*x3>=200000000, "Carboidratos");

//Resolvendo o modelo
  model.optimize();

cout << x1.get(GRB_StringAttr_VarName) << " = "<< x1.get(GRB_DoubleAttr_X) << endl;
cout << x2.get(GRB_StringAttr_VarName) << " = "<< x2.get(GRB_DoubleAttr_X) << endl;
cout << x3.get(GRB_StringAttr_VarName) << " = "<< x3.get(GRB_DoubleAttr_X) << endl;
cout << "Função Objetivo = " << model.get(GRB_DoubleAttr_ObjVal) << endl;

} catch(GRBException e)
{
cout << "Erro = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch(...)
{
cout << "Erro durante a otimizacao" << endl;
}
return 0;
}
//------------------------------------------------------------------------------ 

Thanks
Jorge Reis

Renan Garcia

unread,
Sep 22, 2015, 2:25:33 PM9/22/15
to gur...@googlegroups.com
Here's a snippet of code you can add after printing your objective function that will print the dual and slack for each constraint:

   GRBConstr *c = model.getConstrs();
   for(int i = 0; i < model.get(GRB_IntAttr_NumConstrs); i++) {
      cout << c[i].get(GRB_StringAttr_ConstrName)
           << " pi = "<< c[i].get(GRB_DoubleAttr_Pi)
           << ", slack = "<< c[i].get(GRB_DoubleAttr_Slack) << endl;
   }
   delete c;

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

GuPy

unread,
May 19, 2016, 2:42:36 AM5/19/16
to Gurobi Optimization

Hi Renan,

Can you plz help me in getting slack and dual info for python. Can you plz share a similar kind of snippet of python code. 

Thanks, 

Renan Garcia

unread,
May 19, 2016, 9:31:33 AM5/19/16
to gur...@googlegroups.com
The following line will print the name, dual value and slack for each constraint to the screen:

  model.printAttr(['ConstrName', 'Pi', 'Slack'])

If you want to query for attributes programmatically, please see https://www.gurobi.com/documentation/6.5/refman/attribute_examples.html#sec:AttributeExamples for guidance.

Raza Rafique

unread,
May 19, 2016, 9:36:58 AM5/19/16
to gur...@googlegroups.com
Thanks a lot.

Regards,

Raza Rafique




You received this message because you are subscribed to a topic in the Google Groups "Gurobi Optimization" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gurobi/M_NuDM3QI-A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gurobi+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages