Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Problems.Scheduling/3.3/Encodings/JobSequenceMatrix/JSMDecoder.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: 8.8 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.JobSequenceMatrix {
35  [Item("Job Sequencing Matrix Decoder", "Applies the GifflerThompson algorithm to create an active schedule from a JobSequencing Matrix.")]
36  [StorableClass]
37  public class JSMDecoder : JSSPDecoder<JSMEncoding>, IStochasticOperator {
38    [StorableConstructor]
39    protected JSMDecoder(bool deserializing) : base(deserializing) { }
40    protected JSMDecoder(JSMDecoder original, Cloner cloner)
41      : base(original, cloner) {
42        this.resultingSchedule = cloner.Clone(original.resultingSchedule);
43        this.decodingErrorPolicy = original.decodingErrorPolicy;
44        this.forcingStrategy = original.forcingStrategy;
45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new JSMDecoder(this, cloner);
48    }
49
50    public ILookupParameter<IRandom> RandomParameter {
51      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
52    }
53
54    [Storable]
55    private Schedule resultingSchedule;
56
57    [Storable]
58    private JSMDecodingErrorPolicyTypes decodingErrorPolicy = JSMDecodingErrorPolicyTypes.GuidedPolicy;
59
60    [Storable]
61    private JSMForcingStrategyTypes forcingStrategy = JSMForcingStrategyTypes.ShiftForcing;
62
63    public JSMDecoder()
64      : base() {
65      Parameters.Add(new LookupParameter<IRandom>("Random", "The pseudo random number generator which should be used for stochastic manipulation operators."));
66    }
67
68    private ItemList<JSSPTask> GetEarliestNotScheduledTasks(ItemList<JSSPJob> jobs) {
69      ItemList<JSSPTask> result = new ItemList<JSSPTask>();
70      foreach (JSSPJob j in jobs) {
71        foreach (JSSPTask t in j.Tasks) {
72          if (!t.IsScheduled.Value) {
73            result.Add(t);
74            break;
75          }
76        }
77      }
78      return result;
79    }
80    private JSSPTask GetTaskWithMinimalEC(ItemList<JSSPTask> earliestTasksList) {
81      DoubleValue minEct = new DoubleValue(Double.MaxValue);
82      JSSPTask result = null;
83      foreach (JSSPTask t in earliestTasksList) {
84        DoubleValue ect = ComputeEarliestCompletionTime(t);
85        if (ect.Value < minEct.Value) {
86          result = t;
87          minEct = ect;
88        }
89      }
90      return result;
91    }
92    private ItemList<JSSPTask> GetConflictSetForTask(JSSPTask conflictedTask, ItemList<JSSPTask> earliestTasksList) {
93      ItemList<JSSPTask> result = new ItemList<JSSPTask>();
94      DoubleValue conflictedCompletionTime = ComputeEarliestCompletionTime(conflictedTask);
95      result.Add(conflictedTask);
96      foreach (JSSPTask t in earliestTasksList) {
97        if (t.ResourceNr.Value == conflictedTask.ResourceNr.Value) {
98          if (ComputeEarliestStartTime(t).Value < conflictedCompletionTime.Value)
99            result.Add(t);
100        }
101      }
102      return result;
103    }
104    private JSSPTask SelectTaskFromConflictSet(IntValue conflictedResource, ItemList<JSSPTask> conflictSet, ItemList<Permutation> jsm) {
105      JSSPTask result = null;
106      IntValue progressOnResource = new IntValue (resultingSchedule.Resources[conflictedResource.Value].Tasks.Count);
107
108      if (progressOnResource.Value < jsm[conflictedResource.Value].Length) {
109        IntValue solutionCandidateJobNr = new IntValue(jsm[conflictedResource.Value][progressOnResource.Value]);
110        foreach (JSSPTask t in conflictSet) {
111          if (t.Job.Index.Value == solutionCandidateJobNr.Value)
112            result = t;
113        }
114      } else {
115        return null;
116      }
117
118      //Repairing happens here
119      if (result == null) {
120        //Decoding Error Policy BEGIN
121        //CHOOSE OTHER RESOLUTION FOR CONFLICT SET
122        result = ApplyDecodingErrorPolicy(conflictSet);
123        //result = conflictSet[RandomParameter.ActualValue.Next(conflictSet.Count - 1)];
124        //Decoding Error Policy END
125
126
127        //Determine index of new resolution
128        int index = 0;
129        while (index < jsm[conflictedResource.Value].Length &&
130          jsm[conflictedResource.Value][index] != result.Job.Index.Value)
131          index++;
132
133
134        //Forcing Strategy BEGIN
135        ApplyForcingStrategy(jsm, conflictedResource.Value, index, progressOnResource.Value, result.Job.Index.Value);
136       
137        //ForcingStrategy END
138      }
139      return result;
140    }
141
142    private void ApplyForcingStrategy(ItemList<Permutation> jsm, int conflictedResource, int newResolutionIndex, int progressOnResource, int newResolution) {
143      //if (forcingStrategy == JSMForcingStrategyTypes.SwapForcing) {
144        //SwapForcing
145        jsm[conflictedResource][newResolutionIndex] = jsm[conflictedResource][progressOnResource];
146        jsm[conflictedResource][progressOnResource] = newResolution;
147      /*} else {
148        //ShiftForcing
149
150      } */
151    }
152
153    private JSSPTask ApplyDecodingErrorPolicy(ItemList<JSSPTask> conflictSet) {
154      //if (decodingErrorPolicy == JSMDecodingErrorPolicyTypes.RandomPolicy) {
155        //Random
156        return conflictSet[RandomParameter.ActualValue.Next(conflictSet.Count - 1)];
157      /*} else {
158        //Guided
159
160      }    */
161    }
162    private DoubleValue ComputeEarliestStartTime(JSSPTask t) {
163      Resource affectedResource = resultingSchedule.Resources[t.ResourceNr.Value];
164
165      DoubleValue lastMachineEndTime = affectedResource.TotalDuration;
166      DoubleValue previousJoboperationEndTime = new DoubleValue(double.MinValue);
167      if (t.PreviousTask != null)
168        previousJoboperationEndTime = t.PreviousTask.EndTime;
169
170      return new DoubleValue(Math.Max(previousJoboperationEndTime.Value, lastMachineEndTime.Value));
171    }
172    private DoubleValue ComputeEarliestCompletionTime(JSSPTask t) {
173      return new DoubleValue(ComputeEarliestStartTime(t).Value + t.Duration.Value);
174    }
175    private void AddTaskToSchedule(JSSPTask selectedTask) {
176      Resource affectedResource = resultingSchedule.Resources[selectedTask.ResourceNr.Value];
177      selectedTask.StartTime = ComputeEarliestStartTime(selectedTask);
178      selectedTask.IsScheduled.Value = true;
179      affectedResource.Tasks.Add(selectedTask);
180    }
181
182    public override Schedule CreateScheduleFromEncoding(JSMEncoding solution) {
183      ItemList<Permutation> jobSequenceMatrix = solution.JobSequenceMatrix;
184
185      resultingSchedule = new Schedule(new IntValue(Jobs[0].Tasks.Count));
186
187      //Reset scheduled tasks in result
188      foreach (JSSPJob j in Jobs) {
189        foreach (JSSPTask t in j.Tasks) {
190          t.IsScheduled.Value = false;
191        }
192      }
193
194      //GT-Algorithm
195      //STEP 0 - Compute a list of "earliest operations"
196      ItemList<JSSPTask> earliestTasksList = GetEarliestNotScheduledTasks(Jobs);
197      while (earliestTasksList.Count > 0) {
198        //STEP 1 - Get earliest not scheduled operation with minimal earliest completing time
199        JSSPTask minimal = GetTaskWithMinimalEC(earliestTasksList);
200
201        //STEP 2 - Compute a conflict set of all operations that can be scheduled on the machine the previously selected operation runs on
202        ItemList<JSSPTask> conflictSet = GetConflictSetForTask(minimal, earliestTasksList);
203
204        //STEP 3 - Select an operation from the conflict set (various methods depending on how the algorithm should work..)
205        JSSPTask selectedTask = SelectTaskFromConflictSet(minimal.ResourceNr, conflictSet, jobSequenceMatrix);
206
207        //STEP 4 - Adding the selected operation to the current schedule
208        AddTaskToSchedule(selectedTask);
209
210        //STEP 5 - Back to STEP 1
211        earliestTasksList = GetEarliestNotScheduledTasks(Jobs);
212      }
213
214      return resultingSchedule;
215    }
216
217    public override IOperation Apply() {
218      return base.Apply();
219    }
220  }
221}
Note: See TracBrowser for help on using the repository browser.