This repository has been archived by the owner on Nov 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLittle.cpp
318 lines (252 loc) · 8.83 KB
/
Little.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "Little.h"
Little::Little() {
nodes = stack<LittleNode>();
segments = vector<Segment>();
reference = numeric_limits<double>::max();
fileName = "";
nbOfVisitedNodes = 0;
}
Little::Little(const string &fileName) : fileName(fileName) {
nodes = stack<LittleNode>();
segments = vector<Segment>();
reference = numeric_limits<double>::max();
nbOfVisitedNodes = 0;
}
Little::~Little() {
delete distanceMatrix;
}
const stack<LittleNode> &Little::getNodes() const {
return nodes;
}
void Little::setNodes(const stack<LittleNode> &nodes) {
Little::nodes = nodes;
}
double Little::getReference() const {
return reference;
}
void Little::setReference(double reference) {
Little::reference = reference;
}
const vector<Segment> &Little::getSegments() const {
return segments;
}
void Little::setSegments(const vector<Segment> &segments) {
Little::segments = segments;
}
void Little::start() {
auto start = Clock::now();
distanceMatrix = getDistancesMatrix(getCoordinates(fileName));
nodes.push(LittleNode(new MatrixXd(*distanceMatrix)));
while (nodes.size() > 0) {
LittleNode currentNode = nodes.top();
nodes.pop();
nbOfVisitedNodes++;
double currentNodeValue = currentNode.getValue();
// cout << "Node value: " << currentNodeValue << endl;
// cout << "Reference :" << reference << endl;
if (currentNodeValue < reference) {
examineNode(currentNode);
}
delete currentNode.getMatrix();
}
sortSegments();
printResults();
auto finish = Clock::now();
cout << "Algorithm executed in "
<< chrono::duration_cast<chrono::seconds>(finish - start).count()
<< " seconds."
<< endl;
}
void Little::examineNode(LittleNode node) {
node.disableLoops();
double reducedAmount = node.reduceMatrix();
double newValue = reducedAmount + node.getParentValue();
node.setValue(newValue);
if (newValue < reference) {
if (node.getMatrix()->rows() - node.getSegments().size() > 2) {
//Not last node
Regret maxRegret = node.getMaxRegret();
//Include path matrix
MatrixXd *includeMatrix = new MatrixXd(*node.getMatrix());
LittleNode includeNode = LittleNode(includeMatrix, node.getValue(), node.getSegments());
//Push new segment
vector<Segment> newNodeSegments = includeNode.getSegments();
newNodeSegments.push_back(maxRegret.getSegment());
includeNode.setSegments(newNodeSegments);
//Remove depart row and finish column
includeNode.removeMatrixRow(maxRegret.getSegment().getFrom());
includeNode.removeMatrixCol(maxRegret.getSegment().getTo());
//Exclude path matrix
MatrixXd *excludeMatrix = new MatrixXd(*node.getMatrix());
//Disable the segment on the new matrix
LittleNode excludeNode = LittleNode(excludeMatrix, node.getValue() + maxRegret.getValue(), node.getValue(),
node.getSegments());
excludeNode.disableSegment(maxRegret.getSegment()); //Disable excluded segment
//Push exclude node
nodes.push(excludeNode);
//Push include node
nodes.push(includeNode);
} else {
//Last node
segments = node.getSegments();
//Only 2 segments left to include
for (size_t row = 0; row < node.getMatrix()->rows(); ++row) {
for (int col = 0; col < node.getMatrix()->cols(); ++col) {
if ((*node.getMatrix())(row, col) >= 0) {
segments.push_back(Segment(row, col));
}
}
}
reference = node.getValue();
}
}
}
vector<Coordinates> Little::getCoordinates(string fileName) {
vector<Coordinates> coordinates = vector<Coordinates>();
string currentLine;
ifstream file(fileName);
string delimiter = " ";
while (getline(file, currentLine)) {
size_t pos = 0;
int index = 0;
float x = 0;
float y = 0;
//Getting tower index
pos = currentLine.find(delimiter);
if (pos != string::npos) {
try {
index = stoi(currentLine.substr(0, pos));
} catch (...) {
cout << "Error while converting index into an integer."
<< "Setting to default value : 0."
<< endl;
index = 0;
}
currentLine.erase(0, pos + delimiter.length());
}
//Getting x coordinate
pos = currentLine.find(delimiter);
if (pos != string::npos) {
try {
x = stof(currentLine.substr(0, pos));
} catch (...) {
cout << "Error while converting x into a float."
<< "Setting to default value : 0."
<< endl;
x = 0;
}
currentLine.erase(0, pos + delimiter.length());
}
//Getting y coordinate
pos = currentLine.find(delimiter);
if (pos != string::npos) {
try {
y = stof(currentLine.substr(0, pos));
} catch (...) {
cout << "Error while converting y into a float."
<< "Setting to default value : 0."
<< endl;
y = 0;
}
}
//Log
cout << "Getting coordinates of tower "
<< index
<< " ("
<< x
<< ", "
<< y
<< ")"
<< endl;
//Pushing into the vector
coordinates.push_back(Coordinates(x, y));
}
return coordinates;
}
MatrixXd *Little::getDistancesMatrix(vector<Coordinates> coordinates) {
MatrixXd *matrix = new MatrixXd();
size_t numberOfPoints = coordinates.size();
matrix->resize(numberOfPoints, numberOfPoints);
//Filling the matrix
for (size_t row = 0; row < numberOfPoints; ++row) {
for (size_t column = 0; column < numberOfPoints; ++column) {
if (row != column) {
(*matrix)(row, column) = coordinates[row].computeDistanceWith(coordinates[column]);
} else {
//The distance from a point to itself is set to max value
(*matrix)(row, column) = -1;
}
}
}
return matrix;
}
void Little::printResults() {
double totalDistance = 0;
cout << "Optimal path : " << endl;
for (vector<Segment>::iterator iterator = segments.begin(); iterator < segments.end(); ++iterator) {
long from = iterator->getFrom();
long to = iterator->getTo();
cout << "From "
<< from + 1
<< " to "
<< to + 1
<< " for a distance of "
<< (*distanceMatrix)(from, to)
<< endl;
totalDistance += (*distanceMatrix)(from, to);
}
cout << "Total cost: "
<< totalDistance
<< endl;
cout << "Number of visited nodes: "
<< nbOfVisitedNodes
<< " of "
<< computeMaxNumberOfNodes(distanceMatrix->rows())
<< " maximum nodes."
<< endl;
}
void Little::sortSegments() {
vector<Segment> sortedSegments = vector<Segment>();
vector<Segment> segmentsToSort = segments;
for (vector<Segment>::iterator iterator = segmentsToSort.begin(); iterator < segmentsToSort.end(); ++iterator) {
if (iterator->getFrom() == 0) {
sortedSegments.push_back(*iterator);
segmentsToSort.erase(iterator);
break;
}
}
while (segmentsToSort.size() > 0) {
for (vector<Segment>::iterator iterator = segmentsToSort.begin(); iterator < segmentsToSort.end(); ++iterator) {
if (iterator->getFrom() == sortedSegments.back().getTo()) {
sortedSegments.push_back(*iterator);
segmentsToSort.erase(iterator);
break;
}
if (iterator->getTo() == sortedSegments.front().getFrom()) {
sortedSegments.insert(sortedSegments.begin(), *iterator);
segmentsToSort.erase(iterator);
break;
}
}
}
segments = sortedSegments;
}
MatrixXd *Little::getDistanceMatrix() const {
return distanceMatrix;
}
void Little::setDistanceMatrix(MatrixXd *distanceMatrix) {
Little::distanceMatrix = distanceMatrix;
}
const string &Little::getFileName() const {
return fileName;
}
void Little::setFileName(const string &fileName) {
Little::fileName = fileName;
}
size_t Little::computeMaxNumberOfNodes(size_t nbRows) {
if (nbRows == 3) {
return 8;
} else {
return (nbRows - 1) * (1 + computeMaxNumberOfNodes(nbRows - 1));
}
}