-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUtils.cpp
178 lines (155 loc) · 5.42 KB
/
Utils.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
#include <iostream>
#include <fstream>
#include <string>
#include <regex>
#include "Utils.h"
#include "Point.h"
#include "Vehicle.h"
#include "SubCandidate.h"
#include "VehicleState.h"
#include "Solution.h"
#include <functional>
#define _USE_MATH_DEFINES
#include <math.h>
std::shared_ptr<VehicleState> Utils::origin;
std::list<Point *> Utils::raw_rows;
double **Utils::distances;
void Utils::read_file() {
std::ifstream inst;
inst.open("../Instances/RC208.txt");
//inst.open("../Instances/S-RC2-1000/RC21010.TXT");
//inst.open("../Instances/C101.TXT");
//inst.open("../Instances/RC105.txt");
if (inst.is_open()) {
std::string line;
std::regex e("([0-9]+)");
std::smatch sm;
auto line_count = 0;
while (std::getline(inst, line)) {
line_count++;
if (line_count > 9) // Ignore all lines until the points data
{
std::vector<uint32_t> aux;
aux.reserve(7);
while (std::regex_search(line, sm, e)) {
auto s = sm.str();
aux.push_back(static_cast<uint32_t>(strtol(s.data(), nullptr, 10)));
line = sm.suffix();
}
if (aux.size() == 7) { // We expect exactly 7 elements in this array
Utils::raw_rows.push_back(new Point(aux[0], aux[1], aux[2], aux[3], aux[4], aux[5], aux[6]));
}
}
}
Utils::origin = std::make_shared<VehicleState>(*Utils::raw_rows.begin());
Utils::distances = (double **) malloc(sizeof(double *) * Utils::raw_rows.size());
for (int i = 0; i < Utils::raw_rows.size(); ++i) {
Utils::distances[i] = (double *) malloc(sizeof(double) * Utils::raw_rows.size());
}
inst.close();
} else {
std::cout << "Unable to open file";
exit(1);
}
}
void Utils::calculate_distances() {
for (Point *point: Utils::raw_rows) {
for (Point *point2: Utils::raw_rows) {
Utils::distances[point->id][point2->id] = point->distance(point2);
}
}
}
/**
* Push forward insert heuristic algorithm
* @return
*/
Solution *Utils::pfih() {
std::vector<std::pair<Point *, double>> points(
Utils::raw_rows.size() - 1
); // -1 since we do not count the depot (id == 0 point)
auto it = Utils::raw_rows.begin();
auto origin = *it;
int ec = 0;
++it;
Point *curr;
double degreesdeposit = atan2(origin->y, origin->x) * (180.0 / M_PI);
double newdegree = 0;
while (it != Utils::raw_rows.end()) {
curr = *it;
newdegree = atan2(curr->y, curr->x) * (180.0 / M_PI);
double d = Utils::distances[0][curr->id];
points[curr->id - 1] = std::pair<Point *, double>(curr, (d * -0.7) + (0.1 * curr->due_date) +
(0.2 * (newdegree - degreesdeposit) / 360 * d));
++it;
}
std::sort(points.begin(), points.end(), Utils::pfih_comparator());
//Creates a solution
auto solution = new Solution();
auto v = new Vehicle(solution);
v->id = 0;
solution->vehicles.push_back(v);
v->add_node(points[0].first, 0, ec);
std::list<SubCandidate> candidates;
for (auto it = ++points.begin(); it != points.end(); ++it) {
auto point = *it;
for (auto vit = solution->vehicles.begin(); vit != solution->vehicles.end(); ++vit) {
auto vehicle = *vit;
for (int i = 0; i <= vehicle->nodes.size(); i++) {
ec = 0;
vehicle->add_node(point.first, i, ec);
if (ec == 0) {
SubCandidate sb(solution->total_weight(), i, vehicle);
candidates.push_back(sb);
vehicle->remove_node(i);
}
}
}
if (!candidates.empty()) {
candidates.sort();
auto chosen_one = *(candidates.begin());
ec = 0;
chosen_one.v->add_node(point.first, chosen_one.pos, ec);
point.first->state.insert({solution, std::pair<Vehicle *, int>(chosen_one.v, chosen_one.pos)});
candidates.clear();
} else {
auto nv = new Vehicle(solution);
nv->id = solution->vehicles.size();
solution->vehicles.push_back(nv);
ec = 0;
nv->add_node(point.first, 0, ec);
point.first->state.insert({solution, std::pair<Vehicle *, int>(nv, 0)});
}
}
return solution;
}
Solution *Utils::random_solution() {
auto solution = new Solution;
auto v = new Vehicle(solution);
solution->vehicles.push_back(v);
auto point_pool = Utils::raw_rows;
point_pool.erase(point_pool.begin());
while (!point_pool.empty()) {
auto pit = point_pool.begin();
std::advance(pit, rand() % point_pool.size());
auto p = *pit;
point_pool.erase(pit);
auto ec = 0;
for (auto vehicle: solution->vehicles) {
for (auto i = 0; i <= vehicle->nodes.size(); ++i) {
ec = 0;
vehicle->add_node(p, i, ec);
if (ec == 0)
goto leave;
}
}
if (ec != 0) {
v = new Vehicle(solution);
solution->vehicles.push_back(v);
auto ec = 0;
v->add_node(p, 0, ec);
}
leave:;
}
solution->total_weight();
return solution;
}