|
@@ -5,21 +5,46 @@ import jobshop.Instance;
|
5
|
5
|
import java.util.*;
|
6
|
6
|
import java.util.stream.IntStream;
|
7
|
7
|
|
8
|
|
-public class Schedule {
|
9
|
|
- public final Instance pb;
|
|
8
|
+/** Direct encoding of the solution to JobShop problem.
|
|
9
|
+ *
|
|
10
|
+ * Associates every task to its start time.
|
|
11
|
+ */
|
|
12
|
+public class Schedule extends Encoding {
|
|
13
|
+
|
10
|
14
|
// start times of each job and task
|
11
|
15
|
// times[j][i] is the start time of task (j,i) : i^th task of the j^th job
|
12
|
16
|
final int[][] times;
|
13
|
17
|
|
14
|
18
|
/** Creates a new schedule for the given instance where all start times are uninitialized. */
|
15
|
|
- public Schedule(Instance pb) {
|
16
|
|
- this.pb = pb;
|
17
|
|
- this.times = new int[pb.numJobs][];
|
18
|
|
- for(int j = 0 ; j < pb.numJobs ; j++) {
|
19
|
|
- this.times[j] = new int[pb.numTasks];
|
|
19
|
+ public Schedule(Instance instance) {
|
|
20
|
+ super(instance);
|
|
21
|
+ this.times = new int[instance.numJobs][];
|
|
22
|
+ for(int j = 0; j < instance.numJobs ; j++) {
|
|
23
|
+ this.times[j] = new int[instance.numTasks];
|
20
|
24
|
}
|
21
|
25
|
}
|
22
|
26
|
|
|
27
|
+
|
|
28
|
+ /** Start time of the given task. */
|
|
29
|
+ public int startTime(int job, int task) {
|
|
30
|
+ return times[job][task];
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ /** Start time of the given task. */
|
|
34
|
+ public int startTime(Task task) {
|
|
35
|
+ return startTime(task.job, task.task);
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ /** End time of the given task. */
|
|
39
|
+ public int endTime(int job, int task) {
|
|
40
|
+ return startTime(job, task) + instance.duration(job, task);
|
|
41
|
+ }
|
|
42
|
+
|
|
43
|
+ /** End time of the given task. */
|
|
44
|
+ public int endTime(Task task) {
|
|
45
|
+ return endTime(task.job, task.task);
|
|
46
|
+ }
|
|
47
|
+
|
23
|
48
|
/** Sets the start time of the given task. */
|
24
|
49
|
public void setStartTime(int job, int task, int startTime) {
|
25
|
50
|
times[job][task] = startTime;
|
|
@@ -27,25 +52,25 @@ public class Schedule {
|
27
|
52
|
|
28
|
53
|
/** Returns true if this schedule is valid (no constraint is violated) */
|
29
|
54
|
public boolean isValid() {
|
30
|
|
- for(int j = 0 ; j<pb.numJobs ; j++) {
|
31
|
|
- for(int t = 1 ; t<pb.numTasks ; t++) {
|
32
|
|
- if(startTime(j, t-1) + pb.duration(j, t-1) > startTime(j, t))
|
|
55
|
+ for(int j = 0; j<instance.numJobs ; j++) {
|
|
56
|
+ for(int t = 1; t< instance.numTasks ; t++) {
|
|
57
|
+ if(startTime(j, t-1) + instance.duration(j, t-1) > startTime(j, t))
|
33
|
58
|
return false;
|
34
|
59
|
}
|
35
|
|
- for(int t = 0 ; t<pb.numTasks ; t++) {
|
|
60
|
+ for(int t = 0; t< instance.numTasks ; t++) {
|
36
|
61
|
if(startTime(j, t) < 0)
|
37
|
62
|
return false;
|
38
|
63
|
}
|
39
|
64
|
}
|
40
|
65
|
|
41
|
|
- for (int machine = 0 ; machine < pb.numMachines ; machine++) {
|
42
|
|
- for(int j1=0 ; j1<pb.numJobs ; j1++) {
|
43
|
|
- int t1 = pb.task_with_machine(j1, machine);
|
44
|
|
- for(int j2=j1+1 ; j2<pb.numJobs ; j2++) {
|
45
|
|
- int t2 = pb.task_with_machine(j2, machine);
|
|
66
|
+ for (int machine = 0; machine < instance.numMachines ; machine++) {
|
|
67
|
+ for(int j1 = 0; j1< instance.numJobs ; j1++) {
|
|
68
|
+ int t1 = instance.task_with_machine(j1, machine);
|
|
69
|
+ for(int j2 = j1+1; j2< instance.numJobs ; j2++) {
|
|
70
|
+ int t2 = instance.task_with_machine(j2, machine);
|
46
|
71
|
|
47
|
|
- boolean t1_first = startTime(j1, t1) + pb.duration(j1, t1) <= startTime(j2, t2);
|
48
|
|
- boolean t2_first = startTime(j2, t2) + pb.duration(j2, t2) <= startTime(j1, t1);
|
|
72
|
+ boolean t1_first = startTime(j1, t1) + instance.duration(j1, t1) <= startTime(j2, t2);
|
|
73
|
+ boolean t2_first = startTime(j2, t2) + instance.duration(j2, t2) <= startTime(j1, t1);
|
49
|
74
|
|
50
|
75
|
if(!t1_first && !t2_first)
|
51
|
76
|
return false;
|
|
@@ -61,32 +86,12 @@ public class Schedule {
|
61
|
86
|
*/
|
62
|
87
|
public int makespan() {
|
63
|
88
|
int max = -1;
|
64
|
|
- for(int j = 0 ; j<pb.numJobs ; j++) {
|
65
|
|
- max = Math.max(max, endTime(j, pb.numTasks-1));
|
|
89
|
+ for(int j = 0; j< instance.numJobs ; j++) {
|
|
90
|
+ max = Math.max(max, endTime(j, instance.numTasks-1));
|
66
|
91
|
}
|
67
|
92
|
return max;
|
68
|
93
|
}
|
69
|
94
|
|
70
|
|
- /** Start time of the given task. */
|
71
|
|
- public int startTime(int job, int task) {
|
72
|
|
- return times[job][task];
|
73
|
|
- }
|
74
|
|
-
|
75
|
|
- /** Start time of the given task. */
|
76
|
|
- public int startTime(Task task) {
|
77
|
|
- return startTime(task.job, task.task);
|
78
|
|
- }
|
79
|
|
-
|
80
|
|
- /** End time of the given task. */
|
81
|
|
- public int endTime(int job, int task) {
|
82
|
|
- return startTime(job, task) + pb.duration(job, task);
|
83
|
|
- }
|
84
|
|
-
|
85
|
|
- /** End time of the given task. */
|
86
|
|
- public int endTime(Task task) {
|
87
|
|
- return endTime(task.job, task.task);
|
88
|
|
- }
|
89
|
|
-
|
90
|
95
|
/** Returns true if the given sequence of task is a critical path of the schedule. */
|
91
|
96
|
public boolean isCriticalPath(List<Task> path) {
|
92
|
97
|
if(startTime(path.get(0)) != 0) {
|
|
@@ -108,8 +113,8 @@ public class Schedule {
|
108
|
113
|
*/
|
109
|
114
|
public List<Task> criticalPath() {
|
110
|
115
|
// select task with greatest end time
|
111
|
|
- Task ldd = IntStream.range(0, pb.numJobs)
|
112
|
|
- .mapToObj(j -> new Task(j, pb.numTasks-1))
|
|
116
|
+ Task ldd = IntStream.range(0, instance.numJobs)
|
|
117
|
+ .mapToObj(j -> new Task(j, instance.numTasks-1))
|
113
|
118
|
.max(Comparator.comparing(this::endTime))
|
114
|
119
|
.get();
|
115
|
120
|
assert endTime(ldd) == makespan();
|
|
@@ -124,7 +129,7 @@ public class Schedule {
|
124
|
129
|
// starts a time 0
|
125
|
130
|
while(startTime(path.getFirst()) != 0) {
|
126
|
131
|
Task cur = path.getFirst();
|
127
|
|
- int machine = pb.machine(cur.job, cur.task);
|
|
132
|
+ int machine = instance.machine(cur.job, cur.task);
|
128
|
133
|
|
129
|
134
|
// will contain the task that was delaying the start
|
130
|
135
|
// of our current task
|
|
@@ -140,8 +145,8 @@ public class Schedule {
|
140
|
145
|
}
|
141
|
146
|
if(latestPredecessor.isEmpty()) {
|
142
|
147
|
// no latest predecessor found yet, look among tasks executing on the same machine
|
143
|
|
- latestPredecessor = IntStream.range(0, pb.numJobs)
|
144
|
|
- .mapToObj(j -> new Task(j, pb.task_with_machine(j, machine)))
|
|
148
|
+ latestPredecessor = IntStream.range(0, instance.numJobs)
|
|
149
|
+ .mapToObj(j -> new Task(j, instance.task_with_machine(j, machine)))
|
145
|
150
|
.filter(t -> endTime(t) == startTime(cur))
|
146
|
151
|
.findFirst();
|
147
|
152
|
}
|
|
@@ -158,11 +163,11 @@ public class Schedule {
|
158
|
163
|
public String toString() {
|
159
|
164
|
StringBuilder sb = new StringBuilder();
|
160
|
165
|
sb.append("\nStart times of all tasks:\n");
|
161
|
|
- for(int job=0; job<pb.numJobs; job++) {
|
|
166
|
+ for(int job = 0; job< instance.numJobs; job++) {
|
162
|
167
|
sb.append("Job ");
|
163
|
168
|
sb.append(job);
|
164
|
169
|
sb.append(": ");
|
165
|
|
- for(int task=0; task<pb.numTasks; task++) {
|
|
170
|
+ for(int task = 0; task< instance.numTasks; task++) {
|
166
|
171
|
sb.append(String.format("%5d", startTime(job, task)));
|
167
|
172
|
|
168
|
173
|
}
|
|
@@ -182,15 +187,15 @@ public class Schedule {
|
182
|
187
|
*/
|
183
|
188
|
public String asciiGantt() {
|
184
|
189
|
var criticalPath = this.criticalPath();
|
185
|
|
- int minTaskDur = IntStream.range(0, pb.numJobs).flatMap(job -> IntStream.range(0, pb.numTasks).map(task -> pb.duration(job, task))).min().getAsInt();
|
|
190
|
+ int minTaskDur = IntStream.range(0, instance.numJobs).flatMap(job -> IntStream.range(0, instance.numTasks).map(task -> instance.duration(job, task))).min().getAsInt();
|
186
|
191
|
// time units by character
|
187
|
192
|
int charsPerTimeUnit = minTaskDur >= 5 ? 1 : (5 / minTaskDur) +1;
|
188
|
193
|
StringBuilder sb = new StringBuilder();
|
189
|
194
|
sb.append("\nGantt Chart\n");
|
190
|
|
- for(int job=0; job<pb.numJobs; job++) {
|
|
195
|
+ for(int job = 0; job< instance.numJobs; job++) {
|
191
|
196
|
sb.append(String.format("Job %2d: ", job));
|
192
|
197
|
int cursor = 0;
|
193
|
|
- for(int task=0; task<pb.numTasks; task++) {
|
|
198
|
+ for(int task = 0; task< instance.numTasks; task++) {
|
194
|
199
|
Task t = new Task(job, task);
|
195
|
200
|
var st = startTime(job, task);
|
196
|
201
|
// add spaces until the start of our task
|
|
@@ -214,8 +219,8 @@ public class Schedule {
|
214
|
219
|
String formatTask(Task t, int charPerTimeUnit, boolean isCritical) {
|
215
|
220
|
StringBuilder sb = new StringBuilder();
|
216
|
221
|
String fill = isCritical ? "*" : "-";
|
217
|
|
- int dur = pb.duration(t);
|
218
|
|
- int machine = pb.machine(t);
|
|
222
|
+ int dur = instance.duration(t);
|
|
223
|
+ int machine = instance.machine(t);
|
219
|
224
|
int stringLength = dur * charPerTimeUnit;
|
220
|
225
|
int charsForMachine = machine < 10 ? 1 : 2;
|
221
|
226
|
int numSpaces = stringLength - 2 - charsForMachine; // we use 2 chars for '[' and '[' + 1 or 2 for the machine number
|
|
@@ -230,4 +235,10 @@ public class Schedule {
|
230
|
235
|
sb.append("]");
|
231
|
236
|
return sb.toString();
|
232
|
237
|
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+ @Override
|
|
241
|
+ public Schedule toSchedule() {
|
|
242
|
+ return this;
|
|
243
|
+ }
|
233
|
244
|
}
|