Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Scheduling/HeuristicLab.Problems.Scheduling/3.3/JobShopSchedulingProblem.cs @ 6406

Last change on this file since 6406 was 6406, checked in by jhelm, 13 years ago

#1329: Applied suggestions from codereview. Added unit-tests. Renamed encoding-project.

File size: 11.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.IO;
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;
36
37namespace HeuristicLab.Problems.Scheduling {
38  [Item("JobShop Scheduling Problem", "Represents a standard JobShop Scheduling Problem")]
39  [Creatable("Problems")]
40  [StorableClass]
41  public sealed class JobShopSchedulingProblem : SchedulingProblem {
42    #region Parameter Properties
43    public ValueParameter<ItemList<Job>> JobDataParameter {
44      get { return (ValueParameter<ItemList<Job>>)Parameters["JobData"]; }
45    }
46    public OptionalValueParameter<Schedule> BestKnownSolutionParameter {
47      get { return (OptionalValueParameter<Schedule>)Parameters["BestKnownSolution"]; }
48    }
49
50    public ValueParameter<IntValue> JobsParameter {
51      get { return (ValueParameter<IntValue>)Parameters["Jobs"]; }
52    }
53    public ValueParameter<IntValue> ResourcesParameter {
54      get { return (ValueParameter<IntValue>)Parameters["Resources"]; }
55    }
56    public ValueParameter<SchedulingEvaluator> SolutionEvaluatorParameter {
57      get { return (ValueParameter<SchedulingEvaluator>)Parameters["SolutionEvaluator"]; }
58    }
59    public ValueParameter<BoolValue> DueDatesParameter {
60      get { return (ValueParameter<BoolValue>)Parameters["DueDates"]; }
61    }
62    #endregion
63
64    #region Properties
65    public ItemList<Job> JobData {
66      get { return JobDataParameter.Value; }
67      set { JobDataParameter.Value = value; }
68    }
69    public Schedule BestKnownSolution {
70      get { return BestKnownSolutionParameter.Value; }
71      set { BestKnownSolutionParameter.Value = value; }
72    }
73    public IntValue Jobs {
74      get { return JobsParameter.Value; }
75      set { JobsParameter.Value = value; }
76    }
77    public IntValue Resources {
78      get { return ResourcesParameter.Value; }
79      set { ResourcesParameter.Value = value; }
80    }
81    public SchedulingEvaluator SolutionEvaluator {
82      get { return SolutionEvaluatorParameter.Value; }
83      set { SolutionEvaluatorParameter.Value = value; }
84    }
85    public BoolValue DueDates {
86      get { return DueDatesParameter.Value; }
87      set { DueDatesParameter.Value = value; }
88    }
89    #endregion
90
91    public JobShopSchedulingProblem()
92      : base(new SchedulingEvaluationAlgorithm(), new JSMRandomCreator()) {
93      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>()));
94      Parameters.Add(new OptionalValueParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
95
96      Parameters.Add(new ValueParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", new IntValue()));
97      Parameters.Add(new ValueParameter<IntValue>("Resources", "The number of resources used this JSSP instance.", new IntValue()));
98      Parameters.Add(new ValueParameter<BoolValue>("DueDates", "Determines whether the problem instance uses due dates or not.", new BoolValue()));
99      Parameters.Add(new ValueParameter<SchedulingEvaluator>("SolutionEvaluator", "The evaluator used to determine the quality of a solution.", new MakespanEvaluator()));
100
101      InitializeOperators();
102      InitializeProblemInstance();
103    }
104
105    [StorableConstructor]
106    private JobShopSchedulingProblem(bool deserializing) : base(deserializing) { }
107    private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
108      : base(original, cloner) {
109    }
110    public override IDeepCloneable Clone(Cloner cloner) {
111      return new JobShopSchedulingProblem(this, cloner);
112    }
113
114    #region Events
115    protected override void OnSolutionCreatorChanged() {
116      InitializeOperators();
117    }
118
119    protected override void OnEvaluatorChanged() {
120      base.OnEvaluatorChanged();
121    }
122    #endregion
123
124    #region Helpers
125    private void InitializeProblemInstance() {
126      Jobs = new IntValue(10);
127      Resources = new IntValue(10);
128      BestKnownQuality = new DoubleValue(930);
129      JobData = new ItemList<Job>();
130      List<string> data = new List<string>
131      {"0 29 1 78 2  9 3 36 4 49 5 11 6 62 7 56 8 44 9 21",
132       "0 43 2 90 4 75 9 11 3 69 1 28 6 46 5 46 7 72 8 30",
133       "1 91 0 85 3 39 2 74 8 90 5 10 7 12 6 89 9 45 4 33",
134       "1 81 2 95 0 71 4 99 6  9 8 52 7 85 3 98 9 22 5 43",
135       "2 14 0  6 1 22 5 61 3 26 4 69 8 21 7 49 9 72 6 53",
136       "2 84 1  2 5 52 3 95 8 48 9 72 0 47 6 65 4  6 7 25",
137       "1 46 0 37 3 61 2 13 6 32 5 21 9 32 8 89 7 30 4 55",
138       "2 31 0 86 1 46 5 74 4 32 6 88 8 19 9 48 7 36 3 79",
139       "0 76 1 69 3 76 5 51 2 85 9 11 6 40 7 89 4 26 8 74",
140       "1 85 0 13 2 61 6  7 8 64 9 76 5 47 3 52 4 90 7 45" };
141
142      int jobCount = 0;
143      foreach (string s in data) {
144        List<string> split = SplitString(s);
145        JobData.Add(CreateJobFromData(split, jobCount++));
146      }
147    }
148
149    private void InitializeOperators() {
150      Operators.Clear();
151      ApplyEncoding();
152      Operators.Add(new BestSchedulingSolutionAnalyzer());
153    }
154
155    private void ApplyEncoding() {
156      if (SolutionCreator.GetType().Equals(typeof(JSMRandomCreator))) {
157        Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
158        JSMDecoder decoder = new JSMDecoder();
159        ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<JSMEncoding>(decoder);
160      } else {
161        if (SolutionCreator.GetType().Equals(typeof(PRVRandomCreator))) {
162          Operators.AddRange(ApplicationManager.Manager.GetInstances<IPRVOperator>());
163          PRVDecoder decoder = new PRVDecoder();
164          ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PRVEncoding>(decoder);
165        } else {
166          if (SolutionCreator.GetType().Equals(typeof(PWRRandomCreator))) {
167            Operators.AddRange(ApplicationManager.Manager.GetInstances<IPWROperator>());
168            PWRDecoder decoder = new PWRDecoder();
169            ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PWREncoding>(decoder);
170          }
171        }
172      }
173    }
174    #endregion
175
176    #region Importmethods
177    private List<string> SplitString(string line) {
178      if (line == null)
179        return null;
180      List<string> data = new List<string>(line.Split(' '));
181      List<string> result = new List<string>();
182
183      foreach (string s in data) {
184        if (!String.IsNullOrEmpty(s) && s != "" && s != " ")
185          result.Add(s);
186      }
187
188      return result;
189    }
190    private int[] GetIntArray(List<string> data) {
191      int[] arr = new int[data.Count];
192      for (int i = 0; i < data.Count; i++) {
193        arr[i] = Int32.Parse(data[i]);
194      }
195      return arr;
196    }
197    private Job CreateJobFromData(List<string> data, int jobCount) {
198      DoubleValue dueDate = null;
199      int dataCount = data.Count;
200      if (DueDates.Value) {
201        dueDate = new DoubleValue(Double.Parse(data[data.Count - 1]));
202        dataCount--;
203      }
204      Job j = new Job(new IntValue(jobCount), dueDate);
205      for (int i = 0; i < dataCount; i++) {
206        Task t = new Task(i / 2, Int32.Parse(data[i]), jobCount, Double.Parse(data[i + 1]));
207        j.Tasks.Add(t);
208        i++;
209      }//for
210      return j;
211    }
212
213    public void ImportFromORLibrary(string fileName) {
214      if (!File.Exists(fileName))
215        return;
216      StreamReader problemFile = new StreamReader(fileName);
217      //assures that the parser only reads the first problem instance given in the file
218      bool problemFound = false;
219
220      JobData = new ItemList<Job>();
221
222      while (!problemFile.EndOfStream && !problemFound) {
223        string line = problemFile.ReadLine();
224        List<string> data = SplitString(line);
225        if (data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
226          int jobCount = 0;
227          Jobs = new IntValue(Int32.Parse(data[0]));
228          Resources = new IntValue(Int32.Parse(data[1]));
229          //data[2] = bestKnownQuality (double)
230          //data[3] = dueDates (0|1)
231          DueDates.Value = false;
232          if (data.Count > 2)
233            BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse(data[2]));
234          if (data.Count > 3 && data[3] == "1")
235            DueDates.Value = true;
236          line = problemFile.ReadLine();
237          data = SplitString(line);
238          while (!problemFile.EndOfStream && data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
239            Job j = CreateJobFromData(data, jobCount);
240            this.JobData.Add(j);
241            jobCount++;
242            line = problemFile.ReadLine();
243            data = SplitString(line);
244          }//while
245          problemFound = true;
246        }//if
247      }//while
248      problemFile.Close();
249    }
250
251    public void ImportJSMSolution(string fileName) {
252      if (!File.Exists(fileName))
253        return;
254      StreamReader solutionFile = new StreamReader(fileName);
255      //assures that the parser only reads the first solution instance given in the file
256      bool solutionFound = false;
257      JSMEncoding solution = new JSMEncoding();
258      while (!solutionFile.EndOfStream && !solutionFound) {
259
260        string line = solutionFile.ReadLine();
261        List<string> data = SplitString(line);
262        if (data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
263          if (data.Count > 2)
264            BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse(data[2]));
265          line = solutionFile.ReadLine();
266          data = SplitString(line);
267          while (data != null && data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
268            Permutation p = new Permutation(PermutationTypes.Absolute, GetIntArray(data));
269            solution.JobSequenceMatrix.Add(p);
270
271            line = solutionFile.ReadLine();
272            data = SplitString(line);
273          }//while
274          solutionFound = true;
275        }//if
276      }//while
277      solutionFile.Close();
278
279      JSMDecoder decoder = new JSMDecoder();
280      Schedule result = decoder.CreateScheduleFromEncoding(solution, JobData);
281      BestKnownSolution = result;
282    }
283    #endregion
284
285  }
286}
Note: See TracBrowser for help on using the repository browser.