-
Notifications
You must be signed in to change notification settings - Fork 1
/
permutation_flowshop_scheduling_makespan.hpp
521 lines (458 loc) · 18.6 KB
/
permutation_flowshop_scheduling_makespan.hpp
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* Permutation flow shop scheduling problem, makespan
*
* Problem description:
* See https://github.com/fontanf/orproblems/blob/main/orproblems/permutation_flowshop_scheduling_makespan.hpp
*
* TODO
*/
#pragma once
#include "localsearchsolver/common.hpp"
#include "orproblems/scheduling/permutation_flowshop_scheduling_makespan.hpp"
#include "optimizationtools/utils/utils.hpp"
namespace localsearchsolver
{
namespace permutation_flowshop_scheduling_makespan
{
using namespace orproblems::permutation_flowshop_scheduling_makespan;
class LocalScheme
{
public:
struct Parameters
{
JobPos block_size_max = 8;
bool shuffle_neighborhood_order = true;
Counter number_of_perturbations = 10;
};
/*
* Constructors and destructor
*/
LocalScheme(
const Instance& instance,
Parameters parameters):
instance_(instance),
parameters_(parameters),
positions1_(instance.number_of_jobs()),
positions2_(instance.number_of_jobs()),
times_(instance_.number_of_machines(), 0),
heads_(instance.number_of_jobs() + 1),
tails_(instance.number_of_jobs() + 1),
completion_times_(instance.number_of_jobs() + 1)
{
std::iota(positions1_.begin(), positions1_.end(), 0);
std::iota(positions2_.begin(), positions2_.end(), 0);
for (JobId job_id = 0;
job_id < instance_.number_of_jobs() + 1;
++job_id) {
heads_[job_id] = std::vector<Time>(instance_.number_of_machines(), 0);
tails_[job_id] = std::vector<Time>(instance_.number_of_machines(), 0);
completion_times_[job_id] = std::vector<Time>(instance_.number_of_machines(), 0);
}
}
/*
* Global cost
*/
/** Global cost: <Makespan>. */
using GlobalCost = std::tuple<Time>;
inline Time& makespan(GlobalCost& global_cost) { return std::get<0>(global_cost); }
inline Time makespan(const GlobalCost& global_cost) { return std::get<0>(global_cost); }
/*
* Solutions
*/
struct Solution
{
/** Jobs. */
std::vector<JobId> jobs;
/** Makespan. */
Time makespan = 0;
};
inline Solution empty_solution() const
{
Solution solution;
return solution;
}
inline Solution initial_solution(
Counter,
std::mt19937_64& generator)
{
Solution solution = empty_solution();
std::vector<JobId> jobs(instance_.number_of_jobs());
std::iota(jobs.begin(), jobs.end(), 0);
std::shuffle(jobs.begin(), jobs.end(), generator);
return compact2solution(jobs);
}
inline GlobalCost global_cost(const Solution& solution) const
{
return {
solution.makespan,
};
}
/*
* Local search
*/
struct Perturbation;
inline void local_search(
Solution& solution,
std::mt19937_64& generator,
const Perturbation& = Perturbation())
{
std::vector<Counter> neighborhoods;
for (JobPos block_size = 1;
block_size <= parameters_.block_size_max;
++block_size) {
neighborhoods.push_back(block_size);
}
Counter it = 0;
(void)it;
for (;; ++it) {
//std::cout << "it " << it
// << " c " << to_string(global_cost(solution))
// << std::endl;
//print(std::cout, solution);
if (parameters_.shuffle_neighborhood_order)
std::shuffle(neighborhoods.begin(), neighborhoods.end(), generator);
bool improved = false;
// Loop through neighborhoods.
for (Counter block_size: neighborhoods) {
std::shuffle(positions1_.begin(), positions1_.end(), generator);
std::shuffle(positions2_.begin(), positions2_.end(), generator);
JobPos pos_best = -1;
JobPos pos_new_best = -1;
GlobalCost c_best = global_cost(solution);
for (JobPos pos: positions1_) {
if (pos > (JobPos)solution.jobs.size() - block_size)
continue;
compute_structures(solution, pos, block_size);
for (JobPos pos_new: positions2_) {
if (pos == pos_new || pos_new > (JobPos)solution.jobs.size() - block_size)
continue;
Time makespan = 0;
for (MachineId machine_id = 0;
machine_id < instance_.number_of_machines();
++machine_id) {
makespan = std::max(makespan,
completion_times_[pos_new][machine_id]
+ tails_[pos_new][machine_id]);
}
GlobalCost c = {makespan};
if (c >= c_best)
continue;
if (pos_best != -1 && !dominates(c, c_best))
continue;
pos_best = pos;
pos_new_best = pos_new;
c_best = c;
}
}
if (pos_best != -1) {
improved = true;
assert(makespan(c_best) < solution.makespan);
// Apply best perturbation.
//std::cout << "pos_best " << pos_best
// << " pos_new_best " << pos_new_best
// << " size " << block_size
// << std::endl;
std::vector<JobId> jobs;
if (pos_best > pos_new_best) {
for (JobPos p = 0; p < pos_new_best; ++p)
jobs.push_back(solution.jobs[p]);
for (JobPos p = pos_best; p < pos_best + block_size; ++p)
jobs.push_back(solution.jobs[p]);
for (JobPos p = pos_new_best; p < pos_best; ++p)
jobs.push_back(solution.jobs[p]);
for (JobPos p = pos_best + block_size; p < (JobPos)solution.jobs.size(); ++p)
jobs.push_back(solution.jobs[p]);
} else {
for (JobPos p = 0; p < pos_best; ++p)
jobs.push_back(solution.jobs[p]);
for (JobPos p = pos_best + block_size; p < pos_new_best + block_size; ++p)
jobs.push_back(solution.jobs[p]);
for (JobPos p = pos_best; p < pos_best + block_size; ++p)
jobs.push_back(solution.jobs[p]);
for (JobPos p = pos_new_best + block_size; p < (JobPos)solution.jobs.size(); ++p)
jobs.push_back(solution.jobs[p]);
}
assert((JobPos)jobs.size() <= instance_.number_of_jobs());
compute(solution, jobs);
if (solution.makespan != makespan(c_best)) {
std::cout << "pos_best " << pos_best
<< " pos_new_best " << pos_new_best
<< " size " << block_size
<< std::endl;
std::cout << makespan(c_best) << std::endl;
std::cout << solution.makespan << std::endl;
for (MachineId machine_id = 0;
machine_id < instance_.number_of_machines();
++machine_id) {
std::cout << "machine_id " << machine_id
<< " " << heads_[((pos_new_best <= pos_best)? pos_new_best: pos_new_best - block_size)][machine_id]
<< " " << completion_times_[((pos_new_best <= pos_best)? pos_new_best: pos_new_best - block_size)][machine_id]
<< " " << tails_[((pos_new_best <= pos_best)? pos_new_best: pos_new_best - block_size)][machine_id]
<< std::endl;
}
solution_format(std::cout, solution, 1);
}
assert(solution.makespan == makespan(c_best));
}
if (improved)
break;
}
if (!improved)
break;
}
//print(std::cout, solution);
}
/*
* Iterated local search
*/
struct Perturbation
{
Perturbation(): pos_1(-1), global_cost(worst<GlobalCost>()) { }
JobPos pos_1;
JobPos pos_2;
JobPos pos_3;
JobPos pos_4;
GlobalCost global_cost;
};
inline std::vector<Perturbation> perturbations(
const Solution& solution,
std::mt19937_64& generator)
{
std::vector<Perturbation> perturbations;
for (Counter perturbation_id = 0;
perturbation_id < parameters_.number_of_perturbations;
++perturbation_id) {
std::vector<JobPos> edges = optimizationtools::bob_floyd<JobPos>(
4,
solution.jobs.size() + 1,
generator);
std::sort(edges.begin(), edges.end());
Perturbation perturbation;
perturbation.pos_1 = edges[0];
perturbation.pos_2 = edges[1];
perturbation.pos_3 = edges[2];
perturbation.pos_4 = edges[3];
assert(perturbation.pos_1 >= 0);
assert(perturbation.pos_4 <= (JobPos)solution.jobs.size());
perturbation.global_cost = global_cost(solution);
perturbations.push_back(perturbation);
}
return perturbations;
}
inline void apply_perturbation(
Solution& solution,
const Perturbation& perturbation,
std::mt19937_64&)
{
std::vector<JobId> jobs;
for (JobPos pos = 0; pos < perturbation.pos_1; ++pos)
jobs.push_back(solution.jobs[pos]);
for (JobPos pos = perturbation.pos_3; pos < perturbation.pos_4; ++pos)
jobs.push_back(solution.jobs[pos]);
for (JobPos pos = perturbation.pos_2; pos < perturbation.pos_3; ++pos)
jobs.push_back(solution.jobs[pos]);
for (JobPos pos = perturbation.pos_1; pos < perturbation.pos_2; ++pos)
jobs.push_back(solution.jobs[pos]);
for (JobPos pos = perturbation.pos_4; pos < (JobPos)solution.jobs.size(); ++pos)
jobs.push_back(solution.jobs[pos]);
assert((JobPos)jobs.size() <= instance_.number_of_jobs());
compute(solution, jobs);
}
/*
* Best first local search
*/
/**
* solution[n] is the first job scheduled.
* solution[j] is the index of the job scheduled after job j, n if job
* j is the last job, or -1 if job j is not in the solution.
*/
using CompactSolution = std::vector<JobId>;
struct CompactSolutionHasher
{
std::hash<JobId> hasher;
inline bool operator()(
const std::shared_ptr<CompactSolution>& compact_solution_1,
const std::shared_ptr<CompactSolution>& compact_solution_2) const
{
return *compact_solution_1 == *compact_solution_2;
}
inline std::size_t operator()(
const std::shared_ptr<CompactSolution>& compact_solution) const
{
size_t hash = 0;
for (JobId job_id: *compact_solution)
optimizationtools::hash_combine(hash, hasher(job_id));
return hash;
}
};
inline CompactSolutionHasher compact_solution_hasher() const { return CompactSolutionHasher(); }
CompactSolution solution2compact(const Solution& solution)
{
return solution.jobs;
}
Solution compact2solution(const CompactSolution& compact_solution)
{
auto solution = empty_solution();
compute(solution, compact_solution);
return solution;
}
struct PerturbationHasher
{
inline bool hashable(const Perturbation&) const { return false; }
inline bool operator()(const Perturbation&, const Perturbation&) const { return false; }
inline std::size_t operator()(const Perturbation&) const { return 0; }
};
inline PerturbationHasher perturbation_hasher() const { return PerturbationHasher(); }
/*
* Outputs
*/
void instance_format(
std::ostream& os,
int verbosity_level) const
{
os << "Permutation flow shop scheduling problem, makespan" << std::endl;
instance_.format(os, verbosity_level);
}
void solution_format(
std::ostream& os,
const Solution& solution,
int verbosity_level)
{
(void)verbosity_level;
os << "jobs:";
for (JobId job_id: solution.jobs)
os << " " << job_id;
os << std::endl;
os << "makespan: " << solution.makespan << std::endl;
}
void solution_write(
const Solution& solution,
const 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 + "\".");
}
for (JobId job_id: solution.jobs)
file << job_id << " ";
}
private:
/*
* Manipulate solutions
*/
inline void compute(
Solution& solution,
const std::vector<JobId>& jobs)
{
solution.jobs = jobs;
std::fill(times_.begin(), times_.end(), 0);
for (JobId job_id: solution.jobs) {
times_[0] = times_[0] + instance_.processing_time(job_id, 0);
for (MachineId machine_id = 1;
machine_id < instance_.number_of_machines();
++machine_id) {
if (times_[machine_id - 1] > times_[machine_id]) {
times_[machine_id] = times_[machine_id - 1]
+ instance_.processing_time(job_id, machine_id);
} else {
times_[machine_id] = times_[machine_id]
+ instance_.processing_time(job_id, machine_id);
}
}
}
solution.makespan = times_[instance_.number_of_machines() - 1];
}
/*
* Evaluate moves
*/
inline void compute_structures(
const Solution& solution,
JobPos pos,
JobPos size)
{
// Compute heads_.
for (JobPos pos_new = 0; pos_new < (JobPos)solution.jobs.size() - size; ++pos_new) {
JobId job_id = solution.jobs[((pos_new < pos)? pos_new: pos_new + size)];
heads_[pos_new + 1][0] = heads_[pos_new][0]
+ instance_.processing_time(job_id, 0);
for (MachineId machine_id = 1;
machine_id < instance_.number_of_machines();
++machine_id) {
if (heads_[pos_new + 1][machine_id - 1] > heads_[pos_new][machine_id]) {
heads_[pos_new + 1][machine_id] = heads_[pos_new + 1][machine_id - 1]
+ instance_.processing_time(job_id, machine_id);
} else {
heads_[pos_new + 1][machine_id] = heads_[pos_new][machine_id]
+ instance_.processing_time(job_id, machine_id);
}
}
}
// Compute completion_times_.
for (JobPos pos_new = 0; pos_new <= (JobPos)solution.jobs.size() - size; ++pos_new) {
for (MachineId machine_id = 0;
machine_id < instance_.number_of_machines();
++machine_id) {
completion_times_[pos_new][machine_id] = heads_[pos_new][machine_id];
}
for (JobPos pos_0 = pos; pos_0 < pos + size; ++pos_0) {
JobId job_id_0 = solution.jobs[pos_0];
completion_times_[pos_new][0] = completion_times_[pos_new][0]
+ instance_.processing_time(job_id_0, 0);
for (MachineId machine_id = 1;
machine_id < instance_.number_of_machines();
++machine_id) {
if (completion_times_[pos_new][machine_id] > completion_times_[pos_new][machine_id - 1]) {
completion_times_[pos_new][machine_id] = completion_times_[pos_new][machine_id]
+ instance_.processing_time(job_id_0, machine_id);
} else {
completion_times_[pos_new][machine_id] = completion_times_[pos_new][machine_id - 1]
+ instance_.processing_time(job_id_0, machine_id);
}
}
}
}
// Update tails_.
for (MachineId machine_id = instance_.number_of_machines() - 1;
machine_id >= 0;
--machine_id) {
tails_[solution.jobs.size() - size][machine_id] = 0;
}
for (JobPos pos_new = solution.jobs.size() - size - 1; pos_new >= 0; --pos_new) {
JobId job_id = solution.jobs[((pos_new < pos)? pos_new: pos_new + size)];
assert(job_id >= 0);
assert(job_id < instance_.number_of_jobs());
tails_[pos_new][instance_.number_of_machines() - 1]
= tails_[pos_new + 1][instance_.number_of_machines() - 1]
+ instance_.processing_time(job_id, instance_.number_of_machines() - 1);
for (MachineId machine_id = instance_.number_of_machines() - 2;
machine_id >= 0;
--machine_id) {
if (tails_[pos_new][machine_id + 1] > tails_[pos_new + 1][machine_id]) {
tails_[pos_new][machine_id] = tails_[pos_new][machine_id + 1]
+ instance_.processing_time(job_id, machine_id);
} else {
tails_[pos_new][machine_id] = tails_[pos_new + 1][machine_id]
+ instance_.processing_time(job_id, machine_id);
}
}
}
}
/*
* Private attributes
*/
/** Instance. */
const Instance& instance_;
/** Parmaeters. */
Parameters parameters_;
std::vector<JobPos> positions1_;
std::vector<JobPos> positions2_;
std::vector<Time> times_;
std::vector<std::vector<Time>> heads_;
std::vector<std::vector<Time>> tails_;
std::vector<std::vector<Time>> completion_times_;
};
}
}