Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.BinPacking/3.3/2D/IntegerVectorEncoding/IntegerVectorProblem.cs @ 17738

Last change on this file since 17738 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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