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 |
|
---|
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 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | #endregion
|
---|
180 |
|
---|
181 | #region Importmethods
|
---|
182 | private List<string> SplitString(string line) {
|
---|
183 | if (line == null)
|
---|
184 | return null;
|
---|
185 | List<string> data = new List<string>(line.Split(' '));
|
---|
186 | List<string> result = new List<string>();
|
---|
187 |
|
---|
188 | foreach (string s in data) {
|
---|
189 | if (!String.IsNullOrEmpty(s) && s != "" && s != " ")
|
---|
190 | result.Add(s);
|
---|
191 | }
|
---|
192 |
|
---|
193 | return result;
|
---|
194 | }
|
---|
195 | private int[] GetIntArray(List<string> data) {
|
---|
196 | int[] arr = new int[data.Count];
|
---|
197 | for (int i = 0; i < data.Count; i++) {
|
---|
198 | arr[i] = Int32.Parse(data[i]);
|
---|
199 | }
|
---|
200 | return arr;
|
---|
201 | }
|
---|
202 | private Job CreateJobFromData(List<string> data, int jobCount) {
|
---|
203 | double dueDate = 0;
|
---|
204 | int dataCount = data.Count;
|
---|
205 | if (DueDates.Value) {
|
---|
206 | dueDate = Double.Parse(data[data.Count - 1]);
|
---|
207 | dataCount--;
|
---|
208 | }
|
---|
209 | Job j = new Job(jobCount, dueDate);
|
---|
210 | for (int i = 0; i < dataCount; i++) {
|
---|
211 | Task t = new Task(i / 2, Int32.Parse(data[i]), jobCount, Double.Parse(data[i + 1]));
|
---|
212 | j.Tasks.Add(t);
|
---|
213 | i++;
|
---|
214 | }//for
|
---|
215 | return j;
|
---|
216 | }
|
---|
217 |
|
---|
218 | public void ImportFromORLibrary(string fileName) {
|
---|
219 | if (!File.Exists(fileName))
|
---|
220 | return;
|
---|
221 | StreamReader problemFile = new StreamReader(fileName);
|
---|
222 | //assures that the parser only reads the first problem instance given in the file
|
---|
223 | bool problemFound = false;
|
---|
224 |
|
---|
225 | JobData = new ItemList<Job>();
|
---|
226 |
|
---|
227 | while (!problemFile.EndOfStream && !problemFound) {
|
---|
228 | string line = problemFile.ReadLine();
|
---|
229 | List<string> data = SplitString(line);
|
---|
230 | if (data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
|
---|
231 | int jobCount = 0;
|
---|
232 | Jobs = new IntValue(Int32.Parse(data[0]));
|
---|
233 | Resources = new IntValue(Int32.Parse(data[1]));
|
---|
234 | //data[2] = bestKnownQuality (double)
|
---|
235 | //data[3] = dueDates (0|1)
|
---|
236 | DueDates.Value = false;
|
---|
237 | if (data.Count > 2)
|
---|
238 | BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse(data[2]));
|
---|
239 | if (data.Count > 3 && data[3] == "1")
|
---|
240 | DueDates.Value = true;
|
---|
241 | line = problemFile.ReadLine();
|
---|
242 | data = SplitString(line);
|
---|
243 | while (!problemFile.EndOfStream && data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
|
---|
244 | Job j = CreateJobFromData(data, jobCount);
|
---|
245 | this.JobData.Add(j);
|
---|
246 | jobCount++;
|
---|
247 | line = problemFile.ReadLine();
|
---|
248 | data = SplitString(line);
|
---|
249 | }//while
|
---|
250 | problemFound = true;
|
---|
251 | }//if
|
---|
252 | }//while
|
---|
253 | problemFile.Close();
|
---|
254 | }
|
---|
255 |
|
---|
256 | public void ImportJSMSolution(string fileName) {
|
---|
257 | if (!File.Exists(fileName))
|
---|
258 | return;
|
---|
259 | StreamReader solutionFile = new StreamReader(fileName);
|
---|
260 | //assures that the parser only reads the first solution instance given in the file
|
---|
261 | bool solutionFound = false;
|
---|
262 | JSMEncoding solution = new JSMEncoding();
|
---|
263 | while (!solutionFile.EndOfStream && !solutionFound) {
|
---|
264 |
|
---|
265 | string line = solutionFile.ReadLine();
|
---|
266 | List<string> data = SplitString(line);
|
---|
267 | if (data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
|
---|
268 | if (data.Count > 2)
|
---|
269 | BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse(data[2]));
|
---|
270 | line = solutionFile.ReadLine();
|
---|
271 | data = SplitString(line);
|
---|
272 | while (data != null && data.Count > 0 && ((int)data[0][0] >= 48 && (int)data[0][0] <= 57)) {
|
---|
273 | Permutation p = new Permutation(PermutationTypes.Absolute, GetIntArray(data));
|
---|
274 | solution.JobSequenceMatrix.Add(p);
|
---|
275 |
|
---|
276 | line = solutionFile.ReadLine();
|
---|
277 | data = SplitString(line);
|
---|
278 | }//while
|
---|
279 | solutionFound = true;
|
---|
280 | }//if
|
---|
281 | }//while
|
---|
282 | solutionFile.Close();
|
---|
283 |
|
---|
284 | JSMDecoder decoder = new JSMDecoder();
|
---|
285 | Schedule result = decoder.CreateScheduleFromEncoding(solution, JobData);
|
---|
286 | BestKnownSolution = result;
|
---|
287 | }
|
---|
288 | #endregion
|
---|
289 |
|
---|
290 | }
|
---|
291 | }
|
---|