-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathjob-shop.cpp
executable file
·6437 lines (6351 loc) · 584 KB
/
job-shop.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
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2019
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace Gecode;
/*
* The paper uses ideas from: D. Grimes and E. Hebrard, Solving Variants
* of the Job Shop Scheduling Problem through Conflict-Directed Search,
* INFORMS Jounral of Computing, Volume 27, Issue 2, 2015.
*
* Warning: this solution is a sketch and not competitive as not all
* techniques from the paper have been implemented.
*
*/
/// Default configuration settings
namespace JobShopConfig {
/// Whether to print verbose information
static const bool verbose = false;
/// How many probes to perform
static const unsigned int probes = 50U;
/// How many failures maximal per probe
static const unsigned int fail_probe = 10000U;
/// How much time to spend on probing
static const unsigned int time_probe = 30U * 1000U;
/// How much time to spend on adjusting
static const unsigned int time_adjust = 30U * 1000U;
/// How much time to spend on solving
static const unsigned int time_solve = 60U * 1000U;
/// Restart scale factor
static const double restart_scale = 5000.0;
/// Restart base
static const double restart_base = 1.3;
}
// Instance data
namespace {
// Instances
extern const int* js[];
// Instance names
extern const char* name[];
/// A wrapper class for instance data
class Spec {
protected:
/// Raw instance data
const int* data;
/// Lower and upper bound
int l, u;
/// Name
const char* n;
public:
/// Whether a valid specification has been found
bool valid(void) const {
return data != nullptr;
}
/// Return number of jobs
int jobs(void) const {
return data[0];
}
/// Return number of machines
int machines(void) const {
return data[1];
}
/// Return machine of step \a j in job \a i
int machine(int i, int j) const {
return data[2 + i*machines()*2 + j*2];
}
/// Return duration of step \a j in job \a i
int duration(int i, int j) const {
return data[2 + i*machines()*2 + j*2 + 1];
}
protected:
/// Find instance by name \a s
static const int* find(const char* s) {
for (int i=0; ::name[i] != nullptr; i++)
if (!strcmp(s,::name[i]))
return js[i];
return nullptr;
}
/// Compute lower bound
int clower(void) const {
int l = 0;
Region r;
int* mach = r.alloc<int>(machines());
for (int j=0; j<machines(); j++)
mach[j]=0;
for (int i=0; i<jobs(); i++) {
int job = 0;
for (int j=0; j<machines(); j++) {
mach[machine(i,j)] += duration(i,j);
job += duration(i,j);
}
l = std::max(l,job);
}
for (int j=0; j<machines(); j++)
l = std::max(l,mach[j]);
return l;
}
/// Compute naive upper bound
int cupper(void) const {
int u = 0;
for (int i=0; i<jobs(); i++)
for (int j=0; j<machines(); j++)
u += duration(i,j);
return u;
}
public:
/// Initialize
Spec(const char* s) : data(find(s)), l(0), u(0), n(s) {
if (valid()) {
l = clower(); u = cupper();
}
}
/// Return lower bound
int lower(void) const {
return l;
}
/// Return upper bound
int upper(void) const {
return u;
}
/// Return name
const char* name(void) const {
return n;
}
};
}
/**
* \brief %Options for %JobShop problems
*
*/
class JobShopOptions : public InstanceOptions {
private:
/// Whether to print schedule
Driver::BoolOption _verbose;
/// How many probes
Driver::UnsignedIntOption _probes;
/// How many failures per probe
Driver::UnsignedIntOption _fail_probe;
/// Time for probing
Driver::UnsignedIntOption _time_probe;
/// Time for adjusting
Driver::UnsignedIntOption _time_adjust;
/// Time for solving
Driver::UnsignedIntOption _time_solve;
/// Tie-breaking factor
Driver::DoubleOption _tbf;
public:
/// Initialize options for example with name \a s
JobShopOptions(const char* s)
: InstanceOptions(s),
_verbose("verbose","whether to print schedule",
JobShopConfig::verbose),
_probes("probes","how many probes to perform",
JobShopConfig::probes),
_fail_probe("fail-probe","failure limit per probe",
JobShopConfig::fail_probe),
_time_probe("time-probe","time-out for probing (in milliseconds)",
JobShopConfig::time_probe),
_time_adjust("time-adjust","time-out for adjusting (in milliseconds)",
JobShopConfig::time_adjust),
_time_solve("time-solve","time-out for solving (in milliseconds)",
JobShopConfig::time_solve),
_tbf("tbf", "tie-breaking factor", 0.0) {
add(_verbose);
add(_probes);
add(_fail_probe);
add(_time_probe);
add(_time_adjust);
add(_time_solve);
add(_tbf);
}
/// Return whether to print schedule
bool verbose(void) const {
return _verbose.value();
}
/// Return number of probes
unsigned int probes(void) const {
return _probes.value();
}
/// Return number of failures per probe
unsigned int fail_probe(void) const {
return _fail_probe.value();
}
/// Return time-out for probe
unsigned int time_probe(void) const {
return _time_probe.value();
}
/// Return time-out for adjust
unsigned int time_adjust(void) const {
return _time_adjust.value();
}
/// Return time-out for solve
unsigned int time_solve(void) const {
return _time_solve.value();
}
/// Return tie-breaking factor
double tbf(void) const {
return _tbf.value();
}
/// Print help text for list of instances
virtual void help(void) {
InstanceOptions::help();
std::cerr << "\tAvailable instances:" << std::endl << "\t\t";
for (int i=1; ::name[i] != nullptr; i++) {
std::cerr << ::name[i] << ", ";
if (i % 4 == 0)
std::cerr << std::endl << "\t\t";
}
std::cerr << std::endl;
}
};
/**
* \brief %Example: Job Shop Scheduling
*
* \ingroup Example
*
*/
class JobShopBase : public IntMinimizeScript {
protected:
/// Options
const JobShopOptions& opt;
/// Specification
const Spec spec;
/// Start times for each step in a job
IntVarArray start;
/// Makespan
IntVar makespan;
public:
/// Actual model
JobShopBase(const JobShopOptions& o)
: IntMinimizeScript(o), opt(o), spec(opt.instance()),
start(*this, spec.machines() * spec.jobs(), 0, spec.upper()),
makespan(*this, spec.lower(), spec.upper()) {
// Number of jobs and machines/steps
int n = spec.jobs(), m = spec.machines();
// Endtimes for each job
IntVarArgs end(*this, n, 0, spec.upper());
// Precedence constraints and makespan
for (int i=0; i<n; i++) {
for (int j=1; j<m; j++)
rel(*this, start[i*m+j-1] + spec.duration(i,j) <= start[i*m+j]);
rel(*this, start[i*m+m-1] + spec.duration(i,m-1) <= makespan);
}
}
/// Do not overload machines
void nooverload(void) {
// Number of jobs and machines/steps
int n = spec.jobs(), m = spec.machines();
IntVarArgs jobs(m*n);
IntArgs dur(m*n);
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) {
jobs[spec.machine(i,j)*n+i] = start[i*m+j];
dur[spec.machine(i,j)*n+i] = spec.duration(i,j);
}
for (int j=0; j<m; j++) {
IntVarArgs jpm(n);
IntArgs dpm(n);
for (int i=0; i<n; i++) {
jpm[i] = jobs[j*n+i]; dpm[i] = dur[j*n+i];
}
unary(*this, jpm, dpm);
}
}
/// Return cost
virtual IntVar cost(void) const {
return makespan;
}
/// Constructor for cloning \a s
JobShopBase(JobShopBase& s)
: IntMinimizeScript(s), opt(s.opt), spec(s.spec) {
start.update(*this, s.start);
makespan.update(*this, s.makespan);
}
/// Print solution
virtual void
print(std::ostream& os) const {
os << "\t\t" << spec.name()
<< " [makespan: " << makespan << "]" << std::endl;
if (opt.verbose()) {
// Number of jobs
int n = spec.jobs();
// Number of machines/steps
int m = spec.machines();
for (int i=0; i<n; i++) {
os << "\t\t\t[" << i << "]: ";
for (int j=0; j<m; j++)
os << start[i*m+j] << " ";
os << std::endl;
}
}
}
};
/// Common command line options
class CommonOptions : public Search::Options {
public:
/// Initialize
CommonOptions(const JobShopOptions& opt) {
clone = false;
threads = opt.threads();
c_d = opt.c_d();
a_d = opt.a_d();
nogoods_limit = opt.nogoods() ? opt.nogoods_limit() : 0U;
}
};
/// Model for probing
class JobShopProbe : public JobShopBase {
public:
/// Actual model
JobShopProbe(const JobShopOptions& o)
: JobShopBase(o) {
nooverload();
}
void branch(unsigned int p, Rnd r) {
switch (p) {
case 0U:
Gecode::branch(*this, start, INT_VAR_MIN_MIN(), INT_VAL_MIN());
break;
case 1U:
Gecode::branch(*this, start, INT_VAR_MAX_MIN(), INT_VAL_MIN());
break;
case 2U:
Gecode::branch(*this, start, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
break;
case 3U:
Gecode::branch(*this, start, tiebreak(INT_VAR_MIN_MIN(),
INT_VAR_RND(r)), INT_VAL_MIN());
break;
case 4U:
Gecode::branch(*this, start, tiebreak(INT_VAR_MAX_MIN(),
INT_VAR_RND(r)), INT_VAL_MIN());
break;
default:
if (p & 1U)
Gecode::branch(*this, start, INT_VAR_RND(r), INT_VAL_MIN());
else
Gecode::branch(*this, start, INT_VAR_RND(r), INT_VAL_SPLIT_MIN());
break;
}
assign(*this, makespan, INT_ASSIGN_MIN());
}
/// Constructor for cloning \a s
JobShopProbe(JobShopProbe& s)
: JobShopBase(s) {}
/// Copy during cloning
virtual Space*
copy(void) {
return new JobShopProbe(*this);
}
};
/// Model for solving
class JobShopSolve : public JobShopBase {
protected:
/// Step order variables
BoolVarArray sorder;
/// Record which step is first in order
IntSharedArray fst;
/// Record which step is second in order
IntSharedArray snd;
/// AFC information
IntAFC iafc;
/// AFC-based cost
double afc(BoolVar x, int i) const {
return ((x.afc() + start[fst[i]].afc() + start[snd[i]].afc()) /
(start[fst[i]].size() + start[fst[i]].size()));
}
/// Trampoline function for AFC-based cost
static double afcmerit(const Space& home, BoolVar x, int i) {
return static_cast<const JobShopSolve&>(home).afc(x,i);
}
/// Action information
IntAction iaction; BoolAction baction;
/// Action-based cost
double action(int i) const {
return ((baction[i] + iaction[fst[i]] + iaction[snd[i]]) /
(start[fst[i]].size() + start[fst[i]].size()));
}
/// Trampoline function for Action-based cost
static double actionmerit(const Space& home, BoolVar, int i) {
return static_cast<const JobShopSolve&>(home).action(i);
}
/// CHB information
IntCHB ichb; BoolCHB bchb;
/// CHB-based cost
double chb(int i) const {
return ((bchb[i] + ichb[fst[i]] + ichb[snd[i]]) /
(start[fst[i]].size() + start[fst[i]].size()));
}
/// Trampoline function for CHB-based cost
static double chbmerit(const Space& home, BoolVar, int i) {
return static_cast<const JobShopSolve&>(home).chb(i);
}
/// Random number generator for probing and relaxation
Rnd rnd;
public:
/// Branching to use
enum {
BRANCH_AFC, ///< Branch using AFC
BRANCH_ACTION, ///< Branch using action
BRANCH_CHB ///< Branch using CHB
};
/// Propagation to use
enum {
PROP_ORDER, ///< Only propagate order constraints
PROP_UNARY ///< Also post unary constraints
};
/// Actual model
JobShopSolve(const JobShopOptions& o)
: JobShopBase(o),
sorder(*this, spec.machines()*spec.jobs()*(spec.jobs()-1)/2, 0, 1),
rnd(o.seed()) {
if (opt.propagation() == PROP_UNARY)
nooverload();
// Number of jobs and machines/steps
int n = spec.jobs(), m = spec.machines();
fst.init(m*n*(n-1)/2);
snd.init(m*n*(n-1)/2);
IntArgs jobs(m*n), dur(m*n);
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) {
jobs[spec.machine(i,j)*n+i] = i*m+j;
dur[spec.machine(i,j)*n+i] = spec.duration(i,j);
}
int l=0;
for (int j=0; j<m; j++) {
for (int i1=0; i1<n; i1++)
for (int i2=i1+1; i2<n; i2++) {
if (dur[j*n+i1] > dur[j*n+i2]) {
order(*this,
start[jobs[j*n+i1]], dur[j*n+i1],
start[jobs[j*n+i2]], dur[j*n+i2],
sorder[l]);
fst[l] = j*n+i1; snd[l] = j*n+i2;
} else {
order(*this,
start[jobs[j*n+i2]], dur[j*n+i2],
start[jobs[j*n+i1]], dur[j*n+i1],
sorder[l]);
fst[l] = j*n+i2; snd[l] = j*n+i1;
}
l++;
}
assert(l == (j+1)*n*(n-1)/2);
}
double tbf = opt.tbf();
switch (opt.branching()) {
case BRANCH_AFC:
iafc.init(*this,start,opt.decay());
if (tbf > 0.0) {
auto tbl =
[tbf] (const Space&, double w, double b) {
assert(b >= w);
return b - (b - w) * tbf;
};
branch(*this, sorder, tiebreak(BOOL_VAR_MERIT_MAX(&afcmerit,tbl),
BOOL_VAR_RND(rnd)),
BOOL_VAL_MIN());
} else {
branch(*this, sorder, BOOL_VAR_MERIT_MAX(&afcmerit),
BOOL_VAL_MIN());
}
break;
case BRANCH_ACTION:
iaction.init(*this,start,opt.decay());
baction.init(*this,sorder,opt.decay());
if (tbf > 0.0) {
auto tbl =
[tbf] (const Space&, double w, double b) {
assert(b >= w);
return b - (b - w) * tbf;
};
branch(*this, sorder, tiebreak(BOOL_VAR_MERIT_MAX(&actionmerit,tbl),
BOOL_VAR_RND(rnd)),
BOOL_VAL_MIN());
} else {
branch(*this, sorder, BOOL_VAR_MERIT_MAX(&actionmerit),
BOOL_VAL_MIN());
}
break;
case BRANCH_CHB:
ichb.init(*this,start);
bchb.init(*this,sorder);
if (tbf > 0.0) {
auto tbl =
[tbf] (const Space&, double w, double b) {
assert(b >= w);
return b - (b - w) * tbf;
};
branch(*this, sorder, tiebreak(BOOL_VAR_MERIT_MAX(&chbmerit,tbl),
BOOL_VAR_RND(rnd)),
BOOL_VAL_MIN());
} else {
branch(*this, sorder, BOOL_VAR_MERIT_MAX(&chbmerit),
BOOL_VAL_MIN());
}
break;
}
assign(*this, start, INT_VAR_MIN_MIN(), INT_ASSIGN_MIN());
assign(*this, makespan, INT_ASSIGN_MIN());
}
/// Constructor for cloning \a s
JobShopSolve(JobShopSolve& s)
: JobShopBase(s), sorder(s.sorder), fst(s.fst), snd(s.snd),
iafc(s.iafc), iaction(s.iaction), baction(s.baction),
ichb(s.ichb), bchb(s.bchb), rnd(s.rnd) {}
/// Copy during cloning
virtual Space*
copy(void) {
return new JobShopSolve(*this);
}
};
/// Stop object combining time and failuresa
class FailTimeStop : public Search::Stop {
protected:
Search::FailStop* fs; ///< Used fail stop object
Search::TimeStop* ts; ///< Used time stop object
public:
/// Initialize stop object
FailTimeStop(unsigned int fail, unsigned int time)
: fs(new Search::FailStop(fail)),
ts(new Search::TimeStop(time)) {}
/// Test whether search must be stopped
virtual bool stop(const Search::Statistics& s, const Search::Options& o) {
return fs->stop(s,o) || ts->stop(s,o);
}
/// Whether the stop was due to failures
bool fail(const Search::Statistics& s, const Search::Options& o) const {
return fs->stop(s,o);
}
/// Whether the stop was due to time
bool time(const Search::Statistics& s, const Search::Options& o) const {
return ts->stop(s,o);
}
/// Destructor
~FailTimeStop(void) {
delete fs; delete ts;
}
};
/// Print statistics
void
print(const Search::Statistics& stat, bool restart) {
using namespace std;
cout << "\t\t\tnodes: " << stat.node << endl
<< "\t\t\tfailures: " << stat.fail << endl;
if (restart)
cout << "\t\t\trestarts: " << stat.restart << endl
<< "\t\t\tno-goods: " << stat.nogood << endl;
cout << "\t\t\tpeak depth: " << stat.depth << endl;
}
/// Solver
void
solve(const JobShopOptions& opt) {
Rnd rnd(opt.seed());
/*
* Invariant:
* - There is a solution with makespan u,
* - There is no solution with makespan l
*/
int l, u;
{
Support::Timer t; t.start();
Search::Statistics stat;
JobShopProbe* master = new JobShopProbe(opt);
if (master->status() != SS_SOLVED) {
delete master;
std::cerr << "Error: has no solution..." << std::endl;
return;
}
l = master->cost().min()-1;
u = master->cost().max();
FailTimeStop fts(opt.fail_probe(),opt.time_probe());
CommonOptions so(opt);
so.stop = &fts;
bool stopped = false;
std::cout << "\tProbing..." << std::endl;
for (unsigned int p=0; p<opt.probes(); p++) {
JobShopProbe* jsp = static_cast<JobShopProbe*>(master->clone());
jsp->branch(p,rnd);
DFS<JobShopProbe> dfs(jsp,so);
JobShopProbe* s = dfs.next();
Search::Statistics statj = dfs.statistics();
if (s != nullptr) {
if (u > s->cost().val()) {
u = s->cost().val();
s->print(std::cout);
}
delete s;
} else if (fts.time(statj,so)) {
stopped = true;
break;
}
stat += statj;
}
delete master;
print(stat,false);
std::cout << "\t\t\truntime: ";
Driver::stop(t,std::cout);
std::cout << std::endl;
if (stopped) {
std::cout << "\t\t\t\tstopped due to time-out..." << std::endl;
}
}
std::cout << std::endl << "\tAdjusting..." << std::endl;
// Dictotomic search
{
JobShopSolve* master = new JobShopSolve(opt);
if (master->status() == SS_FAILED) {
delete master;
std::cerr << "Error: has no solution..." << std::endl;
return;
}
{
Support::Timer t; t.start();
Search::Statistics stat;
CommonOptions so(opt);
so.stop = Search::Stop::time(opt.time_adjust());
bool stopped = false;
while (l < u-1) {
std::cout << "\t\tBounds: [" << l << "," << u << "]"
<< std::endl;
JobShopSolve* jss = static_cast<JobShopSolve*>(master->clone());
int m = (l + u) / 2;
rel(*jss, jss->cost() >= l);
rel(*jss, jss->cost() <= m);
so.cutoff = Search::Cutoff::geometric(JobShopConfig::restart_scale,
JobShopConfig::restart_base);
RBS<JobShopSolve,DFS> rbs(jss,so);
JobShopSolve* s = rbs.next();
stat += rbs.statistics();
if (s != nullptr) {
s->print(std::cout);
u = s->cost().val();
delete s;
} else if (rbs.stopped()) {
stopped = true;
break;
} else {
l = m+1;
}
}
print(stat,true);
std::cout << "\t\t\truntime: ";
Driver::stop(t,std::cout);
std::cout << std::endl;
if (stopped) {
std::cout << "\t\t\t\tstopped due to time-out..." << std::endl;
}
}
if (l == u-1) {
delete master;
std::cout << std::endl
<< "\tFound best solution and proved optimality."
<< std::endl;
return;
}
{
Support::Timer t; t.start();
std::cout << std::endl << "\tSolving..." << std::endl;
rel(*master, master->cost() >= l);
rel(*master, master->cost() < u);
CommonOptions so(opt);
so.stop = Search::Stop::time(opt.time_solve());
so.cutoff = Search::Cutoff::geometric(JobShopConfig::restart_scale,
JobShopConfig::restart_base);
RBS<JobShopSolve,BAB> rbs(master,so);
while (JobShopSolve* s = rbs.next()) {
s->print(std::cout);
u = s->cost().val();
delete s;
}
print(rbs.statistics(),true);
std::cout << "\t\t\truntime: ";
Driver::stop(t,std::cout);
std::cout << std::endl;
if (rbs.stopped()) {
std::cout << "\t\t\t\tstopped due to time-out..." << std::endl;
std::cout << std::endl
<< "\tSolution at most ";
double a = (static_cast<double>(u-l+1) / u) * 100.0;
std::cout << std::setprecision(2) << a
<< "% away from optimum."
<< std::endl;
} else {
std::cout << std::endl
<< "\tFound best solution and proved optimality."
<< std::endl;
}
}
}
}
/** \brief Main-function
* \relates JobShop
*/
int
main(int argc, char* argv[]) {
JobShopOptions opt("JobShop");
opt.branching(JobShopSolve::BRANCH_AFC);
opt.branching(JobShopSolve::BRANCH_AFC, "afc");
opt.branching(JobShopSolve::BRANCH_ACTION, "action");
opt.branching(JobShopSolve::BRANCH_CHB, "chb");
opt.propagation(JobShopSolve::PROP_UNARY);
opt.propagation(JobShopSolve::PROP_ORDER,"order");
opt.propagation(JobShopSolve::PROP_UNARY,"unary");
opt.instance("ft06");
opt.restart_base(JobShopConfig::restart_base);
opt.restart_scale(JobShopConfig::restart_scale);
opt.nogoods(true);
opt.parse(argc,argv);
if (!Spec(opt.instance()).valid()) {
std::cerr << "Error: unkown instance" << std::endl;
return 1;
}
solve(opt);
return 0;
}
namespace {
// Test instance
const int test[] = {
4, 6, // Number of jobs and machines
0,1, 1,2, 2,3, 3,4, 4,2, 5,1,
5,4, 1,3, 2,2, 4,1, 0,1, 3,2,
5,1, 4,2, 1,3, 0,4, 2,2, 3,2,
5,4, 4,3, 1,2, 0,1, 3,4, 2,3
};
/*
* These instances are contributed to the OR-Library by
* Dirk C. Mattfeld (email dirk@uni-bremen.de) and
* Rob J.M. Vaessens (email robv@win.tue.nl).
*
* o abz5-abz9 are from
* J. Adams, E. Balas and D. Zawack (1988),
* The shifting bottleneck procedure for job shop scheduling,
* Management Science 34, 391-401.
* o ft06, ft10, and ft20 are from
* H. Fisher, G.L. Thompson (1963),
* Probabilistic learning combinations of local job-shop scheduling rules,
* J.F. Muth, G.L. Thompson (eds.),
* Industrial Scheduling,
* Prentice Hall, Englewood Cliffs, New Jersey,
* 225-251.
* o la01-la40 are from
* S. Lawrence (1984),
* Resource constrained project scheduling: an experimental investigation of
* heuristic scheduling techniques (Supplement),
* Graduate School of Industrial Administration,
* Carnegie-Mellon University, Pittsburgh, Pennsylvania.
* o orb01-orb10 are from
* D. Applegate, W. Cook (1991),
* A computational study of the job-shop scheduling instance,
* ORSA Journal on Computing 3, 149-156.
* (they were generated in Bonn in 1986)
* o swv01-swv20 are from
* R.H. Storer, S.D. Wu, R. Vaccari (1992),
* New search spaces for sequencing instances with application to job shop
* scheduling,
* Management Science 38, 1495-1509.
* o yn1-yn4 are from
* T. Yamada, R. Nakano (1992),
* A genetic algorithm applicable to large-scale job-shop instances,
* R. Manner, B. Manderick (eds.),
* Parallel instance solving from nature 2,
* North-Holland, Amsterdam,
* 281-290.
*
* The following are from E. Taillard, "Benchmarks for basic scheduling problems",
* EJOR 64(2):278-285, 1993.
* o taillard-n-m-i: n jobs, m machines, i-th instance
* the machine numbers are decremented by one to start with machine 0.
*
*/
// Adams, Balas, and Zawack 10x10 instance (Table 1, instance 5)
const int abz5[] = {
10, 10, // Number of jobs and machines
4, 88, 8, 68, 6, 94, 5, 99, 1, 67, 2, 89, 9, 77, 7, 99, 0, 86, 3, 92,
5, 72, 3, 50, 6, 69, 4, 75, 2, 94, 8, 66, 0, 92, 1, 82, 7, 94, 9, 63,
9, 83, 8, 61, 0, 83, 1, 65, 6, 64, 5, 85, 7, 78, 4, 85, 2, 55, 3, 77,
7, 94, 2, 68, 1, 61, 4, 99, 3, 54, 6, 75, 5, 66, 0, 76, 9, 63, 8, 67,
3, 69, 4, 88, 9, 82, 8, 95, 0, 99, 2, 67, 6, 95, 5, 68, 7, 67, 1, 86,
1, 99, 4, 81, 5, 64, 6, 66, 8, 80, 2, 80, 7, 69, 9, 62, 3, 79, 0, 88,
7, 50, 1, 86, 4, 97, 3, 96, 0, 95, 8, 97, 2, 66, 5, 99, 6, 52, 9, 71,
4, 98, 6, 73, 3, 82, 2, 51, 1, 71, 5, 94, 7, 85, 0, 62, 8, 95, 9, 79,
0, 94, 6, 71, 3, 81, 7, 85, 1, 66, 2, 90, 4, 76, 5, 58, 8, 93, 9, 97,
3, 50, 0, 59, 1, 82, 8, 67, 7, 56, 9, 96, 6, 58, 4, 81, 5, 59, 2, 96
};
// Adams, and Zawack 10x10 instance (Table 1, instance 6)
const int abz6[] = {
10, 10, // Number of jobs and machines
7, 62, 8, 24, 5, 25, 3, 84, 4, 47, 6, 38, 2, 82, 0, 93, 9, 24, 1, 66,
5, 47, 2, 97, 8, 92, 9, 22, 1, 93, 4, 29, 7, 56, 3, 80, 0, 78, 6, 67,
1, 45, 7, 46, 6, 22, 2, 26, 9, 38, 0, 69, 4, 40, 3, 33, 8, 75, 5, 96,
4, 85, 8, 76, 5, 68, 9, 88, 3, 36, 6, 75, 2, 56, 1, 35, 0, 77, 7, 85,
8, 60, 9, 20, 7, 25, 3, 63, 4, 81, 0, 52, 1, 30, 5, 98, 6, 54, 2, 86,
3, 87, 9, 73, 5, 51, 2, 95, 4, 65, 1, 86, 6, 22, 8, 58, 0, 80, 7, 65,
5, 81, 2, 53, 7, 57, 6, 71, 9, 81, 0, 43, 4, 26, 8, 54, 3, 58, 1, 69,
4, 20, 6, 86, 5, 21, 8, 79, 9, 62, 2, 34, 0, 27, 1, 81, 7, 30, 3, 46,
9, 68, 6, 66, 5, 98, 8, 86, 7, 66, 0, 56, 3, 82, 1, 95, 4, 47, 2, 78,
0, 30, 3, 50, 7, 34, 2, 58, 1, 77, 5, 34, 8, 84, 4, 40, 9, 46, 6, 44
};
// Adams, Balas, and Zawack 15 x 20 instance (Table 1, instance 7)
const int abz7[] = {
20, 15, // Number of jobs and machines
2, 24, 3, 12, 9, 17, 4, 27, 0, 21, 6, 25, 8, 27, 7, 26, 1, 30, 5, 31, 11, 18, 14, 16, 13, 39, 10, 19, 12, 26,
6, 30, 3, 15, 12, 20, 11, 19, 1, 24, 13, 15, 10, 28, 2, 36, 5, 26, 7, 15, 0, 11, 8, 23, 14, 20, 9, 26, 4, 28,
6, 35, 0, 22, 13, 23, 7, 32, 2, 20, 3, 12, 12, 19, 10, 23, 9, 17, 1, 14, 5, 16, 11, 29, 8, 16, 4, 22, 14, 22,
9, 20, 6, 29, 1, 19, 7, 14, 12, 33, 4, 30, 0, 32, 5, 21, 11, 29, 10, 24, 14, 25, 2, 29, 3, 13, 8, 20, 13, 18,
11, 23, 13, 20, 1, 28, 6, 32, 7, 16, 5, 18, 8, 24, 9, 23, 3, 24, 10, 34, 2, 24, 0, 24, 14, 28, 12, 15, 4, 18,
8, 24, 11, 19, 14, 21, 1, 33, 7, 34, 6, 35, 5, 40, 10, 36, 3, 23, 2, 26, 4, 15, 9, 28, 13, 38, 12, 13, 0, 25,
13, 27, 3, 30, 6, 21, 8, 19, 12, 12, 4, 27, 2, 39, 9, 13, 14, 12, 5, 36, 10, 21, 11, 17, 1, 29, 0, 17, 7, 33,
5, 27, 4, 19, 6, 29, 9, 20, 3, 21, 10, 40, 8, 14, 14, 39, 13, 39, 2, 27, 1, 36, 12, 12, 11, 37, 7, 22, 0, 13,
13, 32, 11, 29, 8, 24, 3, 27, 5, 40, 4, 21, 9, 26, 0, 27, 14, 27, 6, 16, 2, 21, 10, 13, 7, 28, 12, 28, 1, 32,
12, 35, 1, 11, 5, 39, 14, 18, 7, 23, 0, 34, 3, 24, 13, 11, 8, 30, 11, 31, 4, 15, 10, 15, 2, 28, 9, 26, 6, 33,
10, 28, 5, 37, 12, 29, 1, 31, 7, 25, 8, 13, 14, 14, 4, 20, 3, 27, 9, 25, 13, 31, 11, 14, 6, 25, 2, 39, 0, 36,
0, 22, 11, 25, 5, 28, 13, 35, 4, 31, 8, 21, 9, 20, 14, 19, 2, 29, 7, 32, 10, 18, 1, 18, 3, 11, 12, 17, 6, 15,
12, 39, 5, 32, 2, 36, 8, 14, 3, 28, 13, 37, 0, 38, 6, 20, 7, 19, 11, 12, 14, 22, 1, 36, 4, 15, 9, 32, 10, 16,
8, 28, 1, 29, 14, 40, 12, 23, 4, 34, 5, 33, 6, 27, 10, 17, 0, 20, 7, 28, 11, 21, 2, 21, 13, 20, 9, 33, 3, 27,
9, 21, 14, 34, 3, 30, 12, 38, 0, 11, 11, 16, 2, 14, 5, 14, 1, 34, 8, 33, 4, 23, 13, 40, 10, 12, 6, 23, 7, 27,
9, 13, 14, 40, 7, 36, 4, 17, 0, 13, 5, 33, 8, 25, 13, 24, 10, 23, 3, 36, 2, 29, 1, 18, 11, 13, 6, 33, 12, 13,
3, 25, 5, 15, 2, 28, 12, 40, 7, 39, 1, 31, 8, 35, 6, 31, 11, 36, 4, 12, 10, 33, 14, 19, 9, 16, 13, 27, 0, 21,
12, 22, 10, 14, 0, 12, 2, 20, 5, 12, 1, 18, 11, 17, 8, 39, 14, 31, 3, 31, 7, 32, 9, 20, 13, 29, 4, 13, 6, 26,
5, 18, 10, 30, 7, 38, 14, 22, 13, 15, 11, 20, 9, 16, 3, 17, 1, 12, 2, 13, 12, 40, 6, 17, 8, 30, 4, 38, 0, 13,
9, 31, 8, 39, 12, 27, 1, 14, 5, 33, 3, 31, 11, 22, 13, 36, 0, 16, 7, 11, 14, 14, 4, 29, 6, 28, 2, 22, 10, 17
};
// Adams, Balas, and Zawack 15 x 20 instance (Table 1, instance 8)
const int abz8[] = {
20, 15, // Number of jobs and machines
0, 19, 9, 33, 2, 32, 13, 18, 10, 39, 8, 34, 6, 25, 4, 36, 11, 40, 12, 33, 1, 31, 14, 30, 3, 34, 5, 26, 7, 13,
9, 11, 10, 22, 14, 19, 5, 12, 4, 25, 6, 38, 0, 29, 7, 39, 13, 19, 11, 22, 1, 23, 3, 20, 2, 40, 12, 19, 8, 26,
3, 25, 8, 17, 11, 24, 13, 40, 10, 32, 14, 16, 5, 39, 9, 19, 0, 24, 1, 39, 4, 17, 2, 35, 7, 38, 6, 20, 12, 31,
14, 22, 3, 36, 2, 34, 12, 17, 4, 30, 13, 12, 1, 13, 6, 25, 9, 12, 7, 18, 10, 31, 0, 39, 5, 40, 8, 26, 11, 37,
12, 32, 14, 15, 1, 35, 7, 13, 8, 32, 11, 23, 6, 22, 4, 21, 0, 38, 2, 38, 3, 40, 10, 31, 5, 11, 13, 37, 9, 16,
10, 23, 12, 38, 8, 11, 14, 27, 9, 11, 6, 25, 5, 14, 4, 12, 2, 27, 11, 26, 7, 29, 3, 28, 13, 21, 0, 20, 1, 30,
6, 39, 8, 38, 0, 15, 12, 27, 10, 22, 9, 27, 2, 32, 4, 40, 3, 12, 13, 20, 14, 21, 11, 22, 5, 17, 7, 38, 1, 27,
11, 11, 13, 24, 10, 38, 8, 15, 9, 19, 14, 13, 5, 30, 0, 26, 2, 29, 6, 33, 12, 21, 1, 15, 3, 21, 4, 28, 7, 33,
8, 20, 6, 17, 5, 26, 3, 34, 9, 23, 0, 16, 2, 18, 4, 35, 12, 24, 10, 16, 11, 26, 7, 12, 14, 13, 13, 27, 1, 19,
1, 18, 7, 37, 14, 27, 9, 40, 5, 40, 6, 17, 8, 22, 3, 17, 10, 30, 0, 38, 4, 21, 12, 32, 11, 24, 13, 24, 2, 30,
11, 19, 0, 22, 13, 36, 6, 18, 5, 22, 3, 17, 14, 35, 10, 34, 7, 23, 8, 19, 2, 29, 1, 22, 12, 17, 4, 33, 9, 39,
6, 32, 3, 22, 12, 24, 5, 13, 4, 13, 1, 11, 0, 11, 13, 25, 8, 13, 2, 15, 10, 33, 11, 17, 14, 16, 9, 38, 7, 24,
14, 16, 13, 16, 1, 37, 8, 25, 2, 26, 3, 11, 9, 34, 4, 14, 0, 20, 6, 36, 12, 12, 5, 29, 10, 25, 7, 32, 11, 12,
8, 20, 10, 24, 11, 27, 9, 38, 5, 34, 12, 39, 7, 33, 4, 37, 2, 31, 13, 15, 14, 34, 3, 33, 6, 26, 1, 36, 0, 14,
8, 31, 0, 17, 9, 13, 1, 21, 10, 17, 7, 19, 13, 14, 3, 40, 5, 32, 11, 25, 2, 34, 14, 23, 6, 13, 12, 40, 4, 26,
8, 38, 12, 17, 3, 14, 13, 17, 4, 12, 1, 35, 6, 35, 0, 19, 10, 36, 7, 19, 9, 29, 2, 31, 5, 26, 11, 35, 14, 37,
14, 20, 3, 16, 0, 33, 10, 14, 5, 27, 7, 31, 8, 16, 6, 31, 12, 28, 9, 37, 4, 37, 2, 29, 11, 38, 1, 30, 13, 36,
11, 18, 3, 37, 14, 16, 6, 15, 8, 14, 12, 11, 13, 32, 5, 12, 1, 11, 10, 29, 7, 19, 4, 12, 9, 18, 2, 26, 0, 39,
11, 11, 2, 11, 12, 22, 9, 35, 14, 20, 7, 31, 4, 19, 3, 39, 5, 28, 6, 33, 10, 34, 1, 38, 0, 20, 13, 17, 8, 28,
2, 12, 12, 25, 5, 23, 8, 21, 6, 27, 9, 30, 14, 23, 11, 39, 3, 26, 13, 34, 7, 17, 1, 24, 4, 12, 0, 19, 10, 36
};
// Adams, Balas, and Zawack 15 x 20 instance (Table 1, instance 9)
const int abz9[] = {
20, 15, // Number of jobs and machines
6, 14, 5, 21, 8, 13, 4, 11, 1, 11, 14, 35, 13, 20, 11, 17, 10, 18, 12, 11, 2, 23, 3, 13, 0, 15, 7, 11, 9, 35,
1, 35, 5, 31, 0, 13, 3, 26, 6, 14, 9, 17, 7, 38, 12, 20, 10, 19, 13, 12, 8, 16, 4, 34, 11, 15, 14, 12, 2, 14,
0, 30, 4, 35, 2, 40, 10, 35, 6, 30, 14, 23, 8, 29, 13, 37, 7, 38, 3, 40, 9, 26, 12, 11, 1, 40, 11, 36, 5, 17,
7, 40, 5, 18, 4, 12, 8, 23, 0, 23, 9, 14, 13, 16, 12, 14, 10, 23, 3, 12, 6, 16, 14, 32, 1, 40, 11, 25, 2, 29,
2, 35, 3, 15, 12, 31, 11, 28, 6, 32, 4, 30, 10, 27, 7, 29, 0, 38, 13, 11, 1, 23, 14, 17, 5, 27, 9, 37, 8, 29,
5, 33, 3, 33, 6, 19, 12, 40, 10, 19, 0, 33, 13, 26, 2, 31, 11, 28, 7, 36, 4, 38, 1, 21, 14, 25, 9, 40, 8, 35,
13, 25, 0, 32, 11, 33, 12, 18, 4, 32, 6, 28, 5, 15, 3, 35, 9, 14, 2, 34, 7, 23, 10, 32, 1, 17, 14, 26, 8, 19,
2, 16, 12, 33, 9, 34, 11, 30, 13, 40, 8, 12, 14, 26, 5, 26, 6, 15, 3, 21, 1, 40, 4, 32, 0, 14, 7, 30, 10, 35,
2, 17, 10, 16, 14, 20, 6, 24, 8, 26, 3, 36, 12, 22, 0, 14, 13, 11, 9, 20, 7, 23, 1, 29, 11, 23, 4, 15, 5, 40,
4, 27, 9, 37, 3, 40, 11, 14, 13, 25, 7, 30, 0, 34, 2, 11, 5, 15, 12, 32, 1, 36, 10, 12, 14, 28, 8, 31, 6, 23,
13, 25, 0, 22, 3, 27, 8, 14, 5, 25, 6, 20, 14, 18, 7, 14, 1, 19, 2, 17, 4, 27, 9, 22, 12, 22, 11, 27, 10, 21,
14, 34, 10, 15, 0, 22, 3, 29, 13, 34, 6, 40, 7, 17, 2, 32, 12, 20, 5, 39, 4, 31, 11, 16, 1, 37, 8, 33, 9, 13,
6, 12, 12, 27, 4, 17, 2, 24, 8, 11, 5, 19, 14, 11, 3, 17, 9, 25, 1, 11, 11, 31, 13, 33, 7, 31, 10, 12, 0, 22,
5, 22, 14, 15, 0, 16, 8, 32, 7, 20, 4, 22, 9, 11, 13, 19, 1, 30, 12, 33, 6, 29, 11, 18, 3, 34, 10, 32, 2, 18,
5, 27, 3, 26, 10, 28, 6, 37, 4, 18, 12, 12, 11, 11, 13, 26, 7, 27, 9, 40, 14, 19, 1, 24, 2, 18, 0, 12, 8, 34,
8, 15, 5, 28, 9, 25, 6, 32, 1, 13, 7, 38, 11, 11, 2, 34, 4, 25, 0, 20, 10, 32, 3, 23, 12, 14, 14, 16, 13, 20,
1, 15, 4, 13, 8, 37, 3, 14, 10, 22, 5, 24, 12, 26, 7, 22, 9, 34, 14, 22, 11, 19, 13, 32, 0, 29, 2, 13, 6, 35,
7, 36, 5, 33, 13, 28, 9, 20, 10, 30, 4, 33, 14, 29, 0, 34, 3, 22, 11, 12, 6, 30, 8, 12, 1, 35, 2, 13, 12, 35,
14, 26, 11, 31, 5, 35, 2, 38, 13, 19, 10, 35, 4, 27, 8, 29, 3, 39, 9, 13, 6, 14, 7, 26, 0, 17, 1, 22, 12, 15,
1, 36, 7, 34, 11, 33, 8, 17, 14, 38, 6, 39, 5, 16, 3, 27, 13, 29, 2, 16, 0, 16, 4, 19, 9, 40, 12, 35, 10, 39
};
// Fisher and Thompson 6x6 instance, alternate name (mt06)
const int ft06[] = {
6, 6, // Number of jobs and machines
2, 1, 0, 3, 1, 6, 3, 7, 5, 3, 4, 6,
1, 8, 2, 5, 4, 10, 5, 10, 0, 10, 3, 4,
2, 5, 3, 4, 5, 8, 0, 9, 1, 1, 4, 7,
1, 5, 0, 5, 2, 5, 3, 3, 4, 8, 5, 9,
2, 9, 1, 3, 4, 5, 5, 4, 0, 3, 3, 1,
1, 3, 3, 3, 5, 9, 0, 10, 4, 4, 2, 1
};
// Fisher and Thompson 10x10 instance, alternate name (mt10)
const int ft10[] = {
10, 10, // Number of jobs and machines
0, 29, 1, 78, 2, 9, 3, 36, 4, 49, 5, 11, 6, 62, 7, 56, 8, 44, 9, 21,
0, 43, 2, 90, 4, 75, 9, 11, 3, 69, 1, 28, 6, 46, 5, 46, 7, 72, 8, 30,
1, 91, 0, 85, 3, 39, 2, 74, 8, 90, 5, 10, 7, 12, 6, 89, 9, 45, 4, 33,
1, 81, 2, 95, 0, 71, 4, 99, 6, 9, 8, 52, 7, 85, 3, 98, 9, 22, 5, 43,
2, 14, 0, 6, 1, 22, 5, 61, 3, 26, 4, 69, 8, 21, 7, 49, 9, 72, 6, 53,
2, 84, 1, 2, 5, 52, 3, 95, 8, 48, 9, 72, 0, 47, 6, 65, 4, 6, 7, 25,
1, 46, 0, 37, 3, 61, 2, 13, 6, 32, 5, 21, 9, 32, 8, 89, 7, 30, 4, 55,