Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.BinPacking/3.3/3D/IntegerVectorEncoding/IntegerVectorProblem.cs @ 14173

Last change on this file since 14173 was 14167, checked in by gkronber, 8 years ago

#2641:

  • fixed persistence bug
  • added unit test to create BPP sample
  • added BPP sample to start page
  • renaming of properties
File size: 3.7 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.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.BinPacking3D {
33  [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.")]
34  [StorableClass]
35  [Creatable(Category = CreatableAttribute.Categories.CombinatorialProblems, Priority = 330)]
36  public sealed class IntegerVectorProblem : ProblemBase<IntegerVectorEncoding, IntegerVector> {
37    [StorableConstructor]
38    private IntegerVectorProblem(bool deserializing) : base(deserializing) { }
39
40    // cloning
41    private IntegerVectorProblem(IntegerVectorProblem original, Cloner cloner)
42      : base(original, cloner) {
43      RegisterEventHandlers();
44    }
45
46    public IntegerVectorProblem()
47      : base() {
48      Decoder = new ExtremePointIntegerVectorDecoder(); // default decoder
49
50      // the int vector contains the target bin number for each item
51      Encoding = new IntegerVectorEncoding(EncodedSolutionName, Items.Count, min: 0, max: LowerBound + 1); // NOTE: assumes that all items can be packed into LowerBound+1 bins
52      AddOperators();
53      RegisterEventHandlers();
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new IntegerVectorProblem(this, cloner);
57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61      RegisterEventHandlers();
62    }
63
64
65    private void AddOperators() {
66
67      // move operators are not yet supported (TODO)
68      Operators.RemoveAll(x => x is SingleObjectiveMoveGenerator);
69      Operators.RemoveAll(x => x is SingleObjectiveMoveMaker);
70      Operators.RemoveAll(x => x is SingleObjectiveMoveEvaluator);
71
72      Encoding.ConfigureOperators(Operators.OfType<IOperator>()); // gkronber: not strictly necessary (only when customer ops are added)
73    }
74
75    private void RegisterEventHandlers() {
76      // update encoding length when number of items is changed
77      ItemsParameter.ValueChanged += (sender, args) => Encoding.Length = Items.Count;
78      LowerBoundParameter.Value.ValueChanged += (sender, args) => {
79        for (int i = 0; i < Encoding.Bounds.Rows; i++) {
80          Encoding.Bounds[i, 1] = LowerBound + 1;
81        }
82      };
83    }
84
85    #region helpers
86
87    public static List<List<int>> GenerateSequenceMatrix(IntegerVector intVec) {
88      List<List<int>> result = new List<List<int>>();
89      int nrOfBins = intVec.Max() + 1;
90      for (int i = 0; i < nrOfBins; i++)
91        result.Add(new List<int>());
92      for (int i = 0; i < intVec.Length; i++) {
93        result[intVec[i]].Add(i);
94      }
95      return result;
96    }
97    #endregion
98  }
99}
Note: See TracBrowser for help on using the repository browser.