Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.PFSP/3.3/Decoders/PRVDecoder.cs @ 15521

Last change on this file since 15521 was 15521, checked in by fholzing, 6 years ago

#2864: First commit of new branch of Permutation based benchmark problems.

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