Changeset 13480 for branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs
- Timestamp:
- 12/17/15 20:13:57 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs
r12988 r13480 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Threading.Tasks; … … 28 29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 29 30 using HeuristicLab.EvolutionTracking; 31 using HeuristicLab.Optimization; 30 32 using HeuristicLab.Parameters; 31 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 49 51 private const string ApplyLinearScalingParameterName = "ApplyLinearScaling"; 50 52 private const string MutatorParameterName = "Mutator"; 53 private const string CrossoverParameterName = "Crossover"; 51 54 private const string RandomReplacementParameterName = "RandomReplacement"; 52 55 private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees"; … … 54 57 private const string MaxDegreeOfParalellismParameterName = "MaxDegreeOfParallelism"; 55 58 private const string ExclusiveMatchingParameterName = "ExclusiveMatching"; 59 private const string UseAdaptiveReplacementRatioParameterName = "UseAdaptiveReplacementRatio"; 56 60 #endregion 57 61 58 62 #region parameters 63 public ILookupParameter<BoolValue> UseAdaptiveReplacementRatioParameter { 64 get { return (ILookupParameter<BoolValue>)Parameters[UseAdaptiveReplacementRatioParameterName]; } 65 } 59 66 public ILookupParameter<BoolValue> ExclusiveMatchingParameter { 60 67 get { return (ILookupParameter<BoolValue>)Parameters[ExclusiveMatchingParameterName]; } … … 86 93 public ILookupParameter<ISymbolicExpressionTreeManipulator> MutatorParameter { 87 94 get { return (ILookupParameter<ISymbolicExpressionTreeManipulator>)Parameters[MutatorParameterName]; } 95 } 96 public ILookupParameter<ISymbolicExpressionTreeCrossover> CrossoverParameter { 97 get { return (ILookupParameter<ISymbolicExpressionTreeCrossover>)Parameters[CrossoverParameterName]; } 88 98 } 89 99 public ILookupParameter<IRandom> RandomParameter { … … 120 130 private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator(); 121 131 private readonly QueryMatch qm; 132 133 public Func<double, double> ReplacementRule { get; set; } 122 134 123 135 private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer { … … 148 160 Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName)); 149 161 Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName)); 162 Parameters.Add(new LookupParameter<ISymbolicExpressionTreeCrossover>(CrossoverParameterName)); 150 163 Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName)); 151 164 Parameters.Add(new LookupParameter<IntValue>(NumberOfChangedTreesParameterName)); … … 153 166 Parameters.Add(new LookupParameter<IntValue>(MaxDegreeOfParalellismParameterName)); 154 167 Parameters.Add(new LookupParameter<BoolValue>(ExclusiveMatchingParameterName)); 168 Parameters.Add(new LookupParameter<BoolValue>(UseAdaptiveReplacementRatioParameterName)); 155 169 #endregion 156 170 } … … 175 189 public override IOperation Apply() { 176 190 var individuals = ExecutionContext.Scope.SubScopes; // the scopes represent the individuals 191 var trees = individuals.Select(x => (ISymbolicExpressionTree)x.Variables["SymbolicExpressionTree"].Value).ToList(); 192 var qualities = individuals.Select(x => (DoubleValue)x.Variables["Quality"].Value).ToList(); 177 193 178 194 var random = RandomParameter.ActualValue; … … 185 201 // first apply the length and root equality checks in order to filter the individuals 186 202 var exclusiveMatching = ExclusiveMatchingParameter.ActualValue.Value; 187 var filtered = exclusiveMatching ? (from ind in individuals 188 where !ind.Variables.ContainsKey("AlreadyMatched") 189 let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value 190 where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot) 191 select ind).ToList() 192 : (from ind in individuals 193 let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value 194 where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot) 195 select ind).ToList(); 203 var filtered = new List<int>(); 204 for (int i = 0; i < individuals.Count; ++i) { 205 if (exclusiveMatching && individuals[i].Variables.ContainsKey("AlreadyMatched")) continue; 206 var t = trees[i]; 207 var tRoot = t.Root.GetSubtree(0).GetSubtree(0); 208 if (t.Length < s.Length || !qm.Comparer.Equals(tRoot, sRoot)) continue; 209 filtered.Add(i); 210 } 196 211 197 212 // if we don't have enough filtered individuals, then we are done 213 // if the schema exceeds the minimum frequency, it gets processed further 198 214 if (filtered.Count < countThreshold) { 199 215 return base.Apply(); … … 211 227 Parallel.For(0, filtered.Count, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism }, 212 228 i => { 213 var t = (ISymbolicExpressionTree)filtered[i].Variables["SymbolicExpressionTree"].Value; 229 var index = filtered[i]; 230 var t = trees[index]; 214 231 var tNodes = QueryMatch.InitializePostOrder(t.Root.GetSubtree(0).GetSubtree(0)); 215 232 if (qm.Match(tNodes, sNodes)) { … … 218 235 }); 219 236 220 for (int i = 0; i < matching.Length; ++i) { 221 if (matching[i]) 222 matchingIndividuals.Add(filtered[i]); 223 } 237 matchingIndividuals.AddRange(filtered.Where((x, i) => matching[i]).Select(x => individuals[x])); 224 238 } else { 225 239 for (int i = 0; i < filtered.Count; ++i) { … … 228 242 break; 229 243 230 var ind = filtered[i];231 var t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value;232 var tNodes = QueryMatch.InitializePostOrder(t .Root.GetSubtree(0).GetSubtree(0));244 var index = filtered[i]; 245 var tRoot = trees[index].Root.GetSubtree(0).GetSubtree(0); 246 var tNodes = QueryMatch.InitializePostOrder(tRoot); 233 247 if (qm.Match(tNodes, sNodes)) 234 matchingIndividuals.Add(ind );248 matchingIndividuals.Add(individuals[index]); 235 249 } 236 250 } 237 251 238 // additional condition: the average schema quality should be equal or greater than the population average quality239 252 if (matchingIndividuals.Count < countThreshold) { 240 253 return base.Apply(); 241 254 } 242 255 243 var similarity = Calculate PhenotypicSimilarity(matchingIndividuals, calculator, executeInParallel, maxDegreeOfParallelism);256 var similarity = CalculateSimilarity(matchingIndividuals, calculator, executeInParallel, maxDegreeOfParallelism); 244 257 if (similarity < MinimumPhenotypicSimilarity.Value) { 245 258 return base.Apply(); 246 259 } 247 260 248 int n = (int)Math.Floor(matchingIndividuals.Count * ReplacementRatio.Value); 261 double replacementRatio; 262 var adaptiveReplacementRatio = UseAdaptiveReplacementRatioParameter.ActualValue.Value; 263 264 if (adaptiveReplacementRatio) { 265 var r = (double)matchingIndividuals.Count / individuals.Count; 266 replacementRatio = ReplacementRule(r); 267 } else { 268 replacementRatio = ReplacementRatio.Value; 269 } 270 271 int n = (int)Math.Floor(matchingIndividuals.Count * replacementRatio); 249 272 var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n).ToList() 250 273 : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n).ToList(); … … 265 288 } 266 289 267 p rivate static double CalculatePhenotypicSimilarity(ScopeList individuals, SymbolicExpressionTreePhenotypicSimilarityCalculator calculator, bool parallel = false, int nThreads = -1) {290 public static double CalculateSimilarity(ScopeList individuals, ISolutionSimilarityCalculator calculator, bool parallel = false, int nThreads = -1) { 268 291 double similarity = 0; 269 292 int count = individuals.Count; 293 if (count < 2) return double.NaN; 270 294 if (parallel) { 271 295 var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = nThreads };
Note: See TracChangeset
for help on using the changeset viewer.