-
Notifications
You must be signed in to change notification settings - Fork 3
/
solver.cpp
222 lines (175 loc) · 7.12 KB
/
solver.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
#include "solver.h"
#include <float.h>
#include <iostream>
#include <string>
#include "application.h"
#include "operators.h"
#include "random.h"
using namespace std;
Solver::Solver(boost::program_options::variables_map& variablesMap)
: instance(variablesMap["instance"].as<string>()) {
int n = instance.nodes.size() / 2;
qMin = std::min(30, int(0.20 * n));
qMax = std::min(50, int(0.55 * n));
param_P = variablesMap["p-accept"].as<double>();
param_C = variablesMap["c-rate"].as<double>();
param_MaxIt = variablesMap["it"].as<int>();
param_Fast = variablesMap.count("fast") > 0;
if (variablesMap.count("time-limit") > 0) {
Application::SetTimeLimit(variablesMap["time-limit"].as<int>());
}
}
Solver::~Solver() {
}
/**
* @brief Create initial solution based on Ropke and Pisinger (2006):
* Ropke, S. , & Pisinger, D. (2006). An adaptive large neighborhood search
* heuristic for the pickup and delivery problem with time windows.
* Transportation Science, 40 (4), 455–472
* @param instance
* @param useFast
* @return
*/
Solution InitialSolution(const Instance& instance, bool useFast) {
Solution sol;
sol.visits.push_back(0);
sol.visits.push_back(0);
Instance::NodeList pickupNodes = instance.Pickups();
const auto bestInsertionMethod =
useFast ? Operators::EvaluateBestInsertionFast : Operators::EvaluateBestInsertion;
while (pickupNodes.size()) {
int bestPos = -1;
int bestInsertionPosition[2] = {-1, -1};
double bestInsertionCost = DBL_MAX;
for (size_t pos = 0; pos < pickupNodes.size(); pos++) {
const Instance::Node* pickup = pickupNodes[pos];
int insertPosition[2];
double insertCost =
bestInsertionMethod(instance, sol.visits, pickup->idx, pickup->pair, insertPosition);
if (insertCost < bestInsertionCost) {
bestPos = pos;
bestInsertionCost = insertCost;
memcpy(bestInsertionPosition, insertPosition, sizeof(int) * 2);
}
}
sol.cost += bestInsertionCost;
Operators::InsertRequest(sol.visits, pickupNodes[bestPos]->idx, pickupNodes[bestPos]->pair,
bestInsertionPosition);
pickupNodes.erase(pickupNodes.begin() + bestPos);
}
return sol;
}
Instance::NodeList RemoveRequests(const Instance& instance, Solution& sol, int q, int p) {
static std::vector<Operators::PtrRemoveOperator> removalOperators = {
Operators::RandomRemoval, Operators::WorstDistanceRemoval, Operators::BlockRemoval};
Operators::PtrRemoveOperator removalOp = removalOperators[Random::RandomInt(0, removalOperators.size())];
Instance::NodeList removedRequests = removalOp(instance, sol, q, p);
for (const Instance::Node* node : removedRequests) {
sol.cost += Operators::RemoveRequest(instance, sol.visits, node->idx, node->pair);
}
return removedRequests;
}
void ReinsertRequests(const Instance& instance, Solution& sol, Instance::NodeList removedRequests,
bool useFast) {
const auto bestInsertionMethod =
useFast ? Operators::EvaluateBestInsertionFast : Operators::EvaluateBestInsertion;
Random::shuffle(removedRequests.begin(), removedRequests.end());
for (const Instance::Node* node : removedRequests) {
int insertPosition[2];
double insertCost = bestInsertionMethod(instance, sol.visits, node->idx, node->pair, insertPosition);
sol.cost += insertCost;
Operators::InsertRequest(sol.visits, node->idx, node->pair, insertPosition);
}
}
void Solver::Solve() {
iterationCount = 0;
Solution sol, solPrime;
Instance::NodeList removedRequests;
sol = InitialSolution(instance, param_Fast);
evolution.push_back(EvolutionEntry(iterationCount, Application::Ellapsed(), sol.cost));
solBest = sol;
double temperature;
double tempStart = -0.05 * solBest.cost / std::log(0.5);
while ((iterationCount < param_MaxIt || Application::UsingTimeLimit()) && !Application::Timeout()) {
iterationCount++;
solPrime = sol;
int q = Random::RandomInt(qMin, qMax);
removedRequests = RemoveRequests(instance, solPrime, q, param_P);
ReinsertRequests(instance, solPrime, removedRequests, param_Fast);
temperature = tempStart;
temperature *= Application::UsingTimeLimit()
? std::pow(param_C, param_MaxIt * Application::EllapsedRatio())
: std::pow(param_C, static_cast<double>(iterationCount));
double acceptProbability = std::exp(-(solPrime.cost - sol.cost) / temperature);
if (Random::RandomReal() < acceptProbability) {
sol = solPrime;
}
if (solPrime < solBest) {
solBest = solPrime;
evolution.push_back(EvolutionEntry(iterationCount, Application::Ellapsed(), sol.cost));
if (Application::IsVerbose()) {
std::cout << "New solution found: " << solBest.cost << " IT: " << iterationCount << std::endl;
}
}
}
}
void Solver::PrintStats() {
double cost = 0.0;
for (size_t i = 0; i < solBest.visits.size() - 1; i++) {
cost += instance.distances[solBest.visits[i]][solBest.visits[i + 1]];
}
if (Application::IsVerbose()) {
std::cout << std::endl;
std::cout << iterationCount;
std::cout << "; " << Application::Ellapsed();
std::cout << "; " << solBest.cost << std::endl;
} else {
std::cout << "{" << std::endl;
std::cout << " \"version\": \"" << Application::Version() << "\"";
std::cout << "," << std::endl << " \"cost\": " << solBest.cost;
std::cout << "," << std::endl << " \"time\": " << Application::Ellapsed();
std::cout << "," << std::endl << " \"educate\": " << iterationCount;
std::cout << "," << std::endl << " \"solution\": [0";
for (int v = 1; v < solBest.visits.size(); v++) {
std::cout << ", " << solBest.visits[v];
}
std::cout << "]," << std::endl << " \"evolution\": [" << std::endl;
std::vector<EvolutionEntry>::iterator evolIt = evolution.begin();
for (size_t i = 0; i < evolution.size() - 1; i++) {
EvolutionEntry& entry = *evolIt;
std::cout << " {\n";
std::cout << " \"iteration\": " << entry.iteration << ",\n";
std::cout << " \"time\": " << entry.time << ",\n";
std::cout << " \"cost\": " << entry.cost << "\n";
std::cout << " },\n";
evolIt++;
}
EvolutionEntry& entry = *evolIt;
std::cout << " {\n";
std::cout << " \"iteration\": " << entry.iteration << ",\n";
std::cout << " \"time\": " << entry.time << ",\n";
std::cout << " \"cost\": " << entry.cost << "\n";
std::cout << " }\n";
std::cout << " ]";
std::cout << std::endl << "}" << std::endl;
}
}
using namespace std;
int main(int argc, char* argv[]) {
boost::program_options::variables_map variablesMap = Application::initializeVariablesMap(argc, argv);
uint seed = (uint)variablesMap["seed"].as<int>();
std::srand(seed);
if (variablesMap["instance"].empty()) // solve only one instance specified by
// the --instance cmdline argument
{
cout << "No input instance. Use --help for more information" << endl;
exit(1);
}
// Create solver
Solver solver(variablesMap);
// Run
solver.Solve();
// Print
solver.PrintStats();
return 0;
}