将CPLEX与C++结合使用
Using CPLEX with C++
事实上,我是c++的初学者,我试图编写一段代码来解决pédian问题。
当我使用CPLEX接口时,我得到了最佳解决方案,但当我尝试在C++中运行调用CPLEX的代码时,情况并非如此。
所以我认为问题出在我的代码的编写上。
提前谢谢你。
致问候,
注意:这是我在C++中的代码:
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
int main (void) {
IloEnv env;
IloModel model(env);
IloNumVarArray var(env);
try {
int P=2;
int Noeuds=10;
float distance[10][10]={
{0,3.605551275,7,10.77032961,10.04987562,25,27.45906044,26.92582404,31.6227766,23.43074903},
{3.605551275,0,4.472135955,7.280109889,7.071067812,24.04163056,26.2488095,25.05992817,29.54657341,20},
{7,4.472135955,0,5,3.16227766,19.84943324,21.9317122,20.59126028,25.07987241,19.6977156},
{10.77032961,7.280109889,5,0,3,21.47091055,23.02172887,20.51828453,24.41311123,14.86606875},
{10.04987562,7.071067812,3.16227766,3,0,18.86796226,20.61552813,18.60107524,22.82542442,17.72004515},
{25,24.04163056,19.84943324,21.47091055,18.86796226,0,3,7.071067812,11.18033989,33.37663854},
{27.45906044,26.2488095,21.9317122,23.02172887,20.61552813,3,0,5.385164807,8.602325267,33.95585369},
{26.92582404,25.05992817,20.59126028,20.51828453,18.60107524,7.071067812,5.385164807,0,5,29.73213749},
{31.6227766,29.54657341,25.07987241,24.41311123,22.82542442,11.18033989,8.602325267,5,0,31.76476035},
{23.43074903,20,19.6977156,14.86606875,17.72004515,33.37663854,33.95585369,29.73213749,31.76476035,0}};
//cluster = IloNumVarArray(env, P);
IloNumVarArray cluster;
//Noeudsincluster = IloNumVarArray(env, P);
typedef IloArray<IloNumVarArray> IloNumVarArray2 ;
IloNumVarArray2 Noeudsincluster(env,Noeuds);
for(IloInt i = 0; i < P; i++){
cluster[i] = IloNumVar(env, 0, 1, ILOINT);
}
for(IloInt i = 0; i < Noeuds; i++){
Noeudsincluster[i]=IloNumVarArray(env,P);
for(IloInt k = 0; k < P; k++){
Noeudsincluster[i][k] = IloNumVar(env, 0, 1, ILOINT);
}
}
//Sum (c in clusters) Noeudsincluster[n][c]==1
for(IloInt i = 0; i < Noeuds; i++){
IloExpr expr(env);
for(IloInt k = 0; k < P; k++)
{
expr += Noeudsincluster[i][k];
IloConstraint c2 = (expr == 1);
stringstream c2_name;
c2_name << "Cons(2)[" << i << "]";
c2.setName(c2_name.str().c_str());
model.add(c2);
}
}
//Sum (c in clusters) cluster[c]==P
IloExpr expr(env);
for(IloInt k = 0; k < P; k++)
{
expr += cluster[k];
}
IloConstraint c3 = (expr == P);
stringstream c3_name;
c3.setName(c3_name.str().c_str());
model.add(c3);
//Noeudsincluster[n][c] <=cluster[c]
for(IloInt k = 0; k < P; k++)
for(IloInt i = 0; i < Noeuds; i++)
{
IloConstraint c1 = (Noeudsincluster[i][k]<=cluster[k]);
stringstream c1_name;
c1_name << "Cons(1)[" << i << "]" << "[" << k << "]";
c1.setName(c1_name.str().c_str());
model.add(c1);
}
IloExpr Objective(env);
for(IloInt k = 0; k < P; k++)
for(IloInt i = 0; i < Noeuds; i++)
for(IloInt j = 0; j < Noeuds; j++)
if(i!=j)
Objective+=distance[i][j]*Noeudsincluster[i][k]*Noeudsincluster[j][k];
// Adding objective function
model.add(IloMinimize(env, Objective));
IloCplex cplex(model);
cplex.solve();
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
}
catch (IloException& e) {
cerr << "Concert exception caught: " << e << endl;
}
catch (...) {
cerr << "Unknown exception caught" << endl;
}
env.end();
return 0;
}
这是我的CPLEX.mod文件:
//Data
int P = ...;//Nombre des clusters
int n = ...;//nombre des noeuds
range Noeuds = 1..n; //Ensemble des noeuds
range Clusters=1..P; //Ensemble des clusters
tuple edge {int i; int j;}
setof(edge) Edges = {<i,j> |i,j in Noeuds};
float d[Edges] = ...;
//Variables
dvar boolean cluster[Clusters];
dvar boolean Noeudsincluster[Noeuds][Clusters];
//Objective
minimize sum(i in Noeuds ,j in Noeuds :i!=j, c in Clusters) d[<i,j>]*Noeudsincluster[i][c]*Noeudsincluster[j][c];
//Constraints
subject to {
//chaque noeud appartient à un seul cluster
forall( n in Noeuds )
sum( c in Clusters ) Noeudsincluster[n][c] == 1;
//Nombre des clusters égale P
sum( c in Clusters ) cluster[c] == P;
forall( n in Noeuds , c in Clusters )
Noeudsincluster[n][c] <= cluster[c];
}
我认为您需要先对Noeudsincluster
求和,然后添加c2
约束。
//Sum (c in clusters) Noeudsincluster[n][c]==1
for(IloInt i = 0; i < Noeuds; i++){
IloExpr expr(env);
for(IloInt k = 0; k < P; k++)
{
expr += Noeudsincluster[i][k];
}
IloConstraint c2 = (expr == 1);
stringstream c2_name;
c2_name << "Cons(2)[" << i << "]";
c2.setName(c2_name.str().c_str());
model.add(c2);
}
相关文章:
- 多态性和功能结合
- 将 Vulkan 与 SFML 结合使用?
- 将 c++ 生成器应用程序与外部 dll 结合使用
- 如何将增强MSM与增强信号结合使用?
- MS Visual Studio 解决方案结合了非托管C++项目和 C# 项目
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- 将预编译的 C 共享库与 JNI/NDK 结合使用
- C++ 将函数指针与最佳性能相结合
- 将 Lua 与 C++ DLL 结合使用
- 为什么push_back和emplace_back结合在一起时,会有不同的行为
- 将 aws-iot-device-sdk-cpp-v2 与 CMake 结合使用
- 将2个欧拉角与GLM相结合的问题
- 结合std::execution和OpenMP是否可取
- 抽象和派生与std::list相结合
- 想要将 CGAL 与四元数相结合是否合乎逻辑
- 存储/传递 v8 承诺解析器供以后使用的最佳实践?(结合C++线程)
- 如何将 IAudioClient3 (WASAPI) 与实时工作队列 API 结合使用
- 可变参数模板与默认模板参数相结合
- 函数与返回类型 node* 与 C++ 中的 OOP 结合使用
- 是否可以将C++与任何语言相结合?