将 CPLEX 与 C++ 结合使用

Using CPLEX with C++

事实上,我是c++的初学者,我尝试写一个代码来解决P-médian问题。

当我使用 CPLEX 接口时,我得到了最佳解决方案,但当我尝试 运行 我在 C++ 中调用 CPLEX 的代码时,情况并非如此。

所以我认为是我代码写的问题。

提前谢谢你。

此致,

N.B。 : 这是我的 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);
}