Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1329: Added DirectSchedule-Classes for optimization with the direct-schedule encoding.

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