1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HEAL.Attic;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
31 | using HeuristicLab.Encodings.ScheduleEncoding;
|
---|
32 | using HeuristicLab.Optimization;
|
---|
33 | using HeuristicLab.Parameters;
|
---|
34 | using HeuristicLab.Problems.Instances;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.Scheduling {
|
---|
37 | public enum JSSPObjective { Makespan, Tardiness }
|
---|
38 |
|
---|
39 | [Item("Job Shop Scheduling Problem (JSSP)", "Represents a standard Job Shop Scheduling Problem")]
|
---|
40 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 120)]
|
---|
41 | [StorableType("d6c72bd1-c6cc-4efb-9cc5-b73bb3845799")]
|
---|
42 | public sealed class JobShopSchedulingProblem : SingleObjectiveProblem<IScheduleEncoding, IScheduleSolution>, IProblemInstanceConsumer<JSSPData>, IProblemInstanceExporter<JSSPData>, IStorableContent {
|
---|
43 | #region Default Instance
|
---|
44 | private static readonly JSSPData DefaultInstance = new JSSPData() {
|
---|
45 | Jobs = 10,
|
---|
46 | Resources = 10,
|
---|
47 | BestKnownQuality = 930,
|
---|
48 | ProcessingTimes = new double[,] {
|
---|
49 | { 29, 78, 9, 36, 49, 11, 62, 56, 44, 21 },
|
---|
50 | { 43, 90, 75, 11, 69, 28, 46, 46, 72, 30 },
|
---|
51 | { 91, 85, 39, 74, 90, 10, 12, 89, 45, 33 },
|
---|
52 | { 81, 95, 71, 99, 9, 52, 85, 98, 22, 43 },
|
---|
53 | { 14, 6, 22, 61, 26, 69, 21, 49, 72, 53 },
|
---|
54 | { 84, 2, 52, 95, 48, 72, 47, 65, 6, 25 },
|
---|
55 | { 46, 37, 61, 13, 32, 21, 32, 89, 30, 55 },
|
---|
56 | { 31, 86, 46, 74, 32, 88, 19, 48, 36, 79 },
|
---|
57 | { 76, 69, 76, 51, 85, 11, 40, 89, 26, 74 },
|
---|
58 | { 85, 13, 61, 7, 64, 76, 47, 52, 90, 45 }
|
---|
59 | },
|
---|
60 | Demands = new int[,] {
|
---|
61 | { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
|
---|
62 | { 0, 2, 4, 9, 3, 1, 6, 5, 7, 8 },
|
---|
63 | { 1, 0, 3, 2, 8, 5, 7, 6, 9, 4 },
|
---|
64 | { 1, 2, 0, 4, 6, 8, 7, 3, 9, 5 },
|
---|
65 | { 2, 0, 1, 5, 3, 4, 8, 7, 9, 6 },
|
---|
66 | { 2, 1, 5, 3, 8, 9, 0, 6, 4, 7 },
|
---|
67 | { 1, 0, 3, 2, 6, 5, 9, 8, 7, 4 },
|
---|
68 | { 2, 0, 1, 5, 4, 6, 8, 9, 7, 3 },
|
---|
69 | { 0, 1, 3, 5, 2, 9, 6, 7, 4, 8 },
|
---|
70 | { 1, 0, 2, 6, 8, 9, 5, 3, 4, 7 }
|
---|
71 | }
|
---|
72 | };
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | public override Image ItemImage {
|
---|
76 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
77 | }
|
---|
78 |
|
---|
79 | #region Parameter Properties
|
---|
80 | [Storable] public ReferenceParameter<ItemList<Job>> JobDataParameter { get; private set; }
|
---|
81 | [Storable] public OptionalValueParameter<Schedule> BestKnownSolutionParameter { get; private set; }
|
---|
82 | [Storable] public ReferenceParameter<IntValue> JobsParameter { get; private set; }
|
---|
83 | [Storable] public ReferenceParameter<IntValue> ResourcesParameter { get; private set; }
|
---|
84 | [Storable] public IFixedValueParameter<EnumValue<JSSPObjective>> ObjectiveParameter { get; private set; }
|
---|
85 | #endregion
|
---|
86 |
|
---|
87 | #region Properties
|
---|
88 | public ItemList<Job> JobData {
|
---|
89 | get { return JobDataParameter.Value; }
|
---|
90 | set { JobDataParameter.Value = value; }
|
---|
91 | }
|
---|
92 | public Schedule BestKnownSolution {
|
---|
93 | get { return BestKnownSolutionParameter.Value; }
|
---|
94 | set { BestKnownSolutionParameter.Value = value; }
|
---|
95 | }
|
---|
96 | public int Jobs {
|
---|
97 | get { return JobsParameter.Value.Value; }
|
---|
98 | set { JobsParameter.Value.Value = value; }
|
---|
99 | }
|
---|
100 | public int Resources {
|
---|
101 | get { return ResourcesParameter.Value.Value; }
|
---|
102 | set { ResourcesParameter.Value.Value = value; }
|
---|
103 | }
|
---|
104 | public JSSPObjective Objective {
|
---|
105 | get { return ObjectiveParameter.Value.Value; }
|
---|
106 | set { ObjectiveParameter.Value.Value = value; }
|
---|
107 | }
|
---|
108 |
|
---|
109 | public new IScheduleEncoding Encoding {
|
---|
110 | get { return base.Encoding; }
|
---|
111 | set { base.Encoding = value; }
|
---|
112 | }
|
---|
113 | #endregion
|
---|
114 |
|
---|
115 | [StorableConstructor]
|
---|
116 | private JobShopSchedulingProblem(StorableConstructorFlag _) : base(_) { }
|
---|
117 | [StorableHook(HookType.AfterDeserialization)]
|
---|
118 | private void AfterDeserialization() {
|
---|
119 | RegisterEventHandlers();
|
---|
120 | }
|
---|
121 |
|
---|
122 | private JobShopSchedulingProblem(JobShopSchedulingProblem original, Cloner cloner)
|
---|
123 | : base(original, cloner) {
|
---|
124 | JobDataParameter = cloner.Clone(original.JobDataParameter);
|
---|
125 | BestKnownSolutionParameter = cloner.Clone(original.BestKnownSolutionParameter);
|
---|
126 | JobsParameter = cloner.Clone(original.JobsParameter);
|
---|
127 | ResourcesParameter = cloner.Clone(original.ResourcesParameter);
|
---|
128 | ObjectiveParameter = cloner.Clone(original.ObjectiveParameter);
|
---|
129 |
|
---|
130 | RegisterEventHandlers();
|
---|
131 | }
|
---|
132 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
133 | return new JobShopSchedulingProblem(this, cloner);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public JobShopSchedulingProblem()
|
---|
137 | : base(new JobSequenceMatrixEncoding()) {
|
---|
138 | Parameters.Add(JobDataParameter = new ReferenceParameter<ItemList<Job>>("JobData", "Jobdata defining the precedence relationships and the duration of the tasks in this JSSP-Instance.", Encoding.JobDataParameter));
|
---|
139 | Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<Schedule>("BestKnownSolution", "The best known solution of this JSSP instance."));
|
---|
140 | Parameters.Add(JobsParameter = new ReferenceParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", Encoding.JobsParameter));
|
---|
141 | Parameters.Add(ResourcesParameter = new ReferenceParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", Encoding.ResourcesParameter));
|
---|
142 | Parameters.Add(ObjectiveParameter = new FixedValueParameter<EnumValue<JSSPObjective>>("Objective", "The objective to use in the evaluation of a schedule.", new EnumValue<JSSPObjective>(JSSPObjective.Makespan)));
|
---|
143 | EncodingParameter.Hidden = false;
|
---|
144 |
|
---|
145 | RegisterEventHandlers();
|
---|
146 | Load(DefaultInstance);
|
---|
147 | }
|
---|
148 |
|
---|
149 | public override ISingleObjectiveEvaluationResult Evaluate(IScheduleSolution solution, IRandom random, CancellationToken cancellationToken) {
|
---|
150 | var schedule = Encoding.Decode(solution, JobData);
|
---|
151 | switch (Objective) {
|
---|
152 | case JSSPObjective.Makespan:
|
---|
153 | return new SingleObjectiveEvaluationResult(Makespan.Calculate(schedule));
|
---|
154 | case JSSPObjective.Tardiness:
|
---|
155 | return new SingleObjectiveEvaluationResult(MeanTardiness.Calculate(schedule, JobData));
|
---|
156 | default:
|
---|
157 | throw new InvalidOperationException("Objective " + Objective + " unknown");
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | public override void Analyze(IScheduleSolution[] solutions, double[] qualities, ResultCollection results, IRandom random) {
|
---|
162 | base.Analyze(solutions, qualities, results, random);
|
---|
163 |
|
---|
164 | bool max = Maximization;
|
---|
165 |
|
---|
166 | int i = -1;
|
---|
167 | if (!max)
|
---|
168 | i = qualities.Select((x, index) => new { index, x }).OrderBy(x => x.x).First().index;
|
---|
169 | else i = qualities.Select((x, index) => new { index, x }).OrderByDescending(x => x.x).First().index;
|
---|
170 |
|
---|
171 | if (double.IsNaN(BestKnownQuality) ||
|
---|
172 | max && qualities[i] > BestKnownQuality ||
|
---|
173 | !max && qualities[i] < BestKnownQuality) {
|
---|
174 | BestKnownQuality = qualities[i];
|
---|
175 | BestKnownSolution = Encoding.Decode(solutions[i], JobData);
|
---|
176 | }
|
---|
177 | Schedule bestSolution;
|
---|
178 | if (results.TryGetValue("Best Scheduling Solution", out var result)) {
|
---|
179 | bestSolution = result.Value as Schedule;
|
---|
180 | } else bestSolution = null;
|
---|
181 |
|
---|
182 | if (bestSolution == null || IsBetter(bestSolution.Quality, qualities[i]))
|
---|
183 | results.AddOrUpdateResult("Best Scheduling Solution", Encoding.Decode(solutions[i], JobData));
|
---|
184 | }
|
---|
185 |
|
---|
186 | protected override void OnEncodingChanged() {
|
---|
187 | base.OnEncodingChanged();
|
---|
188 | var jobData = (ItemList<Job>)JobData.Clone();
|
---|
189 | var jobs = Jobs;
|
---|
190 | var resources = Resources;
|
---|
191 | Parameters.Remove(JobDataParameter);
|
---|
192 | Parameters.Add(JobDataParameter = new ReferenceParameter<ItemList<Job>>("JobData", "Jobdata defining the precedence relationships and the duration of the tasks in this JSSP-Instance.", Encoding.JobDataParameter));
|
---|
193 | Parameters.Remove(JobsParameter);
|
---|
194 | Parameters.Add(JobsParameter = new ReferenceParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", Encoding.JobsParameter));
|
---|
195 | Parameters.Remove(ResourcesParameter);
|
---|
196 | Parameters.Add(ResourcesParameter = new ReferenceParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", Encoding.ResourcesParameter));
|
---|
197 | JobData = jobData;
|
---|
198 | Jobs = jobs;
|
---|
199 | Resources = resources;
|
---|
200 |
|
---|
201 | RegisterEventHandlers();
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | private void RegisterEventHandlers() {
|
---|
206 | ItemListParameterChangeHandler<Job>.Create(JobDataParameter, JobDataOnChanged);
|
---|
207 | }
|
---|
208 |
|
---|
209 | private void JobDataOnChanged() {
|
---|
210 | Jobs = JobData.Count;
|
---|
211 | Resources = JobData.SelectMany(x => x.Tasks).Select(x => x.ResourceNr).Distinct().Count();
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | #region Problem Instance Handling
|
---|
216 | public void Load(JSSPData data) {
|
---|
217 | var jobData = new ItemList<Job>(data.Jobs);
|
---|
218 | for (int j = 0; j < data.Jobs; j++) {
|
---|
219 | var job = new Job(j, data.DueDates != null ? data.DueDates[j] : double.MaxValue);
|
---|
220 | for (int t = 0; t < data.Resources; t++) {
|
---|
221 | job.Tasks.Add(new Task(t, data.Demands[j, t], j, data.ProcessingTimes[j, t]));
|
---|
222 | }
|
---|
223 | jobData.Add(job);
|
---|
224 | }
|
---|
225 |
|
---|
226 | BestKnownQuality = data.BestKnownQuality ?? double.NaN;
|
---|
227 | if (data.BestKnownSchedule != null) {
|
---|
228 | var enc = new JSM(0);
|
---|
229 | for (int i = 0; i < data.Resources; i++) {
|
---|
230 | enc.JobSequenceMatrix.Add(new Permutation(PermutationTypes.Absolute, new int[data.Jobs]));
|
---|
231 | for (int j = 0; j < data.Jobs; j++) {
|
---|
232 | enc.JobSequenceMatrix[i][j] = data.BestKnownSchedule[i, j];
|
---|
233 | }
|
---|
234 | }
|
---|
235 | BestKnownSolution = JSMDecoder.Decode(enc, jobData, JSMDecodingErrorPolicy.RandomPolicy, JSMForcingStrategy.SwapForcing);
|
---|
236 | switch (Objective) {
|
---|
237 | case JSSPObjective.Makespan:
|
---|
238 | BestKnownQuality = Makespan.Calculate(BestKnownSolution);
|
---|
239 | break;
|
---|
240 | case JSSPObjective.Tardiness:
|
---|
241 | BestKnownQuality = MeanTardiness.Calculate(BestKnownSolution, jobData);
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 |
|
---|
246 | JobData = jobData;
|
---|
247 | Resources = data.Resources;
|
---|
248 | }
|
---|
249 |
|
---|
250 | public JSSPData Export() {
|
---|
251 | var result = new JSSPData {
|
---|
252 | Name = Name,
|
---|
253 | Description = Description,
|
---|
254 | Jobs = Jobs,
|
---|
255 | Resources = Resources,
|
---|
256 | ProcessingTimes = new double[Jobs, Resources],
|
---|
257 | Demands = new int[Jobs, Resources],
|
---|
258 | DueDates = new double[Jobs]
|
---|
259 | };
|
---|
260 |
|
---|
261 | foreach (var job in JobData) {
|
---|
262 | var counter = 0;
|
---|
263 | result.DueDates[job.Index] = job.DueDate;
|
---|
264 | foreach (var task in job.Tasks) {
|
---|
265 | result.ProcessingTimes[task.JobNr, counter] = task.Duration;
|
---|
266 | result.Demands[task.JobNr, counter] = task.ResourceNr;
|
---|
267 | counter++;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | return result;
|
---|
271 | }
|
---|
272 | #endregion
|
---|
273 |
|
---|
274 | }
|
---|
275 | }
|
---|