事实上,我是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];
}
答案 0 :(得分:0)
我认为您需要首先总结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);
}