[6293] | 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 |
|
---|
[6406] | 22 | using HeuristicLab.Common;
|
---|
[6293] | 23 | using HeuristicLab.Core;
|
---|
[6406] | 24 | using HeuristicLab.Encodings.ScheduleEncoding;
|
---|
| 25 | using HeuristicLab.Encodings.ScheduleEncoding.PriorityRulesVector;
|
---|
[6293] | 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
[6406] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[6293] | 29 |
|
---|
[6406] | 30 | namespace HeuristicLab.Problems.Scheduling {
|
---|
| 31 | [Item("JobSequencingMatrixDecoder", "Applies the GifflerThompson algorithm to create an active schedule from a JobSequencing Matrix.")]
|
---|
[6293] | 32 | [StorableClass]
|
---|
[6406] | 33 | public class PRVDecoder : ScheduleDecoder<PRVEncoding>, IStochasticOperator, IJSSPOperator {
|
---|
[6293] | 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) {
|
---|
[6406] | 52 | Task currentResult = tasks[tasks.Count - 1];
|
---|
[6293] | 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 | }
|
---|
[6406] | 69 |
|
---|
[6293] | 70 | //shortest processingtime
|
---|
| 71 | private Task SPTRule(ItemList<Task> tasks) {
|
---|
| 72 | Task currentResult = RandomRule(tasks);
|
---|
| 73 | foreach (Task t in tasks) {
|
---|
[6412] | 74 | if (t.Duration < currentResult.Duration)
|
---|
[6293] | 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) {
|
---|
[6412] | 84 | if (t.Duration > currentResult.Duration)
|
---|
[6293] | 85 | currentResult = t;
|
---|
| 86 | }
|
---|
| 87 | return currentResult;
|
---|
[6406] | 88 | }
|
---|
[6293] | 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;
|
---|
[6412] | 96 | foreach (Task jt in jobs[t.JobNr].Tasks) {
|
---|
| 97 | if (!jt.IsScheduled)
|
---|
| 98 | remainingProcessingTime += jt.Duration;
|
---|
[6293] | 99 | }
|
---|
| 100 | if (remainingProcessingTime > currentLargestRemainingProcessingTime) {
|
---|
| 101 | currentLargestRemainingProcessingTime = remainingProcessingTime;
|
---|
| 102 | currentResult = t;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | return currentResult;
|
---|
| 106 | }
|
---|
[6406] | 107 |
|
---|
[6293] | 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;
|
---|
[6412] | 114 | foreach (Task jt in jobs[t.JobNr].Tasks) {
|
---|
| 115 | if (!jt.IsScheduled)
|
---|
| 116 | remainingProcessingTime += jt.Duration;
|
---|
[6293] | 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;
|
---|
[6412] | 132 | foreach (Task jt in jobs[t.JobNr].Tasks) {
|
---|
| 133 | if (!jt.IsScheduled)
|
---|
[6293] | 134 | nrOfRemainingTasks++;
|
---|
| 135 | }
|
---|
| 136 | if (currentLargestNrOfRemainingTasks < nrOfRemainingTasks) {
|
---|
| 137 | currentLargestNrOfRemainingTasks = nrOfRemainingTasks;
|
---|
| 138 | currentResult = t;
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 | return currentResult;
|
---|
| 142 | }
|
---|
[6406] | 143 |
|
---|
[6293] | 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;
|
---|
[6412] | 150 | foreach (Task jt in jobs[t.JobNr].Tasks) {
|
---|
| 151 | if (!jt.IsScheduled)
|
---|
[6293] | 152 | nrOfRemainingTasks++;
|
---|
| 153 | }
|
---|
| 154 | if (currentSmallestNrOfRemainingTasks > nrOfRemainingTasks) {
|
---|
| 155 | currentSmallestNrOfRemainingTasks = nrOfRemainingTasks;
|
---|
| 156 | currentResult = t;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | return currentResult;
|
---|
| 160 | }
|
---|
[6406] | 161 |
|
---|
[6293] | 162 | //first operation in Queue
|
---|
| 163 | private Task FIFORule(ItemList<Task> tasks) {
|
---|
| 164 | Task currentResult = tasks[0];
|
---|
| 165 | return currentResult;
|
---|
| 166 | }
|
---|
[6406] | 167 |
|
---|
[6293] | 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 |
|
---|
[6406] | 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 |
|
---|
[6293] | 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."));
|
---|
[6406] | 189 | Parameters.Add(new LookupParameter<ItemList<Job>>("JobData", "Job data taken from the SchedulingProblem - Instance."));
|
---|
| 190 | ScheduleEncodingParameter.ActualName = "PriorityRulesVector";
|
---|
[6293] | 191 | }
|
---|
| 192 |
|
---|
| 193 | private Task SelectTaskFromConflictSet(ItemList<Task> conflictSet, int ruleIndex, int nrOfRules) {
|
---|
| 194 | if (conflictSet.Count == 1)
|
---|
| 195 | return conflictSet[0];
|
---|
[6364] | 196 |
|
---|
[6293] | 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);
|
---|
[6364] | 210 | }
|
---|
[6293] | 211 | }
|
---|
| 212 |
|
---|
| 213 | public override Schedule CreateScheduleFromEncoding(PRVEncoding solution) {
|
---|
| 214 | jobs = (ItemList<Job>)JobDataParameter.ActualValue.Clone();
|
---|
[6406] | 215 | resultingSchedule = new Schedule(jobs[0].Tasks.Count);
|
---|
[6293] | 216 |
|
---|
| 217 | //Reset scheduled tasks in result
|
---|
| 218 | foreach (Job j in jobs) {
|
---|
| 219 | foreach (Task t in j.Tasks) {
|
---|
[6412] | 220 | t.IsScheduled = false;
|
---|
[6293] | 221 | }
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | //GT-Algorithm
|
---|
| 225 | //STEP 0 - Compute a list of "earliest operations"
|
---|
[6364] | 226 | ItemList<Task> earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
|
---|
| 227 | //int currentDecisionIndex = 0;
|
---|
[6293] | 228 | while (earliestTasksList.Count > 0) {
|
---|
| 229 | //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
|
---|
[6364] | 230 | Task minimal = GTAlgorithmUtils.GetTaskWithMinimalEC(earliestTasksList, resultingSchedule);
|
---|
[6293] | 231 |
|
---|
| 232 | //STEP 2 - Compute a conflict set of all operations that can be scheduled on the machine the previously selected operation runs on
|
---|
[6364] | 233 | ItemList<Task> conflictSet = GTAlgorithmUtils.GetConflictSetForTask(minimal, earliestTasksList, jobs, resultingSchedule);
|
---|
[6293] | 234 |
|
---|
| 235 | //STEP 3 - Select an operation from the conflict set (various methods depending on how the algorithm should work..)
|
---|
[6364] | 236 | //Task selectedTask = SelectTaskFromConflictSet(conflictSet, solution.PriorityRulesVector [currentDecisionIndex++], solution.NrOfRules.Value);
|
---|
[6412] | 237 | Task selectedTask = SelectTaskFromConflictSet(conflictSet, solution.PriorityRulesVector[minimal.JobNr], solution.NrOfRules.Value);
|
---|
[6293] | 238 |
|
---|
| 239 | //STEP 4 - Adding the selected operation to the current schedule
|
---|
[6412] | 240 | selectedTask.IsScheduled = true;
|
---|
[6406] | 241 | double startTime = GTAlgorithmUtils.ComputeEarliestStartTime(selectedTask, resultingSchedule);
|
---|
[6412] | 242 | resultingSchedule.ScheduleTask(selectedTask.ResourceNr, startTime, selectedTask.Duration, selectedTask.JobNr);
|
---|
[6293] | 243 |
|
---|
| 244 | //STEP 5 - Back to STEP 1
|
---|
[6364] | 245 | earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
|
---|
[6293] | 246 | }
|
---|
| 247 |
|
---|
| 248 | return resultingSchedule;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | public override IOperation Apply() {
|
---|
| 252 | return base.Apply();
|
---|
[6406] | 253 | }
|
---|
[6293] | 254 | }
|
---|
| 255 | }
|
---|