gurobi安装&vs配置gurobi
1、注册账号并登录
2、下载gurobi optimizer
3、获取license:User Portal (gurobi.com)
online course可以免ip验证。
4、GENERATE NOW会生成,打开cmd进入gurobi安装路径(如F:\gurobi1001\win64\bin>),输入grbgetkey 。会在C:\Users\xxx 下生成gurobi.lic,然后将gurobi.lic拷贝到gurobi的安装目录下面:
5、vs2022配置gurobi:
- 在C / C ++ /常规/附加包含目录下,添加:……\win64\include(gurobi安装路径)
- 在C / C ++ /常规/附加包含目录下,添加:……\win64\include(gurobi安装路径)
- 链接器/输入/附加依赖项下,添加gurobi100.lib和gurobi_c++mdd2017.lib
测试代码:
/* Copyright 2018, Gurobi Optimization, LLC */
/* This example formulates and solves the following simple QCP model:
maximize x
subject to x + y + z = 1
x^2 + y^2 <= z^2 (second-order cone)
x^2 <= yz (rotated second-order cone)
*/
#include "gurobi_c++.h"
using namespace std;
int main()
{try {GRBEnv env = GRBEnv();GRBModel model = GRBModel(env);// Create variablesGRBVar x = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "x");GRBVar y = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "y");GRBVar z = model.addVar(0.0, GRB_INFINITY, 0.0, GRB_CONTINUOUS, "z");// Set objectiveGRBLinExpr obj = x;model.setObjective(obj, GRB_MAXIMIZE);// Add linear constraint: x + y + z = 1model.addConstr(x + y + z == 1, "c0");// Add second-order cone: x^2 + y^2 <= z^2model.addQConstr(x * x + y * y <= z * z, "qc0");// Add rotated cone: x^2 <= yzmodel.addQConstr(x * x <= y * z, "qc1");// Optimize modelmodel.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;
}
测试结果如下: