Changeset 8086 for branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
- Timestamp:
- 06/22/12 11:11:38 (12 years ago)
- Location:
- branches/ScatterSearch (trunk integration)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)
- Property svn:ignore
-
old new 20 20 bin 21 21 protoc.exe 22 _ReSharper.HeuristicLab 3.3 Tests
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
r7789 r8086 27 27 using HeuristicLab.Data; 28 28 using HeuristicLab.Operators; 29 using HeuristicLab.Optimization; 29 30 using HeuristicLab.Optimization.Operators; 30 31 using HeuristicLab.Parameters; … … 37 38 [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")] 38 39 [StorableClass] 39 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchOperator { 40 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IScatterSearchOperator, ISimilarityBasedOperator { 41 #region ISimilarityBasedOperator Members 42 public ISimilarityCalculator SimilarityCalculator { get; set; } 43 #endregion 44 40 45 #region Parameter properties 41 46 public ScopeParameter CurrentScopeParameter { … … 53 58 public IValueLookupParameter<IntValue> ReferenceSetSizeParameter { 54 59 get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; } 55 }56 public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter {57 get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; }58 }59 public IValueLookupParameter<IItem> TargetParameter {60 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }61 60 } 62 61 #endregion … … 80 79 set { ReferenceSetSizeParameter.ActualValue = value; } 81 80 } 82 private SimilarityCalculator SimilarityCalculator {83 get { return SimilarityCalculatorParameter.ActualValue; }84 }85 private IItem Target {86 get { return TargetParameter.ActualValue; }87 }88 81 #endregion 89 82 … … 99 92 private void Initialize() { 100 93 #region Create parameters 101 Parameters.Add(new ScopeParameter("CurrentScope")); 102 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization")); 103 Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions")); 104 Parameters.Add(new ValueLookupParameter<IItem>("Quality")); 105 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 106 Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator")); 107 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 94 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that is the reference set.")); 95 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 96 Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions", "True if new solutions have been found, otherwise false.")); 97 Parameters.Add(new ValueLookupParameter<IItem>("Quality", "This parameter is used for name translation only.")); 98 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set.")); 108 99 #endregion 109 100 } 110 101 111 102 public override IOperation Apply() { 112 IScope parentsScope = new Scope("Parents");113 IScope offspringScope = new Scope("Offspring");103 ScopeList parents = new ScopeList(); 104 ScopeList offspring = new ScopeList(); 114 105 115 106 // split parents and offspring 116 107 foreach (var scope in CurrentScope.SubScopes) { 117 parents Scope.SubScopes.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1));118 offspring Scope.SubScopes.AddRange(scope.SubScopes.Last().SubScopes);108 parents.AddRange(scope.SubScopes.Take(scope.SubScopes.Count - 1)); 109 offspring.AddRange(scope.SubScopes.Last().SubScopes); 119 110 } 120 111 121 112 CurrentScope.SubScopes.Clear(); 122 113 123 var orderedParents = Maximization.Value ? parentsScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : 124 parentsScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value); 125 var orderedOffspring = Maximization.Value ? offspringScope.SubScopes.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : 126 offspringScope.SubScopes.OrderBy(x => x.Variables[QualityParameter.ActualName].Value); 114 // attention: assumes that parents are distinct 115 // distinction might cause a too small reference set (e.g. reference set = {1, 2, 2, 2,..., 2} -> union = {1, 2} 116 117 var orderedParents = Maximization.Value ? parents.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : 118 parents.OrderBy(x => x.Variables[QualityParameter.ActualName].Value); 119 var orderedOffspring = Maximization.Value ? offspring.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : 120 offspring.OrderBy(x => x.Variables[QualityParameter.ActualName].Value); 127 121 128 122 CurrentScope.SubScopes.AddRange(orderedParents); … … 136 130 if (orderedOffspring.Any(hasBetterQuality)) { 137 131 // produce the set union 138 // attention: distinction might cause a too small reference set! (e.g. reference set = {1, 2, 2, 2, ..., 2} -> union = {1, 2}139 132 var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.ExecuteCalculation)); 140 if (union.Count() > orderedParents /*.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()))*/.Count()) {133 if (union.Count() > orderedParents.Count()) { 141 134 var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : 142 135 union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value); 143 CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value) .ToList());136 CurrentScope.SubScopes.Replace(orderedUnion.Take(ReferenceSetSize.Value)); 144 137 NewSolutions.Value = true; 145 138 } … … 149 142 } 150 143 151 p rivateclass SolutionEqualityComparer<T> : EqualityComparer<T> {152 private readonly Func<T, T, double> diversityCalculator;144 public class SolutionEqualityComparer<T> : EqualityComparer<T> { 145 private readonly Func<T, T, double> similarityCalculator; 153 146 154 public SolutionEqualityComparer(Func<T, T, double> diversityCalculator) {155 this. diversityCalculator = diversityCalculator;147 public SolutionEqualityComparer(Func<T, T, double> similarityCalculator) { 148 this.similarityCalculator = similarityCalculator; 156 149 } 157 150 158 151 public override bool Equals(T x, T y) { 159 return diversityCalculator(x, y) == 0.0; 152 if (object.ReferenceEquals(x, y)) return true; 153 if (x == null || y == null) return false; 154 return similarityCalculator(x, y) == 1.0; 160 155 } 161 156 162 157 public override int GetHashCode(T obj) { 163 return obj.GetHashCode();158 return 0; // return the same hash code for each object, otherwise Equals will not be called 164 159 } 165 160 }
Note: See TracChangeset
for help on using the changeset viewer.