Changeset 15067


Ignore:
Timestamp:
06/26/17 23:13:15 (4 years ago)
Author:
abeham
Message:

#2730:

  • Unified implementation of all equality comparers and similarity calculators in BinaryVector, IntegerVector, RealVector, Permutation, and LinearLinkage encodings
  • Added Euclidean distance-based similarity calculators for real and integer vectors using a transformation function with scaling parameter
Location:
trunk/sources
Files:
2 added
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/BinaryVectorEqualityComparer.cs

    r14659 r15067  
    1919 */
    2020#endregion
    21  
     21
     22using System;
    2223using System.Collections.Generic;
    23 using System.Linq;
     24using HeuristicLab.PluginInfrastructure;
    2425
    2526namespace HeuristicLab.Encodings.BinaryVectorEncoding {
     27  [NonDiscoverableType]
    2628  public class BinaryVectorEqualityComparer : EqualityComparer<BinaryVector> {
    27     public override bool Equals(BinaryVector first, BinaryVector second) {
    28       return first.SequenceEqual(second);
     29    public override bool Equals(BinaryVector x, BinaryVector y) {
     30      if (x == null && y == null) return true;
     31      if (x == null || y == null) return false;
     32      if (ReferenceEquals(x, y)) return true;
     33      if (x.Length != y.Length) return false;
     34      for (var i = 0; i < x.Length; i++)
     35        if (x[i] != y[i]) return false;
     36      return true;
    2937    }
    3038    public override int GetHashCode(BinaryVector obj) {
     39      if (obj == null) throw new ArgumentNullException("obj", "BinaryVectorEqualityComparer: Cannot compute hash value of null.");
    3140      unchecked {
    3241        int hash = 17;
  • trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/SimilarityCalculators/HammingSimilarityCalculator.cs

    r14659 r15067  
    4545    public static double CalculateSimilarity(BinaryVector left, BinaryVector right) {
    4646      if (left == null || right == null)
    47         throw new ArgumentException("Cannot calculate similarity because one or both of the provided scopes is null.");
     47        throw new ArgumentException("Cannot calculate similarity because one or both of the provided solutions is null.");
    4848      if (left.Length != right.Length)
    4949        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
    50       if (left == right) return 1.0;
     50      if (left.Length == 0)
     51        throw new ArgumentException("Cannot calculate similarity because solutions are of length 0.");
     52      if (ReferenceEquals(left, right)) return 1.0;
    5153
    5254      double similarity = 0.0;
  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/HeuristicLab.Encodings.IntegerVectorEncoding-3.3.csproj

    r14660 r15067  
    147147    <Compile Include="Manipulators\RoundedNormalAllPositionsManipulator.cs" />
    148148    <Compile Include="Manipulators\SelfAdaptiveRoundedNormalAllPositionsManipulator.cs" />
     149    <Compile Include="SimilarityCalculators\EuclideanSimilarityCalculator.cs" />
    149150    <Compile Include="SimilarityCalculators\HammingSimilarityCalculator.cs" />
    150151    <Compile Include="StrategyParameters\StdDevStrategyVectorCreator.cs" />
  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/IntegerVectorEqualityComparer.cs

    r14659 r15067  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
     24using HeuristicLab.PluginInfrastructure;
    2325
    2426namespace HeuristicLab.Encodings.IntegerVectorEncoding {
     27  [NonDiscoverableType]
    2528  public class IntegerVectorEqualityComparer : EqualityComparer<IntegerVector> {
    2629    public override bool Equals(IntegerVector x, IntegerVector y) {
    2730      if (x == null && y == null) return true;
    2831      if (x == null || y == null) return false;
     32      if (ReferenceEquals(x, y)) return true;
    2933      if (x.Length != y.Length) return false;
    3034      for (var i = 0; i < x.Length; i++)
     
    3438
    3539    public override int GetHashCode(IntegerVector obj) {
     40      if (obj == null) throw new ArgumentNullException("obj", "IntegerVectorEqualityComparer: Cannot compute hash value of null.");
    3641      unchecked {
    3742        int hash = 17;
  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/SimilarityCalculators/HammingSimilarityCalculator.cs

    r14659 r15067  
    4545    public static double CalculateSimilarity(IntegerVector left, IntegerVector right) {
    4646      if (left == null || right == null)
    47         throw new ArgumentException("Cannot calculate similarity because one or both of the provided scopes is null.");
     47        throw new ArgumentException("Cannot calculate similarity because one or both of the provided solutions is null.");
    4848      if (left.Length != right.Length)
    4949        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
    50       if (left == right) return 1.0;
     50      if (left.Length == 0)
     51        throw new ArgumentException("Cannot calculate similarity because solutions are of length 0.");
     52      if (ReferenceEquals(left, right)) return 1.0;
    5153
    5254      double similarity = 0.0;
  • trunk/sources/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/LinearLinkageEqualityComparer.cs

    r14659 r15067  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
     24using HeuristicLab.PluginInfrastructure;
    2325
    2426namespace HeuristicLab.Encodings.LinearLinkageEncoding {
     27  [NonDiscoverableType]
    2528  public class LinearLinkageEqualityComparer : EqualityComparer<LinearLinkage> {
    2629    public override bool Equals(LinearLinkage x, LinearLinkage y) {
    2730      if (x == null && y == null) return true;
    2831      if (x == null || y == null) return false;
     32      if (ReferenceEquals(x, y)) return true;
    2933      if (x.Length != y.Length) return false;
    3034      for (var i = 0; i < x.Length; i++)
     
    3438
    3539    public override int GetHashCode(LinearLinkage obj) {
     40      if (obj == null) throw new ArgumentNullException("obj", "LinearLinkageEqualityComparer: Cannot compute hash value of null.");
    3641      unchecked {
    3742        int hash = 17;
  • trunk/sources/HeuristicLab.Encodings.LinearLinkageEncoding/3.4/SimilarityCalculators/HammingSimilarityCalculator.cs

    r14659 r15067  
    3030  [StorableClass]
    3131  public sealed class HammingSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
    32 
    33     protected override bool IsCommutative {
    34       get { return true; }
    35     }
     32    protected override bool IsCommutative { get { return true; } }
    3633
    3734    [StorableConstructor]
    3835    private HammingSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3936    private HammingSimilarityCalculator(HammingSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
    40     public HammingSimilarityCalculator() { }
     37    public HammingSimilarityCalculator() : base() { }
    4138
    4239    public override IDeepCloneable Clone(Cloner cloner) {
     
    4542
    4643    public static double CalculateSimilarity(LinearLinkage left, LinearLinkage right) {
    47       if (left.Length != right.Length) throw new ArgumentException("Comparing encodings of unequal length");
     44      if (left == null || right == null)
     45        throw new ArgumentException("Cannot calculate similarity because one or both of the provided solutions is null.");
     46      if (left.Length != right.Length)
     47        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
     48      if (left.Length == 0)
     49        throw new ArgumentException("Cannot calculate similarity because solutions are of length 0.");
     50      if (ReferenceEquals(left, right)) return 1.0;
     51
    4852      var similarity = 0;
    4953      for (var i = 0; i < left.Length; i++) {
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/PermutationEqualityComparer.cs

    r14185 r15067  
    3030  public class PermutationEqualityComparer : EqualityComparer<Permutation> {
    3131    public override bool Equals(Permutation x, Permutation y) {
     32      if (x == null && y == null) return true;
     33      if (x == null || y == null) return false;
     34      if (ReferenceEquals(x, y)) return true;
     35      if (x.Length != y.Length) return false;
    3236      if (x.PermutationType != y.PermutationType) return false;
    33       if (x.Length != y.Length) return false;
    3437      switch (x.PermutationType) {
    3538        case PermutationTypes.Absolute:
     
    6770
    6871    public override int GetHashCode(Permutation obj) {
    69       if (obj == null) return 0;
     72      if (obj == null) throw new ArgumentNullException("obj", "PermutationEqualityComparer: Cannot compute hash value of null.");
    7073      return GenerateHashString(obj).GetHashCode();
    7174    }
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/SimilarityCalculators/HammingSimilarityCalculator.cs

    r14659 r15067  
    2424using HeuristicLab.Core;
    2525using HeuristicLab.Optimization.Operators;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2627
    2728namespace HeuristicLab.Encodings.PermutationEncoding {
    2829  [Item("Hamming Similarity Calculator for Permutation", "An operator that performs similarity calculation between two permutation-encoded solutions.")]
     30  [StorableClass]
    2931  public sealed class HammingSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
    3032    protected override bool IsCommutative { get { return true; } }
    3133
     34    [StorableConstructor]
    3235    private HammingSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3336    private HammingSimilarityCalculator(HammingSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
     
    4043    public static double CalculateSimilarity(Permutation left, Permutation right) {
    4144      if (left == null || right == null)
    42         throw new ArgumentException("Cannot calculate similarity because one of the provided solutions or both are null.");
     45        throw new ArgumentException("Cannot calculate similarity because one or both of the provided solutions is null.");
    4346      if (left.PermutationType != right.PermutationType)
    44         throw new ArgumentException("Cannot calculate similarity because the provided solutions have different types.");
     47        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different permutation types.");
    4548      if (left.Length != right.Length)
    4649        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
    47       if (object.ReferenceEquals(left, right)) return 1.0;
     50      if (left.Length == 0)
     51        throw new ArgumentException("Cannot calculate similarity because solutions are of length 0.");
     52      if (ReferenceEquals(left, right)) return 1.0;
    4853
    4954      switch (left.PermutationType) {
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj

    r14660 r15067  
    183183    <Compile Include="ReflectiveBoundsChecker.cs" />
    184184    <Compile Include="ShakingOperators\RealVectorShakingOperator.cs" />
     185    <Compile Include="SimilarityCalculators\EuclideanSimilarityCalculator.cs" />
    185186    <Compile Include="SimilarityCalculators\HammingSimilarityCalculator.cs" />
    186187    <Compile Include="StrategyParameters\StdDevStrategyVectorCreator.cs" />
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/RealVectorEqualityComparer.cs

    r14659 r15067  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
     24using HeuristicLab.PluginInfrastructure;
    2325
    2426namespace HeuristicLab.Encodings.RealVectorEncoding {
     27  [NonDiscoverableType]
    2528  public class RealVectorEqualityComparer : EqualityComparer<RealVector> {
    2629    public override bool Equals(RealVector x, RealVector y) {
    2730      if (x == null && y == null) return true;
    2831      if (x == null || y == null) return false;
     32      if (ReferenceEquals(x, y)) return true;
    2933      if (x.Length != y.Length) return false;
    3034      for (var i = 0; i < x.Length; i++)
     
    3438
    3539    public override int GetHashCode(RealVector obj) {
     40      if (obj == null) throw new ArgumentNullException("obj", "RealVectorEqualityComparer: Cannot compute hash value of null.");
    3641      unchecked {
    3742        int hash = 17;
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/SimilarityCalculators/HammingSimilarityCalculator.cs

    r14659 r15067  
    4545    public static double CalculateSimilarity(RealVector left, RealVector right) {
    4646      if (left == null || right == null)
    47         throw new ArgumentException("Cannot calculate similarity because one or both of the provided scopes is null.");
     47        throw new ArgumentException("Cannot calculate similarity because one or both of the provided solutions is null.");
    4848      if (left.Length != right.Length)
    4949        throw new ArgumentException("Cannot calculate similarity because the provided solutions have different lengths.");
    50       if (left == right) return 1.0;
     50      if (left.Length == 0)
     51        throw new ArgumentException("Cannot calculate similarity because solutions are of length 0.");
     52      if (ReferenceEquals(left, right)) return 1.0;
    5153
    5254      double similarity = 0.0;
Note: See TracChangeset for help on using the changeset viewer.