Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/01/15 17:06:17 (10 years ago)
Author:
mkommend
Message:

#2332: Merged all changes from this ticket into stable.

Location:
stable
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Optimization.Operators/3.3/NoSimilarityCalculator.cs

    r12009 r12280  
    3232  [Item("NoSimilarityCalculator", "An item that performs similarity calculation between two solutions. The item always considers two solutions to be distinct.")]
    3333  public sealed class NoSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
     34    protected override bool IsCommutative { get { return true; } }
     35
    3436    private NoSimilarityCalculator(bool deserializing) : base(deserializing) { }
    3537    private NoSimilarityCalculator(NoSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
  • stable/HeuristicLab.Optimization.Operators/3.3/QualitySimilarityCalculator.cs

    r12009 r12280  
    3434  [Item("QualitySimilarityCalculator", "An item that performs similarity calculation between two solutions. The item only considers the qualities of the two solutions.")]
    3535  public sealed class QualitySimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
     36    protected override bool IsCommutative { get { return true; } }
     37
    3638    private QualitySimilarityCalculator(bool deserializing) : base(deserializing) { }
    3739    private QualitySimilarityCalculator(QualitySimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
  • stable/HeuristicLab.Optimization.Operators/3.3/SingleObjectiveSolutionSimilarityCalculator.cs

    r12009 r12280  
    3131  [Item("SimilarityCalculator", "A base class for items that perform similarity calculation between two solutions.")]
    3232  [StorableClass]
     33#pragma warning disable 0618
    3334  public abstract class SingleObjectiveSolutionSimilarityCalculator : SolutionSimilarityCalculator, ISingleObjectiveSolutionSimilarityCalculator {
    34     #region Properties
    35     [Storable]
    36     public string SolutionVariableName { get; set; }
    37     [Storable]
    38     public string QualityVariableName { get; set; }
    39     #endregion
    40 
     35#pragma warning restore 0618
    4136    [StorableConstructor]
    4237    protected SingleObjectiveSolutionSimilarityCalculator(bool deserializing) : base(deserializing) { }
    4338    protected SingleObjectiveSolutionSimilarityCalculator(SingleObjectiveSolutionSimilarityCalculator original, Cloner cloner)
    4439      : base(original, cloner) {
    45       this.SolutionVariableName = original.SolutionVariableName;
    46       this.QualityVariableName = original.QualityVariableName;
     40
    4741    }
    4842    protected SingleObjectiveSolutionSimilarityCalculator() : base() { }
  • stable/HeuristicLab.Optimization.Operators/3.3/SolutionSimilarityCalculator.cs

    r12009 r12280  
    2424using HeuristicLab.Common;
    2525using HeuristicLab.Core;
     26using HeuristicLab.Data;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728
     
    3334  [StorableClass]
    3435  public abstract class SolutionSimilarityCalculator : Item, ISolutionSimilarityCalculator {
     36    protected abstract bool IsCommutative { get; }
     37
     38    #region Properties
     39    [Storable]
     40    public string SolutionVariableName { get; set; }
     41    [Storable]
     42    public string QualityVariableName { get; set; }
     43    #endregion
     44
    3545    [StorableConstructor]
    3646    protected SolutionSimilarityCalculator(bool deserializing) : base(deserializing) { }
    37     protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }
     47
     48    protected SolutionSimilarityCalculator(SolutionSimilarityCalculator original, Cloner cloner)
     49      : base(original, cloner) {
     50      this.SolutionVariableName = original.SolutionVariableName;
     51      this.QualityVariableName = original.QualityVariableName;
     52    }
    3853    protected SolutionSimilarityCalculator() : base() { }
    3954
     
    7186      for (int i = 0; i < individuals.Count; i++) similarityMatrix[i] = new double[individuals.Count];
    7287
    73       for (int i = 0; i < individuals.Count; i++) {
    74         for (int j = i; j < individuals.Count; j++) {
    75           similarityMatrix[i][j] = similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     88      if (IsCommutative) {
     89        for (int i = 0; i < individuals.Count; i++) {
     90          for (int j = i; j < individuals.Count; j++) {
     91            similarityMatrix[i][j] = similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     92          }
     93        }
     94      } else {
     95        for (int i = 0; i < individuals.Count; i++) {
     96          for (int j = i; j < individuals.Count; j++) {
     97            similarityMatrix[i][j] = CalculateSolutionSimilarity(individuals[i], individuals[j]);
     98            if (i == j) continue;
     99            similarityMatrix[j][i] = CalculateSolutionSimilarity(individuals[j], individuals[i]);
     100          }
    76101        }
    77102      }
     
    81106
    82107    public abstract double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution);
    83     public abstract bool Equals(IScope x, IScope y);
    84     public abstract int GetHashCode(IScope obj);
     108
     109    public virtual bool Equals(IScope x, IScope y) {
     110      if (ReferenceEquals(x, y)) return true;
     111      if (x == null || y == null) return false;
     112
     113      var q1 = x.Variables[QualityVariableName].Value;
     114      var q2 = y.Variables[QualityVariableName].Value;
     115
     116      return CheckQualityEquality(q1, q2) && CalculateSolutionSimilarity(x, y).IsAlmost(1.0);
     117    }
     118
     119    public virtual int GetHashCode(IScope scope) {
     120      var quality = scope.Variables[QualityVariableName].Value;
     121      var dv = quality as DoubleValue;
     122      if (dv != null)
     123        return dv.Value.GetHashCode();
     124
     125      var da = quality as DoubleArray;
     126      if (da != null) {
     127        int hash = 17;
     128        unchecked {
     129          for (int i = 0; i < da.Length; ++i) {
     130            hash += hash * 23 + da[i].GetHashCode();
     131          }
     132          return hash;
     133        }
     134      }
     135      return 0;
     136    }
     137
     138    private static bool CheckQualityEquality(IItem q1, IItem q2) {
     139      var d1 = q1 as DoubleValue;
     140      var d2 = q2 as DoubleValue;
     141
     142      if (d1 != null && d2 != null)
     143        return d1.Value.IsAlmost(d2.Value);
     144
     145      var da1 = q1 as DoubleArray;
     146      var da2 = q2 as DoubleArray;
     147
     148      if (da1 != null && da2 != null) {
     149        if (da1.Length != da2.Length)
     150          throw new ArgumentException("The quality arrays must have the same length.");
     151
     152        for (int i = 0; i < da1.Length; ++i) {
     153          if (!da1[i].IsAlmost(da2[i]))
     154            return false;
     155        }
     156
     157        return true;
     158      }
     159
     160      throw new ArgumentException("Could not determine quality equality.");
     161    }
    85162  }
    86163}
Note: See TracChangeset for help on using the changeset viewer.