Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Scheduling/3.3/JobShopSchedulingProblem.cs @ 17680

Last change on this file since 17680 was 17680, checked in by abeham, 4 years ago

#2521: working on problems / fixing P3

File size: 11.6 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.ComponentModel;
24using System.Drawing;
25using System.Linq;
26using System.Threading;
27using HEAL.Attic;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.PermutationEncoding;
32using HeuristicLab.Encodings.ScheduleEncoding;
33using HeuristicLab.Optimization;
34using HeuristicLab.Parameters;
35using HeuristicLab.Problems.Instances;
36
37namespace HeuristicLab.Problems.Scheduling {
38  public enum JSSPObjective { Makespan, Tardiness }
39
40  [Item("Job Shop Scheduling Problem (JSSP)", "Represents a standard Job Shop Scheduling Problem")]
41  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 120)]
42  [StorableType("d6c72bd1-c6cc-4efb-9cc5-b73bb3845799")]
43  public sealed class JobShopSchedulingProblem : SingleObjectiveProblem<IScheduleEncoding, IScheduleSolution>, IProblemInstanceConsumer<JSSPData>, IProblemInstanceExporter<JSSPData>, IStorableContent {
44    #region Default Instance
45    private static readonly JSSPData DefaultInstance = new JSSPData() {
46      Jobs = 10,
47      Resources = 10,
48      BestKnownQuality = 930,
49      ProcessingTimes = new double[,] {
50          { 29, 78,  9, 36, 49, 11, 62, 56, 44, 21 },
51          { 43, 90, 75, 11, 69, 28, 46, 46, 72, 30 },
52          { 91, 85, 39, 74, 90, 10, 12, 89, 45, 33 },
53          { 81, 95, 71, 99,  9, 52, 85, 98, 22, 43 },
54          { 14,  6, 22, 61, 26, 69, 21, 49, 72, 53 },
55          { 84,  2, 52, 95, 48, 72, 47, 65,  6, 25 },
56          { 46, 37, 61, 13, 32, 21, 32, 89, 30, 55 },
57          { 31, 86, 46, 74, 32, 88, 19, 48, 36, 79 },
58          { 76, 69, 76, 51, 85, 11, 40, 89, 26, 74 },
59          { 85, 13, 61,  7, 64, 76, 47, 52, 90, 45 }
60        },
61      Demands = new int[,] {
62          { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
63          { 0, 2, 4, 9, 3, 1, 6, 5, 7, 8 },
64          { 1, 0, 3, 2, 8, 5, 7, 6, 9, 4 },
65          { 1, 2, 0, 4, 6, 8, 7, 3, 9, 5 },
66          { 2, 0, 1, 5, 3, 4, 8, 7, 9, 6 },
67          { 2, 1, 5, 3, 8, 9, 0, 6, 4, 7 },
68          { 1, 0, 3, 2, 6, 5, 9, 8, 7, 4 },
69          { 2, 0, 1, 5, 4, 6, 8, 9, 7, 3 },
70          { 0, 1, 3, 5, 2, 9, 6, 7, 4, 8 },
71          { 1, 0, 2, 6, 8, 9, 5, 3, 4, 7 }
72        }
73    };
74    #endregion
75
76    public override Image ItemImage {
77      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
78    }
79   
80    #region Parameter Properties
81    [Storable] public ReferenceParameter<ItemList<Job>> JobDataParameter { get; private set; }
82    [Storable] public OptionalValueParameter<Schedule> BestKnownSolutionParameter { get; private set; }
83    [Storable] public ReferenceParameter<IntValue> JobsParameter { get; private set; }
84    [Storable] public ReferenceParameter<IntValue> ResourcesParameter { get; private set; }
85    [Storable] public IFixedValueParameter<EnumValue<JSSPObjective>> ObjectiveParameter { get; private set; }
86    #endregion
87
88    #region Properties
89    public ItemList<Job> JobData {
90      get { return JobDataParameter.Value; }
91      set { JobDataParameter.Value = value; }
92    }
93    public Schedule BestKnownSolution {
94      get { return BestKnownSolutionParameter.Value; }
95      set { BestKnownSolutionParameter.Value = value; }
96    }
97    public int Jobs {
98      get { return JobsParameter.Value.Value; }
99      set { JobsParameter.Value.Value = value; }
100    }
101    public int Resources {
102      get { return ResourcesParameter.Value.Value; }
103      set { ResourcesParameter.Value.Value = value; }
104    }
105    public JSSPObjective Objective {
106      get { return ObjectiveParameter.Value.Value; }
107      set { ObjectiveParameter.Value.Value = value; }
108    }
109
110    public new IScheduleEncoding Encoding {
111      get { return base.Encoding; }
112      set { base.Encoding = value; }
113    }
114    #endregion
115
116    [StorableConstructor]
117    private JobShopSchedulingProblem(StorableConstructorFlag _) : base(_) { }
118    [StorableHook(HookType.AfterDeserialization)]
119    private void AfterDeserialization() {
120      RegisterEventHandlers();
121    }
122
123    private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
124      : base(original, cloner) {
125      JobDataParameter = cloner.Clone(original.JobDataParameter);
126      BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter);
127      JobsParameter = cloner.Clone(original.JobsParameter);
128      ResourcesParameter = cloner.Clone(original.ResourcesParameter);
129      ObjectiveParameter = cloner.Clone(original.ObjectiveParameter);
130
131      RegisterEventHandlers();
132    }
133    public override IDeepCloneable Clone(Cloner cloner) {
134      return new JobShopSchedulingProblem(this, cloner);
135    }
136
137    public JobShopSchedulingProblem()
138      : base(new JobSequenceMatrixEncoding()) {
139      Parameters.Add(JobDataParameter = new ReferenceParameter<ItemList<Job>>("JobData", "Jobdata defining the precedence relationships and the duration of the tasks in this JSSP-Instance.", Encoding.JobDataParameter));
140      Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
141      Parameters.Add(JobsParameter = new ReferenceParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", Encoding.JobsParameter));
142      Parameters.Add(ResourcesParameter = new ReferenceParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", Encoding.ResourcesParameter));
143      Parameters.Add(ObjectiveParameter = new FixedValueParameter<EnumValue<JSSPObjective>>("Objective", "The objective to use in the evaluation of a schedule.", new EnumValue<JSSPObjective>(JSSPObjective.Makespan)));
144      EncodingParameter.Hidden = false;
145
146      RegisterEventHandlers();
147      Load(DefaultInstance);
148    }
149
150    public override ISingleObjectiveEvaluationResult Evaluate(IScheduleSolution solution, IRandom random, CancellationToken cancellationToken) {
151      var schedule = Encoding.Decode(solution, JobData);
152      switch (Objective) {
153        case JSSPObjective.Makespan:
154          return new SingleObjectiveEvaluationResult(Makespan.Calculate(schedule));
155        case JSSPObjective.Tardiness:
156          return new SingleObjectiveEvaluationResult(MeanTardiness.Calculate(schedule, JobData));
157        default:
158          throw new InvalidOperationException("Objective " + Objective + " unknown");
159      }
160    }
161
162    public override void Analyze(IScheduleSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) {
163      base.Analyze(solutions, qualities, results, random);
164
165      bool max = Maximization;
166
167      int i = -1;
168      if (!max)
169        i = qualities.Select((x, index) => new { index, x }).OrderBy(x => x.x).First().index;
170      else i = qualities.Select((x, index) => new { index, x }).OrderByDescending(x => x.x).First().index;
171
172      if (double.IsNaN(BestKnownQuality) ||
173          max && qualities[i] > BestKnownQuality ||
174          !max && qualities[i] < BestKnownQuality) {
175        BestKnownQuality = qualities[i];
176        BestKnownSolution = Encoding.Decode(solutions[i], JobData);
177      }
178      Schedule bestSolution;
179      if (results.TryGetValue("Best Scheduling Solution", out var result)) {
180        bestSolution = result.Value as Schedule;
181      } else bestSolution = null;
182
183      if (bestSolution == null || IsBetter(bestSolution.Quality, qualities[i]))
184        results.AddOrUpdateResult("Best Scheduling Solution", Encoding.Decode(solutions[i], JobData));
185    }
186
187    protected override void OnEncodingChanged() {
188      base.OnEncodingChanged();
189      Parameters.Remove(JobDataParameter);
190      Parameters.Add(JobDataParameter = new ReferenceParameter<ItemList<Job>>("JobData", "Jobdata defining the precedence relationships and the duration of the tasks in this JSSP-Instance.", Encoding.JobDataParameter));
191      Parameters.Remove(JobsParameter);
192      Parameters.Add(JobsParameter = new ReferenceParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", Encoding.JobsParameter));
193      Parameters.Remove(ResourcesParameter);
194      Parameters.Add(ResourcesParameter = new ReferenceParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", Encoding.ResourcesParameter));
195    }
196
197
198    private void RegisterEventHandlers() {
199      JobDataParameter.ValueChanged += JobDataParameterOnValueChanged;
200      JobData.PropertyChanged += JobDataOnPropertyChanged;
201    }
202
203    private void JobDataParameterOnValueChanged(object sender, EventArgs e) {
204      JobData.PropertyChanged += JobDataOnPropertyChanged;
205      Jobs = JobData.Count;
206    }
207
208    private void JobDataOnPropertyChanged(object sender, PropertyChangedEventArgs e) {
209      if (e.PropertyName == nameof(JobData.Count)) {
210        Jobs = JobData.Count;
211      }
212    }
213
214    #region Problem Instance Handling
215    public void Load(JSSPData data) {
216      var jobData = new ItemList<Job>(data.Jobs);
217      for (int j = 0; j < data.Jobs; j++) {
218        var job = new Job(j, data.DueDates != null ? data.DueDates[j] : double.MaxValue);
219        for (int t = 0; t < data.Resources; t++) {
220          job.Tasks.Add(new Task(t, data.Demands[j, t], j, data.ProcessingTimes[j, t]));
221        }
222        jobData.Add(job);
223      }
224
225      BestKnownQuality = data.BestKnownQuality ?? double.NaN;
226      if (data.BestKnownSchedule != null) {
227        var enc = new JSM(0);
228        for (int i = 0; i < data.Resources; i++) {
229          enc.JobSequenceMatrix.Add(new Permutation(PermutationTypes.Absolute, new int[data.Jobs]));
230          for (int j = 0; j < data.Jobs; j++) {
231            enc.JobSequenceMatrix[i][j] = data.BestKnownSchedule[i, j];
232          }
233        }
234        BestKnownSolution = JSMDecoder.Decode(enc, jobData, JSMDecodingErrorPolicy.RandomPolicy, JSMForcingStrategy.SwapForcing);
235        switch (Objective) {
236          case JSSPObjective.Makespan:
237            BestKnownQuality = Makespan.Calculate(BestKnownSolution);
238            break;
239          case JSSPObjective.Tardiness:
240            BestKnownQuality = MeanTardiness.Calculate(BestKnownSolution, jobData);
241            break;
242        }
243      }
244
245      JobData = jobData;
246      Resources = data.Resources;
247    }
248
249    public JSSPData Export() {
250      var result = new JSSPData {
251        Name = Name,
252        Description = Description,
253        Jobs = Jobs,
254        Resources = Resources,
255        ProcessingTimes = new double[Jobs, Resources],
256        Demands = new int[Jobs, Resources],
257        DueDates = new double[Jobs]
258      };
259
260      foreach (var job in JobData) {
261        var counter = 0;
262        result.DueDates[job.Index] = job.DueDate;
263        foreach (var task in job.Tasks) {
264          result.ProcessingTimes[task.JobNr, counter] = task.Duration;
265          result.Demands[task.JobNr, counter] = task.ResourceNr;
266          counter++;
267        }
268      }
269      return result;
270    }
271    #endregion
272
273  }
274}
Note: See TracBrowser for help on using the repository browser.