Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Scheduling/3.3/JobShopSchedulingProblem.cs @ 14506

Last change on this file since 14506 was 14506, checked in by abeham, 6 years ago

#2720:

  • fixed instance provider not using the correct description of the instance
  • fixed parser to initialize a writable stream and disposed of streamreaders and writers, fixed api comment
  • fixed jssp problem to adjust its name according to the loaded instance
File size: 14.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Encodings.ScheduleEncoding;
30using HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix;
31using HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition;
32using HeuristicLab.Encodings.ScheduleEncoding.PriorityRulesVector;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Problems.Instances;
37
38namespace HeuristicLab.Problems.Scheduling {
39  [Item("Job Shop Scheduling Problem (JSSP)", "Represents a standard Job Shop Scheduling Problem")]
40  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 120)]
41  [StorableClass]
42  public sealed class JobShopSchedulingProblem : SchedulingProblem, IProblemInstanceConsumer<JSSPData>, IProblemInstanceExporter<JSSPData>, IStorableContent {
43    #region Default Instance
44    private static readonly JSSPData DefaultInstance = new JSSPData() {
45      Jobs = 10,
46      Resources = 10,
47      BestKnownQuality = 930,
48      ProcessingTimes = new double[,] {
49          { 29, 78,  9, 36, 49, 11, 62, 56, 44, 21 },
50          { 43, 90, 75, 11, 69, 28, 46, 46, 72, 30 },
51          { 91, 85, 39, 74, 90, 10, 12, 89, 45, 33 },
52          { 81, 95, 71, 99,  9, 52, 85, 98, 22, 43 },
53          { 14,  6, 22, 61, 26, 69, 21, 49, 72, 53 },
54          { 84,  2, 52, 95, 48, 72, 47, 65,  6, 25 },
55          { 46, 37, 61, 13, 32, 21, 32, 89, 30, 55 },
56          { 31, 86, 46, 74, 32, 88, 19, 48, 36, 79 },
57          { 76, 69, 76, 51, 85, 11, 40, 89, 26, 74 },
58          { 85, 13, 61,  7, 64, 76, 47, 52, 90, 45 }
59        },
60      Demands = new int[,] {
61          { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
62          { 0, 2, 4, 9, 3, 1, 6, 5, 7, 8 },
63          { 1, 0, 3, 2, 8, 5, 7, 6, 9, 4 },
64          { 1, 2, 0, 4, 6, 8, 7, 3, 9, 5 },
65          { 2, 0, 1, 5, 3, 4, 8, 7, 9, 6 },
66          { 2, 1, 5, 3, 8, 9, 0, 6, 4, 7 },
67          { 1, 0, 3, 2, 6, 5, 9, 8, 7, 4 },
68          { 2, 0, 1, 5, 4, 6, 8, 9, 7, 3 },
69          { 0, 1, 3, 5, 2, 9, 6, 7, 4, 8 },
70          { 1, 0, 2, 6, 8, 9, 5, 3, 4, 7 }
71        }
72    };
73    #endregion
74
75    public string Filename { get; set; }
76    public override Image ItemImage {
77      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
78    }
79
80    #region Parameter Properties
81    public IValueParameter<ItemList<Job>> JobDataParameter {
82      get { return (IValueParameter<ItemList<Job>>)Parameters["JobData"]; }
83    }
84    public OptionalValueParameter<Schedule> BestKnownSolutionParameter {
85      get { return (OptionalValueParameter<Schedule>)Parameters["BestKnownSolution"]; }
86    }
87
88    public IFixedValueParameter<IntValue> JobsParameter {
89      get { return (IFixedValueParameter<IntValue>)Parameters["Jobs"]; }
90    }
91    public IFixedValueParameter<IntValue> ResourcesParameter {
92      get { return (IFixedValueParameter<IntValue>)Parameters["Resources"]; }
93    }
94    public IValueParameter<IScheduleEvaluator> ScheduleEvaluatorParameter {
95      get { return (IValueParameter<IScheduleEvaluator>)Parameters["ScheduleEvaluator"]; }
96    }
97    public OptionalValueParameter<IScheduleDecoder> ScheduleDecoderParameter {
98      get { return (OptionalValueParameter<IScheduleDecoder>)Parameters["ScheduleDecoder"]; }
99    }
100    #endregion
101
102    #region Properties
103    public ItemList<Job> JobData {
104      get { return JobDataParameter.Value; }
105      set { JobDataParameter.Value = value; }
106    }
107    public Schedule BestKnownSolution {
108      get { return BestKnownSolutionParameter.Value; }
109      set { BestKnownSolutionParameter.Value = value; }
110    }
111    public int Jobs {
112      get { return JobsParameter.Value.Value; }
113      set { JobsParameter.Value.Value = value; }
114    }
115    public int Resources {
116      get { return ResourcesParameter.Value.Value; }
117      set { ResourcesParameter.Value.Value = value; }
118    }
119    public IScheduleEvaluator ScheduleEvaluator {
120      get { return ScheduleEvaluatorParameter.Value; }
121      set { ScheduleEvaluatorParameter.Value = value; }
122    }
123    public IScheduleDecoder ScheduleDecoder {
124      get { return ScheduleDecoderParameter.Value; }
125      set { ScheduleDecoderParameter.Value = value; }
126    }
127    #endregion
128
129    [StorableConstructor]
130    private JobShopSchedulingProblem(bool deserializing) : base(deserializing) { }
131    private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
132      : base(original, cloner) {
133      RegisterEventHandlers();
134    }
135    public JobShopSchedulingProblem()
136      : base(new SchedulingEvaluator(), new JSMRandomCreator()) {
137      Parameters.Add(new ValueParameter<ItemList<Job>>("JobData", "Jobdata defining the precedence relationships and the duration of the tasks in this JSSP-Instance.", new ItemList<Job>()));
138      Parameters.Add(new OptionalValueParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
139
140      Parameters.Add(new FixedValueParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", new IntValue()));
141      Parameters.Add(new FixedValueParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", new IntValue()));
142      Parameters.Add(new ValueParameter<IScheduleEvaluator>("ScheduleEvaluator", "The evaluator used to determine the quality of a solution.", new MakespanEvaluator()));
143      Parameters.Add(new OptionalValueParameter<IScheduleDecoder>("ScheduleDecoder", "The operator that decodes the representation and creates a schedule.", new JSMDecoder()));
144
145      EvaluatorParameter.GetsCollected = false;
146      EvaluatorParameter.Hidden = true;
147      ScheduleDecoderParameter.Hidden = true;
148
149      InitializeOperators();
150      Load(DefaultInstance);
151      RegisterEventHandlers();
152    }
153
154    public override IDeepCloneable Clone(Cloner cloner) {
155      return new JobShopSchedulingProblem(this, cloner);
156    }
157
158    [StorableHook(HookType.AfterDeserialization)]
159    private void AfterDeserialization() {
160      RegisterEventHandlers();
161    }
162
163    private void RegisterEventHandlers() {
164      ScheduleEvaluatorParameter.ValueChanged += ScheduleEvaluatorParameter_ValueChanged;
165      ScheduleEvaluator.QualityParameter.ActualNameChanged += ScheduleEvaluator_QualityParameter_ActualNameChanged;
166      SolutionCreator.ScheduleEncodingParameter.ActualNameChanged += SolutionCreator_SchedulingEncodingParameter_ActualNameChanged;
167      ScheduleDecoderParameter.ValueChanged += ScheduleDecoderParameter_ValueChanged;
168      if (ScheduleDecoder != null) ScheduleDecoder.ScheduleParameter.ActualNameChanged += ScheduleDecoder_ScheduleParameter_ActualNameChanged;
169    }
170
171    #region Events
172    protected override void OnSolutionCreatorChanged() {
173      SolutionCreator.ScheduleEncodingParameter.ActualNameChanged += SolutionCreator_SchedulingEncodingParameter_ActualNameChanged;
174      InitializeOperators();
175    }
176    protected override void OnEvaluatorChanged() {
177      base.OnEvaluatorChanged();
178      ParameterizeOperators();
179    }
180    private void ScheduleEvaluatorParameter_ValueChanged(object sender, EventArgs eventArgs) {
181      ScheduleEvaluator.QualityParameter.ActualNameChanged += ScheduleEvaluator_QualityParameter_ActualNameChanged;
182      ParameterizeOperators();
183    }
184    private void ScheduleEvaluator_QualityParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
185      ParameterizeOperators();
186    }
187
188    private void SolutionCreator_SchedulingEncodingParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
189      ParameterizeOperators();
190    }
191    private void ScheduleDecoderParameter_ValueChanged(object sender, EventArgs eventArgs) {
192      if (ScheduleDecoder != null) ScheduleDecoder.ScheduleParameter.ActualNameChanged += ScheduleDecoder_ScheduleParameter_ActualNameChanged;
193      ParameterizeOperators();
194    }
195    private void ScheduleDecoder_ScheduleParameter_ActualNameChanged(object sender, EventArgs eventArgs) {
196      ParameterizeOperators();
197    }
198    #endregion
199
200    #region Problem Instance Handling
201    public void Load(JSSPData data) {
202      var jobData = new ItemList<Job>(data.Jobs);
203      for (int j = 0; j < data.Jobs; j++) {
204        var job = new Job(j, data.DueDates != null ? data.DueDates[j] : double.MaxValue);
205        for (int t = 0; t < data.Resources; t++) {
206          job.Tasks.Add(new Task(t, data.Demands[j, t], j, data.ProcessingTimes[j, t]));
207        }
208        jobData.Add(job);
209      }
210
211      BestKnownQuality = data.BestKnownQuality.HasValue ? new DoubleValue(data.BestKnownQuality.Value) : null;
212      if (data.BestKnownSchedule != null) {
213        var enc = new JSMEncoding();
214        enc.JobSequenceMatrix = new ItemList<Permutation>(data.Resources);
215        for (int i = 0; i < data.Resources; i++) {
216          enc.JobSequenceMatrix[i] = new Permutation(PermutationTypes.Absolute, new int[data.Jobs]);
217          for (int j = 0; j < data.Jobs; j++) {
218            enc.JobSequenceMatrix[i][j] = data.BestKnownSchedule[i, j];
219          }
220        }
221        BestKnownSolution = new JSMDecoder().CreateScheduleFromEncoding(enc, jobData);
222        if (ScheduleEvaluator is MeanTardinessEvaluator)
223          BestKnownQuality = new DoubleValue(MeanTardinessEvaluator.GetMeanTardiness(BestKnownSolution, jobData));
224        else if (ScheduleEvaluator is MakespanEvaluator)
225          BestKnownQuality = new DoubleValue(MakespanEvaluator.GetMakespan(BestKnownSolution));
226      }
227      Name = data.Name;
228      Description = data.Description;
229      JobData = jobData;
230      Jobs = data.Jobs;
231      Resources = data.Resources;
232    }
233
234    public JSSPData Export() {
235      var result = new JSSPData {
236        Name = Name,
237        Description = Description,
238        Jobs = Jobs,
239        Resources = Resources,
240        ProcessingTimes = new double[Jobs, Resources],
241        Demands = new int[Jobs, Resources],
242        DueDates = new double[Jobs]
243      };
244
245      foreach (var job in JobData) {
246        var counter = 0;
247        result.DueDates[job.Index] = job.DueDate;
248        foreach (var task in job.Tasks) {
249          result.ProcessingTimes[task.JobNr, counter] = task.Duration;
250          result.Demands[task.JobNr, counter] = task.ResourceNr;
251          counter++;
252        }
253      }
254      return result;
255    }
256    #endregion
257
258    #region Helpers
259    private void InitializeOperators() {
260      Operators.Clear();
261      ApplyEncoding();
262      Operators.Add(new BestSchedulingSolutionAnalyzer());
263      ParameterizeOperators();
264    }
265
266    private void ApplyEncoding() {
267      if (SolutionCreator.GetType() == typeof(JSMRandomCreator)) {
268        Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
269        ScheduleDecoder = new JSMDecoder();
270      } else if (SolutionCreator.GetType() == typeof(PRVRandomCreator)) {
271        Operators.AddRange(ApplicationManager.Manager.GetInstances<IPRVOperator>());
272        ScheduleDecoder = new PRVDecoder();
273      } else if (SolutionCreator.GetType() == typeof(PWRRandomCreator)) {
274        Operators.AddRange(ApplicationManager.Manager.GetInstances<IPWROperator>());
275        ScheduleDecoder = new PWRDecoder();
276      } else if (SolutionCreator.GetType() == typeof(DirectScheduleRandomCreator)) {
277        Operators.AddRange(ApplicationManager.Manager.GetInstances<IDirectScheduleOperator>());
278        ScheduleDecoder = null;
279      }
280    }
281
282    private void ParameterizeOperators() {
283      Evaluator.ScheduleDecoderParameter.ActualName = ScheduleDecoderParameter.Name;
284      Evaluator.ScheduleDecoderParameter.Hidden = true;
285      Evaluator.ScheduleEvaluatorParameter.ActualName = ScheduleEvaluatorParameter.Name;
286      Evaluator.ScheduleEvaluatorParameter.Hidden = true;
287      Evaluator.QualityParameter.ActualName = ScheduleEvaluator.QualityParameter.ActualName;
288      Evaluator.QualityParameter.Hidden = true;
289
290      if (ScheduleDecoder != null)
291        ScheduleDecoder.ScheduleEncodingParameter.ActualName = SolutionCreator.ScheduleEncodingParameter.ActualName;
292
293      if (ScheduleDecoder != null) {
294        ScheduleEvaluator.ScheduleParameter.ActualName = ScheduleDecoder.ScheduleParameter.ActualName;
295        ScheduleEvaluator.ScheduleParameter.Hidden = true;
296      } else if (SolutionCreator is DirectScheduleRandomCreator) {
297        var directEvaluator = (DirectScheduleRandomCreator)SolutionCreator;
298        ScheduleEvaluator.ScheduleParameter.ActualName = directEvaluator.ScheduleEncodingParameter.ActualName;
299        ScheduleEvaluator.ScheduleParameter.Hidden = true;
300      } else {
301        ScheduleEvaluator.ScheduleParameter.ActualName = ScheduleEvaluator.ScheduleParameter.Name;
302        ScheduleEvaluator.ScheduleParameter.Hidden = false;
303      }
304
305      foreach (var op in Operators.OfType<IScheduleManipulator>()) {
306        op.ScheduleEncodingParameter.ActualName = SolutionCreator.ScheduleEncodingParameter.ActualName;
307        op.ScheduleEncodingParameter.Hidden = true;
308      }
309
310      foreach (var op in Operators.OfType<IScheduleCrossover>()) {
311        op.ChildParameter.ActualName = SolutionCreator.ScheduleEncodingParameter.ActualName;
312        op.ChildParameter.Hidden = true;
313        op.ParentsParameter.ActualName = SolutionCreator.ScheduleEncodingParameter.ActualName;
314        op.ParentsParameter.Hidden = true;
315      }
316
317      foreach (var op in Operators.OfType<BestSchedulingSolutionAnalyzer>()) {
318        op.QualityParameter.ActualName = ScheduleEvaluator.QualityParameter.ActualName;
319        if (ScheduleDecoder != null) {
320          op.ScheduleParameter.ActualName = ScheduleDecoder.ScheduleParameter.ActualName;
321          op.ScheduleParameter.Hidden = true;
322        } else if (SolutionCreator is DirectScheduleRandomCreator) {
323          op.ScheduleParameter.ActualName = ((DirectScheduleRandomCreator)SolutionCreator).ScheduleEncodingParameter.ActualName;
324          op.ScheduleParameter.Hidden = true;
325        } else {
326          op.ScheduleParameter.ActualName = op.ScheduleParameter.Name;
327          op.ScheduleParameter.Hidden = false;
328        }
329      }
330    }
331    #endregion
332
333  }
334}
Note: See TracBrowser for help on using the repository browser.