Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.BinPacking/3.3/3D/IntegerVectorEncoding/IntegerVectorProblem.cs @ 15377

Last change on this file since 15377 was 15217, checked in by abeham, 7 years ago

#2666, #2706, #2730, #2736: merged revisions 14412, 14475, 14476, 14659, 14660, 14663, 14779, 14780, 14912, 15050, 15067, 15069, 15079, 15162, 15166, 15172, 15173 to stable

File size: 4.6 KB
Line 
1#region License Information
2
3/* HeuristicLab
4 * Copyright (C) 2002-2016 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.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Encodings.IntegerVectorEncoding;
30using HeuristicLab.Optimization;
31using HeuristicLab.Optimization.Operators;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Problems.BinPacking3D {
35  [Item("Bin Packing Problem (3D, integer vector encoding) (BPP)", "Represents a two-dimensional bin-packing problem using only bins with identical measures and bins/items with rectangular shapes.")]
36  [StorableClass]
37  [Creatable(Category = CreatableAttribute.Categories.CombinatorialProblems, Priority = 330)]
38  public sealed class IntegerVectorProblem : ProblemBase<IntegerVectorEncoding, IntegerVector> {
39    [StorableConstructor]
40    private IntegerVectorProblem(bool deserializing) : base(deserializing) { }
41
42    // cloning
43    private IntegerVectorProblem(IntegerVectorProblem original, Cloner cloner)
44      : base(original, cloner) {
45      RegisterEventHandlers();
46    }
47
48    public IntegerVectorProblem()
49      : base() {
50      Decoder = new ExtremePointIntegerVectorDecoder(); // default decoder
51
52      // the int vector contains the target bin number for each item
53      Encoding = new IntegerVectorEncoding(EncodedSolutionName, Items.Count, min: 0, max: LowerBound + 1); // NOTE: assumes that all items can be packed into LowerBound+1 bins
54      AddOperators();
55      Parameterize();
56      RegisterEventHandlers();
57    }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new IntegerVectorProblem(this, cloner);
60    }
61
62    [StorableHook(HookType.AfterDeserialization)]
63    private void AfterDeserialization() {
64      RegisterEventHandlers();
65    }
66
67    protected override void OnEncodingChanged() {
68      base.OnEncodingChanged();
69      Parameterize();
70    }
71
72    private void AddOperators() {
73      // move operators are not yet supported (TODO)
74      Operators.RemoveAll(x => x is SingleObjectiveMoveGenerator);
75      Operators.RemoveAll(x => x is SingleObjectiveMoveMaker);
76      Operators.RemoveAll(x => x is SingleObjectiveMoveEvaluator);
77      Operators.Add(new HammingSimilarityCalculator());
78      Operators.Add(new EuclideanSimilarityCalculator());
79      Operators.Add(new QualitySimilarityCalculator());
80      Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
81
82      Encoding.ConfigureOperators(Operators.OfType<IOperator>()); // gkronber: not strictly necessary (only when customer ops are added)
83    }
84
85    private void RegisterEventHandlers() {
86      // update encoding length when number of items is changed
87      ItemsParameter.ValueChanged += (sender, args) => Parameterize();
88      LowerBoundParameter.Value.ValueChanged += (sender, args) => Parameterize();
89    }
90
91    #region helpers
92    public static List<List<int>> GenerateSequenceMatrix(IntegerVector intVec) {
93      List<List<int>> result = new List<List<int>>();
94      int nrOfBins = intVec.Max() + 1;
95      for (int i = 0; i < nrOfBins; i++)
96        result.Add(new List<int>());
97      for (int i = 0; i < intVec.Length; i++) {
98        result[intVec[i]].Add(i);
99      }
100      return result;
101    }
102
103    private void Parameterize() {
104      Encoding.Length = Items.Count;
105      for (int i = 0; i < Encoding.Bounds.Rows; i++) {
106        Encoding.Bounds[i, 1] = LowerBound + 1;
107      }
108      foreach (var similarityCalculator in Operators.OfType<ISolutionSimilarityCalculator>()) {
109        similarityCalculator.SolutionVariableName = Encoding.SolutionCreator.IntegerVectorParameter.ActualName;
110        similarityCalculator.QualityVariableName = Evaluator.QualityParameter.ActualName;
111      }
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.