Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
model.add(ctaryLrefs);
model.add(ctaryFullRef);
//
// set as not in clique, all the vertices that do not belong to one of the possible clique
// std::map < Vertex*, bool> okforclique;
// for (Vertex* v : this->inst_->vertices_) okforclique[v] = false;
// for (Clique* c : this->cliques_) {
// for (Vertex* v : c->vertices_) {
// okforclique[v] = true;
// }
// }
// for (Vertex* v : this->inst_->vertices_) {
// if (!okforclique[v]) {
// for (int k = 0; k <= L; k++) {
//
// model.add(hasrank_[v][k] == 0);
// }
// }
// }
}
//
// define the max fully-referenced cplex model
void DiscretizationSolver::defineminpartialip(IloModel & model) {
IloEnv env = model.getEnv();
char name[50];
int nbvertices = this->inst_->nbvertices_;
int L = this->inst_->L();
int U = this->inst_->U();
int nbcliques = this->cliques_.size();
int nbcons = 0;
//////////////////////////////////////////////////////////////////////////////
// Declare the variables
//////////////////////////////////////////////////////////////////////////////
this->declareipvariables(model);
//////////////////////////////////////////////////////////////////////////////
// Set the cplex model that will be solved
//////////////////////////////////////////////////////////////////////////////
//
// objective function
IloExpr obj(env);
obj += 1;
for (Vertex* v : this->inst_->vertices_) {
obj += 1 - isfullref_[v];
}
model.add(IloMinimize(env, obj));
//////////////////////////////////////////////////////////////////////////////
// Constraints that set the vertices belonging to the initial clique
// Enumerate all the cliques and choose one
//////////////////////////////////////////////////////////////////////////////
//
// choose one clique among those that have been enumerated
if (this->modeltype_ != WITNESS) {
IloRange ctOneClique(env, 1, 1, "ctOneClique");
IloExpr sumcliques(env);
for (int c = 0; c < nbcliques; c++) sumcliques += isclique_[c];
ctOneClique = (sumcliques == 1);
model.add(ctOneClique);
if (this->modeltype_ == CCG) {
for (int c = 0 ; c < nbcliques ; c++) {
Clique* initialclique = this->cliques_[c];
if (initialclique->greedyobjvalue_ >= 2) {
IloExpr sumpartref(env);
for (Vertex* v: initialclique->initialpartialrefs_) sumpartref += (1-isfullref_[v]);
model.add(sumpartref >= isclique_[c]);
for (Vertex* v: initialclique->initialfullrefs_) model.add(isfullref_[v] >= isclique_[c]);
}
}
}
}
//
// clique constraints in WITNESS: potential cliques are not enumerated
if (this->modeltype_ == WITNESS) {
// vertices in the clique are not counted as part. ref
IloRangeArray ctaryCliquesAreFull(env);
nbcons = 0;
for (Vertex* v : this->inst_->vertices_) {
ctaryCliquesAreFull.add(isfullref_[v] - isvertexinclique_[v] >= 0);
sprintf(name, "ctaryCliquesAreFull%i", v->id_);
ctaryCliquesAreFull[nbcons++].setName(name);
// there must exactly L+1 vertices in the initial clique
IloRange ctLPlusOneInClique(env, 1, 1, "ctLPlusOneInClique");
IloExpr sumvertex(env);
for (Vertex* v : this->inst_->vertices_) sumvertex += isvertexinclique_[v];
ctLPlusOneInClique = (sumvertex == this->inst_->L() + 1);
model.add(ctLPlusOneInClique);
sumvertex.end();
//
// two non-adjacent vertices are not in the initial clique
IloRangeArray ctaryCliquesAreAdjacent(env);
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : this->inst_->vertices_) {
if ((u == v) || (u->isneighbor(v))) continue;
else {
ctaryCliquesAreAdjacent.add(isvertexinclique_[u] + isvertexinclique_[v] <= 1);
sprintf(name, "ctaryCliquesAreAdjacent%i_%i", u->id_, v->id_);
ctaryCliquesAreAdjacent[nbcons++].setName(name);
}
}
// vertices in the clique are witness to all their neighbors
IloRangeArray ctaryCliqueIsWitness(env);
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
ctaryCliqueIsWitness.add(isbefore_[u][v] - isvertexinclique_[u] >= 0);
sprintf(name, "ctaryCliqueIsWitness_%i_%i", u->id_, v->id_);
ctaryCliqueIsWitness[nbcons++].setName(name);
}
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
model.add(ctaryCliqueIsWitness);
//
// set as not in clique, all the vertices that do not belong to one of the possible clique
// std::map < Vertex*, bool> okforclique;
// for (Vertex* v : this->inst_->vertices_) okforclique[v] = false;
// for (Clique* c : this->cliques_) {
// for (Vertex* v : c->vertices_) {
// okforclique[v] = true;
// }
// }
// for (Vertex* v : this->inst_->vertices_) {
// if (!okforclique[v]) {
// model.add(isvertexinclique_[v] == 0);
// }
// }
}
//////////////////////////////////////////////////////////////////////////////
// Constraints that guarantee that the solution digraph is acyclic
// this is where the different models mostly differ:
// - CYCLES forbid all the two-cycles and most three-cycles
// - CCG, WITNESS: forbid only some cycles of the distance graph, and
// generate the other cycles through a callback
// - RANKS: includes MTZ kinds of constraints instead of cycle
// constraints, but add cycle cuts as valid inequalities anyways
//////////////////////////////////////////////////////////////////////////////
IloRangeArray ctaryNoTwoCycle(env);
IloRangeArray ctaryNoThreeCycle(env);
IloRangeArray ctaryNoFourCycle(env);
//
// in model CYCLES, we need to make sure that the solution is acyclic by
// guaranteeing total order and transitivity
if (this->modeltype_ == CYCLES) {
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : this->inst_->vertices_) {
if (v->id_ <= u->id_) continue;
ctaryNoTwoCycle.add(isbefore_[u][v] + isbefore_[v][u] == 1);
sprintf(name, "ctaryNoTwoCycle_%i_%i", u->id_, v->id_);
ctaryNoTwoCycle[nbcons++].setName(name);
}
}
model.add(ctaryNoTwoCycle);
//
// forbid every three-cycle but those that have no common edge with
// the distance graph (we proved that this is sufficient)
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
if (v->id_ <= u->id_) continue;
for (Vertex* w : this->inst_->vertices_) {
if (w->id_ <= v->id_) continue;
ctaryNoThreeCycle.add(
isbefore_[u][v] + isbefore_[v][w] + isbefore_[w][u]
<= 2);
sprintf(name, "ctaryNoThreeCycle_%i_%i_%i", u->id_, v->id_, w->id_);
ctaryNoThreeCycle[nbcons++].setName(name);
ctaryNoThreeCycle.add(
isbefore_[w][v] + isbefore_[v][u] + isbefore_[u][w]
<= 2);
sprintf(name, "ctaryNoThreeCycle_%i_%i_%i",
w->id_, v->id_, u->id_);
ctaryNoThreeCycle[nbcons++].setName(name);
}
}
}
model.add(ctaryNoThreeCycle);
}
//
// initially forbid cycles of the distance graphs with size <=
// initialcyclesize_ in models CCG, WITNESS and RANKS;
// in CCG and WITNESS; the remaining cycles cuts are added as lazy
// constraints
if ((this->modeltype_ == CCG || this->modeltype_ == WITNESS || this->modeltype_ == RANKS) && (this->initialcyclesize_ >= 2)) {
// - forbid every two-cycle
// - only the edges of the distance graph need to be considered in
// the two models CCG and RANKS
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
if (v->id_ <= u->id_) continue;
if (this->modeltype_ == WITNESS) {
ctaryNoTwoCycle.add(
isbefore_[u][v] + isbefore_[v][u] - isvertexinclique_[u]
<= 1);
} else {
ctaryNoTwoCycle.add(isbefore_[u][v] + isbefore_[v][u] == 1);
sprintf(name, "ctaryNoTwoCycle_%i_%i", u->id_, v->id_);
ctaryNoTwoCycle[nbcons++].setName(name);
}
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
// forbid the three-cycles of the distance graph
if (this->initialcyclesize_ >= 3) {
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
if (v->id_ <= u->id_) continue;
for (Vertex* w : v->neighbors_) {
if (!w->isneighbor(u)) continue;
if (w->id_ <= v->id_) continue;
if (this->modeltype_ == WITNESS) {
ctaryNoThreeCycle.add(
isbefore_[u][v] + isbefore_[v][w]
+ isbefore_[w][u] - isvertexinclique_[u] <= 2);
ctaryNoThreeCycle.add(
isbefore_[w][v] + isbefore_[v][u]
+ isbefore_[u][w] - isvertexinclique_[u] <= 2);
} else {
ctaryNoThreeCycle.add(
isbefore_[u][v] + isbefore_[v][w] + isbefore_[w][u] <= 2);
ctaryNoThreeCycle.add(
isbefore_[w][v] + isbefore_[v][u] + isbefore_[u][w] <= 2);
}
sprintf(name, "ctaryNoThreeCycle_%i_%i_%i",
u->id_, v->id_, w->id_);
ctaryNoThreeCycle[nbcons++].setName(name);
sprintf(name, "ctaryNoThreeCycle_%i_%i_%i",
w->id_, v->id_, u->id_);
ctaryNoThreeCycle[nbcons++].setName(name);
}
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
//
// forbid every four-cycle: the complex verifications aim at adding only
// one constraint per four-cycle that is not implied by two three-cycles
if (this->initialcyclesize_ >= 4) {
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
if (v->id_ <= u->id_) continue;
for (Vertex* w : v->neighbors_) {
for (Vertex* x : w->neighbors_) {
if (!x->isneighbor(u)) continue;
if (w->id_ <= x->id_ && w->id_ <= v->id_) continue;
if (x->isneighbor(v) && w->isneighbor(u)) continue;
if (this->modeltype_ == WITNESS) {
ctaryNoFourCycle.add(
isbefore_[u][v] + isbefore_[v][w] + isbefore_[w][x]
+ isbefore_[x][u] - isvertexinclique_[u] <= 3);
ctaryNoFourCycle.add(
isbefore_[x][w] + isbefore_[w][v] + isbefore_[v][u]
+ isbefore_[u][x] - isvertexinclique_[u] <= 3);
} else {
ctaryNoFourCycle.add(
isbefore_[u][v] + isbefore_[v][w] + isbefore_[w][x]
+ isbefore_[x][u] <= 3);
ctaryNoFourCycle.add(
isbefore_[x][w] + isbefore_[w][v] + isbefore_[v][u]
+ isbefore_[u][x] <= 3);
}
sprintf(name, "ctaryNoFourCycle_%i_%i_%i_%i",
u->id_, v->id_, w->id_, x->id_);
ctaryNoFourCycle[nbcons++].setName(name);
sprintf(name, "ctaryNoFourCycle_%i_%i_%i_%i",
x->id_, w->id_, v->id_, u->id_);
ctaryNoFourCycle[nbcons++].setName(name);
}
// - RANKS: first, make sure that each rank is taken by exaclty one vertex
// then, use MTZ constraints instead of cycle constraints
if (this->modeltype_ == RANKS) {
//
// each rank of the order is taken by exactly one vertex
IloRangeArray ctaryOneVertexPerRank(env);
for (int k = 0; k < nbvertices; k++) {
IloExpr sumvertices(env);
for (Vertex* v : this->inst_->vertices_) sumvertices += hasrank_[v][k];
ctaryOneVertexPerRank.add(sumvertices == 1);
sprintf(name, "ctaryOneVertexPerRank_%i", k);
ctaryOneVertexPerRank[k].setName(name);
}
model.add(ctaryOneVertexPerRank);
//
// each vertex has exactly one rank and his rank is computed
IloRangeArray ctaryOneRankPerVertex(env);
IloRangeArray ctaryComputeRank(env);
for (Vertex* u : this->inst_->vertices_) {
IloExpr sumhasrank(env);
IloExpr sumranks(env);
for (int k = 0; k < nbvertices; k++) sumhasrank += hasrank_[u][k];
for (int k = 0; k < nbvertices; k++) sumranks += k * hasrank_[u][k];
ctaryOneRankPerVertex.add(sumhasrank == 1);
ctaryComputeRank.add(sumranks - rank_[u] == 0);
sprintf(name, "ctaryOneRankPerVertex_%i", u->id_);
ctaryOneRankPerVertex[nbcons].setName(name);
sprintf(name, "ctaryComputeRank_%i", u->id_);
ctaryComputeRank[nbcons++].setName(name);
}
model.add(ctaryOneRankPerVertex);
// Miller-Tucker-Zemlin constraints to ensure that the direction of the
// distance graph edges respect the order of the ranks
IloRangeArray ctaryRankTransitivity(env);
nbcons = 0;
for (Vertex* u : this->inst_->vertices_) {
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
for (Vertex* v : u->neighbors_) {
if (v->id_ <= u->id_) continue;
ctaryRankTransitivity.add(
1 <= nbvertices * isbefore_[u][v] - rank_[v] + rank_[u]
<= nbvertices - 1);
sprintf(name, "ctaryRankTransitivity_%i_%i", u->id_, v->id_);
ctaryRankTransitivity[nbcons++].setName(name);
}
}
model.add(ctaryRankTransitivity);
}
//////////////////////////////////////////////////////////////////////////////
// Constraints that ensure that the ordering is a discretization ordering
// every vertex has at least L references except if it is a member of the initial clique
//////////////////////////////////////////////////////////////////////////////
nbcons = 0;
int nbrefcons = 0;
IloRangeArray ctaryLrefs(env);
//
// in WITNESS, the constraint reflects the difference between witnesses and references
if (this->modeltype_ == WITNESS) {
for (Vertex* u : this->inst_->vertices_) {
IloExpr sumwitness(env);
for (Vertex* v : u->neighbors_) sumwitness += isbefore_[v][u];
ctaryLrefs.add(
sumwitness + (U - L) * (isvertexinclique_[u] - isfullref_[u]) == L);
sprintf(name, "ctaryLRefs_%i", u->id_);
ctaryLrefs[nbrefcons++].setName(name);
sumwitness.end();
}
} else {
for (Vertex* u : this->inst_->vertices_) {
IloExpr sumrefs(env);
IloExpr sumcliques(env);
for (int c : this->cliqueidslist_[u]) {
sumcliques += isclique_[c];
}
for (Vertex* v : u->neighbors_) sumrefs += isbefore_[v][u];
ctaryLrefs.add(sumrefs + U * sumcliques - (U - L) * isfullref_[u] >= L);
sprintf(name, "ctaryLRefs_%i", u->id_);
ctaryLrefs[nbrefcons++].setName(name);
sumrefs.end();
sumcliques.end();
}
}
model.add(ctaryLrefs);
if (this->modeltype_ == RANKS) {
IloRangeArray ctaryRankOfClique(env);
IloExprArray sumrank(env, L + 1);
for (int k = 0; k < L + 1; k++) {
sumrank[k] = IloExpr(env);
for (Vertex* u : this->inst_->vertices_) {
for (int c : this->cliqueidslist_[u]) {
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
if (this->cliques_[c]->vertices_[k] == u) sumrank[k] += isclique_[c];
}
}
for (int k = 0; k < L + 1; k++) {
ctaryRankOfClique.add(hasrank_[u][k] - sumrank[k] == 0);
sprintf(name, "ctaryRankOfClique_%i_%i", u->id_, k);
ctaryRankOfClique[nbcons++].setName(name);
}
}
}
//////////////////////////////////////////////////////////////////////////////
// Constraints that strengthen the formulation
//////////////////////////////////////////////////////////////////////////////
//
// if we could identify cycles composed of low degree vertices add a cut
// specifying that the vertices included in such cycles must include at least
// as many partially referenced vertices as there are cycles
if (this->modeltype_ == CCG) {
IloRangeArray ctaryLowDegreeCycles(env);
for (int i = 0; i < this->lowdegreecycles_.size(); i++) {
std::vector<Vertex*> cycle = this->lowdegreecycles_[i];
int cyclesize = cycle.size();
IloExpr sumfullrefincycle(env);
IloExpr sumcliques(env);
for (Vertex* v : cycle) {
sumfullrefincycle += isfullref_[v];
for (int c : this->cliqueidslist_[v]) {
sumcliques += isclique_[c];
}
}
ctaryLowDegreeCycles.add(
sumfullrefincycle - sumcliques
<= cyclesize - this->nbpartialsincycle_[i]);
sprintf(name, "ctaryLowDegreeCycles_%i", i);
ctaryLowDegreeCycles[i].setName(name);
}
model.add(ctaryLowDegreeCycles);
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
// if we could identify cliques that contain at least one
// partially-referenced vertex, add a constraint to enforce this; the
// two-cliques need not be checked
IloRangeArray ctaryCliquesWithPartialRef(env);
nbcons = 0;
for (Clique* clique : this->cliqueswithpartialref_) {
IloExpr sumfullref(env);
IloExpr sumcliques(env);
for (Vertex* v : clique->vertices_) {
sumfullref += isfullref_[v];
for (int c : this->cliqueidslist_[v]) {
sumcliques += isclique_[c];
}
}
ctaryCliquesWithPartialRef.add(
sumfullref - sumcliques <=
clique->nbvertices() - this->nbpartialinclique_[clique]);
sprintf(name, "ctaryCliquesWithPartialRef_%i", nbcons);
ctaryCliquesWithPartialRef[nbcons++].setName(name);
}
model.add(ctaryCliquesWithPartialRef);
}
}
//
// Integer programming model for the search of a discretization order that
// maximizes the number of vertices with more than free references
// modeltype:
// = 0 : variables that indicate whether a vertex is before another one for
// every pair of vertices
// = 1 : variables only for each edge, but no enumeration of the cliques
// = 2 : variables only for each edge, with enumeration of the cliques
bool DiscretizationSolver::minpartialip(float timelimit) {
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
IloEnv env;
IloTimer cpuClockTotal(env);
IloTimer cpuClockInit(env);
cpuClockTotal.start();
cpuClockInit.start();
this->inst_->computeneighbors();
// this->preallocatelowdegreevertices();
//
// enumerate the potential initial cliques
if (!this->enumeratecliques(this->inst_->L() + 1)) {
this->isfeasible_ = false;
return false;
}
this->eliminateredundantcliques();
//
// solve with greedy and remove every clique that cannot be completed
if (!this->greedysolve()) {
std::cout << "Greedy solve found the problem to be infeasible at the"
" time of mip warm start." << std::endl;
return false;
}
if (this->modeltype_ == CCG) {
// identify clique of vertices that will contain at least one
// partially-referenced vertex
this->computecliquecuts(this->cliquecutsmaxsize_);
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
// identify cycle of vertices with degree smaller than U+1: in each
// of these cycles, at least one vertex is partially referenced
this->enumeratelowdegreecycles();
}
IloModel model(env);
if (this->modeltype_ == VERTEXRANK) {
this->defineminpartial_vertexrank(model);
} else {
this->defineminpartialip(model);
}
//
// create CPLEX object, load the model and provide initial solution
IloCplex cplex(env);
cplex.extract(model);
//
if (!this->greedymipstart(cplex)) {
this->isfeasible_ = false;
return false;
}
if ((this->modeltype_ == CCG) || (this->modeltype_ == WITNESS)) {
cplex.use(CyclesLazyConstraints(env, *(this->inst_), *this));
}
if (this->modeltype_ == CCG) {
IloCplex::MIPCallbackI::NodeId nodeid;
cplex.use(BranchOnCliqueVariables(env, *this, nodeid));
}
cpuClockInit.stop();
//////////////////////////////////////////////////////////////////////////////
// Solve the instance
//////////////////////////////////////////////////////////////////////////////
//
// set CPLEX parameters
cplex.setParam(IloCplex::MIPDisplay, 4);
cplex.setParam(IloCplex::TiLim, timelimit - cpuClockInit.getTime());
cplex.setParam(IloCplex::Threads, 1);
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
//
// solve the integer program
std::cout << "\nrevorder: ----------------------------------------------------------" << std::endl;
std::cout << "revorder: solve an integer program to find an optimal discretization order" << std::endl;
std::cout << "revorder: time limit has been set to " << timelimit << " seconds" << std::endl;
std::cout << "revorder: cplex log coming below\n" << std::endl;
try {
cplex.solve();
} catch (IloException& e) {
std::cerr << e << std::endl;
}
cpuClockTotal.stop();
IloAlgorithm::Status status = cplex.getStatus();
std::cout << "revorder: ----------------------------------------------------------\n" << std::endl;
std::cout << "revorder: CPLEX solution status = " << status << std::endl;
isfeasible_ = (status == IloAlgorithm::Feasible) || (status == IloAlgorithm::Optimal);
isoptimal_ = (status == IloAlgorithm::Optimal);
if (isfeasible_) {
// retrieve the information on the solution process
objvalue_ = std::min((int) cplex.getObjValue(), objvalue_);
relativegap_ = cplex.getMIPRelativeGap();
totaltime_ = cpuClockTotal.getTime();
treatednodes_ = cplex.getNnodes();
std::cout << "revorder: total cpu time = " << cpuClockTotal.getTime() << " s" << std::endl;
std::cout << "revorder: initialization cpu time = " << cpuClockInit.getTime() << " s" << std::endl;
std::cout << "revorder: number of branching nodes = " << treatednodes_ << std::endl;
std::cout << "revorder: value of the objective function = " << objvalue_ << std::endl;
std::cout << std::setprecision(1) << "revorder: value of the relative duality gap = " << 100.0 * relativegap_ << "%" << std::endl;
} else if (!isfeasible_) {
std::string fileInfeasible("InfeasibleModel.lp");
cplex.exportModel(fileInfeasible.c_str());
std::cout << "revorder: no feasible solution was found during the optimization!" << std::endl;
std::cout << "revorder: check the file " << fileInfeasible << " to see the corresponding model" << std::endl;
return false;
}
//
// build a discretization order that has the same value as the solution of
// the integer program
objvalue_ += this->inst_->preallocatedvertices_.size();
this->buildoptimalorder(cplex);
std::cout << "revorder: verification: number of part. ref. vertices = " << this->inst_->nbvertices_ - this->inst_->L() - this->bestnbfullref_ << std::endl;
std::cout << "revorder: with preallocated vertices, we get " << objvalue_ << " part. ref. vertices" << std::endl;
// terminate the cplex objects
cplex.end();
env.end();
return isfeasible_;
}
//
// Declare the variables used in the ip models
void DiscretizationSolver::declareipvariables(IloModel & model) {
IloEnv env = model.getEnv();
char name[50];
int nbvertices = this->inst_->nbvertices_;
int L = this->inst_->L();
int nbcliques = this->cliques_.size();
//
// variables that represent the order relations between the vertices
// - isbefore_[i,j] = 1 if vertex i is before j in the order
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : this->inst_->vertices_) {
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
if (u == v) continue;
isbefore_[u][v] = IloBoolVar(env);
sprintf(name, "isbefore_%i_%i", u->id_, v->id_);
isbefore_[u][v].setName(name);
}
}
//
// isfullref_[v] =1 if vertex v is fully-referenced and 0 otherwise
for (Vertex* v : this->inst_->vertices_) {
isfullref_[v] = IloBoolVar(env);
sprintf(name, "isfullref_%i", v->id_);
isfullref_[v].setName(name);
}
//
// variables that set the initial clique
// - variables that set the initial clique from the enumeration of the cliques
isclique_ = IloBoolVarArray(env, nbcliques);
for (int c = 0; c < nbcliques; c++) {
sprintf(name, "isclique_%i", c);
isclique_[c].setName(name);
}
// - when no enumeration, isvertexinclique_[v]=1 if v is in the initial clique, 0 otherwise
for (Vertex* v : this->inst_->vertices_) {
isvertexinclique_[v] = IloBoolVar(env);
sprintf(name, "isvertexinclique_%i", v->id_);
isvertexinclique_[v].setName(name);
}
//
// variables that set the rank of each vertex in the order
// - hasrank_[v,k] = 1 if vertex i has rank k in the discretization order
// in this model, we use these variables only for the first L vertices
// of the cliques (if modeltype <= 1)
int nbranks = (this->modeltype_ == RANKS) ? nbvertices : L + 1;
for (Vertex* v : this->inst_->vertices_) {
hasrank_[v] = IloBoolVarArray(env, nbranks);
for (int k = 0; k < nbranks; k++) {
sprintf(name, "hasrank_%i_%i", v->id_, k);
hasrank_[v][k].setName(name);
}
}
//
// - rank[v] = integer variable representing the rank of v in the order
if (this->modeltype_ == RANKS) {
for (Vertex* v : this->inst_->vertices_) {
rank_[v] = IloIntVar(env, 0, nbvertices - 1);
sprintf(name, "rank_%i", v->id_);
rank_[v].setName(name);
}
//
// create the cplex model for the relaxation of the IP
void DiscretizationSolver::createrelaxationmodel(IloModel& model, IloModel & relax) {
IloEnv env = model.getEnv();
relax.add(model);
for (Vertex* u : this->inst_->vertices_) {
relax.add(IloConversion(env, isfullref_[u], ILOFLOAT));
if (this->modeltype_ == CCG || this->modeltype_ == WITNESS || this->modeltype_ == RANKS) {
for (Vertex* v : u->neighbors_) {
if (v == u) continue;
relax.add(IloConversion(env, isbefore_[u][v], ILOFLOAT));
}
} else {
for (Vertex* v : this->inst_->vertices_) {
if (v == u) continue;
relax.add(IloConversion(env, isbefore_[u][v], ILOFLOAT));
}
for (int k = 0; k < this->inst_->L()+1; k++) {
relax.add(IloConversion(env, hasrank_[u][k], ILOFLOAT));
}
if (this->modeltype_ == RANKS) {
for (int k = this->inst_->L()+1; k < this->inst_->nbvertices_; k++) {
relax.add(IloConversion(env, hasrank_[u][k], ILOFLOAT));
}
}
for (int n = 0; n < this->cliques_.size(); n++) {
relax.add(IloConversion(env, isclique_[n], ILOFLOAT));
}
}
//
// Solve the linear relaxation of the input model
void DiscretizationSolver::solverelaxation(IloModel & model) {
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
//
// solve the relaxation alone first and disable every generation of cuts and
// presolve of the integer programming problem
IloEnv env = model.getEnv();
IloModel relax(env);
createrelaxationmodel(model, relax);
IloCplex cplexrelax(env);
cplexrelax.extract(relax);
//
// run cplex and retrieve the objective value
std::cout << "\nrevorder: ----------------------------------------------------------" << std::endl;
std::cout << "revorder: solve the relaxation of the discretization order problem" << std::endl;
std::cout << "revorder: cplex log coming below\n" << std::endl;
IloTimer cpuClock(env);
cpuClock.start();
cplexrelax.solve();
cpuClock.stop();
objvaluerelax_ = cplexrelax.getObjValue();
std::cout << "revorder: value of the linear relaxation = " << objvaluerelax_ << std::endl;
std::cout << "revorder: total cplex cpu (relaxation) = " << cpuClock.getTime() << " s" << std::endl;
}
//
// After solving the problem, build an order that produces the same objective
// value as the optimal solution
void DiscretizationSolver::buildoptimalorder(const IloCplex & cplex) {
int nbvertices = this->inst_->nbvertices_;
//
// initialize the data relative to best solution
this->bestfullref_.clear();
for (Vertex* v : this->inst_->vertices_) {
this->bestnbfullref_ = 0;
this->bestnbrefs_[v] = 0;
this->bestisfullref_[v] = false;
this->bestrank_[v] = -1;
}
//
// Compute a topological ordering of the graph
//
// generate the adjacency lists of the vertices corresponding to the current
// cplex solution
if (this->modeltype_ != VERTEXRANK) {
float tolerance = 1e-06;
std::map<Vertex*, std::vector<Vertex*> > adjlist;
for (Vertex* v : this->inst_->vertices_) {
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
if (cplex.getValue(this->isbefore_[u][v]) >= 1 - tolerance) {
if ((this->modeltype_ == WITNESS) && (cplex.getValue(this->isvertexinclique(v)) >= 1 - tolerance)) {
if (cplex.getValue(this->isvertexinclique(u)) >= 1 - tolerance) {
if (u->id_ < v->id_) {
adjlist[u].push_back(v);
this->bestnbrefs_[v]++;
}
} else {
adjlist[u].push_back(v);
this->bestnbrefs_[v]++;
}
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
}
}
//
// search for the root of the digraph
Vertex* root = nullptr;
int minnbrefs = this->inst_->nbvertices_;
for (Vertex* v : this->inst_->vertices_) {
if (this->bestnbrefs_[v] < minnbrefs) {
minnbrefs = this->bestnbrefs_[v];
root = v;
if (minnbrefs == 0) break;
}
}
//
// call the enumeration of cycles, returns false if there are none
std::vector<std::vector<Vertex*> > cycles;
// Search for a topological order that will be valid only if the
// digraph is acyclic
std::vector<Vertex*> reverseorder;
std::map<Vertex*, int> rank;
this->topologicalorder(root, adjlist, reverseorder, rank);
// determine whether the digraph is cyclic or not
//
std::vector<std::pair<Vertex*, Vertex*> > reverseedges;
bool iscyclic = this->getreverseedges(adjlist, reverseorder, rank, reverseedges);
if (iscyclic) {
std::cout << "revorder: error: the final integer solution is cyclic" << std::endl;
throw;
}
// modify the rank attributes of the vertices to represent their rank in the order
for (Vertex* v : this->inst_->vertices_) {
this->bestrank_[v] = rank[v];
if (this->bestnbrefs_[v] >= this->inst_->U()) {
this->bestisfullref_[v] = true;
this->bestfullref_.push_back(v);
this->bestnbfullref_++;
}
}
} else {
// get the rank of each vertex in the order
for (Vertex* u : this->inst_->vertices_) {
for (int k = 0; k < nbvertices; k++) {
if (cplex.getValue(this->hasrank_[u][k]) >= 1 - 1.0e-6) {
this->bestrank_[u] = k;
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
// get the number of references of each vertex
for (Vertex* u : this->inst_->vertices_) {
for (Vertex* v : u->neighbors_) {
if (v->id_ <= u->id_) continue;
if (this->bestrank_[v] < this->bestrank_[u]) this->bestnbrefs_[u]++;
else this->bestnbrefs_[v]++;
}
}
// get fully referenced vertices
for (Vertex* u : this->inst_->vertices_) {
if (this->bestnbrefs_[u] >= this->inst_->U()) {
this->bestisfullref_[u] = true;
this->bestfullref_.push_back(u);
this->bestnbfullref_++;
}
}
}
//
// verify that the order is indeed a referenced order
this->reconstructsolution();
// this->verifyorder(this->bestrank_);
//
// sort the input ranks map by ascening order of values
std::set<std::pair<Vertex*, int>, Comparator> orderedranks(
this->bestrank_.begin(), this->bestrank_.end(), valuecompare);