Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6364 was 6364, checked in by jhelm, 13 years ago

#1329: Did some minor refactoring.

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