Changeset 14427
- Timestamp:
- 11/28/16 17:55:52 (8 years ago)
- Location:
- branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r14312 r14427 317 317 <Compile Include="Tracking\SchemaDiversification\SchemaEvaluator.cs" /> 318 318 <Compile Include="Tracking\SchemaDiversification\SchemaCreator.cs" /> 319 <Compile Include="Tracking\SchemaDiversification\SchemaUtil.cs" /> 319 320 <Compile Include="Tracking\SchemaDiversification\UpdateQualityOperator.cs" /> 320 321 <Compile Include="Tracking\SymbolicDataAnalysisExpressionAfterCrossoverOperator.cs" /> -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/Wildcards/AnyNodeSymbol.cs
r12951 r14427 41 41 } 42 42 43 p rivateAnyNodeSymbol() : base("=", "A wildcard symbol that can match any node of the same type (function or leaf node)") { }43 public AnyNodeSymbol() : base("=", "A wildcard symbol that can match any node of the same type (function or leaf node)") { } 44 44 45 45 public AnyNodeSymbol(int minimumArity, int maximumArity) -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSchemaFrequencyAnalyzer.cs
r13624 r14427 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using System.Threading.Tasks; 27 26 using HeuristicLab.Common; … … 34 33 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 34 36 namespace HeuristicLab.Problems.DataAnalysis.Symbolic .Tracking.Analyzers{35 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 37 36 [Item("SymbolicDataAnalysisSchemaFrequencyAnalyzer", "An analyzer which counts schema frequencies in the population.")] 38 37 [StorableClass] … … 157 156 var generation = Generation.Value; 158 157 159 var population = PopulationGraph. GetByRank(generation).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>().ToList();158 var population = PopulationGraph.Vertices.Where(x => x.InDegree == 2 && x.Rank > generation - 1).ToList(); 160 159 var vertices = population.Where(x => x.InDegree == 2).OrderByDescending(x => x.Quality).ToList(); 161 160 ResultCollection resultCollection; … … 182 181 } 183 182 var schemas = SchemaCreator.GenerateSchemas(vertices, MinimumSchemaLength, StrictSchemaMatching).ToList(); 184 var schemaStrings = schemas.Select(x => SubtreeToString(x.Root.GetSubtree(0).GetSubtree(0),StrictSchemaMatching)).ToList();183 var schemaStrings = schemas.Select(x => x.Root.GetSubtree(0).GetSubtree(0).FormatToString(StrictSchemaMatching)).ToList(); 185 184 int[][] matchingIndices; 186 185 if (ExecuteInParallel) { … … 188 187 Parallel.For(0, schemas.Count, new ParallelOptions { MaxDegreeOfParallelism = MaximumDegreeOfParallelism }, i => { 189 188 var schema = schemas[i]; 190 matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v] .Root, schema.Root)).ToArray();189 matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v], schema)).ToArray(); 191 190 }); 192 191 } else { 193 matchingIndices = schemas.Select(x => Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v] .Root, x.Root)).ToArray()).ToArray();192 matchingIndices = schemas.Select(x => Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v], x)).ToArray()).ToArray(); 194 193 } 195 194 … … 230 229 if (!schemaStatistics.Any()) return base.Apply(); // shouldn't ever happen 231 230 var columnNames = new[] { "Count", "Avg Quality", "Avg Length", "Avg Genotype Similarity", "Avg Phenotype Similarity", "Avg Population Quality" }; 232 var mostFrequent = new DoubleMatrix(schemaStatistics.Count, schemaStatistics[0].Item2.Length); 233 mostFrequent.SortableView = true; 231 var mostFrequent = new DoubleMatrix(schemaStatistics.Count, schemaStatistics[0].Item2.Length) { 232 SortableView = true 233 }; 234 234 schemaStatistics.Sort((a, b) => { if (a.Item2[0].Equals(b.Item2[0])) return b.Item2[1].CompareTo(a.Item2[1]); return b.Item2[0].CompareTo(a.Item2[0]); }); 235 mostFrequentPerGeneration.Add( new Tuple<string, double[]>(schemaStatistics[0].Item1, new[] { (double)generation }.Concat(schemaStatistics[0].Item2).ToArray()));235 mostFrequentPerGeneration.Add(Tuple.Create(schemaStatistics[0].Item1, new[] { (double)generation }.Concat(schemaStatistics[0].Item2).ToArray())); 236 236 mostFrequent.RowNames = schemaStatistics.Select(x => x.Item1); 237 237 mostFrequent.ColumnNames = columnNames; … … 301 301 for (int j = i + 1; j < indices.Length; ++j) { 302 302 var b = indices[j]; 303 if (double.IsNaN(similarityMatrix[a, b])) similarityMatrix[a, b] = similarityFunction(trees[a], trees[b]); 303 if (double.IsNaN(similarityMatrix[a, b])) 304 similarityMatrix[a, b] = similarityFunction(trees[a], trees[b]); 304 305 agg += similarityMatrix[a, b]; 305 306 } … … 307 308 return agg / count; 308 309 } 309 310 private static string SubtreeToString(ISymbolicExpressionTreeNode node, bool strict = false) {311 StringBuilder strBuilder = new StringBuilder();312 // internal nodes or leaf nodes?313 if (node is AnySubtree)314 return "# ";315 316 if (node.SubtreeCount > 0) {317 strBuilder.Append("(");318 // symbol on same line as '('319 string label = string.Empty;320 if (node is AnyNode)321 label = "=";322 else {323 var name = node.Symbol.Name;324 label = ShortNames.ContainsKey(name) ? ShortNames[name] : name;325 }326 strBuilder.Append(label + " ");327 // each subtree expression on a new line328 // and closing ')' also on new line329 foreach (var subtree in node.Subtrees) {330 strBuilder.Append(SubtreeToString(subtree, strict));331 }332 strBuilder.Append(") ");333 } else {334 // symbol in the same line with as '(' and ')'335 var v = node as VariableTreeNode;336 var c = node as ConstantTreeNode;337 var w = node as AnyNode; // wildcard338 string label = string.Empty;339 if (w != null)340 label = "=";341 else if (v != null)342 label = strict ? string.Format("{0:0.00}_{1}", v.Weight, v.VariableName) : string.Format("{0}", v.VariableName);343 else if (c != null)344 label = strict ? string.Format("{0:0.00}", c.Value) : "C";345 strBuilder.Append(label);346 if (node.Parent != null && node != node.Parent.Subtrees.Last())347 strBuilder.Append(" ");348 }349 return strBuilder.ToString();350 }351 310 } 352 311 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaCreator.cs
r13876 r14427 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Text;26 25 using HeuristicLab.Common; 27 26 using HeuristicLab.Core; … … 202 201 select v; 203 202 204 var schemas = new List<ISymbolicExpressionTree>(GenerateSchemas(vertices, MinimumSchemaLength, StrictSchemaMatching));203 var schemas = GenerateSchemas(vertices, MinimumSchemaLength, StrictSchemaMatching).ToList(); 205 204 206 205 #region create schemas and add subscopes representing the individuals … … 226 225 public static IEnumerable<ISymbolicExpressionTree> GenerateSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, bool strict = true) { 227 226 var anySubtreeSymbol = new AnySubtreeSymbol(); 228 // var anyNodeSymbol = new AnyNodeSymbol(); 229 var groups = vertices.GroupBy(x => x.Parents.First()).OrderByDescending(g => g.Count()).ToList(); 227 var groups = vertices.GroupBy(x => x.Parents.First()).OrderByDescending(g => g.Count()); 230 228 var hash = new HashSet<string>(); 231 // var formatter = new SymbolicExpressionTreeStringFormatter { Indent = false, AppendNewLines = false };232 229 foreach (var g in groups) { 233 230 var parent = g.Key; … … 237 234 var schema = (ISymbolicExpressionTree)parent.Data.Clone(); 238 235 var nodes = schema.IterateNodesPrefix().ToList(); 239 var arcs = g.Select(x => x.InArcs.Last()).Where(x => x.Data != null); 240 var indices = (from arc in arcs 241 let fragment = (IFragment<ISymbolicExpressionTreeNode>)arc.Data 242 select fragment.Index1).Distinct().ToArray(); 243 var levels = indices.Select(x => schema.Root.GetBranchLevel(nodes[x])).ToArray(); 244 Array.Sort(levels, indices); 245 // order nodes by their depth so that cutpoints are replaced with wildcards from the bottom up 246 var nodesToReplace = indices.Select(x => nodes[x]).ToList(); 247 for (int i = nodesToReplace.Count - 1; i >= 0; --i) { 248 var node = nodesToReplace[i]; 249 236 var fragments = g.Select(x => x.InArcs.Last().Data).Where(x => x != null).Cast<IFragment<ISymbolicExpressionTreeNode>>(); 237 var indices = fragments.Select(x => x.Index1).Distinct().OrderByDescending(x => schema.Root.GetBranchLevel(nodes[x])); 238 foreach (var i in indices) { 239 var node = nodes[i]; 250 240 // do not replace the node with a wildcard if it would result in a length < MinimumSchemaLength 251 if (schema.Length - node.GetLength() + 1 < minimumSchemaLength) 252 continue; 253 254 var replacement = anySubtreeSymbol.CreateTreeNode(); 255 ReplaceSubtree(node, replacement, false); 241 // if (schema.Length - node.GetLength() + 1 < minimumSchemaLength) 242 // continue; 243 ISymbolicExpressionTreeNode replacement; 244 if (node.SubtreeCount > 0) { 245 var anyNodeSymbol = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MaximumArity); 246 replacement = anyNodeSymbol.CreateTreeNode(); 247 } else { 248 replacement = anySubtreeSymbol.CreateTreeNode(); 249 } 250 SchemaUtil.ReplaceSubtree(node, replacement, true); 256 251 // var replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MinimumArity).CreateTreeNode(); 257 252 // ReplaceSubtree(node, replacement, true); … … 259 254 } 260 255 if (replaced) { 261 // var str = formatter.Format(schema.Root.GetSubtree(0).GetSubtree(0)); 262 var str = SubtreeToString(schema.Root.GetSubtree(0).GetSubtree(0), strict); 263 if (hash.Contains(str)) continue; 264 yield return schema; 265 hash.Add(str); 256 var str = schema.Root.GetSubtree(0).GetSubtree(0).FormatToString(strict); 257 if (hash.Add(str)) 258 yield return schema; 266 259 } 267 260 } 268 261 } 269 270 private static void ReplaceSubtree(ISymbolicExpressionTreeNode original, ISymbolicExpressionTreeNode replacement, bool preserveChildren = true) {271 var parent = original.Parent;272 if (parent == null)273 throw new ArgumentException("Parent cannot be null for node " + original);274 var index = parent.IndexOfSubtree(original);275 parent.RemoveSubtree(index);276 parent.InsertSubtree(index, replacement);277 278 if (preserveChildren) {279 var subtrees = original.Subtrees.ToList();280 281 for (int i = subtrees.Count - 1; i >= 0; --i)282 original.RemoveSubtree(i);283 284 for (int i = 0; i < subtrees.Count; ++i) {285 replacement.AddSubtree(subtrees[i]);286 }287 }288 }289 290 private static string SubtreeToString(ISymbolicExpressionTreeNode node, bool strict = false) {291 StringBuilder strBuilder = new StringBuilder();292 // internal nodes or leaf nodes?293 if (node is AnySubtree)294 return "# ";295 296 if (node.SubtreeCount > 0) {297 strBuilder.Append("(");298 // symbol on same line as '('299 string label = string.Empty;300 if (node is AnyNode)301 label = "=";302 else {303 label = node.Symbol.Name;304 }305 strBuilder.Append(label + " ");306 // each subtree expression on a new line307 // and closing ')' also on new line308 foreach (var subtree in node.Subtrees) {309 strBuilder.Append(SubtreeToString(subtree, strict));310 }311 strBuilder.Append(") ");312 } else {313 // symbol in the same line with as '(' and ')'314 var v = node as VariableTreeNode;315 var c = node as ConstantTreeNode;316 var w = node as AnyNode; // wildcard317 string label = string.Empty;318 if (w != null)319 label = "=";320 else if (v != null)321 label = strict ? string.Format("{0:0.00}_{1}", v.Weight, v.VariableName) : string.Format("{0}", v.VariableName);322 else if (c != null)323 label = strict ? string.Format("{0:0.00}", c.Value) : "C";324 strBuilder.Append(label);325 if (node.Parent != null && node != node.Parent.Subtrees.Last())326 strBuilder.Append(" ");327 //strBuilder.Append(")");328 }329 return strBuilder.ToString();330 }331 262 } 332 263 } -
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs
r13565 r14427 234 234 var t = trees[i]; 235 235 var tRoot = t.Root.GetSubtree(0).GetSubtree(0); 236 if (t.Length < s.Length || !qm. Comparer.Equals(tRoot, sRoot)) continue;236 if (t.Length < s.Length || !qm.EqualityComparer.Equals(tRoot, sRoot)) continue; 237 237 filtered.Add(i); 238 238 }
Note: See TracChangeset
for help on using the changeset viewer.