Ignore:
Timestamp:
04/16/18 08:35:59 (4 years ago)
Author:
bburlacu
Message:

#1772: Refactoring and speed optimization of diversification operators

File:
1 edited

Legend:

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

    r15351 r15906  
    4343    private const string ExecuteInParallelParameterName = "ExecuteInParallel";
    4444    private const string MaximumDegreeOfParallelismParameterName = "MaximumDegreeOfParallelism";
     45    private const string SchemaDefinitionParameterName = "SchemaDefinition";
    4546
    4647    private static readonly Dictionary<string, string> ShortNames = new Dictionary<string, string> {
    47     { "Addition", "+" },
    48     { "Subtraction", "-" },
    49     { "Multiplication", "*" },
    50     { "Division", "/" },
    51     { "Exponential", "exp" },
    52     { "Logarithm", "log" }
    53   };
     48      { "Addition", "+" },
     49      { "Subtraction", "-" },
     50      { "Multiplication", "*" },
     51      { "Division", "/" },
     52      { "Exponential", "exp" },
     53      { "Logarithm", "log" }
     54    };
    5455
    5556    [Storable]
     
    6364    private QueryMatch qm;
    6465
     66
     67    public IConstrainedValueParameter<StringValue> SchemaDefinitionParameter {
     68      get { return (IConstrainedValueParameter<StringValue>)Parameters[SchemaDefinitionParameterName]; }
     69    }
    6570    public IFixedValueParameter<BoolValue> ExecuteInParallelParameter {
    6671      get { return (IFixedValueParameter<BoolValue>)Parameters[ExecuteInParallelParameterName]; }
     
    122127      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName));
    123128      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(InterpreterParameterName));
     129
     130      var schemaDefinitions = new ItemSet<StringValue>(new[] {
     131         new StringValue("="),
     132         new StringValue("#"),
     133         new StringValue("=,#")
     134      });
     135      var schemaDefinitionParameter = new ConstrainedValueParameter<StringValue>(SchemaDefinitionParameterName, schemaDefinitions);
     136      schemaDefinitionParameter.Value = schemaDefinitions.First();
     137      Parameters.Add(schemaDefinitionParameter);
    124138    }
    125139
     
    185199      }
    186200
    187       var schemas = SchemaCreator.GenerateCombinedSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     201      List<ISymbolicExpressionTree> schemas;
     202      var def = SchemaDefinitionParameter.Value.Value;
     203      switch (def) {
     204        case "=":
     205          schemas = SchemaCreator.GenerateAnyNodeSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     206          break;
     207        case "#":
     208          schemas = SchemaCreator.GenerateAnySubtreeSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     209          break;
     210        case "=,#":
     211          schemas = SchemaCreator.GenerateCombinedSchemas(vertices, MinimumSchemaLength, 0, StrictSchemaMatching).ToList();
     212          break;
     213        default:
     214          return base.Apply();
     215      }
    188216      var schemaStrings = schemas.Select(x => x.Root.GetSubtree(0).GetSubtree(0).FormatToString(StrictSchemaMatching)).ToList();
    189       int[][] matchingIndices;
     217      int[][] matchingIndices = new int[schemas.Count][];
     218
     219      var tNodes = trees.Select(x => QueryMatch.InitializePostOrder(x.Root.GetSubtree(0).GetSubtree(0))).ToList();
    190220      if (ExecuteInParallel) {
    191         matchingIndices = new int[schemas.Count][];
    192221        Parallel.For(0, schemas.Count, new ParallelOptions { MaxDegreeOfParallelism = MaximumDegreeOfParallelism }, i => {
    193222          var schema = schemas[i];
    194           matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v], schema)).ToArray();
     223          var sNodes = QueryMatch.InitializePostOrder(schema.Root.GetSubtree(0).GetSubtree(0));
     224          matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(idx => qm.Match(tNodes[idx], sNodes)).ToArray();
    195225        });
    196226      } else {
    197         matchingIndices = schemas.Select(x => Enumerable.Range(0, trees.Count).Where(v => qm.Match(trees[v], x)).ToArray()).ToArray();
     227        for (int i = 0; i < schemas.Count; ++i) {
     228          var schema = schemas[i];
     229          var sNodes = QueryMatch.InitializePostOrder(schema.Root.GetSubtree(0).GetSubtree(0));
     230          matchingIndices[i] = Enumerable.Range(0, trees.Count).Where(idx => qm.Match(tNodes[idx], sNodes)).ToArray();
     231        }
    198232      }
    199233
     
    203237      if (ExecuteInParallel) {
    204238        var locker = new object();
    205         Parallel.For(0, schemas.Count, new ParallelOptions { MaxDegreeOfParallelism = MaximumDegreeOfParallelism }, i => {
     239        Parallel.For(0, schemas.Count, new ParallelOptions {
     240          MaxDegreeOfParallelism = MaximumDegreeOfParallelism
     241        }, i => {
    206242          var indices = matchingIndices[i];
    207243          if (indices.Length > 1) {
Note: See TracChangeset for help on using the changeset viewer.