1、创建空项目,并建立一个cpp
2、设置debug x64
3、c/c++ 常规 附加包含目录
F:\programmsoftware\Groubi\win64\include
4、链接器 常规 附加库目录
F:\programmsoftware\Groubi\win64\lib
5、链接器 输入 附加依赖项 gurobi110.lib gurobi_c++mdd2017.lib 这个文件需要看自己的gurobi安装目录下lib里的数字
6、使用测试代码测试
/* Copyright 2023, 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;
}
7、结果如下
Gurobi Optimizer version 11.0.0 build v11.0.0rc2 (win64 - Windows 11.0 (22621.2))
CPU model: 12th Gen Intel(R) Core(TM) i9-12900H, instruction set [SSE2|AVX|AVX2]
Thread count: 14 physical cores, 20 logical processors, using up to 20 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0xc501370b
Model has 5 quadratic objective terms
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [2e+00, 2e+00]
QObjective range [2e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Presolve time: 0.00s
Presolved: 2 rows, 3 columns, 5 nonzeros
Presolved model has 5 quadratic objective terms
Ordering time: 0.00s
Barrier statistics:
Free vars : 2
AA' NZ : 6.000e+00
Factor NZ : 1.000e+01
Factor Ops : 3.000e+01 (less than 1 second per iteration)
Threads : 1
Objective Residual
Iter Primal Dual Primal Dual Compl Time
0 1.69015022e+05 -1.71012100e+05 1.50e+03 3.33e+02 1.00e+06 0s
1 3.60255402e+04 -3.91306233e+04 2.28e+02 3.82e+01 1.20e+05 0s
2 4.14685168e+00 -4.40925173e+03 1.80e+00 4.00e-01 1.83e+03 0s
3 2.81937163e+00 -1.92736174e+03 1.80e-06 4.00e-07 2.41e+02 0s
4 2.81628339e+00 -1.81287557e-01 8.60e-10 1.91e-10 3.75e-01 0s
5 2.26977145e+00 2.06670895e+00 6.89e-12 1.53e-12 2.54e-02 0s
6 2.11498124e+00 2.11029644e+00 0.00e+00 2.22e-16 5.86e-04 0s
7 2.11111498e+00 2.11111030e+00 0.00e+00 2.50e-16 5.85e-07 0s
8 2.11111111e+00 2.11111111e+00 0.00e+00 4.62e-17 5.86e-10 0s
Barrier solved model in 8 iterations and 0.00 seconds (0.00 work units)
Optimal objective 2.11111111e+00
x 3.255e-09
y 1
z 0.666667
Obj: 2.11111
Gurobi Optimizer version 11.0.0 build v11.0.0rc2 (win64 - Windows 11.0 (22621.2))
CPU model: 12th Gen Intel(R) Core(TM) i9-12900H, instruction set [SSE2|AVX|AVX2]
Thread count: 14 physical cores, 20 logical processors, using up to 20 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x026954d2
Model has 5 quadratic objective terms
Variable types: 0 continuous, 3 integer (0 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [2e+00, 2e+00]
QObjective range [2e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Found heuristic solution: objective 7.0000000
Presolve removed 1 rows and 1 columns
Presolve time: 0.00s
Presolved: 2 rows, 3 columns, 5 nonzeros
Variable types: 0 continuous, 3 integer (3 binary)
Found heuristic solution: objective 3.0000000
Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
Thread count was 20 (of 20 available processors)
Solution count 2: 3 7
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 0
y 1
z 1
Obj: 3
E:\vs_project博\gurobiTest\x64\Debug\gurobiTest.exe (进程 37404)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .