Browse Source

Glouton SPT LPT SRPT LRPT OK

Paul Faure 5 months ago
parent
commit
d97d8141a1

+ 8
- 0
src/main/java/jobshop/Instance.java View File

@@ -59,6 +59,14 @@ public final class Instance {
59 59
         throw new RuntimeException("No task targeting machine "+wanted_machine+" on job "+job);
60 60
     }
61 61
 
62
+    public Task nextTask(Task task) {
63
+        if (task.task < this.numTasks - 1) {
64
+            return new Task(task.job, task.task + 1);
65
+        } else {
66
+            return null;
67
+        }
68
+    }
69
+
62 70
     /**
63 71
      * Creates a new instance, with uninitialized durations and machines.
64 72
      * This should no be called directly. Instead, Instance objects should be created with the

+ 1
- 1
src/main/java/jobshop/MainTest.java View File

@@ -70,7 +70,7 @@ public class MainTest {
70 70
             manualROInvalid.addTaskToMachine(1, new Task(1,0));
71 71
             manualROInvalid.addTaskToMachine(2, new Task(0,2));
72 72
             manualROInvalid.addTaskToMachine(2, new Task(1,2));
73
-            System.out.println("GANTT: " + manualROInvalid.toSchedule().get().asciiGantt());
73
+            //System.out.println("GANTT: " + manualROInvalid.toSchedule().get().asciiGantt());
74 74
 
75 75
         } catch (IOException e) {
76 76
             e.printStackTrace();

+ 25
- 5
src/main/java/jobshop/solvers/GreedySolver.java View File

@@ -2,11 +2,15 @@ package jobshop.solvers;
2 2
 
3 3
 import jobshop.Instance;
4 4
 import jobshop.Result;
5
+import jobshop.encodings.ResourceOrder;
6
+import jobshop.encodings.Schedule;
5 7
 import jobshop.encodings.Task;
6 8
 
7 9
 import java.util.ArrayList;
8 10
 import java.util.Iterator;
9 11
 
12
+import static jobshop.Result.ExitCause.ProvedOptimal;
13
+
10 14
 /** An empty shell to implement a greedy solver. */
11 15
 public class GreedySolver implements Solver {
12 16
 
@@ -84,11 +88,15 @@ public class GreedySolver implements Solver {
84 88
             Task current = iter.next();
85 89
             if (this.prioritaire(instance, task, current)) {
86 90
                 trouve = true;
87
-                this.tachesRestantes.add(index, task);
88 91
             } else {
89 92
                 index++;
90 93
             }
91 94
         }
95
+        this.tachesRestantes.add(index, task);
96
+    }
97
+
98
+    private Task getTask() {
99
+        return this.tachesRestantes.remove(0);
92 100
     }
93 101
 
94 102
     @Override
@@ -97,18 +105,30 @@ public class GreedySolver implements Solver {
97 105
         int i;
98 106
         for (i=0; i<instance.numJobs; i++) {
99 107
             try {
100
-                addTask(instance, new Task(i,0));
108
+                this.addTask(instance, new Task(i,0));
101 109
             } catch (Exception e) {
102 110
                 System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
103 111
                 System.exit(2);
104 112
             }
105 113
         }
106
-        Result result = null;
114
+        ResourceOrder resourceOrder = new ResourceOrder(instance);
115
+        Task task;
116
+        Task nextTask;
107 117
 
108 118
         //Itérations
109 119
         while (!this.tachesRestantes.isEmpty()) {
110
-            //TAF
120
+            task = this.getTask();
121
+            resourceOrder.addTaskToMachine(instance.machine(task), task);
122
+            nextTask = instance.nextTask(task);
123
+            if (nextTask != null) {
124
+                try {
125
+                    this.addTask(instance, new Task(task.job, task.task + 1));
126
+                } catch (Exception e) {
127
+                    System.out.println("ERREUR POLITIQUE DE PRIORITE INCONNUE");
128
+                    System.exit(2);
129
+                }
130
+            }
111 131
         }
112
-        return result;
132
+        return new Result (instance, resourceOrder.toSchedule(), ProvedOptimal);
113 133
     }
114 134
 }

+ 7
- 0
src/main/java/jobshop/solvers/Solver.java View File

@@ -21,6 +21,13 @@ public interface Solver {
21 21
             case "basic": return new BasicSolver();
22 22
             case "random": return new RandomSolver();
23 23
             case "spt": return new GreedySolver(GreedySolver.Priority.SPT);
24
+            case "lpt": return new GreedySolver(GreedySolver.Priority.LPT);
25
+            case "srpt": return new GreedySolver(GreedySolver.Priority.SRPT);
26
+            case "lrpt": return new GreedySolver(GreedySolver.Priority.LRPT);
27
+            case "est_spt": return new GreedySolver(GreedySolver.Priority.EST_SPT);
28
+            case "est_lpt": return new GreedySolver(GreedySolver.Priority.EST_LPT);
29
+            case "est_srpt": return new GreedySolver(GreedySolver.Priority.EST_SRPT);
30
+            case "est_lrpt": return new GreedySolver(GreedySolver.Priority.EST_LRPT);
24 31
             // TODO: add new solvers
25 32
             default: throw new RuntimeException("Unknown solver: "+ name);
26 33
         }

+ 119
- 0
src/test/java/jobshop/solvers/GreedySolverTest.java View File

@@ -0,0 +1,119 @@
1
+package jobshop.solvers;
2
+
3
+import jobshop.BestKnownResults;
4
+import jobshop.Instance;
5
+import jobshop.Result;
6
+import org.junit.Test;
7
+
8
+import java.io.IOException;
9
+import java.nio.file.Paths;
10
+import java.util.List;
11
+import java.util.stream.Collectors;
12
+
13
+import static jobshop.solvers.GreedySolver.Priority.*;
14
+
15
+public class GreedySolverTest {
16
+    @Test
17
+    public void testSPT() {
18
+        List<String> instancesNames = BestKnownResults.instancesMatching("la");
19
+        instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
20
+
21
+        List<Instance> instances = instancesNames.stream().map(name -> {
22
+            Instance instance = null;
23
+            try {
24
+                instance = Instance.fromFile(Paths.get("instances/", name));
25
+            } catch (IOException e) {
26
+                e.printStackTrace();
27
+            }
28
+            return instance;
29
+        }).collect(Collectors.toList());
30
+
31
+        GreedySolver solver = new GreedySolver(SPT);
32
+        Result result;
33
+        for (int i = 0; i < instances.size(); i++) {
34
+            result = solver.solve(instances.get(i), 100000);
35
+            assert result.schedule.get().isValid();
36
+            if (BestKnownResults.isKnown(instancesNames.get(i))) {
37
+                assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i));
38
+            }
39
+        }
40
+    }
41
+
42
+    @Test
43
+    public void testLPT() throws IOException {
44
+        List<String> instancesNames = BestKnownResults.instancesMatching("la");
45
+        instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
46
+
47
+        List<Instance> instances = instancesNames.stream().map(name -> {
48
+            Instance instance = null;
49
+            try {
50
+                instance = Instance.fromFile(Paths.get("instances/", name));
51
+            } catch (IOException e) {
52
+                e.printStackTrace();
53
+            }
54
+            return instance;
55
+        }).collect(Collectors.toList());
56
+
57
+        GreedySolver solver = new GreedySolver(LPT);
58
+        Result result;
59
+        for (int i = 0; i < instances.size(); i++) {
60
+            result = solver.solve(instances.get(i), 100000);
61
+            assert result.schedule.get().isValid();
62
+            if (BestKnownResults.isKnown(instancesNames.get(i))) {
63
+                assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i));
64
+            }
65
+        }
66
+    }
67
+
68
+    @Test
69
+    public void testSRPT() throws IOException {
70
+        List<String> instancesNames = BestKnownResults.instancesMatching("la");
71
+        instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
72
+
73
+        List<Instance> instances = instancesNames.stream().map(name -> {
74
+            Instance instance = null;
75
+            try {
76
+                instance = Instance.fromFile(Paths.get("instances/", name));
77
+            } catch (IOException e) {
78
+                e.printStackTrace();
79
+            }
80
+            return instance;
81
+        }).collect(Collectors.toList());
82
+
83
+        GreedySolver solver = new GreedySolver(SRPT);
84
+        Result result;
85
+        for (int i = 0; i < instances.size(); i++) {
86
+            result = solver.solve(instances.get(i), 100000);
87
+            assert result.schedule.get().isValid();
88
+            if (BestKnownResults.isKnown(instancesNames.get(i))) {
89
+                assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i));
90
+            }
91
+        }
92
+    }
93
+
94
+    @Test
95
+    public void testLRPT() throws IOException {
96
+        List<String> instancesNames = BestKnownResults.instancesMatching("la");
97
+        instancesNames.addAll(BestKnownResults.instancesMatching("ft"));
98
+
99
+        List<Instance> instances = instancesNames.stream().map(name -> {
100
+            Instance instance = null;
101
+            try {
102
+                instance = Instance.fromFile(Paths.get("instances/", name));
103
+            } catch (IOException e) {
104
+                e.printStackTrace();
105
+            }
106
+            return instance;
107
+        }).collect(Collectors.toList());
108
+
109
+        GreedySolver solver = new GreedySolver(LRPT);
110
+        Result result;
111
+        for (int i = 0; i < instances.size(); i++) {
112
+            result = solver.solve(instances.get(i), 100000);
113
+            assert result.schedule.get().isValid();
114
+            if (BestKnownResults.isKnown(instancesNames.get(i))) {
115
+                assert result.schedule.get().makespan() >= BestKnownResults.of(instancesNames.get(i));
116
+            }
117
+        }
118
+    }
119
+}

Loading…
Cancel
Save