Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/old files/GroupingVector/GroupingVectorEncoding.cs @ 16046

Last change on this file since 16046 was 14046, checked in by gkronber, 8 years ago

#1966: unified namespaces

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.BinPacking;
31
32namespace HeuristicLab.Encodings.PackingEncoding.GroupingVector {
33  [Item("Grouping Vector Encoding", "Represents an encoding for a bin packing problem using an array of group-assignments for every packing-item.")]
34  [StorableClass]
35  public class GroupingVectorEncoding : Item, IPackingSolutionEncoding {
36
37    [Storable]
38    public IntegerVector GroupingVector { get; set; }
39
40    [StorableConstructor]
41    protected GroupingVectorEncoding(bool deserializing) : base(deserializing) { }
42    protected GroupingVectorEncoding(GroupingVectorEncoding original, Cloner cloner)
43      : base(original, cloner) {
44        this.GroupingVector = cloner.Clone(original.GroupingVector);
45    }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new GroupingVectorEncoding(this, cloner);
48    }
49
50    public GroupingVectorEncoding()
51      : base() {
52        GroupingVector = new IntegerVector();
53    }
54
55    public List<List<int>> GenerateSequenceMatrix() {
56      List<List<int>> result = new List<List<int>>();
57      int nrOfBins = GroupingVector.Max() + 1;
58      for (int i = 0; i < nrOfBins; i++)
59        result.Add(new List<int>());
60      for (int i = 0; i < GroupingVector.Length; i++) {
61        result[GroupingVector[i]].Add(i);
62      }
63      return result;
64    }
65   
66
67
68    public override string ToString() {
69      StringBuilder sb = new StringBuilder();
70      sb.Append("[ ");
71      sb.Append(GroupingVector.ToString());         
72      sb.Append("]");
73      return sb.ToString();
74    }
75
76
77    public override bool Equals(object obj) {
78      if (obj.GetType() == typeof(GroupingVectorEncoding))
79        return AreEqual(this, obj as GroupingVectorEncoding);
80
81      return false;
82    }
83    public override int GetHashCode() {
84      return GroupingVector.GetHashCode();
85    }
86    private static bool AreEqual(GroupingVectorEncoding groupingVectorEncoding1, GroupingVectorEncoding groupingVectorEncoding2) {
87      return AreEqual(groupingVectorEncoding1.GroupingVector, groupingVectorEncoding2.GroupingVector);
88    }
89
90    private static bool AreEqual(IntArray v1, IntArray v2) {
91      if (v1.Length != v2.Length)
92        return false;
93      for (int i = 0; i < v1.Length; i++) {
94        if (v1[i] != v2[i])
95          return false;
96      }
97      return true;
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.