Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Problems.Scheduling/3.3/Decoders/PRVDecoder.cs @ 6406

Last change on this file since 6406 was 6406, checked in by jhelm, 13 years ago

#1329: Applied suggestions from codereview. Added unit-tests. Renamed encoding-project.

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Encodings.ScheduleEncoding;
25using HeuristicLab.Encodings.ScheduleEncoding.PriorityRulesVector;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.Scheduling {
31  [Item("JobSequencingMatrixDecoder", "Applies the GifflerThompson algorithm to create an active schedule from a JobSequencing Matrix.")]
32  [StorableClass]
33  public class PRVDecoder : ScheduleDecoder<PRVEncoding>, IStochasticOperator, IJSSPOperator {
34    public ILookupParameter<IRandom> RandomParameter {
35      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
36    }
37    public ILookupParameter<ItemList<Job>> JobDataParameter {
38      get { return (LookupParameter<ItemList<Job>>)Parameters["JobData"]; }
39    }
40
41    #region Private Members
42    [Storable]
43    private Schedule resultingSchedule;
44
45    [Storable]
46    private ItemList<Job> jobs;
47    #endregion
48
49    #region Priority Rules
50    //smallest number of remaining tasks
51    private Task FILORule(ItemList<Task> tasks) {
52      Task currentResult = tasks[tasks.Count - 1];
53      return currentResult;
54    }
55
56    //earliest start time
57    private Task ESTRule(ItemList<Task> tasks) {
58      Task currentResult = RandomRule(tasks);
59      double currentEST = double.MaxValue;
60      foreach (Task t in tasks) {
61        double est = GTAlgorithmUtils.ComputeEarliestStartTime(t, resultingSchedule);
62        if (est < currentEST) {
63          currentEST = est;
64          currentResult = t;
65        }
66      }
67      return currentResult;
68    }
69
70    //shortest processingtime
71    private Task SPTRule(ItemList<Task> tasks) {
72      Task currentResult = RandomRule(tasks);
73      foreach (Task t in tasks) {
74        if (t.Duration.Value < currentResult.Duration.Value)
75          currentResult = t;
76      }
77      return currentResult;
78    }
79
80    //longest processing time   
81    private Task LPTRule(ItemList<Task> tasks) {
82      Task currentResult = RandomRule(tasks);
83      foreach (Task t in tasks) {
84        if (t.Duration.Value > currentResult.Duration.Value)
85          currentResult = t;
86      }
87      return currentResult;
88    }
89
90    //most work remaining
91    private Task MWRRule(ItemList<Task> tasks) {
92      Task currentResult = RandomRule(tasks);
93      double currentLargestRemainingProcessingTime = 0;
94      foreach (Task t in tasks) {
95        double remainingProcessingTime = 0;
96        foreach (Task jt in jobs[t.JobNr.Value].Tasks) {
97          if (!jt.IsScheduled.Value)
98            remainingProcessingTime += jt.Duration.Value;
99        }
100        if (remainingProcessingTime > currentLargestRemainingProcessingTime) {
101          currentLargestRemainingProcessingTime = remainingProcessingTime;
102          currentResult = t;
103        }
104      }
105      return currentResult;
106    }
107
108    //least work remaining
109    private Task LWRRule(ItemList<Task> tasks) {
110      Task currentResult = RandomRule(tasks);
111      double currentSmallestRemainingProcessingTime = double.MaxValue;
112      foreach (Task t in tasks) {
113        double remainingProcessingTime = 0;
114        foreach (Task jt in jobs[t.JobNr.Value].Tasks) {
115          if (!jt.IsScheduled.Value)
116            remainingProcessingTime += jt.Duration.Value;
117        }
118        if (remainingProcessingTime < currentSmallestRemainingProcessingTime) {
119          currentSmallestRemainingProcessingTime = remainingProcessingTime;
120          currentResult = t;
121        }
122      }
123      return currentResult;
124    }
125
126    //most operations remaining
127    private Task MORRule(ItemList<Task> tasks) {
128      Task currentResult = RandomRule(tasks);
129      int currentLargestNrOfRemainingTasks = 0;
130      foreach (Task t in tasks) {
131        int nrOfRemainingTasks = 0;
132        foreach (Task jt in jobs[t.JobNr.Value].Tasks) {
133          if (!jt.IsScheduled.Value)
134            nrOfRemainingTasks++;
135        }
136        if (currentLargestNrOfRemainingTasks < nrOfRemainingTasks) {
137          currentLargestNrOfRemainingTasks = nrOfRemainingTasks;
138          currentResult = t;
139        }
140      }
141      return currentResult;
142    }
143
144    //least operationsremaining
145    private Task LORRule(ItemList<Task> tasks) {
146      Task currentResult = RandomRule(tasks);
147      int currentSmallestNrOfRemainingTasks = int.MaxValue;
148      foreach (Task t in tasks) {
149        int nrOfRemainingTasks = 0;
150        foreach (Task jt in jobs[t.JobNr.Value].Tasks) {
151          if (!jt.IsScheduled.Value)
152            nrOfRemainingTasks++;
153        }
154        if (currentSmallestNrOfRemainingTasks > nrOfRemainingTasks) {
155          currentSmallestNrOfRemainingTasks = nrOfRemainingTasks;
156          currentResult = t;
157        }
158      }
159      return currentResult;
160    }
161
162    //first operation in Queue
163    private Task FIFORule(ItemList<Task> tasks) {
164      Task currentResult = tasks[0];
165      return currentResult;
166    }
167
168    //random
169    private Task RandomRule(ItemList<Task> tasks) {
170      Task currentResult = tasks[RandomParameter.ActualValue.Next(tasks.Count)];
171      return currentResult;
172    }
173
174    #endregion
175
176    [StorableConstructor]
177    protected PRVDecoder(bool deserializing) : base(deserializing) { }
178    protected PRVDecoder(PRVDecoder original, Cloner cloner)
179      : base(original, cloner) {
180      this.resultingSchedule = cloner.Clone(original.resultingSchedule);
181    }
182    public override IDeepCloneable Clone(Cloner cloner) {
183      return new PRVDecoder(this, cloner);
184    }
185
186    public PRVDecoder()
187      : base() {
188      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
189      Parameters.Add(new LookupParameter<ItemList<Job>>("JobData", "Job data taken from the SchedulingProblem - Instance."));
190      ScheduleEncodingParameter.ActualName = "PriorityRulesVector";
191    }
192
193    private Task SelectTaskFromConflictSet(ItemList<Task> conflictSet, int ruleIndex, int nrOfRules) {
194      if (conflictSet.Count == 1)
195        return conflictSet[0];
196
197      ruleIndex = ruleIndex % nrOfRules;
198      switch (ruleIndex) {
199        case 0: return FILORule(conflictSet);
200        case 1: return ESTRule(conflictSet);
201        case 2: return SPTRule(conflictSet);
202        case 3: return LPTRule(conflictSet);
203        case 4: return MWRRule(conflictSet);
204        case 5: return LWRRule(conflictSet);
205        case 6: return MORRule(conflictSet);
206        case 7: return LORRule(conflictSet);
207        case 8: return FIFORule(conflictSet);
208        case 9: return RandomRule(conflictSet);
209        default: return RandomRule(conflictSet);
210      }
211    }
212
213    public override Schedule CreateScheduleFromEncoding(PRVEncoding solution) {
214      jobs = (ItemList<Job>)JobDataParameter.ActualValue.Clone();
215      resultingSchedule = new Schedule(jobs[0].Tasks.Count);
216
217      //Reset scheduled tasks in result
218      foreach (Job j in jobs) {
219        foreach (Task t in j.Tasks) {
220          t.IsScheduled.Value = false;
221        }
222      }
223
224      //GT-Algorithm
225      //STEP 0 - Compute a list of "earliest operations"
226      ItemList<Task> earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
227      //int currentDecisionIndex = 0;
228      while (earliestTasksList.Count > 0) {
229        //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
230        Task minimal = GTAlgorithmUtils.GetTaskWithMinimalEC(earliestTasksList, resultingSchedule);
231
232        //STEP 2 - Compute a conflict set of all operations that can be scheduled on the machine the previously selected operation runs on
233        ItemList<Task> conflictSet = GTAlgorithmUtils.GetConflictSetForTask(minimal, earliestTasksList, jobs, resultingSchedule);
234
235        //STEP 3 - Select an operation from the conflict set (various methods depending on how the algorithm should work..)
236        //Task selectedTask = SelectTaskFromConflictSet(conflictSet, solution.PriorityRulesVector [currentDecisionIndex++], solution.NrOfRules.Value);
237        Task selectedTask = SelectTaskFromConflictSet(conflictSet, solution.PriorityRulesVector[minimal.JobNr.Value], solution.NrOfRules.Value);
238
239        //STEP 4 - Adding the selected operation to the current schedule
240        selectedTask.IsScheduled.Value = true;
241        double startTime = GTAlgorithmUtils.ComputeEarliestStartTime(selectedTask, resultingSchedule);
242        resultingSchedule.ScheduleTask(selectedTask.ResourceNr.Value, startTime, selectedTask.Duration.Value, selectedTask.JobNr.Value);
243
244        //STEP 5 - Back to STEP 1
245        earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
246      }
247
248      return resultingSchedule;
249    }
250
251    public override IOperation Apply() {
252      return base.Apply();
253    }
254  }
255}
Note: See TracBrowser for help on using the repository browser.