Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ScopedAlgorithms/HeuristicLab.Problems.Scheduling/3.3/JobShopSchedulingProblem new.cs @ 15632

Last change on this file since 15632 was 13469, checked in by mkommend, 9 years ago

#2521: Refactored problem base classes and adapted scheduling encoding, scheduling problem and unit tests.

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