Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1966: Fixed some problems in MCV-move operators; Added parts of potvin-encoding implementation;

File size: 5.7 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 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    public static ItemList<PackingInformation> CreateListRandomlyWithSortedSequence(int items, int lowerBound, IRandom random) {
142      Permutation permutation = new Permutation(PermutationTypes.Absolute, items);
143      var result = new ItemList<PackingInformation>();
144      foreach (int itemIndex in permutation) {
145        result.Add(new PackingInformation(itemIndex, random.Next(0, lowerBound + 1), false));
146      }
147      return result;
148    }
149  }
150
151}
Note: See TracBrowser for help on using the repository browser.