Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/09/15 00:16:03 (9 years ago)
Author:
bburlacu
Message:

#1772: Performance improvement changes

  • QueryMatch.cs: eliminate unnecessary ToList() call and expensive GetBranchLevel calls
  • Diversification: eliminated creation of shallow copies of the individual subscopes as it was either too slow (due to events being registered/deregistered when variables are added to the scope) or too leaky (if attempting to clear the scopes without clearing the variables then the code is leaking EventHandlers)
  • Aggregated diversification statistics separately with the help of some parameters set up in the SchemaCreator and SchemaEvaluator
  • Made code in the UpdateEstimatedValuesOperator perform exactly as in the evaluator (updating quality and estimated values)
  • Removed no longer needed SchemaCleanupOperator
  • Do not evaluate intermediate vertices in the genealogy analyzer if the TrimOlderGenerations flag is activated

New functionality:

  • parameter to control the fraction of the population to be considered by the diversification strategy
  • parameter to control whether individuals may be matched by any schema and mutated only once (exclusive matching)
  • parameter to control whether linear scaling should be applied to the estimated values used for the calculation of phenotypic similarity (default: yes)
File:
1 edited

Legend:

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

    r12979 r12988  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    24 using HeuristicLab.Analysis;
    2525using HeuristicLab.Common;
    2626using HeuristicLab.Core;
     
    2828using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2929using HeuristicLab.EvolutionTracking;
    30 using HeuristicLab.Operators;
    31 using HeuristicLab.Optimization.Operators;
    3230using HeuristicLab.Parameters;
    3331using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4442    private const string ExecuteInParallelParameterName = "ExecuteInParallel";
    4543    private const string MaxDegreeOfParalellismParameterName = "MaxDegreeOfParallelism";
     44    private const string PercentageOfPopulationParameterName = "PercentageOfPopulationToDiversify";
     45    private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues";
     46    private const string ExclusiveMatchingParameterName = "ExclusiveMatching";
     47    private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees";
     48    private const string NumberOfSchemasParameterName = "NumberOfSchemas";
     49    private const string AverageSchemaLengthParameterName = "AverageSchemaLength";
    4650
    4751    #region parameters
     52    public IFixedValueParameter<BoolValue> ExclusiveMatchingParameter {
     53      get { return (IFixedValueParameter<BoolValue>)Parameters[ExclusiveMatchingParameterName]; }
     54    }
     55    public IFixedValueParameter<BoolValue> ScaleEstimatedValuesParameter {
     56      get { return (IFixedValueParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; }
     57    }
     58    public IFixedValueParameter<PercentValue> PercentageOfPopulationParameter {
     59      get { return (IFixedValueParameter<PercentValue>)Parameters[PercentageOfPopulationParameterName]; }
     60    }
    4861    public IFixedValueParameter<IntValue> MinimumSchemaLengthParameter {
    4962      get { return (IFixedValueParameter<IntValue>)Parameters[MinimumSchemaLengthParameterName]; }
    5063    }
    51 
    5264    public IFixedValueParameter<BoolValue> ExecuteInParallelParameter {
    5365      get { return (IFixedValueParameter<BoolValue>)Parameters[ExecuteInParallelParameterName]; }
    5466    }
    55 
    5667    public IFixedValueParameter<IntValue> MaxDegreeOfParallelismParameter {
    5768      get { return (IFixedValueParameter<IntValue>)Parameters[MaxDegreeOfParalellismParameterName]; }
    5869    }
    59 
    6070    public IFixedValueParameter<PercentValue> MinimumSchemaFrequencyParameter {
    6171      get { return (IFixedValueParameter<PercentValue>)Parameters[MinimumSchemaFrequencyParameterName]; }
    6272    }
    63 
    6473    public IFixedValueParameter<PercentValue> MinimumPhenotypicSimilarityParameter {
    6574      get { return (IFixedValueParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; }
    6675    }
    67 
    6876    public IFixedValueParameter<PercentValue> ReplacementRatioParameter {
    6977      get { return (IFixedValueParameter<PercentValue>)Parameters[ReplacementRatioParameterName]; }
     78    }
     79    public IValueParameter<IntValue> NumberOfSchemasParameter {
     80      get { return (IValueParameter<IntValue>)Parameters[NumberOfSchemasParameterName]; }
     81    }
     82    public IValueParameter<DoubleValue> AverageSchemaLengthParameter {
     83      get { return (IValueParameter<DoubleValue>)Parameters[AverageSchemaLengthParameterName]; }
     84    }
     85    public IValueParameter<IntValue> NumberOfChangedTreesParameter {
     86      get { return (IValueParameter<IntValue>)Parameters[NumberOfChangedTreesParameterName]; }
    7087    }
    7188    #endregion
     
    7592    public int MaxDegreeOfParallelism { get { return MaxDegreeOfParallelismParameter.Value.Value; } }
    7693    public bool ExecuteInParallel { get { return ExecuteInParallelParameter.Value.Value; } }
     94    public double PercentageOfPopulation { get { return PercentageOfPopulationParameter.Value.Value; } }
    7795    #endregion
    7896
    79     private SchemaEvaluator schemaEvaluator;
    80     private SchemaCleanupOperator schemaCleanupOperator;
    81     private DataReducer changedTreesReducer;
    82     private DataTableValuesCollector valuesCollector;
    83     private ResultsCollector resultsCollector;
    8497    private UpdateEstimatedValuesOperator updateEstimatedValuesOperator;
    85 
    86     private void ParameterizeOperators() {
    87       schemaEvaluator = new SchemaEvaluator();
    88       schemaCleanupOperator = new SchemaCleanupOperator();
    89 
    90       changedTreesReducer = new DataReducer();
    91       changedTreesReducer.ParameterToReduce.ActualName = schemaEvaluator.ChangedTreesParameter.ActualName;
    92       changedTreesReducer.ReductionOperation.Value = new ReductionOperation(ReductionOperations.Sum);
    93       changedTreesReducer.TargetOperation.Value = new ReductionOperation(ReductionOperations.Assign); // asign the sum to the target parameter
    94       changedTreesReducer.TargetParameter.ActualName = "NumberOfChangedTrees";
    95 
    96       valuesCollector = new DataTableValuesCollector();
    97       valuesCollector.CollectedValues.Add(new LookupParameter<IntValue>("NumberOfChangedTrees"));
    98       valuesCollector.DataTableParameter.ActualName = "Diversification";
    99 
    100       resultsCollector = new ResultsCollector();
    101       resultsCollector.CollectedValues.Add(new LookupParameter<DataTable>("Diversification"));
    102 
    103       updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
    104     }
     98    private DiversificationStatisticsOperator diversificationStatisticsOperator;
    10599
    106100    public SchemaCreator() {
     
    109103      Parameters.Add(new FixedValueParameter<PercentValue>(MinimumPhenotypicSimilarityParameterName, new PercentValue(0.9)));
    110104      Parameters.Add(new FixedValueParameter<PercentValue>(ReplacementRatioParameterName, new PercentValue(0.9)));
     105      Parameters.Add(new FixedValueParameter<PercentValue>(PercentageOfPopulationParameterName, new PercentValue(1)));
    111106      Parameters.Add(new FixedValueParameter<BoolValue>(RandomReplacementParameterName, new BoolValue(false)));
    112107      Parameters.Add(new FixedValueParameter<BoolValue>(ExecuteInParallelParameterName, new BoolValue(false)));
    113108      Parameters.Add(new FixedValueParameter<IntValue>(MaxDegreeOfParalellismParameterName, new IntValue(-1)));
     109      Parameters.Add(new FixedValueParameter<BoolValue>(ScaleEstimatedValuesParameterName, new BoolValue(true)));
     110      Parameters.Add(new FixedValueParameter<BoolValue>(ExclusiveMatchingParameterName, new BoolValue(false)));
     111      Parameters.Add(new ValueParameter<IntValue>(NumberOfChangedTreesParameterName, new IntValue(0)));
     112      Parameters.Add(new ValueParameter<IntValue>(NumberOfSchemasParameterName, new IntValue(0)));
     113      Parameters.Add(new ValueParameter<DoubleValue>(AverageSchemaLengthParameterName, new DoubleValue(0)));
     114
     115      NumberOfChangedTreesParameter.Hidden = true;
     116      NumberOfSchemasParameter.Hidden = true;
     117      AverageSchemaLengthParameter.Hidden = true;
    114118
    115119      ExecuteInParallelParameter.Hidden = true;
    116120      MaxDegreeOfParallelismParameter.Hidden = true;
    117 
    118       ParameterizeOperators();
    119121    }
    120122
     
    133135      if (gen < 1 || GenealogyGraph == null)
    134136        return base.Apply();
     137
     138      var n = (int)Math.Round(ExecutionContext.Scope.SubScopes.Count * PercentageOfPopulation);
     139      var scopes = new ScopeList(ExecutionContext.Scope.SubScopes.Take(n));
     140
    135141      // for now, only consider crossover offspring
    136 
    137       var scopes = new ScopeList(this.ExecutionContext.Scope.SubScopes);
    138       var vertices = GenealogyGraph.GetByRank(gen).Where(x => x.InDegree == 2).Cast<IGenealogyGraphNode<ISymbolicExpressionTree>>();
     142      var vertices = from s in scopes
     143                     let t = (ISymbolicExpressionTree)s.Variables["SymbolicExpressionTree"].Value
     144                     let v = GenealogyGraph.GetByContent(t)
     145                     where v.InDegree == 2
     146                     select v;
     147
    139148      var groups = vertices.GroupBy(x => x.Parents.First()).OrderByDescending(g => g.Count()).ToList();
    140149      var anySubtreeSymbol = new AnySubtreeSymbol();
    141150
    142       if (schemaEvaluator == null || schemaCleanupOperator == null || changedTreesReducer == null || valuesCollector == null || resultsCollector == null)
    143         ParameterizeOperators();
    144 
    145       var reduceChangedTrees = ExecutionContext.CreateChildOperation(changedTreesReducer);
    146       var collectValues = ExecutionContext.CreateChildOperation(valuesCollector);
    147       var collectResults = ExecutionContext.CreateChildOperation(resultsCollector);
    148 
    149151      var updateEstimatedValues = new OperationCollection { Parallel = true };
     152      if (updateEstimatedValuesOperator == null)
     153        updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
     154
    150155      foreach (var s in scopes) {
    151156        if (!s.Variables.ContainsKey("EstimatedValues"))
     
    153158        updateEstimatedValues.Add(ExecutionContext.CreateChildOperation(updateEstimatedValuesOperator, s));
    154159      }
    155       var oc = new OperationCollection { updateEstimatedValues };
    156 
     160
     161      var evaluateSchemas = new OperationCollection();
     162      var schemas = new List<ISymbolicExpressionTree>();
     163      var wildCardCounts = new List<double>();
    157164      #region create schemas and add subscopes representing the individuals
    158165      foreach (var g in groups) {
     
    167174        Array.Sort(indices);
    168175        var nodesToReplace = indices.Select(x => nodes[x]).ToList();
     176        int w = 0;
    169177        for (int i = nodesToReplace.Count - 1; i >= 0; --i) {
    170178          var node = nodesToReplace[i];
     
    177185          ReplaceSubtree(node, replacement, false);
    178186          replaced = true;
     187          ++w;
    179188        }
    180189        if (replaced) {
    181           var scope = new Scope("Schema");
    182           scope.Variables.Add(new Core.Variable("Schema", schema));
    183           scope.SubScopes.AddRange(ShallowCopy(scopes));
    184 
    185           oc.Add(ExecutionContext.CreateChildOperation(schemaEvaluator, scope));
    186           ExecutionContext.Scope.SubScopes.Add(scope);
     190          // store the schemas and the number of wildcards they contain in two lists
     191          schemas.Add(schema);
     192          wildCardCounts.Add(w);
    187193        }
    188194      }
     195
     196      for (int i = 0; i < schemas.Count; ++i) {
     197        var schema = schemas[i];
     198        evaluateSchemas.Add(ExecutionContext.CreateChildOperation(new SchemaEvaluator { Schema = schema }, ExecutionContext.Scope));
     199      }
    189200      #endregion
    190201
    191       var cleanup = ExecutionContext.CreateChildOperation(schemaCleanupOperator);
     202      if (diversificationStatisticsOperator == null)
     203        diversificationStatisticsOperator = new DiversificationStatisticsOperator();
     204
     205      var calculateStatistics = ExecutionContext.CreateChildOperation(diversificationStatisticsOperator);
     206
     207      // set parameters for statistics
     208      AverageSchemaLengthParameter.Value = new DoubleValue(schemas.Average(x => x.Length));
     209      NumberOfSchemasParameter.Value = new IntValue(schemas.Count);
     210      NumberOfChangedTreesParameter.Value = new IntValue(0);
     211
    192212      // return an operation collection containing all the scope operations + base.Apply()
    193       return new OperationCollection { oc, reduceChangedTrees, collectValues, collectResults, cleanup, base.Apply() };
    194     }
    195 
    196     private static ScopeList ShallowCopy(ScopeList scopes) {
    197       var scopeList = new ScopeList();
    198       // shallow-copy means that we create a new scope but reuse the variables
    199       foreach (var scope in scopes) {
    200         var s = new Scope(scope.Name);
    201         foreach (var v in scope.Variables)
    202           s.Variables.Add(v);
    203         scopeList.Add(s);
    204       }
    205       return scopeList;
     213      return new OperationCollection { updateEstimatedValues, evaluateSchemas, calculateStatistics, base.Apply() };
    206214    }
    207215
Note: See TracChangeset for help on using the changeset viewer.