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.Collections.Generic;
|
---|
23 | using System.Text;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.BinPacking.Interfaces;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Encodings.PackingEncoding.Potvin {
|
---|
32 | [Item("Potvin Encoding", "Represents an encoding for a bin packing problem using a delimited permutation of item-indexes.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class PotvinEncoding : Item, IPackingSolutionEncoding{
|
---|
35 |
|
---|
36 | [Storable]
|
---|
37 | public IntegerVector IntVector { get; set; }
|
---|
38 | [Storable]
|
---|
39 | public bool DelimiterInitialized { get; set; }
|
---|
40 |
|
---|
41 | private int nrOfBins = -1;
|
---|
42 | public int NrOfBins {
|
---|
43 | get {
|
---|
44 | if (nrOfBins < 0) {
|
---|
45 | nrOfBins = 1;
|
---|
46 | foreach (int i in IntVector) {
|
---|
47 | if (i == -1)
|
---|
48 | nrOfBins++;
|
---|
49 | }
|
---|
50 | }
|
---|
51 | return nrOfBins;
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | public List<int> GetItemsInBin(int binNr) {
|
---|
56 | int binCount = 0;
|
---|
57 | List<int> result = new List<int> ();
|
---|
58 | foreach (int i in IntVector) {
|
---|
59 | if (i == -1) {
|
---|
60 | binCount++;
|
---|
61 | if (binCount > binNr)
|
---|
62 | break;
|
---|
63 | }
|
---|
64 | if (binCount == binNr)
|
---|
65 | result.Add(i);
|
---|
66 | }
|
---|
67 | return result;
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableConstructor]
|
---|
71 | protected PotvinEncoding(bool deserializing) : base(deserializing) { }
|
---|
72 | protected PotvinEncoding(PotvinEncoding original, Cloner cloner)
|
---|
73 | : base(original, cloner) {
|
---|
74 | this.IntVector = cloner.Clone(original.IntVector);
|
---|
75 | }
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new PotvinEncoding(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public PotvinEncoding()
|
---|
81 | : base() {
|
---|
82 | IntVector = new IntegerVector();
|
---|
83 | DelimiterInitialized = false;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override string ToString() {
|
---|
87 | StringBuilder sb = new StringBuilder();
|
---|
88 | sb.Append("[ ");
|
---|
89 | sb.Append(IntVector.ToString());
|
---|
90 | sb.Append("]");
|
---|
91 | return sb.ToString();
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | public override bool Equals(object obj) {
|
---|
96 | if (obj.GetType() == typeof(PotvinEncoding))
|
---|
97 | return AreEqual(this, obj as PotvinEncoding);
|
---|
98 |
|
---|
99 | return false;
|
---|
100 | }
|
---|
101 | public override int GetHashCode() {
|
---|
102 | return IntVector.GetHashCode();
|
---|
103 | }
|
---|
104 | private static bool AreEqual(PotvinEncoding PotvinEncoding1, PotvinEncoding PotvinEncoding2) {
|
---|
105 | return AreEqual(PotvinEncoding1.IntVector, PotvinEncoding2.IntVector);
|
---|
106 | }
|
---|
107 |
|
---|
108 | private static bool AreEqual(IntArray v1, IntArray v2) {
|
---|
109 | if (v1.Length != v2.Length)
|
---|
110 | return false;
|
---|
111 | for (int i = 0; i < v1.Length; i++) {
|
---|
112 | if (v1[i] != v2[i])
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|