-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequential_ordering_main.cpp
405 lines (344 loc) · 11.8 KB
/
sequential_ordering_main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* Sequential ordering problem
*
* Problem description:
* https://github.com/fontanf/orproblems/blob/main/include/orproblems/scheduling/sequential_ordering.hpp
*
* Tree search:
* - forward branching
* - guide: current length + distance to the closest next child
*
*/
#include "read_args.hpp"
#include "optimizationtools/utils/utils.hpp"
#include "optimizationtools/containers/sorted_on_demand_array.hpp"
#include "orproblems/scheduling/sequential_ordering.hpp"
#include <memory>
#include <sstream>
using namespace treesearchsolver;
using namespace orproblems::sequential_ordering;
using NodeId = int64_t;
class BranchingScheme
{
public:
struct Node
{
/** Parent node. */
std::shared_ptr<Node> parent = nullptr;
/** Array indicating for each vertex, if it has been visited. */
std::vector<bool> visited;
/** Last visited vertex. */
LocationId last_location_id = 0;
/** Number of visited locations. */
LocationId number_of_locations = 1;
/** Length of the partial solution. */
Distance length = 0;
/**
* Sum of, for each unvisited vertex, the distance to its clostest
* neighbor.
*
* This is used to compute the outgoing bound efficiently.
*/
Distance bound_outgoing = 0;
/** Bound. */
Distance bound = 0;
/** Guide. */
Distance guide = 0;
/** Next child to generate. */
LocationPos next_child_pos = 0;
/** Unique id of the node. */
NodeId id = -1;
};
BranchingScheme(
const Instance& instance):
instance_(instance),
sorted_locations_(instance.number_of_locations()),
generator_(0)
{
// Initialize sorted_locations_.
for (LocationId location_id = 0;
location_id < instance_.number_of_locations();
++location_id) {
sorted_locations_[location_id].reset(instance.number_of_locations());
for (LocationId location_id_2 = 0;
location_id_2 < instance_.number_of_locations();
++location_id_2) {
sorted_locations_[location_id].set_cost(
location_id_2,
instance_.distance(location_id, location_id_2));
}
}
}
inline LocationId neighbor(
LocationId location_id,
LocationPos pos) const
{
return sorted_locations_[location_id].get(pos, generator_);
}
inline const std::shared_ptr<Node> root() const
{
auto r = std::shared_ptr<Node>(new BranchingScheme::Node());
r->id = node_id_;
node_id_++;
r->visited.resize(instance_.number_of_locations(), false);
// Bound.
r->bound_outgoing = 0;
for (LocationId location_id = 0;
location_id < instance_.number_of_locations();
++location_id) {
Distance d = instance_.distance(location_id, neighbor(location_id, 0));
if (d != std::numeric_limits<Distance>::max()) {
r->bound_outgoing += instance_.distance(
location_id,
neighbor(location_id, 0));
}
}
r->bound = r->bound_outgoing;
r->guide = r->bound;
return r;
}
inline std::shared_ptr<Node> next_child(
const std::shared_ptr<Node>& parent) const
{
assert(!infertile(parent));
assert(!leaf(parent));
// Get the next vertex to visit.
LocationId location_id_next = neighbor(parent->last_location_id, parent->next_child_pos);
Distance d = instance_.distance(parent->last_location_id, location_id_next);
// Update parent
parent->next_child_pos++;
Distance d_next = instance_.distance(
parent->last_location_id,
neighbor(parent->last_location_id, parent->next_child_pos));
if (d_next == std::numeric_limits<Distance>::max()) {
parent->guide = -1;
} else {
parent->bound = parent->bound - d + d_next;
parent->guide = parent->bound;
}
// Check if the vertex has already been visited.
if (parent->visited[location_id_next]
|| d == std::numeric_limits<Distance>::max())
return nullptr;
// Check if the predecessors of the location have already been visited.
for (LocationId location_id_pred: instance_.predecessors(location_id_next)) {
if (location_id_pred != parent->last_location_id
&& !parent->visited[location_id_pred]) {
return nullptr;
}
}
// Compute new child.
auto child = std::shared_ptr<Node>(new BranchingScheme::Node());
child->id = node_id_;
node_id_++;
child->parent = parent;
child->visited = parent->visited;
child->visited[parent->last_location_id] = true;
child->last_location_id = location_id_next;
child->number_of_locations = parent->number_of_locations + 1;
child->length = parent->length + d;
child->bound_outgoing = child->parent->bound_outgoing
- instance_.distance(
parent->last_location_id,
neighbor(parent->last_location_id, 0));
child->bound = child->length + child->bound_outgoing;
child->guide = child->bound;
return child;
}
inline bool infertile(
const std::shared_ptr<Node>& node) const
{
assert(node != nullptr);
return (node->guide == -1);
}
inline bool operator()(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
assert(!infertile(node_1));
assert(!infertile(node_2));
if (node_1->guide != node_2->guide)
return node_1->guide < node_2->guide;
return node_1->id < node_2->id;
}
inline bool leaf(
const std::shared_ptr<Node>& node) const
{
return node->number_of_locations == instance_.number_of_locations();
}
bool bound(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
if (node_2->number_of_locations != instance_.number_of_locations())
return false;
return node_1->bound >= node_2->length;
}
/*
* Solution pool.
*/
bool better(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
if (node_1->number_of_locations < instance_.number_of_locations())
return false;
if (node_2->number_of_locations < instance_.number_of_locations())
return true;
return node_1->length < node_2->length;
}
bool equals(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
(void)node_1;
(void)node_2;
return false;
}
/*
* Dominances.
*/
inline bool comparable(
const std::shared_ptr<Node>&) const
{
return true;
}
struct NodeHasher
{
std::hash<LocationId> hasher_1;
std::hash<std::vector<bool>> hasher_2;
inline bool operator()(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
if (node_1->last_location_id != node_2->last_location_id)
return false;
return node_1->visited == node_2->visited;
}
inline std::size_t operator()(
const std::shared_ptr<Node>& node) const
{
assert(node != nullptr);
size_t hash = hasher_1(node->last_location_id);
optimizationtools::hash_combine(hash, hasher_2(node->visited));
return hash;
}
};
inline NodeHasher node_hasher() const { return NodeHasher(); }
inline bool dominates(
const std::shared_ptr<Node>& node_1,
const std::shared_ptr<Node>& node_2) const
{
if (node_1->length <= node_2->length)
return true;
return false;
}
/*
* Outputs
*/
void instance_format(
std::ostream& os,
int verbosity_level) const
{
instance_.format(os, verbosity_level);
}
std::string display(const std::shared_ptr<Node>& node) const
{
if (node->number_of_locations != instance_.number_of_locations())
return "";
std::stringstream ss;
ss << node->length;
return ss.str();
}
void solution_format(
const std::shared_ptr<Node>& node,
std::ostream& os,
int verbosity_level) const
{
if (verbosity_level >= 1) {
os
<< "Length: " << node->length << std::endl
;
}
if (verbosity_level >= 2) {
}
}
inline void solution_write(
const std::shared_ptr<Node>& node,
std::string certificate_path) const
{
if (certificate_path.empty())
return;
std::ofstream file(certificate_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + certificate_path + "\".");
}
std::vector<LocationId> locations;
for (auto node_tmp = node;
node_tmp->parent != nullptr;
node_tmp = node_tmp->parent) {
locations.push_back(node_tmp->last_location_id);
}
std::reverse(locations.begin(), locations.end());
for (LocationId location_id: locations)
file << location_id << " ";
}
private:
/** Instance. */
const Instance& instance_;
/** Sorted locations. */
mutable std::vector<optimizationtools::SortedOnDemandArray> sorted_locations_;
/** Generator. */
mutable std::mt19937_64 generator_;
mutable NodeId node_id_ = 0;
};
int main(int argc, char *argv[])
{
// Setup options.
boost::program_options::options_description desc = setup_args();
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << std::endl;;
throw "";
}
try {
boost::program_options::notify(vm);
} catch (const boost::program_options::required_option& e) {
std::cout << desc << std::endl;;
throw "";
}
// Create instance.
InstanceBuilder instance_builder;
instance_builder.read(
vm["input"].as<std::string>(),
vm["format"].as<std::string>());
const Instance instance = instance_builder.build();
// Create branching scheme.
BranchingScheme branching_scheme(instance);
// Run algorithm.
std::string algorithm = vm["algorithm"].as<std::string>();
Output<BranchingScheme> output =
(algorithm == "greedy")?
run_greedy(branching_scheme, vm):
(algorithm == "best-first-search")?
run_best_first_search(branching_scheme, vm):
(algorithm == "iterative-beam-search")?
run_iterative_beam_search(branching_scheme, vm):
(algorithm == "anytime-column-search")?
run_anytime_column_search(branching_scheme, vm):
run_iterative_memory_bounded_best_first_search(branching_scheme, vm);
// Run checker.
if (vm["print-checker"].as<int>() > 0
&& vm["certificate"].as<std::string>() != "") {
std::cout << std::endl
<< "Checker" << std::endl
<< "-------" << std::endl;
instance.check(
vm["certificate"].as<std::string>(),
std::cout,
vm["print-checker"].as<int>());
}
return 0;
}