一、涉及软件


Visual Studio 2017、Visual Studio 2019

CPLEX 12.9.0



二、配置效果


1、可以实现C++调用CPLEX求解线性规划,混合整数规划等;

2、可以在debug和release两种模式下进行调试或者运行代码





三、配置步骤


1、首先选择代码运行的环境


20210701105646394.png


(1)、将平台设置为 x64 或者 (活动)x64 ,此处一定需要修改!不能使用 x32 平台模式


(2)、配置的设置可以根据需要自行选择 release 模式或者 debug 模式(作者建议两种都进行配置一下,方便后续使用)





2、打开项目的属性项


20210701101229166.png




3、修改C/C++附加包含目录


20210701101622924.png


这里找到CPLEX的安装目录(根据自己的安装目录进行修改),如本人的是:


(注:下述若不做特殊说明,均需要将目录替换为自己的目录,若安装时没进行修改,从IBM/ILOG/…的目录同下述目录应该相同)



将上述两项加入到附加目录之中,点击确定,效果如下:

20210701102357509.png




4、修改C/C++预处理器中的预处理器定义项

20210701102638816.png


添加下述命令到预处理器定义中( 此处无需进行修改,直接复制粘贴就好 ):




5、修改C/C++代码生成中的运行库

20210701102950615.png

将此处的改为 多线程DLL(/MD) 或者 多线程调试DLL(/MDd)




6、修改链接器常规项中的“附加库目录项”

20210701104452281.png


将下述两个目录添加到“附加库目录项”中,目录地址同上述CPLEX安装目录相同



7、修改链接器输入中的“附加依赖项”

20210701105221893.png

将下述两个目录添加到“附加库目录项”中,目录地址同上述CPLEX安装目录相同


8、 点击确定,点击应用,大功告成!





四、运行测试代码


1、下面给出CPLEX官方测试文档中的 cutstock 代码进行测试

#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
#define RC_EPS 1.0e-6
static void readData(const char* filename, IloNum& rollWidth,
    IloNumArray& size, IloNumArray& amount);
static void report1(IloCplex& cutSolver, IloNumVarArray Cut,
    IloRangeArray Fill);
static void report2(IloAlgorithm& patSolver,
    IloNumVarArray Use,
    IloObjective obj);
static void report3(IloCplex& cutSolver, IloNumVarArray Cut);
/// MAIN PROGRAM ///
int main(int argc, char **argv)
    IloEnv env;
    try {
        IloInt  i, j;
        IloNum      rollWidth;
        IloNumArray amount(env);
        IloNumArray size(env);
        if (argc > 1)
            readData(argv[1], rollWidth, size, amount);
            readData("cutstock.dat",
                rollWidth, size, amount);
        /// CUTTING-OPTIMIZATION PROBLEM ///
        IloModel cutOpt(env);
        IloObjective   RollsUsed = IloAdd(cutOpt, IloMinimize(env));
        IloRangeArray  Fill = IloAdd(cutOpt,
            IloRangeArray(env, amount, IloInfinity));
        IloNumVarArray Cut(env);
        IloInt nWdth = size.getSize();
        for (j = 0; j < nWdth; j++) {
            Cut.add(IloNumVar(RollsUsed(1) + Fill[j](int(rollWidth / size[j]))));
        IloCplex cutSolver(cutOpt);
        /// PATTERN-GENERATION PROBLEM ///
        IloModel patGen(env);
        IloObjective ReducedCost = IloAdd(patGen, IloMinimize(env, 1));
        IloNumVarArray Use(env, nWdth, 0.0, IloInfinity, ILOINT);
        patGen.add(IloScalProd(size, Use) <= rollWidth);
        IloCplex patSolver(patGen);
        /// COLUMN-GENERATION PROCEDURE ///
        IloNumArray price(env, nWdth);
        IloNumArray newPatt(env, nWdth);
        /// COLUMN-GENERATION PROCEDURE ///
        for (;;) {
            /// OPTIMIZE OVER CURRENT PATTERNS ///
            cutSolver.solve();
            report1(cutSolver, Cut, Fill);
            /// FIND AND ADD A NEW PATTERN ///
            for (i = 0; i < nWdth; i++) {
                price[i] = -cutSolver.getDual(Fill[i]);
            ReducedCost.setLinearCoefs(Use, price);
            patSolver.solve();
            report2(patSolver, Use, ReducedCost);
            if (patSolver.getValue(ReducedCost) > -RC_EPS) break;
            patSolver.getValues(newPatt, Use);
            Cut.add(IloNumVar(RollsUsed(1) + Fill(newPatt)));
        cutOpt.add(IloConversion(env, Cut, ILOINT));
        cutSolver.solve();
        cout << "Solution status: " << cutSolver.getStatus() << endl;
        report3(cutSolver, Cut);
    catch (IloException& ex) {
        cerr << "Error: " << ex << endl;
    catch (...) {
        cerr << "Error" << endl;
    env.end();
    return 0;
static void readData(const char* filename, IloNum& rollWidth,
    IloNumArray& size, IloNumArray& amount)
    ifstream in(filename);
    if (in) {
        in >> rollWidth;
        in >> size;
        in >> amount;
    else {
        cerr << "No such file: " << filename << endl;
        throw(1);
static void report1(IloCplex& cutSolver, IloNumVarArray Cut,
    IloRangeArray Fill)
    cout << endl;
    cout << "Using " << cutSolver.getObjValue() << " rolls" << endl;
    cout << endl;
    for (IloInt j = 0; j < Cut.getSize(); j++) {
        cout << "  Cut" << j << " = " << cutSolver.getValue(Cut[j]) << endl;
    cout << endl;
    for (IloInt i = 0; i < Fill.getSize(); i++) {
        cout << "  Fill" << i << " = " << cutSolver.getDual(Fill[i]) << endl;
    cout << endl;
static void report2(IloAlgorithm& patSolver, IloNumVarArray Use,
    IloObjective obj)
    cout << endl;
    cout << "Reduced cost is " << patSolver.getValue(obj) << endl;
    cout << endl;
    if (patSolver.getValue(obj) <= -RC_EPS) {
        for (IloInt i = 0; i < Use.getSize(); i++) {
            cout << "  Use" << i << " = " << patSolver.getValue(Use[i]) << endl;
        cout << endl;
static void report3(IloCplex& cutSolver, IloNumVarArray Cut)
    cout << endl;
    cout << "Best integer solution uses "
        << cutSolver.getObjValue() << " rolls" << endl;
    cout << endl;
    for (IloInt j = 0; j < Cut.getSize(); j++) {
        cout << "  Cut" << j << " = " << cutSolver.getValue(Cut[j]) << endl;


2、首先编译代码,出现下面截图说明配置成功辽

20210701110649913.png


3、给出输入文件信息如下:




4、运行结果如下:


20210701111039800.png


THE END


谢谢观看,欢迎批评指正













Visual Studio 2022 版本 17.4 预览版 3 中对c++编译时优化的内容你都知道吗
Visual Studio 2022 版本 17.4 预览版 3 中对c++编译时优化的内容你都知道吗
Linux Ubuntu配置Visual Studio Code及C++环境的方法
本文介绍在Linux Ubuntu操作系统下,配置Visual Studio Code软件与C++代码开发环境的方法~
0基础都能看懂的 Visual Studio Code(VScode)使用脚本一键配置安装C/C++环境、编译运行Windows版本教程(内附脚本、安装包下载链接)
网上很多配置VScode的C、C++环境的教程,但是很多时候跟着从头到尾做了之后反而还是运行不了,于是笔者在网上翻阅资料后,发现了一个自动配置环境的脚本,亲测有效,大概5分钟就可以配置好环境了。直接进入教程。
基于Windows和WSL 2的Visual Studio Code (VS Code) 安装及搭建 C/C++ 编译环境完整详细步骤
基于Windows和WSL 2的Visual Studio Code (VS Code) 安装及搭建 C/C++ 编译环境完整详细步骤
【错误记录】Visual Studio 编译 C++ 代码报错 ( To disable deprecation, use _CRT_SECURE_NO_WARNINGS. )
【错误记录】Visual Studio 编译 C++ 代码报错 ( To disable deprecation, use _CRT_SECURE_NO_WARNINGS. )
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(二)
【Visual Studio】Visual Studio 2019 创建 Windows 控制台程序 ( 安装 ‘使用 C++ 的桌面开发‘ 组件 | 创建并运行 Windows 控制台程序 )(二)