Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Encodings.ScheduleEncoding/3.3/JobSequenceMatrix/Decoder/JSMDecoder.cs @ 13443

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

#2521: Adapted decoders for SchedulingProblem.

File size: 8.4 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Encodings.ScheduleEncoding {
33  [Item("JobSequenceMatrixDecoder", "Applies the GifflerThompson algorithm to create an active schedule from a JobSequence Matrix.")]
34  [StorableClass]
35  public class JSMDecoder : ScheduleDecoder {
36
37    public IValueParameter<JSMDecodingErrorPolicy> DecodingErrorPolicyParameter {
38      get { return (IValueParameter<JSMDecodingErrorPolicy>)Parameters["DecodingErrorPolicy"]; }
39    }
40    public IValueParameter<JSMForcingStrategy> ForcingStrategyParameter {
41      get { return (IValueParameter<JSMForcingStrategy>)Parameters["ForcingStrategy"]; }
42    }
43
44    private JSMDecodingErrorPolicyTypes DecodingErrorPolicy {
45      get { return DecodingErrorPolicyParameter.Value.Value; }
46    }
47
48    private JSMForcingStrategyTypes ForcingStrategy {
49      get { return ForcingStrategyParameter.Value.Value; }
50    }
51
52    [StorableConstructor]
53    protected JSMDecoder(bool deserializing) : base(deserializing) { }
54    protected JSMDecoder(JSMDecoder original, Cloner cloner) : base(original, cloner) { }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new JSMDecoder(this, cloner);
57    }
58
59    public JSMDecoder()
60      : base() {
61      Parameters.Add(new ValueParameter<JSMDecodingErrorPolicy>("DecodingErrorPolicy", "Specify the policy that should be used to handle decoding errors.", new JSMDecodingErrorPolicy(JSMDecodingErrorPolicyTypes.RandomPolicy)));
62      Parameters.Add(new ValueParameter<JSMForcingStrategy>("ForcingStrategy", "Specifies a forcing strategy.", new JSMForcingStrategy(JSMForcingStrategyTypes.SwapForcing)));
63    }
64
65    private static Task SelectTaskFromConflictSet(JSMEncoding solution, JSMDecodingErrorPolicyTypes decodingErrorPolicy, JSMForcingStrategyTypes forcingStrategy, int conflictedResourceNr, int progressOnConflictedResource, ItemList<Task> conflictSet, IRandom random) {
66      if (conflictSet.Count == 1)
67        return conflictSet[0];
68
69      var jsm = solution.JobSequenceMatrix;
70
71      //get solutionCandidate from jobSequencingMatrix
72      int solutionCandidateJobNr = jsm[conflictedResourceNr][progressOnConflictedResource];
73
74      //scan conflictSet for given solutionCandidate, and return if found
75      foreach (Task t in conflictSet) {
76        if (t.JobNr == solutionCandidateJobNr)
77          return t;
78      }
79
80      //if solutionCandidate wasn't found in conflictSet apply DecodingErrorPolicy and ForcingPolicy
81      Task result = ApplyDecodingErrorPolicy(decodingErrorPolicy, conflictSet, jsm[conflictedResourceNr], progressOnConflictedResource, random);
82      int newResolutionIndex = 0;
83
84      while (newResolutionIndex < jsm[conflictedResourceNr].Length && jsm[conflictedResourceNr][newResolutionIndex] != result.JobNr)
85        newResolutionIndex++;
86      ApplyForcingStrategy(forcingStrategy, solution, conflictedResourceNr, newResolutionIndex, progressOnConflictedResource, result.JobNr);
87
88      return result;
89    }
90
91    private static Task ApplyDecodingErrorPolicy(JSMDecodingErrorPolicyTypes decodingErrorPolicy, ItemList<Task> conflictSet, Permutation resource, int progress, IRandom random) {
92      if (decodingErrorPolicy == JSMDecodingErrorPolicyTypes.RandomPolicy) {
93        //Random
94        return conflictSet[random.Next(conflictSet.Count - 1)];
95      } else {
96        //Guided
97        for (int i = progress; i < resource.Length; i++) {
98          int j = 0;
99          while (j < conflictSet.Count && conflictSet[j].JobNr != resource[i])
100            j++;
101
102          if (j < conflictSet.Count)
103            return (conflictSet[j]);
104        }
105        return conflictSet[random.Next(conflictSet.Count - 1)];
106      }
107    }
108
109    private static void ApplyForcingStrategy(JSMForcingStrategyTypes forcingStrategy, JSMEncoding solution, int conflictedResource, int newResolutionIndex, int progressOnResource, int newResolution) {
110      var jsm = solution.JobSequenceMatrix;
111      if (forcingStrategy == JSMForcingStrategyTypes.SwapForcing) {
112        //SwapForcing
113        jsm[conflictedResource][newResolutionIndex] = jsm[conflictedResource][progressOnResource];
114        jsm[conflictedResource][progressOnResource] = newResolution;
115      } else if (forcingStrategy == JSMForcingStrategyTypes.ShiftForcing) {
116        //ShiftForcing
117        List<int> asList = jsm[conflictedResource].ToList<int>();
118        if (newResolutionIndex > progressOnResource) {
119          asList.RemoveAt(newResolutionIndex);
120          asList.Insert(progressOnResource, newResolution);
121        } else {
122          asList.Insert(progressOnResource, newResolution);
123          asList.RemoveAt(newResolutionIndex);
124        }
125        jsm[conflictedResource] = new Permutation(PermutationTypes.Absolute, asList.ToArray<int>());
126      } else {
127        throw new InvalidOperationException(string.Format("JSMDecoder encountered unknown forcing strategy {0}", forcingStrategy));
128      }
129    }
130
131    public override Schedule DecodeSchedule(ISchedule encoding, ItemList<Job> jobData) {
132      var solution = encoding as JSMEncoding;
133      if (solution == null) throw new InvalidOperationException("Encoding is not of type JSMEncoding");
134      return DecodeSchedule(solution, jobData, DecodingErrorPolicy, ForcingStrategy);
135    }
136
137    public static Schedule DecodeSchedule(JSMEncoding solution, ItemList<Job> jobData, JSMDecodingErrorPolicyTypes decodingErrorPolicy, JSMForcingStrategyTypes forcingStrategy) {
138      var random = new FastRandom(solution.RandomSeed);
139      var jobs = (ItemList<Job>)jobData.Clone();
140      var resultingSchedule = new Schedule(jobs[0].Tasks.Count);
141
142      //Reset scheduled tasks in result
143      foreach (Job j in jobs) {
144        foreach (Task t in j.Tasks) {
145          t.IsScheduled = false;
146        }
147      }
148
149      //GT-Algorithm
150      //STEP 0 - Compute a list of "earliest operations"
151      ItemList<Task> earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
152      while (earliestTasksList.Count > 0) {
153        //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
154        Task minimal = GTAlgorithmUtils.GetTaskWithMinimalEC(earliestTasksList, resultingSchedule);
155        int conflictedResourceNr = minimal.ResourceNr;
156        Resource conflictedResource = resultingSchedule.Resources[conflictedResourceNr];
157
158        //STEP 2 - Compute a conflict set of all operations that can be scheduled on the conflicted resource
159        ItemList<Task> conflictSet = GTAlgorithmUtils.GetConflictSetForTask(minimal, earliestTasksList, resultingSchedule);
160
161        //STEP 3 - Select a task from the conflict set
162        int progressOnResource = conflictedResource.Tasks.Count;
163        Task selectedTask = SelectTaskFromConflictSet(solution, decodingErrorPolicy, forcingStrategy, conflictedResourceNr, progressOnResource, conflictSet, random);
164
165        //STEP 4 - Add the selected task to the current schedule
166        selectedTask.IsScheduled = true;
167        double startTime = GTAlgorithmUtils.ComputeEarliestStartTime(selectedTask, resultingSchedule);
168        resultingSchedule.ScheduleTask(selectedTask.ResourceNr, startTime, selectedTask.Duration, selectedTask.JobNr);
169
170        //STEP 5 - Back to STEP 1
171        earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
172      }
173
174      return resultingSchedule;
175    }
176  }
177}
Note: See TracBrowser for help on using the repository browser.