Browse Source

Ajoute l'extraction de chemin critique dans la classe Schedule.

Arthur Bit-Monnot 1 year ago
parent
commit
bf50002bd6
1 changed files with 72 additions and 3 deletions
  1. 72
    3
      src/main/java/jobshop/Schedule.java

+ 72
- 3
src/main/java/jobshop/Schedule.java View File

@@ -1,7 +1,10 @@
1 1
 package jobshop;
2 2
 
3 3
 
4
-import java.util.Arrays;
4
+import jobshop.encodings.Task;
5
+
6
+import java.util.*;
7
+import java.util.stream.IntStream;
5 8
 
6 9
 public class Schedule {
7 10
     public final Instance pb;
@@ -60,7 +63,73 @@ public class Schedule {
60 63
         return max;
61 64
     }
62 65
 
63
-    public Schedule copy() {
64
-        return new Schedule(this.pb, this.times);
66
+    public int startTime(Task task) {
67
+        return startTime(task.job, task.task);
68
+    }
69
+
70
+    public int endTime(Task task) {
71
+        return startTime(task) + pb.duration(task.job, task.task);
72
+    }
73
+
74
+    public boolean isCriticalPath(List<Task> path) {
75
+        if(startTime(path.get(0)) != 0) {
76
+            return false;
77
+        }
78
+        if(endTime(path.get(path.size()-1)) != makespan()) {
79
+            return false;
80
+        }
81
+        for(int i=0 ; i<path.size()-1 ; i++) {
82
+            if(endTime(path.get(i)) != startTime(path.get(i+1)))
83
+                return false;
84
+        }
85
+        return true;
86
+    }
87
+
88
+    public List<Task> criticalPath() {
89
+        // select task with greatest end time
90
+        Task ldd = IntStream.range(0, pb.numJobs)
91
+                .mapToObj(j -> new Task(j, pb.numTasks-1))
92
+                .max(Comparator.comparing(this::endTime))
93
+                .get();
94
+        assert endTime(ldd) == makespan();
95
+
96
+        // list that will contain the critical path.
97
+        // we construct it from the end, starting with the
98
+        // task that finishes last
99
+        LinkedList<Task> path = new LinkedList<>();
100
+        path.add(0,ldd);
101
+
102
+        // keep adding tasks to the path until the first task in the path
103
+        // starts a time 0
104
+        while(startTime(path.getFirst()) != 0) {
105
+            Task cur = path.getFirst();
106
+            int machine = pb.machine(cur.job, cur.task);
107
+
108
+            // will contain the task that was delaying the start
109
+            // of our current task
110
+            Optional<Task> latestPredecessor = Optional.empty();
111
+
112
+            if(cur.task > 0) {
113
+                // our current task has a predecessor on the job
114
+                Task predOnJob = new Task(cur.job, cur.task -1);
115
+
116
+                // if it was the delaying task, save it to predecessor
117
+                if(endTime(predOnJob) == startTime(cur))
118
+                    latestPredecessor = Optional.of(predOnJob);
119
+            }
120
+            if(!latestPredecessor.isPresent()) {
121
+                // no latest predecessor found yet, look among tasks executing on the same machine
122
+                latestPredecessor = IntStream.range(0, pb.numJobs)
123
+                        .mapToObj(j -> new Task(j, pb.task_with_machine(j, machine)))
124
+                        .filter(t -> endTime(t) == startTime(cur))
125
+                        .findFirst();
126
+            }
127
+            // at this point we should have identified a latest predecessor, either on the job or on the machine
128
+            assert latestPredecessor.isPresent() && endTime(latestPredecessor.get()) == startTime(cur);
129
+            // insert predecessor at the beginning of the path
130
+            path.add(0, latestPredecessor.get());
131
+        }
132
+        assert isCriticalPath(path);
133
+        return path;
65 134
     }
66 135
 }

Loading…
Cancel
Save