Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Problems.Scheduling/3.3/Encodings/PriorityRulesVector/PRVDecoder.cs @ 6177

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

#1329: Implemented PriorityRulesVector based encoding and added new operators to the JSMEncoding. Added Gantt-Charts for visualization of schedules and problems.

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