C++ Gurobi programming for Beginners

Hey! Once you set up your C++ and Gurobi, you might want to quickly start solving your first problem! (If you haven’t set up your C++ and Gurobi under the Linux environment and don’t know how to compile and run a code under the Linux environment, see this topic: Setting up Gurobi with C++ in the Linux environment - #2 by yuanruisang)

To get started, the following is a very common C++ code for beginners, and code outputs “Hello World!” to the command window:

#include<iostream>
using namespace std;
int main()
{
 cout << "Hello World!" << endl;
 return 0;
}

If you would like to learn C++ systematically, I recommend this tutorial: http://www.cplusplus.com/doc/tutorial/

To write a C++ code that can use the Gurobi solver, we need to:

  1. Add #include “gurobi_c++.h” to the code;
  2. Create Gurobi environment;
  3. Define a model;
  4. Define the variables in the model;
  5. Define the objective function in the model;
  6. Define the constraints in the model;
  7. Solve the model;
  8. Output the results.

Next, we will learn how to write a C++ Gurobi code using a very simple example.

Let’s say we want to solve an optimization problem:

Objective: min(20x+30y)
Constraints:
0<=x<=50
0<=y<=100
x+y=70

The following is the C++ Gurobi code for the problem:

#include "gurobi_c++.h"
#include<iostream>
using namespace std;
int main()
{
	GRBEnv env = GRBEnv(); // Set up the Gurobi environment
	GRBModel SimpleExample = GRBModel(env); // Define the model as "SimpleExample" - but you could name the model whatever you like
	GRBVar x = SimpleExample.addVar(0, 50, 20, GRB_CONTINUOUS, "x"); // Define variable x in the model and its range (0-50)
	GRBVar y = SimpleExample.addVar(0, 100, 30, GRB_CONTINUOUS, "y"); // Define variable y in the model and its range (0-100)
	SimpleExample.setObjective(20*x+30*y, GRB_MINIMIZE); // Define the objective function as to minimize 20x+30y
	SimpleExample.addConstr(x + y == 70); // Define the third constraint (x+y=70)
	SimpleExample.optimize(); // Solve the model
	std::cout << "Objective value: " << SimpleExample.get(GRB_DoubleAttr_ObjVal) << endl;  // Output the objective value
	std::cout << "x = " << x.get(GRB_DoubleAttr_X) << endl; // Output the value of x
	std::cout << "y = " << y.get(GRB_DoubleAttr_X) << endl; // Output the value of y
	return 0;
}

If you compile and run the code, you will get something similar to the following:

Academic license - for non-commercial use only
Gurobi Optimizer version 9.0.2 build v9.0.2rc0 (linux64)
Optimize a model with 1 rows, 2 columns and 2 nonzeros
Model fingerprint: 0x0699e64d
Coefficient statistics:
  Matrix range     [1e+00, 1e+00]
  Objective range  [2e+01, 3e+01]
  Bounds range     [5e+01, 1e+02]
  RHS range        [7e+01, 7e+01]
Presolve removed 1 rows and 2 columns
Presolve time: 0.00s
Presolve: All rows and columns removed
Iteration    Objective       Primal Inf.    Dual Inf.      Time
       0    1.6000000e+03   0.000000e+00   0.000000e+00      0s

Solved in 0 iterations and 0.01 seconds
Optimal objective  1.600000000e+03
Objective value: 1600
x = 50
y = 20

The last three rows are our results.

Curious about how I formatted the code here? Look at this topic: Make your posts easier to read using the Markdown language