Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Problems.Scheduling/3.3/Decoders/JSMDecoder.cs @ 8603

Last change on this file since 8603 was 8603, checked in by gkronber, 12 years ago

#1329:

  • updated copy-right year
  • trimmed assembly and plugin references
  • removed x86 and x64 build configurations
File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Encodings.ScheduleEncoding;
28using HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.Scheduling {
34  [Item("JobSequenceMatrixDecoder", "Applies the GifflerThompson algorithm to create an active schedule from a JobSequence Matrix.")]
35  [StorableClass]
36  public class JSMDecoder : ScheduleDecoder<JSMEncoding>, IStochasticOperator, IJSSPOperator {
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 Private Members
45    [Storable]
46    private Schedule resultingSchedule;
47
48    [Storable]
49    private ItemList<Job> jobs;
50
51    [Storable]
52    private JSMDecodingErrorPolicyTypes decodingErrorPolicy = JSMDecodingErrorPolicyTypes.GuidedPolicy;
53
54    [Storable]
55    private JSMForcingStrategyTypes forcingStrategy = JSMForcingStrategyTypes.ShiftForcing;
56    #endregion
57
58    [StorableConstructor]
59    protected JSMDecoder(bool deserializing) : base(deserializing) { }
60    protected JSMDecoder(JSMDecoder original, Cloner cloner)
61      : base(original, cloner) {
62      this.resultingSchedule = cloner.Clone(original.resultingSchedule);
63      this.jobs = cloner.Clone(original.jobs);
64      this.decodingErrorPolicy = original.decodingErrorPolicy;
65      this.forcingStrategy = original.forcingStrategy;
66    }
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new JSMDecoder(this, cloner);
69    }
70
71    public JSMDecoder()
72      : base() {
73      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
74      Parameters.Add(new LookupParameter<ItemList<Job>>("JobData", "Job data taken from the Schedulingproblem - Instance."));
75      ScheduleEncodingParameter.ActualName = "JobSequenceMatrix";
76    }
77
78
79    private Task SelectTaskFromConflictSet(int conflictedResourceNr, int progressOnConflictedResource, ItemList<Task> conflictSet, ItemList<Permutation> jsm) {
80      if (conflictSet.Count == 1)
81        return conflictSet[0];
82
83      //get solutionCandidate from jobSequencingMatrix
84      int solutionCandidateJobNr = jsm[conflictedResourceNr][progressOnConflictedResource];
85
86      //scan conflictSet for given solutionCandidate, and return if found
87      foreach (Task t in conflictSet) {
88        if (t.JobNr == solutionCandidateJobNr)
89          return t;
90      }
91
92      //if solutionCandidate wasn't found in conflictSet apply DecodingErrorPolicy and ForcingPolicy
93      Task result = ApplyDecodingErrorPolicy(conflictSet, jsm[conflictedResourceNr], progressOnConflictedResource);
94      int newResolutionIndex = 0;
95
96      while (newResolutionIndex < jsm[conflictedResourceNr].Length && jsm[conflictedResourceNr][newResolutionIndex] != result.JobNr)
97        newResolutionIndex++;
98      ApplyForcingStrategy(jsm, conflictedResourceNr, newResolutionIndex, progressOnConflictedResource, result.JobNr);
99
100      return result;
101    }
102    private Task ApplyDecodingErrorPolicy(ItemList<Task> conflictSet, Permutation resource, int progress) {
103      if (decodingErrorPolicy == JSMDecodingErrorPolicyTypes.RandomPolicy) {
104        //Random
105        return conflictSet[RandomParameter.ActualValue.Next(conflictSet.Count - 1)];
106      } else {
107        //Guided
108        for (int i = progress; i < resource.Length; i++) {
109          int j = 0;
110          while (j < conflictSet.Count && conflictSet[j].JobNr != resource[i])
111            j++;
112
113          if (j < conflictSet.Count)
114            return (conflictSet[j]);
115        }
116        return conflictSet[RandomParameter.ActualValue.Next(conflictSet.Count - 1)];
117      }
118    }
119    private void ApplyForcingStrategy(ItemList<Permutation> jsm, int conflictedResource, int newResolutionIndex, int progressOnResource, int newResolution) {
120      if (forcingStrategy == JSMForcingStrategyTypes.SwapForcing) {
121        //SwapForcing
122        jsm[conflictedResource][newResolutionIndex] = jsm[conflictedResource][progressOnResource];
123        jsm[conflictedResource][progressOnResource] = newResolution;
124      } else {
125        //ShiftForcing
126        List<int> asList = jsm[conflictedResource].ToList<int>();
127        if (newResolutionIndex > progressOnResource) {
128          asList.RemoveAt(newResolutionIndex);
129          asList.Insert(progressOnResource, newResolution);
130        } else {
131          asList.Insert(progressOnResource, newResolution);
132          asList.RemoveAt(newResolutionIndex);
133        }
134        jsm[conflictedResource] = new Permutation(PermutationTypes.Absolute, asList.ToArray<int>());
135      }
136    }
137
138    public Schedule CreateScheduleFromEncoding(JSMEncoding solution, ItemList<Job> jobData) {
139      ItemList<Permutation> jobSequenceMatrix = solution.JobSequenceMatrix;
140
141      jobs = (ItemList<Job>)jobData.Clone();
142      resultingSchedule = new Schedule(jobs[0].Tasks.Count);
143
144      //Reset scheduled tasks in result
145      foreach (Job j in jobs) {
146        foreach (Task t in j.Tasks) {
147          t.IsScheduled = false;
148        }
149      }
150
151      //GT-Algorithm
152      //STEP 0 - Compute a list of "earliest operations"
153      ItemList<Task> earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
154      while (earliestTasksList.Count > 0) {
155        //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
156        Task minimal = GTAlgorithmUtils.GetTaskWithMinimalEC(earliestTasksList, resultingSchedule);
157        int conflictedResourceNr = minimal.ResourceNr;
158        Resource conflictedResource = resultingSchedule.Resources[conflictedResourceNr];
159
160        //STEP 2 - Compute a conflict set of all operations that can be scheduled on the conflicted resource
161        ItemList<Task> conflictSet = GTAlgorithmUtils.GetConflictSetForTask(minimal, earliestTasksList, jobs, resultingSchedule);
162
163        //STEP 3 - Select a task from the conflict set
164        int progressOnResource = conflictedResource.Tasks.Count;
165        Task selectedTask = SelectTaskFromConflictSet(conflictedResourceNr, progressOnResource, conflictSet, jobSequenceMatrix);
166
167        //STEP 4 - Add the selected task to the current schedule
168        selectedTask.IsScheduled = true;
169        double startTime = GTAlgorithmUtils.ComputeEarliestStartTime(selectedTask, resultingSchedule);
170        resultingSchedule.ScheduleTask(selectedTask.ResourceNr, startTime, selectedTask.Duration, selectedTask.JobNr);
171
172        //STEP 5 - Back to STEP 1
173        earliestTasksList = GTAlgorithmUtils.GetEarliestNotScheduledTasks(jobs);
174      }
175
176      return resultingSchedule;
177    }
178
179    public override Schedule CreateScheduleFromEncoding(JSMEncoding solution) {
180      return CreateScheduleFromEncoding(solution, JobDataParameter.ActualValue);
181    }
182
183    public override IOperation Apply() {
184      return base.Apply();
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.