Gurobi学习(三):VS2019中Gurobi的配置与测试
使用Visual Studio 2019配置一个Gurobi C++项目
一、Visual Studio 2019的下载安装
官网: 本地下载:
1.从官网:进入下载 一般选择Community 2019下载
2.下载完成后双击进行安装
3.安装完成后,看个人需要选择你要安装的工作负载,(工 作负载后期还可以再装的,不用着急全部装完),修改安装路径 选择好安装路径后,点击安装开始安装 安装完成后关闭 安装负载窗口
4.在电脑开始处点击 Visual Stadio 2019即可打开进行相应操作
二、Visual Studio 2019配置一个Gurobi C++项目
1.打开安装好的vs2019 在“文件”菜单下,选择“新建”——“项目”;在“已安装的模板”面板上,选择第一个“空项目”,点击下一步进行创建
2.在“解决方案资源管理器”面板中项目名称下方的“源文件”下,新建项,取名为test.cpp。新建完成如下 3.在“解决方案资源管理器”面板中右键单击项目名称,然后选择“属性”。 如下操作是针对64位Gurobi库,32位同理
3.1将平台设置为x64;在C / C ++ /常规/附加包含目录下,添加:……win64include(gurobi安装路径)
3.2在链接器/常规/附加库目录下,添加:……win64 lib
3.3在“ C / C ++” /“预编译头” /“预编译头”下,选择“不使用预编译头”
3.4在链接器/输入/其他依赖项下,添加gurobi90.lib和gurobi_c++mdd2019.lib
点击“确定”配置完成 4.测试test.cpp
/* 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 variables GRBVar 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 objective GRBLinExpr obj = x; model.setObjective(obj, GRB_MAXIMIZE); // Add linear constraint: x + y + z = 1 model.addConstr(x + y + z == 1, "c0"); // Add second-order cone: x^2 + y^2 <= z^2 model.addQConstr(x * x + y * y <= z * z, "qc0"); // Add rotated cone: x^2 <= yz model.addQConstr(x * x <= y * z, "qc1"); // 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; }
点击“调试”——“开始执行” 得到如下结果,测试成功
下一篇:
超简单利用java实现小游戏剪刀石头布