Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.PFSP/3.3/PermutationFlowshopSchedulingProblem.cs @ 15640

Last change on this file since 15640 was 15639, checked in by fholzing, 6 years ago

#2864: Adapted PFSP and LOP to the new BasicProblem

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