Cplex安装与入门教程(二)
作者:互联网
一、写在前面
在文章《Cplex安装与入门教程(一)史上最全》的基础上,补充代码的部分
二、如何写代码
- 代码头,请无脑直接抄。
#include <ilcplex/ilocplex.h>
#include <stdio.h>
using namespace std;
ILOSTLBEGIN
- 声明环境,建立模型。
IloEnv env;
IloModel model(env);
- 定义决策变量,使用ilocplex包含的数据类型,或自己定义新的变量。
IloInt
IloNumVarArray
IloNumArray
IloIntVarArray
IloIntArray
typedef IloArray<IloNumVarArray> IloNumVarArray2;
typedef IloArray<IloNumVarArray2> IloNumVarArray3;
IloNumVarArray3 t(env, I);
for (int i = 0; i < I; i++)
{
t[i] = IloNumVarArray2(env, I);
for (int j = 0; j < I; j++)
{
t[i][j] = IloNumVarArray(env, I, 0, 100000);
}
}
- 定义模型系数,可使用常用数据类型(int等)或ilocplex包含的数据类型。 可以直接赋值,也可以通过读取.txt/.dat文件输入
ifstream inn;
inn.open("data.txt");
if (inn)
{ inn >> b_1; inn >> b_2; inn >> b_3; inn >> b_4; }
inn.close();
-
使用以下网站查询各数据类型的用法:
http://www-eio.upc.es/lceio/manuals/cplex-11/html/refcppcplex/html/index/index.html#I -
定义目标函数,可以直接写,也可以使用IloExpr定义一个表达式。
// 适用于简单表达
model.add(IloMaximize(env, vars[0] + 2 * vars[1] + 3 * vars[2]));
// 适用于求和等需要迭代的表达
IloExpr v3(env);
v3 += (x[i][j] * b[i][j]);
model.add(IloMinimize(env, v3));
- 添加约束条件,同样有两种方法。
model.add(-vars[0] + vars[1] + vars[2] <= 20);
IloExpr v1(env);
for (i = 0; i < 4; i++)
{
v1.clear();
for (j = 0; j < 4; j++)
{ v1 += (x[i][j]); }
model.add(v1 == 1);
}
- 求解,无脑直接抄+1。
IloCplex cplex(model);
if (!cplex.solve()) {
env.error() << "Failed to optimize LP." << endl;
throw(-1);
}
- 输出结果,env.out() << 语句。
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
env.out() << "Values = " << vars << endl;
三、Example 1
- 问题:
min x1 + 2 x2 + 3 x3
s.t. -x1 + x2 + x3 ≤ 20
x1 - 3 x2 + x3 ≤ 30
0 ≤ x1 ≤ 40
0 ≤ x2
0 ≤ x3
#include <ilcplex/ilocplex.h>
#include <stdio.h>
using namespace std;
ILOSTLBEGIN
int main(void *) {
IloEnv env;
try {
IloModel model(env);
IloNumVarArray vars(env);
vars.add(IloNumVar(env, 0.0, 40.0)); // 0 <= x1 <= 40
vars.add(IloNumVar(env)); // 0 <= x2
vars.add(IloNumVar(env)); // 0 <= x3
model.add(IloMaximize(env, vars[0] + 2 * vars[1] + 3 * vars[2])); //maximize x1 + 2 x2 + 3 x3
model.add(-vars[0] + vars[1] + vars[2] <= 20);//subject to -x1 + x2 + x3 <= 20
model.add(vars[0] - 3 * vars[1] + vars[2] <= 30);//x1 - 3 x2 + x3 <=30
IloCplex cplex(model);
if (!cplex.solve()) {
env.error() << "Failed to optimize LP." << endl;
throw(-1);
}
IloNumArray vals(env);
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
cplex.getValues(vals, vars);
env.out() << "Values = " << vals << endl;
}
catch (IloException & e) { cerr << "Concert exception caught: " << e << endl; }
catch (...) { cerr << "Unknown exception caught" << endl; }
env.end();
system("pause");
return 0;
}
四、Example 2
- 问题:
#ifndef abcde//ifndef做预处理,进行条件编译
#include <ilcplex/ilocplex.h>
typedef IloArray<IloIntVarArray> IntVarMatrix;
typedef IloArray<IloNumVarArray> NumVarMatrix;
typedef IloArray<IloIntArray> IntMatrix;
typedef IloArray<IloNumArray> NumMatrix;
//定义各种数组
#endif
ILOSTLBEGIN
int main(int argc, char **argv)
{
IloEnv env;
IloModel model(env);
IloExpr obj(env);
IloInt i, j;
IloInt c;
IloExpr v1(env);
IloExpr v2(env);
IloExpr v3(env);
IloNumArray b_1(env);
IloNumArray b_2(env);
IloNumArray b_3(env);
IloNumArray b_4(env);
//文件输入
ifstream inn;
inn.open("data.txt");
if (inn)
{
inn >> b_1; inn >> b_2; inn >> b_3; inn >> b_4;
}
inn.close();
NumMatrix b(env, 4);
for (i = 0; i < 4; i++)
{
b[i] = IloNumArray(env, 4);
}
b[0] = b_1; b[1] = b_2; b[2] = b_3; b[3] = b_4;
IntVarMatrix x(env, 4);
for (i = 0; i < 4; i++)
{
x[i] = IloIntVarArray(env, 4, 0, 1);
}
for (i = 0; i < 4; i++)
{
v1.clear();
v2.clear();
for (j = 0; j < 4; j++)
{
v1 += (x[i][j]);
v2 += (x[j][i]);
v3 += (x[i][j] * b[i][j]);
}
model.add(v1 == 1);
model.add(v2 == 1);
}
model.add(IloMinimize(env, v3));
obj.end();
//设置模型迭代结束条件
IloCplex cplex(model);
cplex.setParam(cplex.EpAGap, 0.001);
cplex.setParam(cplex.EpGap, 0.001);
//处理异常
cout << "begin to solve." << endl;
try
{
if (!cplex.solve())
{
if ((cplex.getStatus() == IloAlgorithm::Infeasible) ||
(cplex.getStatus() == IloAlgorithm::InfeasibleOrUnbounded))
{
cout << endl << "No solution - starting Conflict refinement" << endl;
}
env.error() << "Failed to optimize LP." << endl;
throw(-1);
}
cout << "异常处理完毕" << endl;
//输出结果及状态
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
env.out() << "obj = " << cplex.getValue(v3) << endl;
}
catch (IloException& e)
{
cerr << "Concert exception caught: " << e << endl;
//save results
}
catch (...)
{
cerr << "Unknown exception caught" << endl;
}
//目标结果输出
ofstream of;
of.open("result.txt");
of << cplex.getValue(v3) << "\n";
of.close();
//决策变量输出
ofstream xij;
xij.open("result_xij.txt");
if (!xij)
{
cout << "dabuda" << endl;
}
else
{
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
c = cplex.getValue(x[i][j]);
xij << c << ",";
cout << c << ",";
}
xij << endl;
cout << endl;
}
}
xij.close();
env.out() << endl;
env.end();
system("pause");
return 0;
标签:vars,++,入门教程,Cplex,cplex,inn,env,model,安装 来源: https://blog.csdn.net/tms988/article/details/121034221