Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2747_CFSAP/HeuristicLab.Problems.Scheduling.CFSAP/3.3/MultiNestCFSAP.cs @ 17247

Last change on this file since 17247 was 15472, checked in by abeham, 7 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: 6.7 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.BinaryVectorEncoding;
7using HeuristicLab.Encodings.PermutationEncoding;
8using HeuristicLab.Optimization;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11using HeuristicLab.Problems.Instances;
12
13namespace HeuristicLab.Problems.Scheduling.CFSAP {
14  [Item("Cyclic flow shop with two machines and multiple nests (multi-CFSAP)", "Non-permutational cyclic flow shop scheduling problem with multiple nests of two machines each from W. Bozejko.")]
15  [Creatable(CreatableAttribute.Categories.CombinatorialProblems)]
16  [StorableClass]
17  public class MultiNestCFSAP : SingleObjectiveBasicProblem<MultiEncoding>, IProblemInstanceConsumer<CFSAPData> {
18    public override bool Maximization { get { return false; } }
19
20    public IValueParameter<ItemList<IntMatrix>> ProcessingTimesParameter {
21      get { return (IValueParameter<ItemList<IntMatrix>>)Parameters["ProcessingTimes"]; }
22    }
23
24    public ItemList<IntMatrix> ProcessingTimes {
25      get { return ProcessingTimesParameter.Value; }
26      set { ProcessingTimesParameter.Value = value; }
27    }
28
29    public IValueParameter<ItemList<ItemList<IntMatrix>>> SetupTimesParameter {
30      get { return (IValueParameter<ItemList<ItemList<IntMatrix>>>)Parameters["SetupTimes"]; }
31    }
32
33    public ItemList<ItemList<IntMatrix>> SetupTimes {
34      get { return SetupTimesParameter.Value; }
35      set { SetupTimesParameter.Value = value; }
36    }
37
38    [StorableConstructor]
39    protected MultiNestCFSAP(bool deserializing) : base(deserializing) { }
40    protected MultiNestCFSAP(MultiNestCFSAP original, Cloner cloner)
41      : base(original, cloner) { }
42    public MultiNestCFSAP() {
43      Parameters.Add(new ValueParameter<ItemList<IntMatrix>>("ProcessingTimes", "The processing times of each nest, each machine, each job.") { GetsCollected = false });
44      Parameters.Add(new ValueParameter<ItemList<ItemList<IntMatrix>>>("SetupTimes", "The sequence dependent set up times for each nest, each machine, and each job to each other.") { GetsCollected = false });
45
46      ProcessingTimesParameter.Value = new ItemList<IntMatrix>() {
47        new IntMatrix(new int[,] {
48          { 5, 4, 3, 2, 1 },
49          { 1, 2, 3, 4, 5 }
50        })
51      };
52
53      SetupTimesParameter.Value = new ItemList<ItemList<IntMatrix>>() { new ItemList<IntMatrix>() };
54      SetupTimesParameter.Value[0].Add(new IntMatrix(new int[,] {
55        { 3, 4, 5, 4, 3 },
56        { 3, 4, 5, 4, 3 },
57        { 3, 4, 5, 4, 3 },
58        { 3, 4, 5, 4, 3 },
59        { 3, 4, 5, 4, 3 },
60      }));
61      SetupTimesParameter.Value[0].Add(new IntMatrix(new int[,] {
62        { 5, 4, 3, 4, 5 },
63        { 5, 4, 3, 4, 5 },
64        { 5, 4, 3, 4, 5 },
65        { 5, 4, 3, 4, 5 },
66        { 5, 4, 3, 4, 5 },
67      }));
68
69      Encoding.Add(new PermutationEncoding("seq0", 5, PermutationTypes.RelativeDirected));
70      Encoding.Add(new BinaryVectorEncoding("assign0", 5));
71
72      EncodingParameter.GetsCollected = false;
73      foreach (var param in ((IEncoding)Encoding).Parameters.OfType<IValueParameter>().ToList()) {
74        param.GetsCollected = false;
75      }
76
77      Operators.RemoveAll(x => x is SingleObjectiveMoveGenerator);
78      Operators.RemoveAll(x => x is SingleObjectiveMoveEvaluator);
79      Operators.RemoveAll(x => x is SingleObjectiveMoveMaker);
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new MultiNestCFSAP(this, cloner);
84    }
85
86    public override double Evaluate(Individual individual, IRandom random) {
87      var nests = ProcessingTimesParameter.Value.Count;
88      var maxT = int.MinValue;
89      for (var i = 0; i < nests; i++) {
90        var order = individual.Permutation("seq" + i);
91        var assignment = individual.BinaryVector("assign" + i);
92        int T = CFSAP.EvaluateAssignement(order, assignment.Select(x => x ? 1 : 0).ToArray(),
93          ProcessingTimesParameter.Value[i], SetupTimesParameter.Value[i]);
94        if (T > maxT) maxT = T;
95      }
96      return maxT;
97    }
98
99    /// <summary>
100    /// Imports the all nests given in the CFSAPData.
101    /// </summary>
102    /// <param name="data">The data of all nests.</param>
103    public void Load(CFSAPData data) {
104      if (data.Machines.Any(x => x != 2)) throw new ArgumentException("Currently only two machines per nest are supported.");
105      ProcessingTimesParameter.Value = new ItemList<IntMatrix>();
106      for (var n = 0; n < data.Nests; n++) {
107        var pr = new int[data.Machines[n], data.Jobs];
108        for (var i = 0; i < data.Machines[n]; i++)
109          for (var j = 0; j < data.Jobs; j++)
110            pr[i, j] = data.ProcessingTimes[n][i][j];
111        ProcessingTimesParameter.Value.Add(new IntMatrix(pr));
112      }
113      SetupTimesParameter.Value = new ItemList<ItemList<IntMatrix>>();
114      for (var n = 0; n < data.Nests; n++) {
115        var setups = new ItemList<IntMatrix>(data.Machines[n]);
116        for (var m = 0; m < data.SetupTimes[n].GetLength(0); m++) {
117          var setupTimes = new int[data.Jobs, data.Jobs];
118          for (var i = 0; i < data.Jobs; i++)
119            for (var j = 0; j < data.Jobs; j++)
120              setupTimes[i, j] = data.SetupTimes[n][m][i][j];
121          setups.Add(new IntMatrix(setupTimes));
122        }
123        SetupTimesParameter.Value.Add(setups);
124      }
125      foreach (var enc in Encoding.Encodings.ToList())
126        Encoding.Remove(enc);
127      for (var n = 0; n < data.Nests; n++) {
128        Encoding.Add(new PermutationEncoding("seq" + n, data.Jobs, PermutationTypes.RelativeDirected));
129        Encoding.Add(new BinaryVectorEncoding("assign" + n, data.Jobs));
130      }
131
132      #region Reduce run size by removing collected parameters
133      foreach (var param in ((IEncoding)Encoding).Parameters.OfType<IValueParameter>().ToList())
134        param.GetsCollected = false;
135
136      var solCreator = SolutionCreator as IParameterizedItem;
137      if (solCreator != null) {
138        foreach (var param in solCreator.Parameters.OfType<IValueParameter>().ToList()) {
139          param.GetsCollected = false;
140          var secondLevel = param.Value as IParameterizedItem;
141          if (secondLevel != null) {
142            foreach (var secondLevelParam in secondLevel.Parameters.OfType<IValueParameter>().ToList())
143              secondLevelParam.GetsCollected = false;
144          }
145        }
146      }
147      #endregion
148
149      Name = data.Name;
150      Description = data.Description;
151      if (data.BestKnownCycleTime.HasValue)
152        BestKnownQuality = data.BestKnownCycleTime.Value;
153      else BestKnownQualityParameter.Value = null;
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.