Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14778 was 14509, checked in by abeham, 8 years ago

#2720: fixed default instance name

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