1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- #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_BINARY, "x");
- GRBVar y = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "y");
- GRBVar z = model.addVar(0.0, 1.0, 0.0, GRB_BINARY, "z");
- // Integrate new variables
- model.update();
- // Set objective: maximize x + y + 2 z
- model.setObjective(x + y + 2 * z, GRB_MAXIMIZE);
- // 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;
- } catch(GRBException e) {
- cout << "Error code = " << e.getErrorCode() << endl;
- cout << e.getMessage() << endl;
- } catch(...) {
- cout << "Exception during optimization" << endl;
- }
- return 0;
- }
|