Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/06/17 20:51:26 (7 years ago)
Author:
bburlacu
Message:

#1772: Merge trunk changes, add a couple of convenience parameters to the SchemaCreator.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/Analyzers/SymbolicDataAnalysisSchemaFrequencyAnalyzer.cs

    r14626 r15351  
    185185      }
    186186
    187       var schemas = SchemaCreator.GenerateSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     187      var schemas = SchemaCreator.GenerateCombinedSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
    188188      var schemaStrings = schemas.Select(x => x.Root.GetSubtree(0).GetSubtree(0).FormatToString(StrictSchemaMatching)).ToList();
    189189      int[][] matchingIndices;
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaCreator.cs

    r14627 r15351  
    5252    private const string ReplacementRatioUpdateRuleParameterName = "ReplacementRatioUpdateRule";
    5353    private const string StrictSchemaMatchingParameterName = "StrictSchemaMatching";
     54    private const string SchemaDefinitionParameterName = "SchemaDefinition";
    5455    #endregion
    5556
    5657    #region parameters
    5758
     59    public IConstrainedValueParameter<StringValue> SchemaDefinitionParameter {
     60      get { return (IConstrainedValueParameter<StringValue>)Parameters[SchemaDefinitionParameterName]; }
     61    }
    5862    public IConstrainedValueParameter<StringValue> ReplacementRatioUpdateRuleParameter {
    5963      get { return (IConstrainedValueParameter<StringValue>)Parameters[ReplacementRatioUpdateRuleParameterName]; }
     
    147151        new StringValue("f(x) = 1-sqrt(1-x)")
    148152      });
    149       Parameters.Add(new ConstrainedValueParameter<StringValue>(ReplacementRatioUpdateRuleParameterName, replacementRatioUpdateRules));
     153      var replacementRatioUpdateRuleParameter = new ConstrainedValueParameter<StringValue>(ReplacementRatioUpdateRuleParameterName, replacementRatioUpdateRules);
     154      replacementRatioUpdateRuleParameter.Value = replacementRatioUpdateRules.First();
     155      Parameters.Add(replacementRatioUpdateRuleParameter);
     156
     157      var schemaDefinitions = new ItemSet<StringValue>(new[] {
     158         new StringValue("="),
     159         new StringValue("#"),
     160         new StringValue("=,#")
     161      });
     162      var schemaDefinitionParameter = new ConstrainedValueParameter<StringValue>(SchemaDefinitionParameterName, schemaDefinitions);
     163      schemaDefinitionParameter.Value = schemaDefinitions.First();
     164      Parameters.Add(schemaDefinitionParameter);
    150165      #endregion
     166
    151167      NumberOfChangedTreesParameter.Hidden = true;
    152168      NumberOfSchemasParameter.Hidden = true;
     
    201217                     select v;
    202218
    203       var schemas = GenerateSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     219      List<ISymbolicExpressionTree> schemas;
     220      var def = SchemaDefinitionParameter.Value.Value;
     221      switch (def) {
     222        case "=":
     223          schemas = GenerateAnyNodeSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     224          break;
     225        case "#":
     226          schemas = GenerateAnySubtreeSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     227          break;
     228        case "=,#":
     229          schemas = GenerateCombinedSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     230          break;
     231        default:
     232          return base.Apply();
     233      }
     234
     235      if (!schemas.Any())
     236        return base.Apply();
    204237
    205238      #region create schemas and add subscopes representing the individuals
     
    223256    }
    224257
    225     public static IEnumerable<ISymbolicExpressionTree> GenerateSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
     258    #region schema generation
     259    public static IEnumerable<ISymbolicExpressionTree> GenerateAnyNodeSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
     260      var anySubtreeSymbol = new AnySubtreeSymbol();
     261      var groups = vertices.GroupBy(x => x.Parents.First()).Where(g => g.Count() >= minOffspringCount).OrderByDescending(g => g.Count());
     262      var hash = new HashSet<string>();
     263      foreach (var g in groups) {
     264        var parent = g.Key;
     265        if (parent.Data.Length < minimumSchemaLength)
     266          continue;
     267        bool replaced = false;
     268        var schema = (ISymbolicExpressionTree)parent.Data.Clone();
     269        var nodes = schema.IterateNodesPrefix().ToList();
     270        var fragments = g.Select(x => x.InArcs.Last().Data).Where(x => x != null).Cast<IFragment<ISymbolicExpressionTreeNode>>();
     271        var indices = fragments.Select(x => x.Index1).Distinct().OrderByDescending(x => schema.Root.GetBranchLevel(nodes[x]));
     272        foreach (var i in indices) {
     273          var node = nodes[i];
     274          var anyNodeSymbol = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MaximumArity);
     275          var replacement = anyNodeSymbol.CreateTreeNode();
     276          SchemaUtil.ReplaceSubtree(node, replacement, true);
     277          replaced = true;
     278        }
     279        if (replaced) {
     280          var str = schema.Root.GetSubtree(0).GetSubtree(0).FormatToString(strict);
     281          if (hash.Add(str))
     282            yield return schema;
     283        }
     284      }
     285    }
     286
     287    public static IEnumerable<ISymbolicExpressionTree> GenerateAnySubtreeSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
    226288      var anySubtreeSymbol = new AnySubtreeSymbol();
    227289      var groups = vertices.GroupBy(x => x.Parents.First()).Where(g => g.Count() >= minOffspringCount).OrderByDescending(g => g.Count());
     
    239301          var node = nodes[i];
    240302          // do not replace the node with a wildcard if it would result in a length < MinimumSchemaLength
    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);
    251           //          var replacement = anySubtreeSymbol.CreateTreeNode();
    252           //          var replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MinimumArity).CreateTreeNode();
    253           //          ReplaceSubtree(node, replacement, true);
    254           //          SchemaUtil.ReplaceSubtree(node, replacement, false);
     303          if (schema.Length - node.GetLength() + 1 < minimumSchemaLength)
     304            continue;
     305          var replacement = anySubtreeSymbol.CreateTreeNode();
     306          SchemaUtil.ReplaceSubtree(node, replacement, false);
    255307          replaced = true;
    256308        }
     
    262314      }
    263315    }
     316
     317    public static IEnumerable<ISymbolicExpressionTree> GenerateCombinedSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength, int minOffspringCount = 1, bool strict = true) {
     318      var anySubtreeSymbol = new AnySubtreeSymbol();
     319      var groups = vertices.GroupBy(x => x.Parents.First()).Where(g => g.Count() >= minOffspringCount).OrderByDescending(g => g.Count());
     320      var hash = new HashSet<string>();
     321      foreach (var g in groups) {
     322        var parent = g.Key;
     323        if (parent.Data.Length < minimumSchemaLength)
     324          continue;
     325        bool replaced = false;
     326        var schema = (ISymbolicExpressionTree)parent.Data.Clone();
     327        var nodes = schema.IterateNodesPrefix().ToList();
     328        var fragments = g.Select(x => x.InArcs.Last().Data).Where(x => x != null).Cast<IFragment<ISymbolicExpressionTreeNode>>();
     329        var indices = fragments.Select(x => x.Index1).Distinct().OrderByDescending(x => schema.Root.GetBranchLevel(nodes[x]));
     330        foreach (var i in indices) {
     331          var node = nodes[i];
     332          ISymbolicExpressionTreeNode replacement;
     333          if (node.SubtreeCount > 0) {
     334            replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MaximumArity).CreateTreeNode();
     335            SchemaUtil.ReplaceSubtree(node, replacement, true);
     336          } else {
     337            replacement = anySubtreeSymbol.CreateTreeNode();
     338            SchemaUtil.ReplaceSubtree(node, replacement, false);
     339          }
     340          replaced = true;
     341        }
     342        if (replaced) {
     343          var str = schema.Root.GetSubtree(0).GetSubtree(0).FormatToString(strict);
     344          if (hash.Add(str))
     345            yield return schema;
     346        }
     347      }
     348    }
     349    #endregion
    264350  }
    265351}
Note: See TracChangeset for help on using the changeset viewer.