Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CFSAP/HeuristicLab.Problems.Scheduling.CFSAP/3.3/CFSAP.cs @ 15472

Last change on this file since 15472 was 15472, checked in by abeham, 6 years ago

#2747: worked on the CFSAP

  • merged HeuristicLab.Problems.Instances from trunk
  • updated best known qualities
  • reduced memory footprint of run
  • added convergence graph result to solving strategy
File size: 7.5 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2017 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#endregion
23
24using System;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.BinaryVectorEncoding;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.Problems.Instances;
35
36namespace HeuristicLab.Problems.Scheduling.CFSAP {
37  [Item("Cyclic flow shop with two machines and a single nest (CFSAP)", "Non-permutational cyclic flow shop scheduling problem with a single nest of two machine from W. Bozejko.")]
38  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
39  [StorableClass]
40  public class CFSAP : SingleObjectiveBasicProblem<MultiEncoding>, ICFSAP, IProblemInstanceConsumer<CFSAPData> {
41    public override bool Maximization { get { return false; } }
42
43    public IValueParameter<IntMatrix> ProcessingTimesParameter {
44      get { return (IValueParameter<IntMatrix>)Parameters["ProcessingTimes"]; }
45    }
46
47    public IntMatrix ProcessingTimes {
48      get { return ProcessingTimesParameter.Value; }
49      set { ProcessingTimesParameter.Value = value; }
50    }
51
52    public IValueParameter<ItemList<IntMatrix>> SetupTimesParameter {
53      get { return (IValueParameter<ItemList<IntMatrix>>)Parameters["SetupTimes"]; }
54    }
55
56    public ItemList<IntMatrix> SetupTimes {
57      get { return SetupTimesParameter.Value; }
58      set { SetupTimesParameter.Value = value; }
59    }
60
61    [StorableConstructor]
62    protected CFSAP(bool deserializing) : base(deserializing) {}
63    protected CFSAP(CFSAP original, Cloner cloner)
64      : base(original, cloner) {}
65    public CFSAP() {
66      Parameters.Add(new ValueParameter<IntMatrix>("ProcessingTimes", "The processing times of each machine and each job.") { GetsCollected = false });
67      Parameters.Add(new ValueParameter<ItemList<IntMatrix>>("SetupTimes", "The sequence dependent set up times of each machine and between all jobs.") { GetsCollected = false });
68
69      ProcessingTimesParameter.Value = new IntMatrix(new int[,] {
70        { 5, 4, 3, 2, 1 },
71        { 1, 2, 3, 4, 5 }
72      });
73
74      SetupTimesParameter.Value = new ItemList<IntMatrix>(2);
75      SetupTimesParameter.Value.Add(new IntMatrix(new int[,] {
76        { 3, 4, 5, 4, 3 },
77        { 3, 4, 5, 4, 3 },
78        { 3, 4, 5, 4, 3 },
79        { 3, 4, 5, 4, 3 },
80        { 3, 4, 5, 4, 3 },
81      }));
82      SetupTimesParameter.Value.Add(new IntMatrix(new int[,] {
83        { 5, 4, 3, 4, 5 },
84        { 5, 4, 3, 4, 5 },
85        { 5, 4, 3, 4, 5 },
86        { 5, 4, 3, 4, 5 },
87        { 5, 4, 3, 4, 5 },
88      }));
89
90      Encoding.Add(new PermutationEncoding("sequence", 5, PermutationTypes.RelativeDirected));
91      Encoding.Add(new BinaryVectorEncoding("assignment", 5));
92
93      EncodingParameter.GetsCollected = false;
94      foreach (var param in ((IEncoding)Encoding).Parameters.OfType<IValueParameter>().ToList()) {
95        param.GetsCollected = false;
96      }
97
98      Operators.RemoveAll(x => x is SingleObjectiveMoveGenerator);
99      Operators.RemoveAll(x => x is SingleObjectiveMoveEvaluator);
100      Operators.RemoveAll(x => x is SingleObjectiveMoveMaker);
101    }
102
103    public override IDeepCloneable Clone(Cloner cloner) {
104      return new CFSAP(this, cloner);
105    }
106
107    public override double Evaluate(Individual individual, IRandom random) {
108      var order = individual.Permutation("sequence");
109      var assignment = individual.BinaryVector("assignment");
110      int T = EvaluateAssignement(order, assignment.Select(x => x ? 1 : 0).ToArray(),
111        ProcessingTimesParameter.Value, SetupTimesParameter.Value);
112      return T;
113    }
114
115    //Function to evaluate individual with the specified assignment
116    public static int EvaluateAssignement(Permutation order, int[] assignment, IntMatrix processingTimes, ItemList<IntMatrix> setupTimes) {
117      var N = order.Length;
118      int T = 0;
119
120      for (int i = 0; i < N; i++) {
121        int operation = order[i];
122        int machine = assignment[operation];
123        T += processingTimes[machine, operation];
124      }
125
126      for (int machine = 0; machine < 2; machine++) {
127        int first = -1;
128        int last = -1;
129        for (int i = 0; i < N; i++) {
130          int operation = order[i];
131          if (assignment[operation] == machine) {
132            if (first == -1)
133              first = operation;
134            else
135              T += setupTimes[machine][last, operation];
136            last = operation;
137          }
138        }
139        if (last != -1 && first != -1)
140          T += setupTimes[machine][last, first];
141      }
142
143      return T;
144    }
145
146    public void UpdateEncoding() {
147      Encoding.Encodings.OfType<PermutationEncoding>().Single().Length = ProcessingTimes.Columns;
148      Encoding.Encodings.OfType<BinaryVectorEncoding>().Single().Length = ProcessingTimes.Columns;
149    }
150
151    /// <summary>
152    /// Imports the first nest (index 0) given in the CFSAPData.
153    /// This is the same as calling Load(data, 0).
154    /// </summary>
155    /// <param name="data">The data of all nests.</param>
156    public void Load(CFSAPData data) {
157      Load(data, 0);
158    }
159
160    /// <summary>
161    /// Imports a specific nest given in the CFSAPData.
162    /// </summary>
163    /// <param name="data">The data of all nests.</param>
164    /// <param name="nest">The zero-based index of the nest that should be imported.</param>
165    public void Load(CFSAPData data, int nest) {
166      if (data.Machines[nest] != 2) throw new ArgumentException("Currently only two machines per nest are supported.");
167      if (nest < 0 || nest >= data.Nests) throw new ArgumentException("Nest must be a zero-based index.");
168      var pr = new int[data.Machines[nest], data.Jobs];
169      for (var i = 0; i < data.Machines[nest]; i++)
170        for (var j = 0; j < data.Jobs; j++)
171          pr[i, j] = data.ProcessingTimes[nest][i][j];
172      ProcessingTimesParameter.Value = new IntMatrix(pr);
173      var setups = new ItemList<IntMatrix>(data.Machines[nest]);
174      for (var m = 0; m < data.SetupTimes[nest].GetLength(0); m++) {
175        var setupTimes = new int[data.Jobs, data.Jobs];
176        for (var i = 0; i < data.Jobs; i++)
177          for (var j = 0; j < data.Jobs; j++)
178            setupTimes[i, j] = data.SetupTimes[nest][m][i][j];
179        setups.Add(new IntMatrix(setupTimes));
180      }
181      SetupTimesParameter.Value = setups;
182      UpdateEncoding();
183      Name = data.Name + "-nest" + nest;
184      Description = data.Description;
185      if (data.BestKnownCycleTime.HasValue)
186        BestKnownQuality = data.BestKnownCycleTime.Value;
187      else BestKnownQualityParameter.Value = null;
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.