Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1329: Implemented basic functionalities of problemdefinition, encodings and primitive operators. A GA can already be applied on the problem to compute a solution it seems however that the current implementation isn't very efficient so the process takes some time.

File size: 6.7 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;
43
44namespace HeuristicLab.Problems.Scheduling {
45  [Item("JobShop Scheduling Problem", "Represents a standard JobShop Scheduling Problem")]
46  [Creatable("Problems")]
47  [StorableClass]
48  public sealed class JobShopSchedulingProblem : SchedulingProblem {
49    [StorableConstructor]
50    private JobShopSchedulingProblem(bool deserializing) : base(deserializing) { }
51    private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
52      : base(original, cloner) {
53        operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new JobShopSchedulingProblem(this, cloner);
57    }
58
59
60    #region Parameter Properties
61    public ValueParameter<IntValue> NrOfResourcesParameter {
62      get { return (ValueParameter<IntValue>)Parameters["NrOfResources"]; }
63    }
64    public ValueParameter<ItemList<JSSPJob>> JobsParameter {
65      get { return (ValueParameter<ItemList<JSSPJob>>)Parameters["Jobs"]; }
66    }
67    public OptionalValueParameter<JSSPEncoding> BestKnownSolutionParameter {
68      get { return (OptionalValueParameter<JSSPEncoding>)Parameters["BestKnownSolution"]; }
69    }
70    #endregion
71
72    #region Properties
73    public IntValue NrOfResources {
74      get { return NrOfResourcesParameter.Value; }
75      set { NrOfResourcesParameter.Value = value; }
76    }
77    public ItemList<JSSPJob> Jobs {
78      get { return JobsParameter.Value; }
79      set { JobsParameter.Value = value; }
80    }
81    public JSSPEncoding BestKnownSolution {
82      get { return BestKnownSolutionParameter.Value; }
83      set { BestKnownSolutionParameter.Value = value; }
84    }
85    #endregion
86
87
88    [Storable]
89    private List<IOperator> operators;
90
91
92    public JobShopSchedulingProblem()
93      : base(new MakespanEvaluator (), new JSMRandomCreator ()) {
94        Parameters.Add(new ValueParameter<IntValue>("NrOfResources", "Number of Resources used in this JSSP-Instance.", new IntValue()));
95        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>()));
96        Parameters.Add(new OptionalValueParameter<JSSPEncoding>("BestKnownSolution", "The best known solution of this JSSP instance."));
97
98        InitializeOperators();
99    }
100
101    #region Events
102    //TODO: Implement event handlers for problem specific events!
103    #endregion
104
105    #region Helpers
106    private void InitializeOperators() {
107      Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
108      Operators.Add(new BestSchedulingSolutionAnalyzer());
109      //ParameterizeAnalyzer();
110      //ParameterizeOperators();
111    }
112
113    private void AttachEventHandlers() {
114      //TODO: Attach event handlers implemented aabove to appropriate events of the problem.
115    }
116
117    private void ParameterizeOperators() {
118      throw new NotImplementedException();
119    }
120
121    private void ParameterizeAnalyzer() {
122      throw new NotImplementedException();
123    }
124    #endregion
125
126    #region Importmethods
127    private List<string> SplitString(string line) {
128      List<string> data = new List<string>(line.Split(' '));
129      List<string> result = new List<string>();
130
131      foreach (string s in data) {
132        if (!String.IsNullOrEmpty(s) && s != "" && s != " ")
133          result.Add(s);
134      }
135
136      return result;
137    }
138    public void ImportFromORLibrary(string fileName) {
139      if (!File.Exists(fileName))
140        return;
141      StreamReader problemFile = new StreamReader(fileName);
142      //assures that the parser only reads the first problem instance given in the file
143      bool problemFound = false;
144
145      Jobs = new ItemList<JSSPJob>();
146
147      while (!problemFile.EndOfStream && !problemFound) {
148        string line = problemFile.ReadLine();
149        List<string> data = SplitString(line);
150        int taskCount = 0;
151        if (data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
152          NrOfResources = new IntValue(Int32.Parse(data[1]));
153          int jobCount = 0;
154          line = problemFile.ReadLine();
155          data = SplitString(line);
156          while (!problemFile.EndOfStream && data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
157            JSSPJob j = new JSSPJob(new IntValue(jobCount));
158            for (int i = 0; i < data.Count; i++) {
159              JSSPTask t = new JSSPTask(new IntValue (taskCount), new IntValue(Int32.Parse(data[i])), new DoubleValue(Double.Parse(data[i + 1])), j);
160              taskCount++;
161              if (j.Tasks.Count > 0)
162                t.PreviousTask = j.Tasks[j.Tasks.Count - 1];
163              j.Tasks.Add(t);
164              i++;
165            }//for
166            this.Jobs.Add(j);
167            jobCount++;
168            line = problemFile.ReadLine();
169            data = SplitString(line);
170          }//while
171          problemFound = true;
172        }//if
173      }//while
174      problemFile.Close();
175    }
176    #endregion
177
178  }
179}
Note: See TracBrowser for help on using the repository browser.