Teach you step by step how to configure Gurobi in C++

Welcome to follow our WeChat public account Yun Xiaochi

Insert image description here

Configuring Gurobi in C++: Video Tutorial

The following is a detailed explanation video of configuring Gurobi in C++ recorded by the author for the convenience of readers to watch and learn.

Configuring Gurobi in C++

Let’s take VS 2022 as an example to explain.

I have installed Gurobi 9.5.1. The installation directory is as follows:

Insert image description here

Next, let's explain C++ configuration Gurobi in detail.

Create a new C++ project

First, we create a new C++ project. You can name it whatever you want.

The steps are

【File】→ \rightarrow【New】→ \rightarrow【Project】→ \rightarrow Then name the project and click [Create].

Insert image description here
Insert image description here

Then we first prepare the two directories that need to be added later. as follows:

  • D:\Develop\Gurobi951\win64\include
  • D:\Develop\Gurobi951\win64\lib

Insert image description here
The files in these two directories are as follows:
Insert image description here
Insert image description here
Next, Gurobi is officially configured.

Configure C/C++ external include directories

The specific steps are

Right-click [Gurobi_Notes] (note, this is your project name) → \rightarrow【Attribute】→ \rightarrow【C/C++】 → \rightarrow [External include directory]→ \rightarrow Enter the directoryD:\Develop\Gurobi951\win64\includeand add→ \rightarrow [OK].

The specific interface is as shown below. This step is actually to configure the required header files, that is, .hfiles gurobi_c++.h.

Configure C/C++ external include directories
In addition, you need to ensure that the submenu item [Precompiled Header] of [C/C++] is set to [Do not use precompiled header].

Configure additional catalogs and additional dependencies for the linker

The next configuration is all in the [Linker] section. The purpose of this configuration step is to configure some dependent libraries so that VS 2022 can recognize them.

The first is 【链接器】the configuration of [Additional Library Directory], the method is as follows:

【Linker】→ \rightarrow【General】→ \rightarrow Under [Additional Library Directory], add the following file path:D:\Develop\Gurobi951\win64\lib.

As shown below.

Configuring additional directories for the linker

Next, you need to configure additional dependencies of [Linker], as follows.

Here, we first introduce the 2 dependencies that need to be added. We first move to the Gurobi installation directory D:\Develop\Gurobi951\win64\lib, and we can see the following files.
2 dependencies that need to be added
The filenames we need are gurobi95.liband gurobi_c++mdd2019.lib.

【Linker】→ \rightarrow【Input】→ \rightarrow Under [Additional Library Directory], add: the names of the two lib files, that is, addgurobi95.libandgurobi_c++mdd2019.lib. Note that they are separated by semicolons, that isgurobi95.lib;gurobi_c++mdd2019.lib, if there is a problem, delete the other parts here and keep only themgurobi95.lib;gurobi_c++mdd2019.lib. details as follows.

2 dependencies added successfully

Once configured, click OK. At this point, the configuration is complete and case testing can be carried out. Happy :)

case test

We use Gurobi's official example for testing. The directory of the example is: D:\Develop\Gurobi951\win64\examples\c++\mip1_c++.cpp. The specific code is as follows:

/* Copyright 2022, Gurobi Optimization, LLC */

/* This example formulates and solves the following simple MIP model:

     maximize    x +   y + 2 z
     subject to  x + 2 y + 3 z <= 4
                 x +   y       >= 1
                 x, y, z binary
*/

#include "gurobi_c++.h"
using namespace std;

int
main(int   argc,
     char *argv[])
{
    
    
  try {
    
    

    // Create an environment
    GRBEnv env = GRBEnv(true);
    env.set("LogFile", "mip1.log");
    env.start();

    // Create an empty model
    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");

    // 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;
}

Run program

success! The results are as follows.

operation result

References

  1. Gurobi 9.5.1, examples, mip1_c++.cpp.

CSDN link

The CSDN version of this article is as follows:

Guess you like

Origin blog.csdn.net/HsinglukLiu/article/details/127761606