Skip to content

Commit 1a8eee7

Browse files
committed
Finally working! Solved many bugs
1 parent b59ea07 commit 1a8eee7

File tree

1 file changed

+86
-45
lines changed

1 file changed

+86
-45
lines changed

TIPEcppTest1/TIPEcpp_PDSTSP.cpp

Lines changed: 86 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using namespace std;
1717
//IloNumVarArray is an 1-dimentionnal decision variable
1818
typedef IloArray<IloNumVarArray> NumVar2D;
1919

20-
typedef tuple<int, int> Arc;
20+
typedef tuple<size_t, size_t> Arc;
2121
typedef vector<Arc> TupleList;
2222

2323
bool ContainsValue(map<int, bool> dict, bool Value)
@@ -260,7 +260,7 @@ int main(int argc, char** argv)
260260
auto func_out = FileManager::read_file(truck_filename);
261261
auto func_out_2 = FileManager::read_file(drone_filename);
262262
//Stops
263-
const int n = sqrt(func_out.size() - 1);
263+
const int n = sqrt(func_out.size() - 1) - 1;
264264
cout << "n: " << n << endl;
265265
//Distances matrix, from 1..n
266266
float** Distance = new float* [n];
@@ -270,23 +270,21 @@ int main(int argc, char** argv)
270270
static const size_t Nd_size = 4;
271271
std::set<float> Nd = { 1, 3, 7, 8 };
272272
// M = number of drones
273-
static const size_t M = 1;
274-
Distance = FileManager::read_standardized_csv_trucks(func_out, useTime);
273+
static const size_t M = 2;
274+
Distance = FileManager::read_standardized_csv_trucks(func_out, useTime, true);
275275
Drone_dist = FileManager::read_standardized_csv_drones(func_out_2, useTime, true);
276276

277+
// Subtours generation
278+
vector<int> arg;
277279
std::cout << "[";
278-
for (int i = 0; i < n; i++)
279-
{
280-
std::cout << ",i: " << i << "|" << Drone_dist[i];
281-
}
282-
std::cout << "]" << std::endl;
283-
/*vector<int> arg;
284-
for (int i = 0; i < n; i++)
280+
for (int i = 1; i <= n; i++)
285281
{
286282
arg.push_back(i);
283+
std::cout << "| i: " << i;
287284
}
285+
std::cout << "]" << endl;
288286
vector<vector<int>> sub = utilities::subset(arg);
289-
utilities::print_subsets(sub);*/
287+
utilities::print_subsets(sub);
290288

291289
auto start_1 = chrono::high_resolution_clock::now();
292290

@@ -327,11 +325,12 @@ int main(int argc, char** argv)
327325
//Our decision variable Y[][] -> A Matrix
328326
// env, numberOfRows
329327
// y[i][m] client i livré par le drone m
330-
NumVar2D Y(env, M);
328+
NumVar2D Y(env, Nd.size());
331329

332-
for (int i = 1; i <= M; i++)
330+
// WARNING, Y should be of size Nd_size
331+
for (int i = 0; i < Nd.size(); i++)
333332
{
334-
Y[i] = IloNumVarArray(env, Nd_size, 0, IloInfinity, ILOBOOL);
333+
Y[i] = IloNumVarArray(env, M, 0, IloInfinity, ILOBOOL);
335334
}
336335

337336
// T: temps total
@@ -363,31 +362,36 @@ int main(int argc, char** argv)
363362

364363
// ct 3.8
365364
// For each drone m
366-
for (size_t m = 1; m <= M; m++)
365+
for (size_t m = 0; m < M; m++)
367366
{
368367
IloExpr expr3_8(env);
369-
// Sum on each i in Nd of t~[i] * y[i][m]
368+
// Sum on each i in Nd of t^[i] * y[i][m]
370369
for (size_t i : Nd)
371370
{
372-
expr3_8 += Drone_dist[i] * Y[i][m];
371+
int index = std::distance(Nd.begin(), Nd.find(i));
372+
expr3_8 += Drone_dist[i] * Y[index][m];
373373
}
374374
Model.add(T >= expr3_8);
375375
}
376376

377377
// ct 3.9
378378
// For each i in N
379+
// EDITED: i in 0..n
380+
// BIG BUG HERE
379381
for (size_t i = 1; i <= n; i++)
380382
{
381383
// If i not in Nd
382-
if (std::find(Nd.begin(), Nd.end(), i) == Nd.end())
384+
if (Nd.find(i) == Nd.end())
383385
{
386+
std::cout << "ct3.9 -> Z[" << i - 1 << "] == 1" << endl;
384387
// z[i] == 1
385-
Model.add(Z[i] == 1);
388+
Model.add(Z[i - 1] == 1);
386389
}
387390
}
388391

389392
// ct 3.10
390393
// For each i in N
394+
// EDITED: i in 0..n
391395
for (size_t i = 1; i <= n; i++)
392396
{
393397
IloExpr expr3_10(env);
@@ -403,7 +407,7 @@ int main(int argc, char** argv)
403407
}
404408
}
405409
// Strange stuff with the same variable named i
406-
Model.add(expr3_10 == Z[i]);
410+
Model.add(expr3_10 == Z[i - 1]);
407411
}
408412

409413
// ct 3.11
@@ -412,11 +416,12 @@ int main(int argc, char** argv)
412416
{
413417
IloExpr expr3_11(env);
414418
// Sum on each 1 <= m <= M
415-
for (int m = 1; m <= M; m++)
419+
for (int m = 0; m < M; m++)
416420
{
417-
expr3_11 += Y[i][m];
421+
int index = std::distance(Nd.begin(), Nd.find(i));
422+
expr3_11 += Y[index][m];
418423
}
419-
Model.add(expr3_11 == 1 - Z[i]);
424+
Model.add(expr3_11 == 1 - Z[i - 1]);
420425
}
421426

422427
// ct 3.12
@@ -456,39 +461,57 @@ int main(int argc, char** argv)
456461
std::tie(k, temp) = a;
457462
if (i == temp)
458463
{
459-
expr3_13_a += X[k][i];
464+
expr3_13_b += X[k][i];
460465
}
461466
}
462467
Model.add(expr3_13_a == expr3_13_b);
463468
}
464469

465470
//SECs
466-
/*for (int s = 0; s < sub.size(); s++)
471+
// ct 3.14
472+
// For each subset S of N
473+
for (size_t s = 0; s < sub.size(); s++)
467474
{
468-
if (2 <= sub[s].size() && sub[s].size() <= n - 1)
475+
vector<int> S = sub[s];
476+
if (S.size() > 0 && S.size() != n)
469477
{
470-
//ct3_4
471-
IloExpr expr3_4(env);
472-
for (int i : sub[s])
478+
// For each i in a subtour S
479+
for (int i : S)
473480
{
474-
for (int j : sub[s])
481+
IloExpr expr3_14(env);
482+
// std::cout << "ct3.14 ->";
483+
// For each j in S
484+
for (int j : S)
475485
{
476-
expr3_4 += X[i][j];
486+
// For each k not in S
487+
// == For each k in N not in S
488+
// EDITED: k in 0..n
489+
for (int k = 1; k <= n; k++)
490+
{
491+
// If k not in S
492+
if (std::find(S.begin(), S.end(), k) == S.end())
493+
{
494+
expr3_14 += X[j][k];
495+
// std::cout << "+ X[" << j << "][" << k << "]";
496+
}
497+
}
477498
}
499+
// std::cout << ">= Z[" << i - 1 << "]" << std::endl;
500+
Model.add(expr3_14 >= Z[i - 1]);
478501
}
479-
Model.add(expr3_4 <= int(sub[s].size() - 1));
480502
}
481-
}*/
503+
}
482504

483505
/* =========
484506
INTEGRALITY CONSTRAINTS
485507
======== */
486508

487509
// ct 3.15
488510
// For each i in N
511+
// EDITED: i in 0..n
489512
for (size_t i = 1; i <= n; i++)
490513
{
491-
Model.add(Z[i] == 0 || Z[i] == 1);
514+
Model.add(Z[i - 1] == 0 || Z[i - 1] == 1);
492515
}
493516

494517
// ct 3.16
@@ -505,9 +528,10 @@ int main(int argc, char** argv)
505528
for (int i : Nd)
506529
{
507530
// For each 1 <= m <= M
508-
for (int m = 1; m <= M; m++)
531+
for (int m = 0; m < M; m++)
509532
{
510-
Model.add(Y[i][m] == 0 || Y[i][m] == 1);
533+
int index = std::distance(Nd.begin(), Nd.find(i));
534+
Model.add(Y[index][m] == 0 || Y[index][m] == 1);
511535
}
512536
}
513537

@@ -519,6 +543,7 @@ int main(int argc, char** argv)
519543

520544
#pragma region Solving
521545

546+
std::cout << "Welcome to c++" << std::endl;
522547
// Solving
523548
IloCplex cplex(Model);
524549
// Export the model, useful for debugging
@@ -542,7 +567,7 @@ int main(int argc, char** argv)
542567
cplex.setParam(IloCplex::Param::MIP::Strategy::Search, IloCplex::Traditional);
543568

544569
//Registering callbacks
545-
cplex.use(LazyCallback(env, Y, n));
570+
//cplex.use(LazyCallback(env, Y, n));
546571
bool solved = false;
547572

548573
try
@@ -581,18 +606,34 @@ int main(int argc, char** argv)
581606
//Solving output
582607
map<int, int> G;
583608
cout << "Solution (" << cplex.getStatus() << ") with objective " << objective << endl;
584-
for (int i = 0; i < n; i++)
609+
cout << "Clients delivered using a truck Z[i] == 1 || drone Z[i] == 0:" << endl;
610+
for (int i = 1; i <= n; i++)
585611
{
586-
for (int j = 0; j < n; j++)
612+
float value = cplex.getValue(Z[i - 1]);
613+
std::cout << "Z[" << i << "]=" << value << std::endl;
614+
}
615+
for (int i = 0; i <= n; i++)
616+
{
617+
for (int j = 0; j <= n; j++)
587618
{
588-
float value = cplex.getValue(Y[i][j]);
589-
if (value == 1)
619+
if (j == i) continue;
620+
try
590621
{
591-
cout << i << "->" << j << endl;
592-
G[i] = j;
622+
float value = cplex.getValue(X[i][j]);
623+
if (value == 1)
624+
{
625+
cout << i << "->" << j << endl;
626+
G[i] = j;
627+
}
628+
}
629+
catch (exception e)
630+
{
631+
std::cout << "Error at (i, j): (" << i << ", " << j << ")" << std::endl;
632+
std::cout << "Catched an error while trying to reach the solution" << std::endl;
593633
}
594634
}
595635
}
636+
/*
596637
ofstream file;
597638
file.open("Results.txt", std::ios::app);
598639
if (file.is_open())
@@ -612,7 +653,7 @@ int main(int argc, char** argv)
612653
i = G[i];
613654
file << " → " << i;
614655
}
615-
}
656+
}*/
616657
}
617658
else
618659
{

0 commit comments

Comments
 (0)