Skip to content

Commit f26b773

Browse files
committed
V1.0 - Final working build
1 parent 7de8f73 commit f26b773

File tree

3 files changed

+111
-97
lines changed

3 files changed

+111
-97
lines changed

TIPEcppTest1/FileManager.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,26 @@ float* FileManager::read_standardized_csv_drones(vector<vector<string>> lines, b
9494
}
9595
}
9696
return Distance;
97+
}
98+
99+
std::set<int> FileManager::get_drone_clients_csv(vector<vector<string>> lines, bool EnableCout)
100+
{
101+
// Conditions to be drone-eligible: distance < 8000 && weight < 5kg
102+
std::set<int> data = {};
103+
//We ignore two first lines
104+
for (int p = 2; p < lines.size(); p++)
105+
{
106+
int i = std::stoi(lines[p][0]);
107+
float weight = std::stof(lines[p][5]);
108+
float distance = std::stof(lines[p][7]);
109+
if (weight < 5.0 && distance < 8000.0)
110+
{
111+
data.insert(data.end(), i);
112+
if (EnableCout)
113+
{
114+
cout << "Nd_i: " << i << endl;
115+
}
116+
}
117+
}
118+
return data;
97119
}

TIPEcppTest1/FileManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <vector>
33
#include <string>
4+
#include <set>
45

56
using namespace std;
67

@@ -31,4 +32,12 @@ static class FileManager
3132
/// <param name="EnableCout">Enable debug output to console</param>
3233
/// <returns>A list containing the data of the csv</returns>
3334
static float* read_standardized_csv_drones(vector<vector<string>> lines, bool useTime, bool EnableCout = false);
35+
36+
/// <summary>
37+
/// This is a parser, parses the wanted csv file to retrieve the wanted list
38+
/// </summary>
39+
/// <param name="lines">The csv files, already organized</param>
40+
/// <param name="EnableCout">Enable debug output to console</param>
41+
/// <returns>A list containing the data of the csv</returns>
42+
static std::set<int> get_drone_clients_csv(vector<vector<string>> lines, bool EnableCout = false);
3443
};

TIPEcppTest1/TIPEcpp_PDSTSP.cpp

Lines changed: 80 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "utilities.h"
1212
#include "FileManager.h"
1313
#include <set>
14+
#include <algorithm>
1415
using namespace std;
1516

1617
//Here we define a Matrix decision variable
@@ -46,6 +47,10 @@ int GetNextValue(map<int, int> dict, int curValue)
4647
return i.first;
4748
}
4849
}
50+
if (next)
51+
{
52+
return dict.begin()->first;
53+
}
4954
}
5055

5156
#pragma region LazyContraintsCallbacks
@@ -69,7 +74,7 @@ vector<vector<int>> CalculateSubtours(map<int, int> G1)
6974
i = GetNextValue(G1, i);
7075
if (!Visited[i])
7176
{
72-
//cout << "i: " << i << " Visited count: " << Visited.count(i) << " bool: " << Visited[i] << endl;
77+
cout << "i: " << i << " Visited count: " << Visited.count(i) << " bool: " << Visited[i] << endl;
7378
// start <- i , S <- {i}
7479
int start = i;
7580
vector<int> S;
@@ -79,16 +84,18 @@ vector<vector<int>> CalculateSubtours(map<int, int> G1)
7984
bool containsDepot = false;
8085
// While the successor j of i(xij = 1) is not equal to start do
8186
int j = G1[i];
87+
//cout << "i:" << i << "j:" << j << "start:" << start << endl;
8288
while (j != start)
8389
{
8490
i = j;
8591
Visited[i] = true;
92+
//cout << "i:" << i << "j:" << j << endl;
8693
S.push_back(i);
8794
if (i == 0)
8895
{
8996
containsDepot = true;
9097
}
91-
j = G1[i];
98+
j = G1.at(i);
9299
}
93100
if (!containsDepot)
94101
{
@@ -118,15 +125,17 @@ ILOLAZYCONSTRAINTCALLBACK3(LazyCallback, NumVar2D, Xmatrix, IloInt, n, IloNumVar
118125
map<int, int> G1;
119126
cout << "===================" << endl;
120127
cout << "Lazy here!" << endl;
121-
for (int i = 0; i < n; i++)
128+
for (int i = 0; i <= n; i++)
122129
{
123-
for (int j = 0; j < n; j++)
130+
for (int j = 0; j <= n; j++)
124131
{
125132
if (i == j) continue;
126-
cout << i << "->" << j << endl;
127133
float value = getValue(Xmatrix[i][j]);
134+
135+
//cout << i << "->" << j << " - value:" << getValue(Xmatrix[i][j]) << endl;
128136
if (value == 1)
129137
{
138+
cout << i << "->" << j << endl;
130139
//We assume that the path is unique (should be since it respects go-to and come-from constraints)
131140
G1[i] = j;
132141
}
@@ -147,72 +156,37 @@ ILOLAZYCONSTRAINTCALLBACK3(LazyCallback, NumVar2D, Xmatrix, IloInt, n, IloNumVar
147156
{
148157
IloExpr expr3_14(getEnv());
149158
float sum = 0;
150-
// std::cout << "ct3.14 ->";
159+
std::cout << "ct3.14 ->";
151160
// For each j in S
152161
for (int j : sub)
153162
{
154163
// For each k not in S
155164
// == For each k in N not in S
156165
// EDITED: k in 0..n
157-
for (int k = 1; k <= n; k++)
166+
for (int k = 0; k <= n; k++)
158167
{
159168
// If k not in S
160169
if (std::find(sub.begin(), sub.end(), k) == sub.end())
161170
{
162171
expr3_14 += Xmatrix[j][k];
163172
sum += getValue(Xmatrix[j][k]);
164-
// std::cout << "+ X[" << j << "][" << k << "]";
173+
std::cout << "+ X[" << j << "][" << k << "]";
165174
}
166175
}
167176
}
168-
// std::cout << ">= Z[" << i - 1 << "]" << std::endl;
169-
if (sum >= getValue(Zmatrix[i - 1]))
177+
std::cout << ">= Z[" << i << "]" << std::endl;
178+
if (sum >= float(getValue(Zmatrix[i - 1])))
170179
{
171180
cout << "One subset is correct" << endl;
172181
}
173182
else
174183
{
175-
cout << "Adding lazy, SEC constraint " << sum << " <= " << int(getValue(Zmatrix[i - 1])) << " is violated" << endl;
184+
cout << "Adding lazy, SEC constraint " << sum << " >= " << float(getValue(Zmatrix[i - 1])) << " is violated" << endl;
176185
add(expr3_14 >= Zmatrix[i - 1]);
177186
}
178187
}
179188
}
180189
}
181-
/*
182-
for (vector<int> sub : subToursList)
183-
{
184-
if (2 <= sub.size() && sub.size() <= n - 1)
185-
{
186-
float sum = 0;
187-
for (int i = 0; i < n; i++)
188-
{
189-
for (int j = 0; j < n; j++)
190-
{
191-
sum += getValue(Xmatrix[i][j]);
192-
}
193-
}
194-
if (sum <= sub.size() - 1)
195-
{
196-
// Success! This subset does not break the SECs
197-
cout << "One subset is correct" << endl;
198-
}
199-
else
200-
{
201-
// SEC violated, thus we'll add a lazy constraint on this subset
202-
cout << "Adding lazy, SEC constraint " << sum << " <= " << int(sub.size() - 1) << " is violated" << endl;
203-
//ct3_4
204-
IloExpr expr3_4(getEnv());
205-
for (int i : sub)
206-
{
207-
for (int j : sub)
208-
{
209-
expr3_4 += Xmatrix[i][j];
210-
}
211-
}
212-
add(expr3_4 <= int(sub.size() - 1));
213-
}
214-
}
215-
}*/
216190
auto end_lazy = chrono::high_resolution_clock::now();
217191
auto ElapsedLazy = chrono::duration_cast<chrono::milliseconds>(end_lazy - start_lazy);
218192

@@ -236,7 +210,7 @@ void usage(char* progname)
236210

237211
int main(int argc, char** argv)
238212
{
239-
bool useTime = false;
213+
bool useTime = true;
240214
#pragma region ArgumentsParsing
241215
string truck_filename;
242216
string drone_filename;
@@ -310,26 +284,25 @@ int main(int argc, char** argv)
310284
float* Drone_dist = new float[n];
311285
// TODO: Make a better management for the Nd float*
312286
// Clients eligible for drone delivery
313-
static const size_t Nd_size = 4;
314-
std::set<float> Nd = { 1, 3, 7, 8 };
315287
// M = number of drones
316288
static const size_t M = 2;
317289
Distance = FileManager::read_standardized_csv_trucks(func_out, useTime, true);
318290
Drone_dist = FileManager::read_standardized_csv_drones(func_out_2, useTime, true);
291+
std::set<int> Nd = FileManager::get_drone_clients_csv(func_out_2, true);
319292

320293
// Subtours generation
321-
/*
322-
vector<int> arg;
323-
std::cout << "[";
324-
for (int i = 1; i <= n; i++)
325-
{
326-
arg.push_back(i);
327-
std::cout << "| i: " << i;
328-
}
329-
std::cout << "]" << endl;
330-
vector<vector<int>> sub = utilities::subset(arg);
331-
utilities::print_subsets(sub);
332-
*/
294+
295+
//vector<int> arg;
296+
//std::cout << "[";
297+
//for (int i = 1; i <= n; i++)
298+
//{
299+
// arg.push_back(i);
300+
// std::cout << "| i: " << i;
301+
//}
302+
//std::cout << "]" << endl;
303+
//vector<vector<int>> sub = utilities::subset(arg);
304+
//utilities::print_subsets(sub);
305+
333306
auto start_1 = chrono::high_resolution_clock::now();
334307

335308
#pragma endregion
@@ -387,7 +360,7 @@ int main(int argc, char** argv)
387360
Model.add(IloMinimize(env, T));
388361

389362
// TODO: What is this useful for?
390-
IloRange range();
363+
//IloRange range();
391364

392365
#pragma endregion
393366

@@ -427,7 +400,7 @@ int main(int argc, char** argv)
427400
// If i not in Nd
428401
if (Nd.find(i) == Nd.end())
429402
{
430-
std::cout << "ct3.9 -> Z[" << i - 1 << "] == 1" << endl;
403+
std::cout << "ct3.9 -> Z[" << i << "] == 1" << endl;
431404
// z[i] == 1
432405
Model.add(Z[i - 1] == 1);
433406
}
@@ -495,8 +468,10 @@ int main(int argc, char** argv)
495468
if (i == temp)
496469
{
497470
expr3_13_a += X[i][j];
471+
std::cout << "+ X[" << i << "][" << j << "]";
498472
}
499473
}
474+
std::cout << " == ";
500475
IloExpr expr3_13_b(env);
501476
// Sum on each Arc(k, i) in A
502477
for (Arc a : Arcs)
@@ -506,46 +481,48 @@ int main(int argc, char** argv)
506481
if (i == temp)
507482
{
508483
expr3_13_b += X[k][i];
484+
std::cout << "+ X[" << k << "][" << i << "]";
509485
}
510486
}
487+
std::cout << endl;
511488
Model.add(expr3_13_a == expr3_13_b);
512489
}
513490

514491
//SECs
515492
// ct 3.14
516493
// For each subset S of N
517-
/*
518-
for (size_t s = 0; s < sub.size(); s++)
519-
{
520-
vector<int> S = sub[s];
521-
if (S.size() > 0 && S.size() != n)
522-
{
523-
// For each i in a subtour S
524-
for (int i : S)
525-
{
526-
IloExpr expr3_14(env);
527-
// std::cout << "ct3.14 ->";
528-
// For each j in S
529-
for (int j : S)
530-
{
531-
// For each k not in S
532-
// == For each k in N not in S
533-
// EDITED: k in 0..n
534-
for (int k = 1; k <= n; k++)
535-
{
536-
// If k not in S
537-
if (std::find(S.begin(), S.end(), k) == S.end())
538-
{
539-
expr3_14 += X[j][k];
540-
// std::cout << "+ X[" << j << "][" << k << "]";
541-
}
542-
}
543-
}
544-
// std::cout << ">= Z[" << i - 1 << "]" << std::endl;
545-
Model.add(expr3_14 >= Z[i - 1]);
546-
}
547-
}
548-
}*/
494+
495+
//for (size_t s = 0; s < sub.size(); s++)
496+
//{
497+
// vector<int> S = sub[s];
498+
// if (S.size() > 0 && S.size() != n)
499+
// {
500+
// // For each i in a subtour S
501+
// for (int i : S)
502+
// {
503+
// IloExpr expr3_14(env);
504+
// // std::cout << "ct3.14 ->";
505+
// // For each j in S
506+
// for (int j : S)
507+
// {
508+
// // For each k not in S
509+
// // == For each k in N not in S
510+
// // EDITED: k in 0..n
511+
// for (int k = 0; k <= n; k++)
512+
// {
513+
// // If k not in S
514+
// if (std::find(S.begin(), S.end(), k) == S.end())
515+
// {
516+
// expr3_14 += X[j][k];
517+
// // std::cout << "+ X[" << j << "][" << k << "]";
518+
// }
519+
// }
520+
// }
521+
// // std::cout << ">= Z[" << i - 1 << "]" << std::endl;
522+
// Model.add(expr3_14 >= Z[i - 1]);
523+
// }
524+
// }
525+
//}
549526

550527
/* =========
551528
INTEGRALITY CONSTRAINTS
@@ -612,7 +589,7 @@ int main(int argc, char** argv)
612589
cplex.setParam(IloCplex::Param::MIP::Strategy::Search, IloCplex::Traditional);
613590

614591
//Registering callbacks
615-
cplex.use(LazyCallback(env, Y, n, Z));
592+
cplex.use(LazyCallback(env, X, n, Z));
616593
bool solved = false;
617594

618595
try
@@ -698,9 +675,15 @@ int main(int argc, char** argv)
698675
if (file_X.is_open())
699676
{
700677
int i = 1;
678+
for (pair<int, int> i0 : G)
679+
{
680+
i = i0.first;
681+
break;
682+
}
701683
file_X << i << "," << G[i];
684+
int beginning = i;
702685
i = G[i];
703-
while (i != 1)
686+
while (i != beginning)
704687
{
705688
i = G[i];
706689
file_X << "," << i;

0 commit comments

Comments
 (0)