Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1329: Did some minor refactoring.

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