Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/18/16 19:36:09 (9 years ago)
Author:
bburlacu
Message:

#1772: Small refactor of the SchemaCreator to avoid generating duplicate schemas when non-strict matching is used.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaCreator.cs

    r13565 r13625  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using System.Text;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    201202                     select v;
    202203
    203       var schemas = new List<ISymbolicExpressionTree>(GenerateSchemas(vertices, MinimumSchemaLength));
     204      var schemas = new List<ISymbolicExpressionTree>(GenerateSchemas(vertices, MinimumSchemaLength, StrictSchemaMatching));
    204205
    205206      #region create schemas and add subscopes representing the individuals
     
    223224    }
    224225
    225     public static IEnumerable<ISymbolicExpressionTree> GenerateSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength) {
     226    public static IEnumerable<ISymbolicExpressionTree> GenerateSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, bool strict = true) {
    226227      var anySubtreeSymbol = new AnySubtreeSymbol();
    227228      //            var anyNodeSymbol = new AnyNodeSymbol();
    228229      var groups = vertices.GroupBy(x => x.Parents.First()).OrderByDescending(g => g.Count()).ToList();
    229230      var hash = new HashSet<string>();
    230       var formatter = new SymbolicExpressionTreeStringFormatter { Indent = false, AppendNewLines = false };
     231      //      var formatter = new SymbolicExpressionTreeStringFormatter { Indent = false, AppendNewLines = false };
    231232      foreach (var g in groups) {
    232233        var parent = g.Key;
     
    256257        }
    257258        if (replaced) {
    258           var str = formatter.Format(schema.Root.GetSubtree(0).GetSubtree(0));
     259          //          var str = formatter.Format(schema.Root.GetSubtree(0).GetSubtree(0));
     260          var str = SubtreeToString(schema.Root.GetSubtree(0).GetSubtree(0), strict);
    259261          if (hash.Contains(str)) continue;
    260262          yield return schema;
     
    283285      }
    284286    }
     287
     288    private static string SubtreeToString(ISymbolicExpressionTreeNode node, bool strict = false) {
     289      StringBuilder strBuilder = new StringBuilder();
     290      // internal nodes or leaf nodes?
     291      if (node is AnySubtree)
     292        return "# ";
     293
     294      if (node.SubtreeCount > 0) {
     295        strBuilder.Append("(");
     296        // symbol on same line as '('
     297        string label = string.Empty;
     298        if (node is AnyNode)
     299          label = "=";
     300        else {
     301          label = node.Symbol.Name;
     302        }
     303        strBuilder.Append(label + " ");
     304        // each subtree expression on a new line
     305        // and closing ')' also on new line
     306        foreach (var subtree in node.Subtrees) {
     307          strBuilder.Append(SubtreeToString(subtree, strict));
     308        }
     309        strBuilder.Append(") ");
     310      } else {
     311        // symbol in the same line with as '(' and ')'
     312        var v = node as VariableTreeNode;
     313        var c = node as ConstantTreeNode;
     314        var w = node as AnyNode; // wildcard
     315        string label = string.Empty;
     316        if (w != null)
     317          label = "=";
     318        else if (v != null)
     319          label = strict ? string.Format("{0:0.00}_{1}", v.Weight, v.VariableName) : string.Format("{0}", v.VariableName);
     320        else if (c != null)
     321          label = strict ? string.Format("{0:0.00}", c.Value) : "C";
     322        strBuilder.Append(label);
     323        if (node.Parent != null && node != node.Parent.Subtrees.Last())
     324          strBuilder.Append(" ");
     325        //strBuilder.Append(")");
     326      }
     327      return strBuilder.ToString();
     328    }
    285329  }
    286330}
Note: See TracChangeset for help on using the changeset viewer.