Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6219 was 6177, checked in by jhelm, 14 years ago

#1329: Implemented PriorityRulesVector based encoding and added new operators to the JSMEncoding. Added Gantt-Charts for visualization of schedules and problems.

File size: 7.8 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.Encodings;
34using HeuristicLab.Problems.Scheduling.Evaluators;
35using HeuristicLab.Parameters;
36using HeuristicLab.Problems.Scheduling.Encodings.JobSequenceMatrix;
37using HeuristicLab.Encodings.PermutationEncoding;
38using HeuristicLab.Problems.Scheduling.Analyzers;
39using HeuristicLab.PluginInfrastructure;
40using HeuristicLab.Problems.Scheduling.Encodings.JobSequenceMatrix.Manipulators;
41using HeuristicLab.Problems.Scheduling.Encodings.JobSequenceMatrix.Crossovers;
42using HeuristicLab.Problems.Scheduling.Interfaces;
43using HeuristicLab.Problems.Scheduling.Encodings.PriorityRulesVector;
44using HeuristicLab.Encodings.IntegerVectorEncoding;
45
46namespace HeuristicLab.Problems.Scheduling {
47  [Item("JobShop Scheduling Problem", "Represents a standard JobShop Scheduling Problem")]
48  [Creatable("Problems")]
49  [StorableClass]
50  public sealed class JobShopSchedulingProblem : SchedulingProblem {
51    [StorableConstructor]
52    private JobShopSchedulingProblem(bool deserializing) : base(deserializing) { }
53    private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
54      : base(original, cloner) {
55        operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
56        this.encodingType = original.encodingType;
57        AttachEventHandlers();
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new JobShopSchedulingProblem(this, cloner);
61    }
62
63
64    #region Parameter Properties
65    public ValueParameter<ItemList<JSSPJob>> JobsParameter {
66      get { return (ValueParameter<ItemList<JSSPJob>>)Parameters["Jobs"]; }
67    }
68    public OptionalValueParameter<Schedule> BestKnownSolutionParameter {
69      get { return (OptionalValueParameter<Schedule>)Parameters["BestKnownSolution"]; }
70    }
71    #endregion
72
73    #region Properties
74    public ItemList<JSSPJob> Jobs {
75      get { return JobsParameter.Value; }
76      set { JobsParameter.Value = value; }
77    }
78    public Schedule BestKnownSolution {
79      get { return BestKnownSolutionParameter.Value; }
80      set { BestKnownSolutionParameter.Value = value; }
81    }
82    #endregion
83
84
85    [Storable]
86    private List<IOperator> operators;
87
88    [Storable]
89    private JSSPEncodingTypes encodingType;
90
91    public JobShopSchedulingProblem()
92      : base(new SchedulingEvaluator (), new JSMRandomCreator ()) {
93        Parameters.Add(new ValueParameter<ItemList<JSSPJob>>("Jobs", "Jobdata defining the precedence relationships and the duration of the tasks in this JSSP-Instance.", new ItemList<JSSPJob>()));
94        Parameters.Add(new OptionalValueParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
95        encodingType = JSSPEncodingTypes.JSMEncoding;
96        InitializeOperators();
97        AttachEventHandlers();
98    }
99
100    #region Events
101    //TODO: Implement event handlers for problem specific events!
102    protected override void OnSolutionCreatorChanged() {
103      if (SolutionCreator.GetType().Equals(typeof(JSMRandomCreator)))
104        encodingType = JSSPEncodingTypes.JSMEncoding;
105      else
106        encodingType = JSSPEncodingTypes.PRVEncoding;
107
108      InitializeOperators();
109    }
110    #endregion
111
112    #region Helpers
113    private void InitializeOperators() {
114      Operators.Clear();
115      ApplyEncoding();
116      Operators.Add(new BestSchedulingSolutionAnalyzer());
117      //ParameterizeAnalyzer();
118      //ParameterizeOperators();
119    }
120
121    private void ApplyEncoding() {
122      if (encodingType == JSSPEncodingTypes.JSMEncoding) {
123        Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
124        ((SchedulingEvaluator)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<JSMEncoding>(new JSMDecoder(), new MakespanEvaluator());
125      } else {
126        if (encodingType == JSSPEncodingTypes.PRVEncoding) {
127          Operators.AddRange(ApplicationManager.Manager.GetInstances<IPRVOperator>());
128          ((SchedulingEvaluator)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PRVEncoding>(new PRVDecoder(), new MakespanEvaluator());
129        }
130      }
131    }
132
133    private void AttachEventHandlers() {
134      //TODO: Attach event handlers implemented aabove to appropriate events of the problem.
135    }
136
137    private void ParameterizeOperators() {
138      throw new NotImplementedException();
139    }
140
141    private void ParameterizeAnalyzer() {
142      throw new NotImplementedException();
143    }
144    #endregion
145
146    #region Importmethods
147    private List<string> SplitString(string line) {
148      List<string> data = new List<string>(line.Split(' '));
149      List<string> result = new List<string>();
150
151      foreach (string s in data) {
152        if (!String.IsNullOrEmpty(s) && s != "" && s != " ")
153          result.Add(s);
154      }
155
156      return result;
157    }
158    public void ImportFromORLibrary(string fileName) {
159      if (!File.Exists(fileName))
160        return;
161      StreamReader problemFile = new StreamReader(fileName);
162      //assures that the parser only reads the first problem instance given in the file
163      bool problemFound = false;
164
165      Jobs = new ItemList<JSSPJob>();
166
167      while (!problemFile.EndOfStream && !problemFound) {
168        string line = problemFile.ReadLine();
169        List<string> data = SplitString(line);
170        if (data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
171          int jobCount = 0;
172          //data[0] = nrOfJobs
173          //data[1] = nrOfResources
174          if (data.Count > 2)
175            BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse (data[2]));
176          line = problemFile.ReadLine();
177          data = SplitString(line);
178          while (!problemFile.EndOfStream && data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
179            JSSPJob j = new JSSPJob(new IntValue(jobCount));
180            for (int i = 0; i < data.Count; i++) {
181              JSSPTask t = new JSSPTask(new IntValue (i / 2), new IntValue(Int32.Parse(data[i])), new DoubleValue(Double.Parse(data[i + 1])), j);
182              if (j.Tasks.Count > 0) {
183                t.PreviousTask = j.Tasks[j.Tasks.Count - 1];
184                j.Tasks[j.Tasks.Count - 1].NextTask = t;
185                t.StartTime = new DoubleValue (t.PreviousTask.EndTime.Value + 2);
186              }
187              j.Tasks.Add(t);
188              i++;
189            }//for
190            this.Jobs.Add(j);
191            jobCount++;
192            line = problemFile.ReadLine();
193            data = SplitString(line);
194          }//while
195          problemFound = true;
196        }//if
197      }//while
198      problemFile.Close();
199    }
200    #endregion
201
202  }
203}
Note: See TracBrowser for help on using the repository browser.