Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.Encodings.ScheduleEncoding/3.3/PriorityRulesVector/Decoder/PRVDecoder.cs @ 15870

Last change on this file since 15870 was 13469, checked in by mkommend, 9 years ago

#2521: Refactored problem base classes and adapted scheduling encoding, scheduling problem and unit tests.

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HeuristicLab.Random;
27
28namespace HeuristicLab.Encodings.ScheduleEncoding {
29  [Item("JobSequencingMatrixDecoder", "Applies the GifflerThompson algorithm to create an active schedule from a JobSequencing Matrix.")]
30  [StorableClass]
31  public class PRVDecoder : ScheduleDecoder<PRVEncoding> {
32    #region Priority Rules
33    //smallest number of remaining tasks
34    private static Task FILORule(ItemList<Task> tasks) {
35      Task currentResult = tasks[tasks.Count - 1];
36      return currentResult;
37    }
38
39    //earliest start time
40    private static Task ESTRule(ItemList<Task> tasks, Schedule schedule) {
41      Task currentResult = tasks.First();
42      double currentEST = double.MaxValue;
43      foreach (Task t in tasks) {
44        double est = GTAlgorithmUtils.ComputeEarliestStartTime(t, schedule);
45        if (est < currentEST) {
46          currentEST = est;
47          currentResult = t;
48        }
49      }
50      return currentResult;
51    }
52
53    //shortest processingtime
54    private static Task SPTRule(ItemList<Task> tasks) {
55      Task currentResult = tasks.First();
56      foreach (Task t in tasks) {
57        if (t.Duration < currentResult.Duration)
58          currentResult = t;
59      }
60      return currentResult;
61    }
62
63    //longest processing time   
64    private static Task LPTRule(ItemList<Task> tasks) {
65      Task currentResult = tasks.First();
66      foreach (Task t in tasks) {
67        if (t.Duration > currentResult.Duration)
68          currentResult = t;
69      }
70      return currentResult;
71    }
72
73    //most work remaining
74    private static Task MWRRule(ItemList<Task> tasks, ItemList<Job> jobs) {
75      Task currentResult = tasks.First();
76      double currentLargestRemainingProcessingTime = 0;
77      foreach (Task t in tasks) {
78        double remainingProcessingTime = 0;
79        foreach (Task jt in jobs[t.JobNr].Tasks) {
80          if (!jt.IsScheduled)
81            remainingProcessingTime += jt.Duration;
82        }
83        if (remainingProcessingTime > currentLargestRemainingProcessingTime) {
84          currentLargestRemainingProcessingTime = remainingProcessingTime;
85          currentResult = t;
86        }
87      }
88      return currentResult;
89    }
90
91    //least work remaining
92    private static Task LWRRule(ItemList<Task> tasks, ItemList<Job> jobs) {
93      Task currentResult = tasks.First();
94      double currentSmallestRemainingProcessingTime = double.MaxValue;
95      foreach (Task t in tasks) {
96        double remainingProcessingTime = 0;
97        foreach (Task jt in jobs[t.JobNr].Tasks) {
98          if (!jt.IsScheduled)
99            remainingProcessingTime += jt.Duration;
100        }
101        if (remainingProcessingTime < currentSmallestRemainingProcessingTime) {
102          currentSmallestRemainingProcessingTime = remainingProcessingTime;
103          currentResult = t;
104        }
105      }
106      return currentResult;
107    }
108
109    //most operations remaining
110    private static Task MORRule(ItemList<Task> tasks, ItemList<Job> jobs) {
111      Task currentResult = tasks.First();
112      int currentLargestNrOfRemainingTasks = 0;
113      foreach (Task t in tasks) {
114        int nrOfRemainingTasks = 0;
115        foreach (Task jt in jobs[t.JobNr].Tasks) {
116          if (!jt.IsScheduled)
117            nrOfRemainingTasks++;
118        }
119        if (currentLargestNrOfRemainingTasks < nrOfRemainingTasks) {
120          currentLargestNrOfRemainingTasks = nrOfRemainingTasks;
121          currentResult = t;
122        }
123      }
124      return currentResult;
125    }
126
127    //least operationsremaining
128    private static Task LORRule(ItemList<Task> tasks, ItemList<Job> jobs) {
129      Task currentResult = tasks.First();
130      int currentSmallestNrOfRemainingTasks = int.MaxValue;
131      foreach (Task t in tasks) {
132        int nrOfRemainingTasks = 0;
133        foreach (Task jt in jobs[t.JobNr].Tasks) {
134          if (!jt.IsScheduled)
135            nrOfRemainingTasks++;
136        }
137        if (currentSmallestNrOfRemainingTasks > nrOfRemainingTasks) {
138          currentSmallestNrOfRemainingTasks = nrOfRemainingTasks;
139          currentResult = t;
140        }
141      }
142      return currentResult;
143    }
144
145    //first operation in Queue
146    private static Task FIFORule(ItemList<Task> tasks) {
147      Task currentResult = tasks[0];
148      return currentResult;
149    }
150
151    //random
152    private static Task RandomRule(ItemList<Task> tasks, IRandom random) {
153      Task currentResult = tasks[random.Next(tasks.Count)];
154      return currentResult;
155    }
156
157    #endregion
158
159    [StorableConstructor]
160    protected PRVDecoder(bool deserializing) : base(deserializing) { }
161    protected PRVDecoder(PRVDecoder original, Cloner cloner) : base(original, cloner) { }
162    public PRVDecoder() : base() { }
163
164    public override IDeepCloneable Clone(Cloner cloner) {
165      return new PRVDecoder(this, cloner);
166    }
167
168    private static Task SelectTaskFromConflictSet(ItemList<Task> conflictSet, int ruleIndex, Schedule schedule, ItemList<Job> jobs, IRandom random) {
169      if (conflictSet.Count == 1)
170        return conflictSet[0];
171
172      //TODO change to property, Encoding parameter?
173      ruleIndex = ruleIndex % 10;
174      switch (ruleIndex) {
175        case 0: return FILORule(conflictSet);
176        case 1: return ESTRule(conflictSet, schedule);
177        case 2: return SPTRule(conflictSet);
178        case 3: return LPTRule(conflictSet);
179        case 4: return MWRRule(conflictSet, jobs);
180        case 5: return LWRRule(conflictSet, jobs);
181        case 6: return MORRule(conflictSet, jobs);
182        case 7: return LORRule(conflictSet, jobs);
183        case 8: return FIFORule(conflictSet);
184        case 9: return RandomRule(conflictSet, random);
185        default: return RandomRule(conflictSet, random);
186      }
187    }
188
189    public override Schedule DecodeSchedule(PRVEncoding encoding, ItemList<Job> jobData) {
190      return Decode(encoding, jobData);
191    }
192
193    public static Schedule Decode(PRVEncoding solution, ItemList<Job> jobData) {
194      var random = new FastRandom(solution.RandomSeed);
195      var jobs = (ItemList<Job>)jobData.Clone();
196      var resultingSchedule = new Schedule(jobs[0].Tasks.Count);
197
198      //Reset scheduled tasks in result
199      foreach (Job j in jobs) {
200        foreach (Task t in j.Tasks) {
201          t.IsScheduled = false;
202        }
203      }
204
205      //GT-Algorithm
206      //STEP 0 - Compute a list of "earliest operations"
207      ItemList<Task> earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
208      //int currentDecisionIndex = 0;
209      while (earliestTasksList.Count > 0) {
210        //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
211        Task minimal = GTAlgorithmUtils.GetTaskWithMinimalEC(earliestTasksList, resultingSchedule);
212
213        //STEP 2 - Compute a conflict set of all operations that can be scheduled on the machine the previously selected operation runs on
214        ItemList<Task> conflictSet = GTAlgorithmUtils.GetConflictSetForTask(minimal, earliestTasksList, resultingSchedule);
215
216        //STEP 3 - Select an operation from the conflict set (various methods depending on how the algorithm should work..)
217        //Task selectedTask = SelectTaskFromConflictSet(conflictSet, solution.PriorityRulesVector [currentDecisionIndex++], solution.NrOfRules.Value);
218        Task selectedTask = SelectTaskFromConflictSet(conflictSet, solution.PriorityRulesVector[minimal.JobNr], resultingSchedule, jobs, random);
219
220        //STEP 4 - Adding the selected operation to the current schedule
221        selectedTask.IsScheduled = true;
222        double startTime = GTAlgorithmUtils.ComputeEarliestStartTime(selectedTask, resultingSchedule);
223        resultingSchedule.ScheduleTask(selectedTask.ResourceNr, startTime, selectedTask.Duration, selectedTask.JobNr);
224
225        //STEP 5 - Back to STEP 1
226        earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
227      }
228
229      return resultingSchedule;
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.