|
@@ -2,6 +2,10 @@ package jobshop.solvers;
|
2
|
2
|
|
3
|
3
|
import jobshop.Instance;
|
4
|
4
|
import jobshop.Result;
|
|
5
|
+import jobshop.encodings.Task;
|
|
6
|
+
|
|
7
|
+import java.util.ArrayList;
|
|
8
|
+import java.util.Iterator;
|
5
|
9
|
|
6
|
10
|
/** An empty shell to implement a greedy solver. */
|
7
|
11
|
public class GreedySolver implements Solver {
|
|
@@ -14,13 +18,97 @@ public class GreedySolver implements Solver {
|
14
|
18
|
/** Priority that the solver should use. */
|
15
|
19
|
final Priority priority;
|
16
|
20
|
|
|
21
|
+ final ArrayList<Task> tachesRestantes;
|
|
22
|
+
|
|
23
|
+
|
17
|
24
|
/** Creates a new greedy solver that will use the given priority. */
|
18
|
25
|
public GreedySolver(Priority p) {
|
19
|
26
|
this.priority = p;
|
|
27
|
+ this.tachesRestantes = new ArrayList<>();
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ /** return true if t1 est plus prioritaire que t1 **/
|
|
31
|
+ private boolean prioritaire(Instance instance, Task t1, Task t2) throws Exception {
|
|
32
|
+ boolean rt = false;
|
|
33
|
+ switch(priority) {
|
|
34
|
+ case SPT:
|
|
35
|
+ rt = instance.duration(t1) <= instance.duration(t2);
|
|
36
|
+ break;
|
|
37
|
+ case LPT:
|
|
38
|
+ rt = instance.duration(t1) >= instance.duration(t2);
|
|
39
|
+ break;
|
|
40
|
+ case SRPT:
|
|
41
|
+ int i;
|
|
42
|
+ int sigmaT1 = 0;
|
|
43
|
+ int sigmaT2 = 0;
|
|
44
|
+ for (i=t1.task; i<instance.numTasks; i++) {
|
|
45
|
+ sigmaT1 += instance.duration(t1.job, i);
|
|
46
|
+ }
|
|
47
|
+ for (i=t2.task; i<instance.numTasks; i++) {
|
|
48
|
+ sigmaT2 += instance.duration(t2.job, i);
|
|
49
|
+ }
|
|
50
|
+ rt = sigmaT1 <= sigmaT2;
|
|
51
|
+ break;
|
|
52
|
+ case LRPT:
|
|
53
|
+ sigmaT1 = 0;
|
|
54
|
+ sigmaT2 = 0;
|
|
55
|
+ for (i=t1.task; i<instance.numTasks; i++) {
|
|
56
|
+ sigmaT1 += instance.duration(t1.job, i);
|
|
57
|
+ }
|
|
58
|
+ for (i=t2.task; i<instance.numTasks; i++) {
|
|
59
|
+ sigmaT2 += instance.duration(t2.job, i);
|
|
60
|
+ }
|
|
61
|
+ rt = sigmaT1 >= sigmaT2;
|
|
62
|
+ break;
|
|
63
|
+ case EST_SPT:
|
|
64
|
+ throw new Exception("UNKNOWN PRIORITY");
|
|
65
|
+ //break;
|
|
66
|
+ case EST_LPT:
|
|
67
|
+ throw new Exception("UNKNOWN PRIORITY");
|
|
68
|
+ //break;
|
|
69
|
+ case EST_SRPT:
|
|
70
|
+ throw new Exception("UNKNOWN PRIORITY");
|
|
71
|
+ //break;
|
|
72
|
+ case EST_LRPT:
|
|
73
|
+ throw new Exception("UNKNOWN PRIORITY");
|
|
74
|
+ //break;
|
|
75
|
+ }
|
|
76
|
+ return rt;
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ private void addTask(Instance instance, Task task) throws Exception {
|
|
80
|
+ Iterator<Task> iter = this.tachesRestantes.iterator();
|
|
81
|
+ int index = 0;
|
|
82
|
+ boolean trouve = false;
|
|
83
|
+ while (iter.hasNext() && !trouve) {
|
|
84
|
+ Task current = iter.next();
|
|
85
|
+ if (this.prioritaire(instance, task, current)) {
|
|
86
|
+ trouve = true;
|
|
87
|
+ this.tachesRestantes.add(index, task);
|
|
88
|
+ } else {
|
|
89
|
+ index++;
|
|
90
|
+ }
|
|
91
|
+ }
|
20
|
92
|
}
|
21
|
93
|
|
22
|
94
|
@Override
|
23
|
95
|
public Result solve(Instance instance, long deadline) {
|
24
|
|
- throw new UnsupportedOperationException();
|
|
96
|
+ //Initialisation
|
|
97
|
+ int i;
|
|
98
|
+ for (i=0; i<instance.numJobs; i++) {
|
|
99
|
+ try {
|
|
100
|
+ addTask(instance, new Task(i,0));
|
|
101
|
+ } catch (Exception e) {
|
|
102
|
+ System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
|
|
103
|
+ System.exit(2);
|
|
104
|
+ }
|
|
105
|
+ }
|
|
106
|
+ Result result = null;
|
|
107
|
+
|
|
108
|
+ //Itérations
|
|
109
|
+ while (!this.tachesRestantes.isEmpty()) {
|
|
110
|
+ //TAF
|
|
111
|
+ }
|
|
112
|
+ return result;
|
25
|
113
|
}
|
26
|
114
|
}
|