- Timestamp:
- 02/26/15 14:38:08 (10 years ago)
- Location:
- trunk/sources
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Optimization.Operators/3.3/SingleObjectiveSolutionSimilarityCalculator.cs
r12012 r12085 32 32 [StorableClass] 33 33 public abstract class SingleObjectiveSolutionSimilarityCalculator : SolutionSimilarityCalculator, ISingleObjectiveSolutionSimilarityCalculator { 34 #region Properties35 [Storable]36 public string SolutionVariableName { get; set; }37 [Storable]38 public string QualityVariableName { get; set; }39 #endregion40 41 34 [StorableConstructor] 42 35 protected SingleObjectiveSolutionSimilarityCalculator(bool deserializing) : base(deserializing) { } 43 36 protected SingleObjectiveSolutionSimilarityCalculator(SingleObjectiveSolutionSimilarityCalculator original, Cloner cloner) 44 37 : base(original, cloner) { 45 this.SolutionVariableName = original.SolutionVariableName; 46 this.QualityVariableName = original.QualityVariableName; 38 47 39 } 48 40 protected SingleObjectiveSolutionSimilarityCalculator() : base() { } -
trunk/sources/HeuristicLab.Optimization.Operators/3.3/SolutionSimilarityCalculator.cs
r12070 r12085 24 24 using HeuristicLab.Common; 25 25 using HeuristicLab.Core; 26 using HeuristicLab.Data; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 … … 35 36 protected abstract bool IsCommutative { get; } 36 37 38 #region Properties 39 [Storable] 40 public string SolutionVariableName { get; set; } 41 [Storable] 42 public string QualityVariableName { get; set; } 43 #endregion 44 37 45 [StorableConstructor] 38 46 protected SolutionSimilarityCalculator(bool deserializing) : base(deserializing) { } 39 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 } 40 53 protected SolutionSimilarityCalculator() : base() { } 41 54 … … 93 106 94 107 public abstract double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution); 95 public abstract bool Equals(IScope x, IScope y); 96 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]; 114 var q2 = x.Variables[QualityVariableName]; 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; // throw exception? 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 return da1.SequenceEqual(da2); 150 151 throw new ArgumentException("Could not determine quality equality."); 152 } 97 153 } 98 154 } -
trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISingleObjectiveSolutionSimilarityCalculator.cs
r12012 r12085 25 25 /// </summary> 26 26 public interface ISingleObjectiveSolutionSimilarityCalculator : ISolutionSimilarityCalculator, ISingleObjectiveOperator { 27 string SolutionVariableName { get; set; } 28 string QualityVariableName { get; set; } 27 29 28 } 30 29 } -
trunk/sources/HeuristicLab.Optimization/3.3/Interfaces/ISolutionSimilarityCalculator.cs
r12012 r12085 28 28 /// </summary> 29 29 public interface ISolutionSimilarityCalculator : IItem, IEqualityComparer<IScope> { 30 string SolutionVariableName { get; set; } 31 string QualityVariableName { get; set; } 32 30 33 /// <summary> 31 34 /// Calculates the similarity of two solutions.
Note: See TracChangeset
for help on using the changeset viewer.