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