Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.BinPacking/HeuristicLab.Problems.BinPacking/3.3/Encodings/MultiComponentVector/MultiComponentVectorEncoding.cs @ 9563

Last change on this file since 9563 was 9563, checked in by jhelm, 11 years ago

#1966: Implemented additional Operator-Wrappers for PackingSequence and GroupingVector; Implemented additional problem-class for Rosenbauer-Problemstatement; Added marker-interfaces for decoder-types;

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Text;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Encodings.PermutationEncoding;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.BinPacking.Interfaces;
32
33namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector {
34  [Item("Multi Component Vector Encoding", "Represents an encoding for a bin packing problem using an array of objects containing multiple packing-informations for every packing-item.")]
35  [StorableClass]
36  public class MultiComponentVectorEncoding : Item, IPackingSolutionEncoding {
37
38    [Storable]
39    public ObservableDictionary<int, ItemList<PackingInformation>> PackingInformations {get;set;}
40
41    public int NrOfBins { get { return PackingInformations.Count; } }
42    public int NrOfItems {
43      get {
44        int nrOfItems = 0;
45        foreach (var entry in PackingInformations) { nrOfItems += entry.Value.Count; }
46        return nrOfItems;
47      }
48    } 
49
50    [StorableConstructor]
51    protected MultiComponentVectorEncoding(bool deserializing) : base(deserializing) { }
52    protected MultiComponentVectorEncoding(MultiComponentVectorEncoding original, Cloner cloner)
53      : base(original, cloner) {
54        this.PackingInformations = new ObservableDictionary<int,ItemList<PackingInformation>>();
55      foreach (var entry in original.PackingInformations) {
56        this.PackingInformations[entry.Key] = cloner.Clone(entry.Value);
57      }
58    }
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new MultiComponentVectorEncoding(this, cloner);
61    }
62
63    public MultiComponentVectorEncoding()
64      : base() {
65        PackingInformations = new ObservableDictionary<int, ItemList<PackingInformation>>();
66    }       
67
68    public override string ToString() {
69      StringBuilder sb = new StringBuilder();
70      sb.Append("[ ");
71      foreach (var gi in PackingInformations) {
72        sb.Append("g#" + gi.Key.ToString() + "{ ");
73        foreach (var pi in gi.Value) {
74          sb.Append(pi.ToString() + " ");
75        }
76        sb.Append("} ");
77      }
78      sb.Append("]");
79      return sb.ToString();
80    }
81
82
83    public override bool Equals(object obj) {
84      MultiComponentVectorEncoding mce = obj as MultiComponentVectorEncoding;
85      if (mce != null && mce.PackingInformations != null && mce.PackingInformations.Count == this.PackingInformations.Count) {
86        for (int i = 0; i < mce.PackingInformations.Count; i++) {
87          if (mce.PackingInformations[i] != this.PackingInformations[i])
88            return false;
89        }
90      }
91
92      return true;
93    }
94    public override int GetHashCode() {
95      return PackingInformations.GetHashCode();
96    }
97
98  }
99
100  [Item("Packing Information", "Represents a composition of packing informations for the multi-component packing-encoding.")]
101  [StorableClass]
102  public class PackingInformation : Item {
103    [Storable]
104    public int ItemID { get; set; }
105    [Storable]
106    public bool Rotated { get; set; }
107
108    public PackingInformation(int itemID, bool rotated) {
109      this.ItemID   = itemID;
110      this.Rotated      = rotated;
111    }     
112    public PackingInformation(PackingInformation original) {
113      this.ItemID = original.ItemID;
114      this.Rotated = original.Rotated;
115    }
116
117    [StorableConstructor]
118    protected PackingInformation(bool deserializing) : base(deserializing) { }
119    protected PackingInformation(PackingInformation original, Cloner cloner)
120      : base(original, cloner) {
121        this.ItemID   = original.ItemID;
122        this.Rotated      = original.Rotated;
123    }
124    public override IDeepCloneable Clone(Cloner cloner) {
125      return new PackingInformation(this, cloner);
126    }
127
128    public override string ToString() {
129      return String.Format("{0}", ItemID.ToString());
130      //return String.Format("({0}, {1}) ", ItemID, Rotated ? "t" : "f");
131    }
132
133    public override bool Equals(object obj) {
134      PackingInformation pi = obj as PackingInformation;
135      if (pi != null)
136        return this.ItemID.Equals(pi.ItemID) && this.Rotated.Equals(pi.Rotated);
137
138      return false;
139    }
140
141    public override int GetHashCode() {
142      return base.GetHashCode();
143    }
144
145    public static ObservableDictionary<int, ItemList<PackingInformation>> CreateDictionaryRandomly(int items, int nrOfBins, IRandom random) {
146      var result = new ObservableDictionary<int, ItemList<PackingInformation>>();
147      for (int i = 0; i < nrOfBins; i++)
148        result[i] = new ItemList<PackingInformation>();
149
150      Permutation randomItemSequence = new Permutation(PermutationTypes.Absolute, items, random);
151      foreach (int i in randomItemSequence) {
152        result[random.Next(nrOfBins)].Add(new PackingInformation(i, random.Next(100) > 60 ? true : false));
153      }
154      return result;
155    }
156
157    public static ObservableDictionary<int, ItemList<PackingInformation>> CreateDictionaryRandomlyWithSortedSequence(int items, int nrOfBins, IRandom random) {
158      var result = new ObservableDictionary<int, ItemList<PackingInformation>>();
159      for (int i = 0; i < nrOfBins; i++)
160        result[i] = new ItemList<PackingInformation>();
161
162      for (int i = 0; i < items; i++) {
163        result[random.Next(nrOfBins)].Add(new PackingInformation(i, false));
164      }
165      return result;
166    }
167  }
168
169}
Note: See TracBrowser for help on using the repository browser.