Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.PermutationProblems/3.3/PermutationFlowshopSchedulingProblem.cs @ 17366

Last change on this file since 17366 was 16834, checked in by fholzing, 6 years ago

#2864: Made the branch compile again (References, new Persistence, ...)

File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Problems.Instances;
32
33namespace HeuristicLab.Problems.PermutationProblems {
34  [Item("Permutation Flowshop Scheduling Problem (PFSP)", "Represents a Permutation Flowshop Scheduling Problem")]
35  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
36  [StorableType("18A7BE3A-5605-493D-A457-12D8B340F489")]
37  public sealed class PermutationFlowshopSchedulingProblem : SingleObjectiveBasicProblem<PermutationEncoding>, IProblemInstanceConsumer<FSSPData>, IProblemInstanceExporter<FSSPData> {
38    #region Fields
39    private static readonly FSSPData DefaultInstance = new FSSPData() {
40      Name = "Permutation Flowshop Scheduling Problem (PFSP)",
41      Description = "The default instance of the PFSP problem in HeuristicLab",
42      Jobs = 4,
43      Machines = 4,
44      //BestKnownQuality = 328,
45      ProcessingTimes = new double[,] {
46                {5 ,3, 6 ,6 },
47                {2 ,4, 8 ,4},
48                {4 ,2, 7 ,4},
49                {5 ,3, 8 ,6}
50            }
51    };
52    public event EventHandler BestKnownSolutionChanged;
53    #endregion
54
55    #region Getter/Setter
56    public OptionalValueParameter<Permutation> BestKnownSolutionParameter {
57      get { return (OptionalValueParameter<Permutation>)Parameters["BestKnownSolution"]; }
58    }
59    public OptionalValueParameter<DoubleMatrix> JobMatrixParameter {
60      get { return (OptionalValueParameter<DoubleMatrix>)Parameters["JobMatrix"]; }
61    }
62    public Permutation BestKnownSolution {
63      get { return BestKnownSolutionParameter.Value; }
64      set {
65        BestKnownSolutionParameter.Value = value;
66        if (BestKnownSolutionChanged != null) { OnBestKnownSolutionChanged(); }
67      }
68    }
69    public DoubleMatrix JobMatrix {
70      get { return JobMatrixParameter.Value; }
71      set { JobMatrixParameter.Value = value; }
72    }
73
74    public override bool Maximization { get { return false; } }
75    #endregion
76
77    #region Ctor
78    [StorableConstructor]
79    private PermutationFlowshopSchedulingProblem(StorableConstructorFlag _) : base(_) { }
80
81    private PermutationFlowshopSchedulingProblem(PermutationFlowshopSchedulingProblem original, Cloner cloner) : base(original, cloner) { }
82    public PermutationFlowshopSchedulingProblem() {
83      Parameters.Add(new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution of this FSSP instance."));
84      Parameters.Add(new OptionalValueParameter<DoubleMatrix>("JobMatrix", "The matrix which contains the jobs,machines and duration."));
85
86      Load(DefaultInstance);
87      EvaluatorParameter.GetsCollected = false;
88      EvaluatorParameter.Hidden = true;
89
90      Evaluator.QualityParameter.ActualName = "Makespan";
91    }
92    #endregion
93
94    #region Methods
95    public override IDeepCloneable Clone(Cloner cloner) {
96      return new PermutationFlowshopSchedulingProblem(this, cloner);
97    }
98
99    public void Load(FSSPData data) {
100      if (data.BestKnownQuality.HasValue) {
101        BestKnownQuality = data.BestKnownQuality.Value;
102      }
103      Name = data.Name;
104      Description = data.Description;
105      JobMatrix = new DoubleMatrix(data.ProcessingTimes);
106      Encoding.Length = JobMatrix.Columns;
107
108      if (data.BestKnownSchedule != null) {
109        int[] permut = data.BestKnownSchedule;
110        //Clean up if the first index = 1
111        if (!permut.Contains(0)) { permut = permut.Select(v => v - 1).ToArray(); }
112
113        BestKnownSolution = new Permutation(PermutationTypes.Absolute, data.BestKnownSchedule);
114        BestKnownQuality = Evaluate(permut, JobMatrix);
115      }
116    }
117    public FSSPData Export() {
118      var result = new FSSPData {
119        Name = Name,
120        Description = Description,
121        ProcessingTimes = new double[JobMatrix.Rows, JobMatrix.Columns]
122      };
123
124      result.BestKnownQuality = BestKnownQuality;
125
126      if (JobMatrix != null) {
127        result.Jobs = JobMatrix.Rows;
128        result.Machines = JobMatrix.Columns;
129      }
130
131      for (int i = 0; i < JobMatrix.Rows; i++) {
132        for (int j = 0; j < JobMatrix.Columns; j++) {
133          result.ProcessingTimes[i, j] = JobMatrix[i, j];
134        }
135      }
136
137      result.BestKnownSchedule = BestKnownSolution != null ? (BestKnownSolution as IntArray).ToArray() : null;
138
139      return result;
140    }
141
142    public override double Evaluate(Individual individual, IRandom random) {
143      return Evaluate(individual.Permutation().ToArray(), JobMatrix);
144    }
145    #endregion
146
147    #region Helper Methods
148    private void OnBestKnownSolutionChanged() {
149      BestKnownSolutionChanged?.Invoke(this, EventArgs.Empty);
150    }
151
152    /// <summary>
153    /// Calculates the makespan (cMax), meaning the total time from start till the last job on the last machine is done
154    /// </summary>
155    /// <param name="permutation"></param>
156    /// <param name="matrix"></param>
157    /// <returns></returns>
158    private double Evaluate(int[] permutation, DoubleMatrix matrix) {
159      DoubleMatrix calculatedTime = new DoubleMatrix(matrix.Rows, matrix.Columns);
160      double runtimePrev;
161
162      double runtimePrevMachine;
163      double runtimePrevJobOnThisMachine;
164
165      for (var machineIdx = 0; machineIdx < matrix.Rows; machineIdx++) {
166        for (var jobIdx = 0; jobIdx < matrix.Columns; jobIdx++) {
167          runtimePrev = 0;
168
169          if (jobIdx == 0 && machineIdx == 0) {  //Nothing to calculate
170          } else if (machineIdx == 0) {
171            runtimePrev = calculatedTime[machineIdx, jobIdx - 1];
172          } else if (jobIdx == 0) {
173            runtimePrev = calculatedTime[machineIdx - 1, jobIdx];
174          } else {
175            runtimePrevMachine = calculatedTime[machineIdx - 1, jobIdx];
176            runtimePrevJobOnThisMachine = calculatedTime[machineIdx, jobIdx - 1];
177            runtimePrev = runtimePrevMachine > runtimePrevJobOnThisMachine ? runtimePrevMachine : runtimePrevJobOnThisMachine;
178          }
179          calculatedTime[machineIdx, jobIdx] = matrix[machineIdx, permutation[jobIdx]] + runtimePrev;
180        }
181      }
182      return calculatedTime[calculatedTime.Rows - 1, calculatedTime.Columns - 1];
183    }
184    #endregion
185  }
186}
Note: See TracBrowser for help on using the repository browser.