#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Joseph Helm and Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.BinPacking; namespace HeuristicLab.Encodings.PackingEncoding.MultiComponentVector { [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.")] [StorableClass] public class MultiComponentVectorEncoding : Item, IPackingSolutionEncoding { [Storable] public ObservableDictionary> PackingInformations { get; set; } public int NrOfBins { get { return PackingInformations.Count; } } public int NrOfItems { get { int nrOfItems = 0; foreach (var entry in PackingInformations) { nrOfItems += entry.Value.Count; } return nrOfItems; } } [StorableConstructor] protected MultiComponentVectorEncoding(bool deserializing) : base(deserializing) { } protected MultiComponentVectorEncoding(MultiComponentVectorEncoding original, Cloner cloner) : base(original, cloner) { this.PackingInformations = new ObservableDictionary>(); foreach (var entry in original.PackingInformations) { this.PackingInformations[entry.Key] = cloner.Clone(entry.Value); } } public override IDeepCloneable Clone(Cloner cloner) { return new MultiComponentVectorEncoding(this, cloner); } public MultiComponentVectorEncoding() : base() { PackingInformations = new ObservableDictionary>(); } public List> GenerateSequenceMatrix() { List> result = new List>(); foreach (var bi in PackingInformations) { var temp = new List(); foreach (var piEntry in bi.Value) { temp.Add(piEntry.ItemID); } result.Add(temp); } return result; } public Dictionary GenerateRotationArray() { Dictionary result = new Dictionary(); foreach (var bi in PackingInformations) foreach (var pi in bi.Value) result[pi.ItemID] = pi.Rotated; return result; } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("[ "); foreach (var gi in PackingInformations) { sb.Append("g#" + gi.Key.ToString() + "{ "); foreach (var pi in gi.Value) { sb.Append(pi.ToString() + " "); } sb.Append("} "); } sb.Append("]"); return sb.ToString(); } public override bool Equals(object obj) { MultiComponentVectorEncoding mce = obj as MultiComponentVectorEncoding; if (mce != null && mce.PackingInformations != null && mce.PackingInformations.Count == this.PackingInformations.Count) { for (int i = 0; i < mce.PackingInformations.Count; i++) { if (mce.PackingInformations[i] != this.PackingInformations[i]) return false; } } return true; } public override int GetHashCode() { return PackingInformations.GetHashCode(); } } [Item("Packing Information", "Represents a composition of packing informations for the multi-component packing-encoding.")] [StorableClass] public class PackingInformation : Item { [Storable] public int ItemID { get; set; } [Storable] public bool Rotated { get; set; } public PackingInformation(int itemID, bool rotated) { this.ItemID = itemID; this.Rotated = rotated; } public PackingInformation(PackingInformation original) { this.ItemID = original.ItemID; this.Rotated = original.Rotated; } [StorableConstructor] protected PackingInformation(bool deserializing) : base(deserializing) { } protected PackingInformation(PackingInformation original, Cloner cloner) : base(original, cloner) { this.ItemID = original.ItemID; this.Rotated = original.Rotated; } public override IDeepCloneable Clone(Cloner cloner) { return new PackingInformation(this, cloner); } public override string ToString() { return String.Format("{0}", ItemID.ToString()); //return String.Format("({0}, {1}) ", ItemID, Rotated ? "t" : "f"); } public override bool Equals(object obj) { PackingInformation pi = obj as PackingInformation; if (pi != null) return this.ItemID.Equals(pi.ItemID) && this.Rotated.Equals(pi.Rotated); return false; } public override int GetHashCode() { return base.GetHashCode(); } public static ObservableDictionary> CreateDictionaryRandomly(int items, int nrOfBins, IRandom random) { var result = new ObservableDictionary>(); for (int i = 0; i < nrOfBins; i++) result[i] = new ItemList(); Permutation randomItemSequence = new Permutation(PermutationTypes.Absolute, items, random); foreach (int i in randomItemSequence) { result[random.Next(nrOfBins)].Add(new PackingInformation(i, random.Next(100) > 60 ? true : false)); } return result; } public static ObservableDictionary> CreateDictionaryRandomlyWithSortedSequence(int items, int nrOfBins, IRandom random) { var result = new ObservableDictionary>(); for (int i = 0; i < nrOfBins; i++) result[i] = new ItemList(); for (int i = 0; i < items; i++) { result[random.Next(nrOfBins)].Add(new PackingInformation(i, false)); } return result; } } }