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