[6121] | 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;
|
---|
[6412] | 24 | using System.Drawing;
|
---|
[6406] | 25 | using System.IO;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[6121] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[6406] | 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;
|
---|
[6121] | 34 | using HeuristicLab.Parameters;
|
---|
[6406] | 35 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[6121] | 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]
|
---|
[6412] | 42 | public sealed class JobShopSchedulingProblem : SchedulingProblem, IStorableContent {
|
---|
[6121] | 43 | #region Parameter Properties
|
---|
[6293] | 44 | public ValueParameter<ItemList<Job>> JobDataParameter {
|
---|
| 45 | get { return (ValueParameter<ItemList<Job>>)Parameters["JobData"]; }
|
---|
[6121] | 46 | }
|
---|
[6177] | 47 | public OptionalValueParameter<Schedule> BestKnownSolutionParameter {
|
---|
| 48 | get { return (OptionalValueParameter<Schedule>)Parameters["BestKnownSolution"]; }
|
---|
[6121] | 49 | }
|
---|
[6266] | 50 |
|
---|
[6293] | 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 | }
|
---|
[6364] | 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"]; }
|
---|
[6406] | 62 | }
|
---|
[6121] | 63 | #endregion
|
---|
| 64 |
|
---|
| 65 | #region Properties
|
---|
[6293] | 66 | public ItemList<Job> JobData {
|
---|
| 67 | get { return JobDataParameter.Value; }
|
---|
| 68 | set { JobDataParameter.Value = value; }
|
---|
[6121] | 69 | }
|
---|
[6177] | 70 | public Schedule BestKnownSolution {
|
---|
[6121] | 71 | get { return BestKnownSolutionParameter.Value; }
|
---|
| 72 | set { BestKnownSolutionParameter.Value = value; }
|
---|
| 73 | }
|
---|
[6293] | 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 | }
|
---|
[6364] | 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 | }
|
---|
[6412] | 90 | public override Image ItemImage {
|
---|
| 91 | get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
|
---|
| 92 | }
|
---|
| 93 | public string Filename { get; set; }
|
---|
[6121] | 94 | #endregion
|
---|
| 95 |
|
---|
| 96 | public JobShopSchedulingProblem()
|
---|
[6406] | 97 | : base(new SchedulingEvaluationAlgorithm(), new JSMRandomCreator()) {
|
---|
[6293] | 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."));
|
---|
[6364] | 100 |
|
---|
| 101 | Parameters.Add(new ValueParameter<IntValue>("Jobs", "The number of jobs used in this JSSP instance.", new IntValue()));
|
---|
[6412] | 102 | Parameters.Add(new ValueParameter<IntValue>("Resources", "The number of resources used in this JSSP instance.", new IntValue()));
|
---|
[6364] | 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();
|
---|
[6121] | 108 | }
|
---|
| 109 |
|
---|
[6406] | 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 |
|
---|
[6121] | 119 | #region Events
|
---|
[6177] | 120 | protected override void OnSolutionCreatorChanged() {
|
---|
| 121 | InitializeOperators();
|
---|
| 122 | }
|
---|
[6406] | 123 |
|
---|
| 124 | protected override void OnEvaluatorChanged() {
|
---|
| 125 | base.OnEvaluatorChanged();
|
---|
| 126 | }
|
---|
[6121] | 127 | #endregion
|
---|
| 128 |
|
---|
| 129 | #region Helpers
|
---|
[6406] | 130 | private void InitializeProblemInstance() {
|
---|
| 131 | Jobs = new IntValue(10);
|
---|
| 132 | Resources = new IntValue(10);
|
---|
| 133 | BestKnownQuality = new DoubleValue(930);
|
---|
[6364] | 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 |
|
---|
[6121] | 154 | private void InitializeOperators() {
|
---|
[6177] | 155 | Operators.Clear();
|
---|
| 156 | ApplyEncoding();
|
---|
[6121] | 157 | Operators.Add(new BestSchedulingSolutionAnalyzer());
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[6177] | 160 | private void ApplyEncoding() {
|
---|
[6364] | 161 | if (SolutionCreator.GetType().Equals(typeof(JSMRandomCreator))) {
|
---|
| 162 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IJSMOperator>());
|
---|
[6406] | 163 | JSMDecoder decoder = new JSMDecoder();
|
---|
| 164 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<JSMEncoding>(decoder);
|
---|
[6177] | 165 | } else {
|
---|
[6364] | 166 | if (SolutionCreator.GetType().Equals(typeof(PRVRandomCreator))) {
|
---|
| 167 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPRVOperator>());
|
---|
[6406] | 168 | PRVDecoder decoder = new PRVDecoder();
|
---|
| 169 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PRVEncoding>(decoder);
|
---|
[6260] | 170 | } else {
|
---|
[6364] | 171 | if (SolutionCreator.GetType().Equals(typeof(PWRRandomCreator))) {
|
---|
| 172 | Operators.AddRange(ApplicationManager.Manager.GetInstances<IPWROperator>());
|
---|
[6406] | 173 | PWRDecoder decoder = new PWRDecoder();
|
---|
| 174 | ((SchedulingEvaluationAlgorithm)this.EvaluatorParameter.ActualValue).InitializeOperatorGraph<PWREncoding>(decoder);
|
---|
[6260] | 175 | }
|
---|
[6177] | 176 | }
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
[6121] | 179 | #endregion
|
---|
| 180 |
|
---|
| 181 | #region Importmethods
|
---|
| 182 | private List<string> SplitString(string line) {
|
---|
[6260] | 183 | if (line == null)
|
---|
| 184 | return null;
|
---|
[6121] | 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 | }
|
---|
[6260] | 195 | private int[] GetIntArray(List<string> data) {
|
---|
[6406] | 196 | int[] arr = new int[data.Count];
|
---|
| 197 | for (int i = 0; i < data.Count; i++) {
|
---|
[6260] | 198 | arr[i] = Int32.Parse(data[i]);
|
---|
| 199 | }
|
---|
| 200 | return arr;
|
---|
| 201 | }
|
---|
[6406] | 202 | private Job CreateJobFromData(List<string> data, int jobCount) {
|
---|
[6412] | 203 | double dueDate = 0;
|
---|
[6364] | 204 | int dataCount = data.Count;
|
---|
| 205 | if (DueDates.Value) {
|
---|
[6412] | 206 | dueDate = Double.Parse(data[data.Count - 1]);
|
---|
[6364] | 207 | dataCount--;
|
---|
| 208 | }
|
---|
[6412] | 209 | Job j = new Job(jobCount, dueDate);
|
---|
[6364] | 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 |
|
---|
[6121] | 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 |
|
---|
[6293] | 225 | JobData = new ItemList<Job>();
|
---|
[6121] | 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;
|
---|
[6406] | 232 | Jobs = new IntValue(Int32.Parse(data[0]));
|
---|
| 233 | Resources = new IntValue(Int32.Parse(data[1]));
|
---|
[6266] | 234 | //data[2] = bestKnownQuality (double)
|
---|
| 235 | //data[3] = dueDates (0|1)
|
---|
[6364] | 236 | DueDates.Value = false;
|
---|
[6177] | 237 | if (data.Count > 2)
|
---|
[6406] | 238 | BestKnownQualityParameter.ActualValue = new DoubleValue(Double.Parse(data[2]));
|
---|
[6266] | 239 | if (data.Count > 3 && data[3] == "1")
|
---|
[6364] | 240 | DueDates.Value = true;
|
---|
[6121] | 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)) {
|
---|
[6364] | 244 | Job j = CreateJobFromData(data, jobCount);
|
---|
[6293] | 245 | this.JobData.Add(j);
|
---|
[6121] | 246 | jobCount++;
|
---|
| 247 | line = problemFile.ReadLine();
|
---|
| 248 | data = SplitString(line);
|
---|
| 249 | }//while
|
---|
| 250 | problemFound = true;
|
---|
| 251 | }//if
|
---|
| 252 | }//while
|
---|
| 253 | problemFile.Close();
|
---|
| 254 | }
|
---|
[6260] | 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) {
|
---|
[6406] | 264 |
|
---|
[6260] | 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)) {
|
---|
[6406] | 273 | Permutation p = new Permutation(PermutationTypes.Absolute, GetIntArray(data));
|
---|
[6260] | 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();
|
---|
[6293] | 285 | Schedule result = decoder.CreateScheduleFromEncoding(solution, JobData);
|
---|
[6260] | 286 | BestKnownSolution = result;
|
---|
| 287 | }
|
---|
[6121] | 288 | #endregion
|
---|
| 289 |
|
---|
| 290 | }
|
---|
| 291 | }
|
---|