qp_c++.cpp#
/* Copyright 2024, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple QP model:
minimize x^2 + x*y + y^2 + y*z + z^2 + 2 x
subject to x + 2 y + 3 z >= 4
x + y >= 1
x, y, z non-negative
It solves it once as a continuous model, and once as an integer model.
*/
#include "gurobi_c++.h"
using namespace std;
int
main(int argc,
char *argv[])
{
try {
GRBEnv env = GRBEnv();
GRBModel model = GRBModel(env);
// Create variables
GRBVar x = model.addVar(0.0, 1.0, 0.0, GRB_CONTINUOUS, "x");
GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB_CONTINUOUS, "y");
GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB_CONTINUOUS, "z");
// Set objective
GRBQuadExpr obj = x*x + x*y + y*y + y*z + z*z + 2*x;
model.setObjective(obj);
// Add constraint: x + 2 y + 3 z >= 4
model.addConstr(x + 2 * y + 3 * z >= 4, "c0");
// Add constraint: x + y >= 1
model.addConstr(x + y >= 1, "c1");
// Optimize model
model.optimize();
cout << x.get(GRB_StringAttr_VarName) << " "
<< x.get(GRB_DoubleAttr_X) << endl;
cout << y.get(GRB_StringAttr_VarName) << " "
<< y.get(GRB_DoubleAttr_X) << endl;
cout << z.get(GRB_StringAttr_VarName) << " "
<< z.get(GRB_DoubleAttr_X) << endl;
cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
// Change variable types to integer
x.set(GRB_CharAttr_VType, GRB_INTEGER);
y.set(GRB_CharAttr_VType, GRB_INTEGER);
z.set(GRB_CharAttr_VType, GRB_INTEGER);
// Optimize model
model.optimize();
cout << x.get(GRB_StringAttr_VarName) << " "
<< x.get(GRB_DoubleAttr_X) << endl;
cout << y.get(GRB_StringAttr_VarName) << " "
<< y.get(GRB_DoubleAttr_X) << endl;
cout << z.get(GRB_StringAttr_VarName) << " "
<< z.get(GRB_DoubleAttr_X) << endl;
cout << "Obj: " << model.get(GRB_DoubleAttr_ObjVal) << endl;
} catch(GRBException e) {
cout << "Error code = " << e.getErrorCode() << endl;
cout << e.getMessage() << endl;
} catch(...) {
cout << "Exception during optimization" << endl;
}
return 0;
}