#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.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.IntegerVectorEncoding;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.BinPacking;
namespace HeuristicLab.Encodings.PackingEncoding.GroupingVector {
[Item("Grouping Vector Encoding", "Represents an encoding for a bin packing problem using an array of group-assignments for every packing-item.")]
[StorableClass]
public class GroupingVectorEncoding : Item, IPackingSolutionEncoding {
[Storable]
public IntegerVector GroupingVector { get; set; }
[StorableConstructor]
protected GroupingVectorEncoding(bool deserializing) : base(deserializing) { }
protected GroupingVectorEncoding(GroupingVectorEncoding original, Cloner cloner)
: base(original, cloner) {
this.GroupingVector = cloner.Clone(original.GroupingVector);
}
public override IDeepCloneable Clone(Cloner cloner) {
return new GroupingVectorEncoding(this, cloner);
}
public GroupingVectorEncoding()
: base() {
GroupingVector = new IntegerVector();
}
public List> GenerateSequenceMatrix() {
List> result = new List>();
int nrOfBins = GroupingVector.Max() + 1;
for (int i = 0; i < nrOfBins; i++)
result.Add(new List());
for (int i = 0; i < GroupingVector.Length; i++) {
result[GroupingVector[i]].Add(i);
}
return result;
}
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.Append("[ ");
sb.Append(GroupingVector.ToString());
sb.Append("]");
return sb.ToString();
}
public override bool Equals(object obj) {
if (obj.GetType() == typeof(GroupingVectorEncoding))
return AreEqual(this, obj as GroupingVectorEncoding);
return false;
}
public override int GetHashCode() {
return GroupingVector.GetHashCode();
}
private static bool AreEqual(GroupingVectorEncoding groupingVectorEncoding1, GroupingVectorEncoding groupingVectorEncoding2) {
return AreEqual(groupingVectorEncoding1.GroupingVector, groupingVectorEncoding2.GroupingVector);
}
private static bool AreEqual(IntArray v1, IntArray v2) {
if (v1.Length != v2.Length)
return false;
for (int i = 0; i < v1.Length; i++) {
if (v1[i] != v2[i])
return false;
}
return true;
}
}
}