Visual Studio(VS2017/VS2019) C++ 配置 CPLEX 教程

一、涉及软件

Visual Studio 2017、Visual Studio 2019
CPLEX 12.9.0

二、配置效果

1、可以实现C++调用CPLEX求解线性规划,混合整数规划等;
2、可以在debug和release两种模式下进行调试或者运行代码

三、配置步骤

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

在这里插入图片描述
(1)、将平台设置为x64或者(活动)x64,此处一定需要修改!不能使用x32平台模式
(2)、配置的设置可以根据需要自行选择release模式或者debug模式(作者建议两种都进行配置一下,方便后续使用)

2、打开项目的属性项

打开项目的属性

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

修改C++常规项中的附加包含目录
这里找到CPLEX的安装目录(根据自己的安装目录进行修改),如本人的是:
(注:下述若不做特殊说明,均需要将目录替换为自己的目录,若安装时没进行修改,从IBM/ILOG/…的目录同下述目录应该相同)

//CPLEX安装目录
D:\Program Files\IBM\ILOG\CPLEX_Studio129\concert\include
D:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\include

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

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

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

NDEBUG
_CONSOLE
IL_STD

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

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

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

在这里插入图片描述
将下述两个目录添加到“附加库目录项”中,目录地址同上述CPLEX安装目录相同

D:\Program Files\IBM\ILOG\CPLEX_Studio129\concert\lib\x64_windows_vs2017\stat_mda
D:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\lib\x64_windows_vs2017\stat_mda

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

在这里插入图片描述
将下述两个目录添加到“附加库目录项”中,目录地址同上述CPLEX安装目录相同

D:\Program Files\IBM\ILOG\CPLEX_Studio129\concert\lib\x64_windows_vs2017\stat_mda\concert.lib
D:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\lib\x64_windows_vs2017\stat_mda\cplex1290.lib
D:\Program Files\IBM\ILOG\CPLEX_Studio129\cplex\lib\x64_windows_vs2017\stat_mda\ilocplex.lib

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);
		else
			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、首先编译代码,出现下面截图说明配置成功辽

在这里插入图片描述

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

115
[25, 40, 50, 55, 70]
[50, 36, 24, 8, 30]

4、运行结果如下:

在这里插入图片描述

THE END

谢谢观看,欢迎批评指正

  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dragon Fly

多谢老板赏钱[抱拳抱拳抱拳]

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值