1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.IO;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 | using HeuristicLab.Encodings.ScheduleEncoding;
|
---|
31 | using HeuristicLab.Encodings.ScheduleEncoding.JobSequenceMatrix;
|
---|
32 | using HeuristicLab.Encodings.ScheduleEncoding.PermutationWithRepetition;
|
---|
33 | using HeuristicLab.Encodings.ScheduleEncoding.PriorityRulesVector;
|
---|
34 | using HeuristicLab.Parameters;
|
---|
35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
36 | using HeuristicLab.PluginInfrastructure;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Problems.Scheduling {
|
---|
39 | [Item("JobShop Scheduling Problem", "Represents a standard JobShop Scheduling Problem")]
|
---|
40 | [Creatable("Problems")]
|
---|
41 | [StorableClass]
|
---|
42 | public sealed class JobShopSchedulingProblem : SchedulingProblem, IStorableContent {
|
---|
43 | #region Parameter Properties
|
---|
44 | public ValueParameter<ItemList<Job>> JobDataParameter {
|
---|
45 | get { return (ValueParameter<ItemList<Job>>)Parameters["JobData"]; }
|
---|
46 | }
|
---|
47 | public OptionalValueParameter<Schedule> BestKnownSolutionParameter {
|
---|
48 | get { return (OptionalValueParameter<Schedule>)Parameters["BestKnownSolution"]; }
|
---|
49 | }
|
---|
50 |
|
---|
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 | }
|
---|
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"]; }
|
---|
62 | }
|
---|
63 | #endregion
|
---|
64 |
|
---|
65 | #region Properties
|
---|
66 | public ItemList<Job> JobData {
|
---|
67 | get { return JobDataParameter.Value; }
|
---|
68 | set { JobDataParameter.Value = value; }
|
---|
69 | }
|
---|
70 | public Schedule BestKnownSolution {
|
---|
71 | get { return BestKnownSolutionParameter.Value; }
|
---|
72 | set { BestKnownSolutionParameter.Value = value; }
|
---|
73 | }
|
---|
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 | }
|
---|
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 | }
|
---|
90 | public override Image ItemImage {
|
---|
91 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
92 | }
|
---|
93 | public string Filename { get; set; }
|
---|
94 | #endregion
|
---|
95 |
|
---|
96 | public JobShopSchedulingProblem()
|
---|
97 | : base(new SchedulingEvaluationAlgorithm(), new JSMRandomCreator()) {
|
---|
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."));
|
---|
100 |
|
---|
101 | Parameters.Add(new ValueParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", new IntValue()));
|
---|
102 | Parameters.Add(new ValueParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", new IntValue()));
|
---|
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();
|
---|
108 | }
|
---|
109 |
|
---|
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 |
|
---|
119 | #region Events
|
---|
120 | protected override void OnSolutionCreatorChanged() {
|
---|
121 | InitializeOperators();
|
---|
122 | }
|
---|
123 |
|
---|
124 | protected override void OnEvaluatorChanged() {
|
---|
125 | base.OnEvaluatorChanged();
|
---|
126 | }
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 | #region Helpers
|
---|
130 | private void InitializeProblemInstance() {
|
---|
131 | Jobs = new IntValue(10);
|
---|
132 | Resources = new IntValue(10);
|
---|
133 | BestKnownQuality = new DoubleValue(930);
|
---|
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 |
|
---|
154 | private void InitializeOperators() {
|
---|
155 | Operators.Clear();
|
---|
156 | ApplyEncoding();
|
---|
157 | Operators.Add(new BestSchedulingSolutionAnalyzer());
|
---|
158 | }
|
---|
159 |
|
---|
160 | private void ApplyEncoding() {
|
---|
161 | if (SolutionCreator.GetType().Equals(typeof(JSMRandomCreator))) {
|
---|
162 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
|
---|
163 | JSMDecoder decoder = new JSMDecoder();
|
---|
164 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<JSMEncoding>(decoder);
|
---|
165 | } else {
|
---|
166 | if (SolutionCreator.GetType().Equals(typeof(PRVRandomCreator))) {
|
---|
167 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPRVOperator>());
|
---|
168 | PRVDecoder decoder = new PRVDecoder();
|
---|
169 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PRVEncoding>(decoder);
|
---|
170 | } else {
|
---|
171 | if (SolutionCreator.GetType().Equals(typeof(PWRRandomCreator))) {
|
---|
172 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPWROperator>());
|
---|
173 | PWRDecoder decoder = new PWRDecoder();
|
---|
174 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PWREncoding>(decoder);
|
---|
175 | } else {
|
---|
176 | if (SolutionCreator.GetType().Equals(typeof(DirectScheduleRandomCreator))) {
|
---|
177 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IDirectScheduleOperator>());
|
---|
178 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<Schedule>();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | #endregion
|
---|
185 |
|
---|
186 | #region Importmethods
|
---|
187 | private List<string> SplitString(string line) {
|
---|
188 | if (line == null)
|
---|
189 | return null;
|
---|
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 | }
|
---|
200 | private int[] GetIntArray(List<string> data) {
|
---|
201 | int[] arr = new int[data.Count];
|
---|
202 | for (int i = 0; i < data.Count; i++) {
|
---|
203 | arr[i] = Int32.Parse(data[i]);
|
---|
204 | }
|
---|
205 | return arr;
|
---|
206 | }
|
---|
207 | private Job CreateJobFromData(List<string> data, int jobCount) {
|
---|
208 | double dueDate = 0;
|
---|
209 | int dataCount = data.Count;
|
---|
210 | if (DueDates.Value) {
|
---|
211 | dueDate = Double.Parse(data[data.Count - 1]);
|
---|
212 | dataCount--;
|
---|
213 | }
|
---|
214 | Job j = new Job(jobCount, dueDate);
|
---|
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 |
|
---|
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 |
|
---|
230 | JobData = new ItemList<Job>();
|
---|
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;
|
---|
237 | Jobs = new IntValue(Int32.Parse(data[0]));
|
---|
238 | Resources = new IntValue(Int32.Parse(data[1]));
|
---|
239 | //data[2] = bestKnownQuality (double)
|
---|
240 | //data[3] = dueDates (0|1)
|
---|
241 | DueDates.Value = false;
|
---|
242 | if (data.Count > 2)
|
---|
243 | BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse(data[2]));
|
---|
244 | if (data.Count > 3 && data[3] == "1")
|
---|
245 | DueDates.Value = true;
|
---|
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)) {
|
---|
249 | Job j = CreateJobFromData(data, jobCount);
|
---|
250 | this.JobData.Add(j);
|
---|
251 | jobCount++;
|
---|
252 | line = problemFile.ReadLine();
|
---|
253 | data = SplitString(line);
|
---|
254 | }//while
|
---|
255 | problemFound = true;
|
---|
256 | }//if
|
---|
257 | }//while
|
---|
258 | problemFile.Close();
|
---|
259 | }
|
---|
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) {
|
---|
269 |
|
---|
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)) {
|
---|
278 | Permutation p = new Permutation(PermutationTypes.Absolute, GetIntArray(data));
|
---|
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();
|
---|
290 | Schedule result = decoder.CreateScheduleFromEncoding(solution, JobData);
|
---|
291 | BestKnownSolution = result;
|
---|
292 | }
|
---|
293 | #endregion
|
---|
294 |
|
---|
295 | }
|
---|
296 | }
|
---|