Skip to content

Commit e79762e

Browse files
authored
chore: minor flowshop adjustments (#85)
1 parent cecb5f6 commit e79762e

File tree

5 files changed

+71
-67
lines changed

5 files changed

+71
-67
lines changed

src/main/java/ai/timefold/solver/benchmarks/examples/flowshop/domain/Job.java

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import ai.timefold.solver.core.api.domain.entity.PlanningEntity;
44
import ai.timefold.solver.core.api.domain.lookup.PlanningId;
5-
import ai.timefold.solver.core.api.domain.variable.IndexShadowVariable;
5+
import ai.timefold.solver.core.api.domain.variable.InverseRelationShadowVariable;
66
import ai.timefold.solver.core.api.domain.variable.PreviousElementShadowVariable;
77
import ai.timefold.solver.core.api.domain.variable.ShadowSources;
88
import ai.timefold.solver.core.api.domain.variable.ShadowVariable;
@@ -20,15 +20,15 @@ public class Job {
2020
private int id;
2121
@JsonIdentityReference(alwaysAsId = true)
2222
private Machine[] allMachines;
23-
@IndexShadowVariable(sourceVariableName = "jobs")
23+
@InverseRelationShadowVariable(sourceVariableName = "jobs")
2424
@JsonIgnore
25-
private Integer index;
25+
private Machine machine;
2626
@PreviousElementShadowVariable(sourceVariableName = "jobs")
2727
@JsonIgnore
2828
private Job previousJob;
29-
@ShadowVariable(supplierName = "updateMakespan")
29+
@ShadowVariable(supplierName = "updateCompletionTime")
3030
@JsonIgnore
31-
private JobMakespan makespan;
31+
private JobCompletionTime completionTime;
3232
private int processTimeSum = 0;
3333

3434
public Job() {
@@ -55,17 +55,17 @@ public void setPreviousJob(Job previousJob) {
5555
this.previousJob = previousJob;
5656
}
5757

58-
public JobMakespan getMakespan() {
59-
return makespan;
58+
public JobCompletionTime getCompletionTime() {
59+
return completionTime;
6060
}
6161

62-
public void setMakespan(JobMakespan makespan) {
63-
this.makespan = makespan;
62+
public void setCompletionTime(JobCompletionTime completionTime) {
63+
this.completionTime = completionTime;
6464
}
6565

6666
public int getProcessingTimeSum() {
6767
if (processTimeSum == 0) {
68-
for (Machine allMachine : allMachines) {
68+
for (var allMachine : allMachines) {
6969
processTimeSum += allMachine.getProcessTime(id);
7070
}
7171
}
@@ -77,52 +77,51 @@ public int getProcessingTime(int machineId) {
7777
}
7878

7979
@JsonIgnore
80-
@ShadowSources(value = { "previousJob.makespan", "index" })
81-
public JobMakespan updateMakespan() {
82-
if (index == null) {
80+
@ShadowSources(value = { "previousJob.completionTime", "machine" })
81+
public JobCompletionTime updateCompletionTime() {
82+
if (machine == null) {
8383
return null;
8484
}
85-
var newMakespan = new JobMakespan(allMachines.length);
85+
var newCompletionTime = new JobCompletionTime(allMachines.length);
8686
// A machine can perform only one job at a time,
8787
// and a job can only start on one machine after finishing the process at the previous machine.
88-
// The makespan of this job in the first machine depends only on the previous job makespan.
88+
// The completion time of this job in the first machine depends only on the previous job completion time.
8989
// It can only start after the previous job is completed.
90-
var newPreviousMakespan = newMakespan.setMakespan(0, getPreviousMakespan(0) + allMachines[0].getProcessTime(id));
90+
var previousMachineCompletionTime = newCompletionTime.setCompletionTime(0, getPreviousCompletionTime(0) + allMachines[0].getProcessTime(id));
9191
for (var i = 1; i < allMachines.length; i++) {
92-
// The job execution for the following machines relies on the makespan of either the previous job
92+
// The job execution for the following machines relies on the completion time of either the previous job
9393
// or the previous machine,
9494
// depending on which is greater.
9595
// That way, the job can only begin on the machine once it has completed on the previous machine
9696
// or after the prior job has finished.
97-
newPreviousMakespan = newMakespan.setMakespan(i,
98-
Math.max(getPreviousMakespan(i), newPreviousMakespan) + allMachines[i].getProcessTime(id));
97+
previousMachineCompletionTime = newCompletionTime.setCompletionTime(i,
98+
Math.max(getPreviousCompletionTime(i), previousMachineCompletionTime) + allMachines[i].getProcessTime(id));
9999
}
100-
return newMakespan;
100+
return newCompletionTime;
101101
}
102102

103103
@JsonIgnore
104-
private int getPreviousMakespan(int machineId) {
104+
private int getPreviousCompletionTime(int machineId) {
105105
if (previousJob != null) {
106-
return previousJob.getMakespan(machineId);
106+
return previousJob.getCompletionTime(machineId);
107107
}
108108
return 0;
109109
}
110110

111111
@JsonIgnore
112-
public int getMakespan(int machineId) {
113-
if (makespan == null) {
112+
public int getCompletionTime(int machineId) {
113+
if (completionTime == null) {
114114
return 0;
115115
}
116-
return makespan.getMakespan(machineId);
116+
return completionTime.getCompletionTime(machineId);
117117
}
118118

119119
@JsonIgnore
120-
public int getLastMachineMakespan() {
121-
if (makespan == null) {
120+
public int getCompletionTimeLastMachine() {
121+
if (completionTime == null) {
122122
return 0;
123123
}
124-
// The makespan is given by the makespan of the last machine
125-
return makespan.getLastMachineMakespan();
124+
return completionTime.getCompletionTimeLastMachine();
126125
}
127126

128127
@Override
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package ai.timefold.solver.benchmarks.examples.flowshop.domain;
2+
3+
import java.util.Arrays;
4+
5+
public class JobCompletionTime {
6+
7+
private final int[] completionTime;
8+
9+
public JobCompletionTime(int numberOfMachines) {
10+
this.completionTime = new int[numberOfMachines];
11+
}
12+
13+
public int setCompletionTime(int machineId, int value) {
14+
return completionTime[machineId] = value;
15+
}
16+
17+
public int getCompletionTime(int machineId) {
18+
return completionTime[machineId];
19+
}
20+
21+
public int getCompletionTimeLastMachine() {
22+
return completionTime[completionTime.length - 1];
23+
}
24+
25+
@Override
26+
public boolean equals(Object o) {
27+
if (!(o instanceof JobCompletionTime makespan)) {
28+
return false;
29+
}
30+
return Arrays.equals(completionTime, makespan.completionTime);
31+
}
32+
33+
@Override
34+
public int hashCode() {
35+
return Arrays.hashCode(completionTime);
36+
}
37+
}

src/main/java/ai/timefold/solver/benchmarks/examples/flowshop/domain/JobMakespan.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/java/ai/timefold/solver/benchmarks/examples/flowshop/domain/Machine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public int getMakespan() {
5858
if (jobs.isEmpty()) {
5959
return 0;
6060
}
61-
return jobs.getLast().getLastMachineMakespan();
61+
return jobs.getLast().getCompletionTimeLastMachine();
6262
}
6363

6464
@Override

src/main/resources/ai/timefold/solver/benchmarks/examples/flowshop/flowShopSolverConfig.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
<termination>
1919
<minutesSpentLimit>2</minutesSpentLimit>
2020
</termination>
21+
<customPhase>
22+
<customPhaseCommandClass>ai.timefold.solver.benchmarks.examples.flowshop.phase.NEHCustomPhase
23+
</customPhaseCommandClass>
24+
</customPhase>
25+
<localSearch/>
2126
</solver>

0 commit comments

Comments
 (0)