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

Last change on this file since 8886 was 8886, checked in by abeham, 10 years ago

#1329:

  • Removed unnecessary DueDates parameter
  • Changed ValueParameter to FixedValueParameter for Jobs and Resources
  • Made Job and Task INotifyPropertyChanged
  • Added a JobView and TaskView
File size: 9.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Drawing;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Encodings.ScheduleEncoding;
28using HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix;
29using HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition;
30using HeuristicLab.Encodings.ScheduleEncoding.PriorityRulesVector;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Problems.Scheduling {
37  [Item("Job Shop Scheduling Problem", "Represents a standard Job Shop Scheduling Problem")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class JobShopSchedulingProblem : SchedulingProblem, IProblemInstanceConsumer<JSSPData>, IProblemInstanceExporter<JSSPData>, IStorableContent {
41    #region Default Instance
42    private static readonly JSSPData DefaultInstance = new JSSPData() {
43      Jobs = 10,
44      Resources = 10,
45      BestKnownQuality = 930,
46      ProcessingTimes = new double[,] {
47          { 29, 78,  9, 36, 49, 11, 62, 56, 44, 21 },
48          { 43, 90, 75, 11, 69, 28, 46, 46, 72, 30 },
49          { 91, 85, 39, 74, 90, 10, 12, 89, 45, 33 },
50          { 81, 95, 71, 99,  9, 52, 85, 98, 22, 43 },
51          { 14,  6, 22, 61, 26, 69, 21, 49, 72, 53 },
52          { 84,  2, 52, 95, 48, 72, 47, 65,  6, 25 },
53          { 46, 37, 61, 13, 32, 21, 32, 89, 30, 55 },
54          { 31, 86, 46, 74, 32, 88, 19, 48, 36, 79 },
55          { 76, 69, 76, 51, 85, 11, 40, 89, 26, 74 },
56          { 85, 13, 61,  7, 64, 76, 47, 52, 90, 45 }
57        },
58      Demands = new int[,] {
59          { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
60          { 0, 2, 4, 9, 3, 1, 6, 5, 7, 8 },
61          { 1, 0, 3, 2, 8, 5, 7, 6, 9, 4 },
62          { 1, 2, 0, 4, 6, 8, 7, 3, 9, 5 },
63          { 2, 0, 1, 5, 3, 4, 8, 7, 9, 6 },
64          { 2, 1, 5, 3, 8, 9, 0, 6, 4, 7 },
65          { 1, 0, 3, 2, 6, 5, 9, 8, 7, 4 },
66          { 2, 0, 1, 5, 4, 6, 8, 9, 7, 3 },
67          { 0, 1, 3, 5, 2, 9, 6, 7, 4, 8 },
68          { 1, 0, 2, 6, 8, 9, 5, 3, 4, 7 }
69        }
70    };
71    #endregion
72
73    #region Parameter Properties
74    public ValueParameter<ItemList<Job>> JobDataParameter {
75      get { return (ValueParameter<ItemList<Job>>)Parameters["JobData"]; }
76    }
77    public OptionalValueParameter<Schedule> BestKnownSolutionParameter {
78      get { return (OptionalValueParameter<Schedule>)Parameters["BestKnownSolution"]; }
79    }
80
81    public IFixedValueParameter<IntValue> JobsParameter {
82      get { return (IFixedValueParameter<IntValue>)Parameters["Jobs"]; }
83    }
84    public IFixedValueParameter<IntValue> ResourcesParameter {
85      get { return (IFixedValueParameter<IntValue>)Parameters["Resources"]; }
86    }
87    public ValueParameter<SchedulingEvaluator> SolutionEvaluatorParameter {
88      get { return (ValueParameter<SchedulingEvaluator>)Parameters["SolutionEvaluator"]; }
89    }
90    #endregion
91
92    #region Properties
93    public ItemList<Job> JobData {
94      get { return JobDataParameter.Value; }
95      set { JobDataParameter.Value = value; }
96    }
97    public Schedule BestKnownSolution {
98      get { return BestKnownSolutionParameter.Value; }
99      set { BestKnownSolutionParameter.Value = value; }
100    }
101    public int Jobs {
102      get { return JobsParameter.Value.Value; }
103      set { JobsParameter.Value.Value = value; }
104    }
105    public int Resources {
106      get { return ResourcesParameter.Value.Value; }
107      set { ResourcesParameter.Value.Value = value; }
108    }
109    public SchedulingEvaluator SolutionEvaluator {
110      get { return SolutionEvaluatorParameter.Value; }
111      set { SolutionEvaluatorParameter.Value = value; }
112    }
113    public override Image ItemImage {
114      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
115    }
116    public string Filename { get; set; }
117    #endregion
118
119    public JobShopSchedulingProblem()
120      : base(new SchedulingEvaluationAlgorithm(), new JSMRandomCreator()) {
121      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>()));
122      Parameters.Add(new OptionalValueParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
123
124      Parameters.Add(new FixedValueParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", new IntValue()));
125      Parameters.Add(new FixedValueParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", new IntValue()));
126      Parameters.Add(new ValueParameter<SchedulingEvaluator>("SolutionEvaluator", "The evaluator used to determine the quality of a solution.", new MakespanEvaluator()));
127
128      InitializeOperators();
129      Load(DefaultInstance);
130    }
131
132    [StorableConstructor]
133    private JobShopSchedulingProblem(bool deserializing) : base(deserializing) { }
134    private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
135      : base(original, cloner) {
136    }
137    public override IDeepCloneable Clone(Cloner cloner) {
138      return new JobShopSchedulingProblem(this, cloner);
139    }
140
141    #region Events
142    protected override void OnSolutionCreatorChanged() {
143      InitializeOperators();
144    }
145    #endregion
146
147    #region Problem Instance Handling
148    public void Load(JSSPData data) {
149      var jobData = new ItemList<Job>(data.Jobs);
150      for (int j = 0; j < data.Jobs; j++) {
151        var job = new Job(j, data.DueDates != null ? data.DueDates[j] : double.MaxValue);
152        for (int t = 0; t < data.Resources; t++) {
153          job.Tasks.Add(new Task(t, data.Demands[j, t], j, data.ProcessingTimes[j, t]));
154        }
155        jobData.Add(job);
156      }
157
158      if (data.BestKnownQuality.HasValue) BestKnownQuality = new DoubleValue(data.BestKnownQuality.Value);
159      else BestKnownQuality = null;
160      if (data.BestKnownSchedule != null) {
161        var enc = new JSMEncoding();
162        enc.JobSequenceMatrix = new ItemList<Permutation>(data.Resources);
163        for (int i = 0; i < data.Resources; i++) {
164          enc.JobSequenceMatrix[i] = new Permutation(PermutationTypes.Absolute, new int[data.Jobs]);
165          for (int j = 0; j < data.Jobs; j++) {
166            enc.JobSequenceMatrix[i][j] = data.BestKnownSchedule[i, j];
167          }
168        }
169        BestKnownSolution = new JSMDecoder().CreateScheduleFromEncoding(enc, jobData);
170        if (SolutionEvaluator is MeanTardinessEvaluator)
171          BestKnownQuality = new DoubleValue(MeanTardinessEvaluator.GetMeanTardiness(BestKnownSolution, jobData));
172        else if (SolutionEvaluator is MakespanEvaluator)
173          BestKnownQuality = new DoubleValue(MakespanEvaluator.GetMakespan(BestKnownSolution));
174      }
175
176      JobData = jobData;
177      Jobs = data.Jobs;
178      Resources = data.Resources;
179    }
180
181    public JSSPData Export() {
182      var result = new JSSPData {
183        Name = Name,
184        Description = Description,
185        Jobs = Jobs,
186        Resources = Resources,
187        ProcessingTimes = new double[Jobs, Resources],
188        Demands = new int[Jobs, Resources],
189        DueDates = new double[Jobs]
190      };
191
192      foreach (var job in JobData) {
193        var counter = 0;
194        result.DueDates[job.Index] = job.DueDate;
195        foreach (var task in job.Tasks) {
196          result.ProcessingTimes[task.JobNr, counter] = task.Duration;
197          result.Demands[task.JobNr, counter] = task.ResourceNr;
198          counter++;
199        }
200      }
201      return result;
202    }
203    #endregion
204
205    #region Helpers
206    private void InitializeOperators() {
207      Operators.Clear();
208      ApplyEncoding();
209      Operators.Add(new BestSchedulingSolutionAnalyzer());
210    }
211
212    private void ApplyEncoding() {
213      if (SolutionCreator.GetType() == typeof(JSMRandomCreator)) {
214        Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
215        var decoder = new JSMDecoder();
216        ((SchedulingEvaluationAlgorithm)EvaluatorParameter.ActualValue).InitializeOperatorGraph(decoder);
217      } else if (SolutionCreator.GetType() == typeof(PRVRandomCreator)) {
218        Operators.AddRange(ApplicationManager.Manager.GetInstances<IPRVOperator>());
219        var decoder = new PRVDecoder();
220        ((SchedulingEvaluationAlgorithm)EvaluatorParameter.ActualValue).InitializeOperatorGraph(decoder);
221      } else if (SolutionCreator.GetType() == typeof(PWRRandomCreator)) {
222        Operators.AddRange(ApplicationManager.Manager.GetInstances<IPWROperator>());
223        var decoder = new PWRDecoder();
224        ((SchedulingEvaluationAlgorithm)EvaluatorParameter.ActualValue).InitializeOperatorGraph(decoder);
225      } else if (SolutionCreator.GetType() == typeof(DirectScheduleRandomCreator)) {
226        Operators.AddRange(ApplicationManager.Manager.GetInstances<IDirectScheduleOperator>());
227        ((SchedulingEvaluationAlgorithm)EvaluatorParameter.ActualValue).InitializeOperatorGraph<Schedule>();
228      }
229    }
230    #endregion
231
232  }
233}
Note: See TracBrowser for help on using the repository browser.