#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Text; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.IntegerVectorEncoding; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Problems.BinPacking.Interfaces; 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 ItemList PackingInformations {get;set;} [StorableConstructor] protected MultiComponentVectorEncoding(bool deserializing) : base(deserializing) { } protected MultiComponentVectorEncoding(MultiComponentVectorEncoding original, Cloner cloner) : base(original, cloner) { this.PackingInformations = cloner.Clone(original.PackingInformations); } public override IDeepCloneable Clone(Cloner cloner) { return new MultiComponentVectorEncoding(this, cloner); } public MultiComponentVectorEncoding() : base() { PackingInformations = new ItemList(); } public override string ToString() { StringBuilder sb = new StringBuilder(); sb.Append("[ "); foreach (var pi in PackingInformations) { sb.Append(pi.ToString() + " "); } 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 ItemIndex { get; set; } [Storable] public int AssignedBin { get; set; } [Storable] public bool Rotated { get; set; } public PackingInformation(int itemIndex, int assignedBin, bool rotated) { this.AssignedBin = assignedBin; this.ItemIndex = itemIndex; this.Rotated = rotated; } public PackingInformation(PackingInformation original) { this.AssignedBin = original.AssignedBin; this.ItemIndex = original.ItemIndex; this.Rotated = original.Rotated; } [StorableConstructor] protected PackingInformation(bool deserializing) : base(deserializing) { } protected PackingInformation(PackingInformation original, Cloner cloner) : base(original, cloner) { this.AssignedBin = original.AssignedBin; this.ItemIndex = original.ItemIndex; this.Rotated = original.Rotated; } public override IDeepCloneable Clone(Cloner cloner) { return new PackingInformation(this, cloner); } public override string ToString() { return String.Format("({0}, {1}, {2}) ", ItemIndex, AssignedBin, Rotated); } public override bool Equals(object obj) { PackingInformation pi = obj as PackingInformation; if (pi != null) return this.ItemIndex.Equals(pi.ItemIndex) && this.AssignedBin.Equals(pi.AssignedBin) && this.Rotated.Equals(pi.Rotated); return false; } public override int GetHashCode() { return base.GetHashCode(); } public static ItemList CreateListRandomly(int items, int lowerBound, IRandom random) { Permutation permutation = new Permutation(PermutationTypes.Absolute, items, random); var result = new ItemList(); foreach (int itemIndex in permutation) { result.Add(new PackingInformation(itemIndex, random.Next(0, lowerBound + 1), random.Next(0, 100) > 60 ? true : false)); } return result; } public static ItemList CreateListRandomlyWithSortedSequence(int items, int lowerBound, IRandom random) { Permutation permutation = new Permutation(PermutationTypes.Absolute, items); var result = new ItemList(); foreach (int itemIndex in permutation) { result.Add(new PackingInformation(itemIndex, random.Next(0, lowerBound + 1), false)); } return result; } } }