Advertisement
aircampro

example of gurobi API

Jun 25th, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. /*
  2.     This example formulates and solves the following simple QCP model:
  3.     using the Gurobi API
  4.  
  5.      maximize    2x + 3y
  6.      subject to  x + y/2 + 3z = 5
  7.                  x^2 - y^2 <= z^2 (second-order cone)
  8.                  2(x^2) <= yz        (rotated second-order cone)
  9. */
  10.  
  11. #define GU_NO_ERROR 0
  12. #define GU_ERR_GRP_EXCEPTION 2
  13. #define GU_ERR_EXCEPTION 3
  14.  
  15. #include "gurobi_c++.h"
  16. using namespace std;
  17.  
  18. int
  19. main(int   argc,
  20.      char *argv[])
  21. {
  22.   try {
  23.  
  24.     GRBEnv* env = 0;
  25.     GRPVar* x = 0;
  26.     GRPVar* y = 0;
  27.     GRPVar* z = 0;
  28.     int sequenceState = 0;
  29.     int retCode = GU_NO_ERROR;
  30.  
  31.     env = new GRBEnv();                                                 // Create Environment
  32.  
  33.     GRBModel model = GRBModel(*env);                                    // Make Model
  34.     sequenceState = 1;
  35.     model.set(GRB_StringAttr_ModelName, "QCP");
  36.  
  37.     x = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "x");      // Create variables as per the model
  38.     sequenceState = 2;
  39.     y = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "y");
  40.     sequenceState = 3;
  41.     z = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "z");
  42.     sequenceState = 4;
  43.  
  44.  
  45.     GRBLinExpr obj = (2*x) + y;                                           // Set objective equation and maximize method
  46.     model.setObjective(obj, GRB_MAXIMIZE);
  47.  
  48.     //  x + y/2 + 3z = 5
  49.     model.addConstr(x + (y/2.0) + (3.0*z) == 5.0, "c0");                 // Add each constraint
  50.  
  51.     //  x^2 - y^2 <= z^2
  52.     model.addQConstr(x*x - y*y <= z*z, "qc0");
  53.  
  54.     //  2x^2 <= yz
  55.     model.addQConstr((2.0*(x*x)) <= y*z, "qc1");
  56.  
  57.     // Optimize model
  58.     model.optimize();
  59.  
  60.     cout << x.get(GRB_StringAttr_VarName) << " "
  61.          << x.get(GRB_DoubleAttr_X) << endl;
  62.     cout << y.get(GRB_StringAttr_VarName) << " "
  63.          << y.get(GRB_DoubleAttr_X) << endl;
  64.     cout << z.get(GRB_StringAttr_VarName) << " "
  65.          << z.get(GRB_DoubleAttr_X) << endl;
  66.  
  67.     cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
  68.  
  69.   } catch(GRBException e) {
  70.     cout << "Error code = " << e.getErrorCode() << endl;
  71.     cout << e.getMessage() << endl;
  72.     retCode = GU_ERR_GRP_EXCEPTION;
  73.   } catch(...) {
  74.     cout << "Exception during optimization" << endl;
  75.     retCode = GU_ERR_EXCEPTION;
  76.   }
  77.  
  78.   if (sequenceState >= 4)  delete[] z;
  79.   if (sequenceState >= 3)  delete[] y;
  80.   if (sequenceState >= 2)  delete[] x;
  81.   if (sequenceState >= 1)  delete env;
  82.   return retCode;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement