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.Text;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
32 |
|
---|
33 | namespace 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 ItemList<PackingInformation> PackingInformations {get;set;}
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected MultiComponentVectorEncoding(bool deserializing) : base(deserializing) { }
|
---|
43 | protected MultiComponentVectorEncoding(MultiComponentVectorEncoding original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | this.PackingInformations = cloner.Clone(original.PackingInformations);
|
---|
46 | }
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new MultiComponentVectorEncoding(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public MultiComponentVectorEncoding()
|
---|
52 | : base() {
|
---|
53 | PackingInformations = new ItemList<PackingInformation>();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public override string ToString() {
|
---|
57 | StringBuilder sb = new StringBuilder();
|
---|
58 | sb.Append("[ ");
|
---|
59 | foreach (var pi in PackingInformations) {
|
---|
60 | sb.Append(pi.ToString() + " ");
|
---|
61 | }
|
---|
62 | sb.Append("]");
|
---|
63 | return sb.ToString();
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | public override bool Equals(object obj) {
|
---|
68 | MultiComponentVectorEncoding mce = obj as MultiComponentVectorEncoding;
|
---|
69 | if (mce != null && mce.PackingInformations != null && mce.PackingInformations.Count == this.PackingInformations.Count) {
|
---|
70 | for (int i = 0; i <= mce.PackingInformations.Count; i++) {
|
---|
71 | if (mce.PackingInformations[i] != this.PackingInformations[i])
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 | public override int GetHashCode() {
|
---|
79 | return PackingInformations.GetHashCode();
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | [Item("Packing Information", "Represents a composition of packing informations for the multi-component packing-encoding.")]
|
---|
84 | [StorableClass]
|
---|
85 | public class PackingInformation : Item {
|
---|
86 | [Storable]
|
---|
87 | public int ItemIndex { get; set; }
|
---|
88 | [Storable]
|
---|
89 | public int AssignedBin { get; set; }
|
---|
90 | [Storable]
|
---|
91 | public bool Rotated { get; set; }
|
---|
92 |
|
---|
93 | public PackingInformation(int itemIndex, int assignedBin, bool rotated) {
|
---|
94 | this.AssignedBin = assignedBin;
|
---|
95 | this.ItemIndex = itemIndex;
|
---|
96 | this.Rotated = rotated;
|
---|
97 | }
|
---|
98 | public PackingInformation(PackingInformation original) {
|
---|
99 | this.AssignedBin = original.AssignedBin;
|
---|
100 | this.ItemIndex = original.ItemIndex;
|
---|
101 | this.Rotated = original.Rotated;
|
---|
102 | }
|
---|
103 |
|
---|
104 | [StorableConstructor]
|
---|
105 | protected PackingInformation(bool deserializing) : base(deserializing) { }
|
---|
106 | protected PackingInformation(PackingInformation original, Cloner cloner)
|
---|
107 | : base(original, cloner) {
|
---|
108 | this.AssignedBin = original.AssignedBin;
|
---|
109 | this.ItemIndex = original.ItemIndex;
|
---|
110 | this.Rotated = original.Rotated;
|
---|
111 | }
|
---|
112 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
113 | return new PackingInformation(this, cloner);
|
---|
114 | }
|
---|
115 |
|
---|
116 | public override string ToString() {
|
---|
117 | return String.Format("({0}, {1}, {2}) ", ItemIndex, AssignedBin, Rotated);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public override bool Equals(object obj) {
|
---|
121 | PackingInformation pi = obj as PackingInformation;
|
---|
122 | if (pi != null)
|
---|
123 | return this.ItemIndex.Equals(pi.ItemIndex) && this.AssignedBin.Equals(pi.AssignedBin) && this.Rotated.Equals(pi.Rotated);
|
---|
124 |
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | public override int GetHashCode() {
|
---|
129 | return base.GetHashCode();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public static ItemList<PackingInformation> CreateListRandomly(int items, int lowerBound, IRandom random) {
|
---|
133 | Permutation permutation = new Permutation(PermutationTypes.Absolute, items, random);
|
---|
134 | var result = new ItemList<PackingInformation>();
|
---|
135 | foreach (int itemIndex in permutation) {
|
---|
136 | result.Add(new PackingInformation (itemIndex, random.Next(0, lowerBound + 1), random.Next(0,100) > 60 ? true : false));
|
---|
137 | }
|
---|
138 | return result;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|