Changeset 12988


Ignore:
Timestamp:
10/09/15 00:16:03 (7 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)
Location:
branches/HeuristicLab.EvolutionTracking
Files:
6 edited
1 moved

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.EvolutionTracking/3.4/Analyzers/GenealogyAnalyzer.cs

    r12958 r12988  
    314314      }
    315315      // update qualities for intermediate vertices
    316       EvaluateIntermediateChildren();
     316      if (!TrimOlderGenerations)
     317        EvaluateIntermediateChildren();
    317318
    318319      // remove extra graph nodes (added by the instrumented operators in the case of offspring selection)
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r12966 r12988  
    316316    <Compile Include="Tracking\Analyzers\SymbolicDataAnalysisSubtreeSampleCountAnalyzer.cs" />
    317317    <Compile Include="Tracking\Analyzers\SymbolicDataAnalysisTraceOverlapAnalyzer.cs" />
    318     <Compile Include="Tracking\SchemaDiversification\SchemaCleanupOperator.cs" />
     318    <Compile Include="Tracking\SchemaDiversification\DiversificationStatisticsOperator.cs" />
    319319    <Compile Include="Tracking\SchemaDiversification\SchemaEvaluator.cs" />
    320320    <Compile Include="Tracking\SchemaDiversification\SchemaCreator.cs" />
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/DiversificationStatisticsOperator.cs

    r12966 r12988  
    2020#endregion
    2121
    22 using System.Linq;
     22using HeuristicLab.Analysis;
    2323using HeuristicLab.Common;
    2424using HeuristicLab.Core;
     25using HeuristicLab.Data;
    2526using HeuristicLab.Operators;
     27using HeuristicLab.Optimization;
     28using HeuristicLab.Parameters;
    2629using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2730
     
    2932  [Item("SchemaCleanupOperator", "Operator which removes the schemas from the global scope after they have been evaluated.")]
    3033  [StorableClass]
    31   public class SchemaCleanupOperator : SingleSuccessorOperator {
    32     public SchemaCleanupOperator() { }
     34  public class DiversificationStatisticsOperator : SingleSuccessorOperator {
     35    private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees";
     36    private const string NumberOfSchemasParameterName = "NumberOfSchemas";
     37    private const string AverageSchemaLengthParameterName = "AverageSchemaLength";
     38    private const string ResultCollectionParameterName = "Results";
    3339
    34     protected SchemaCleanupOperator(SchemaCleanupOperator original, Cloner cloner) : base(original, cloner) { }
     40    public ILookupParameter<IntValue> NumberOfChangedTreesParameter {
     41      get { return (ILookupParameter<IntValue>)Parameters[NumberOfChangedTreesParameterName]; }
     42    }
     43    public ILookupParameter<IntValue> NumberOfSchemasParameter {
     44      get { return (ILookupParameter<IntValue>)Parameters[NumberOfSchemasParameterName]; }
     45    }
     46    public ILookupParameter<DoubleValue> AverageSchemaLengthParameter {
     47      get { return (ILookupParameter<DoubleValue>)Parameters[AverageSchemaLengthParameterName]; }
     48    }
     49    public ILookupParameter<ResultCollection> ResultCollectionParameter {
     50      get { return (ILookupParameter<ResultCollection>)Parameters[ResultCollectionParameterName]; }
     51    }
    3552
    36     public override IDeepCloneable Clone(Cloner cloner) { return new SchemaCleanupOperator(this, cloner); }
     53    public DiversificationStatisticsOperator() {
     54      Parameters.Add(new LookupParameter<IntValue>(NumberOfChangedTreesParameterName));
     55      Parameters.Add(new LookupParameter<IntValue>(NumberOfSchemasParameterName));
     56      Parameters.Add(new LookupParameter<DoubleValue>(AverageSchemaLengthParameterName));
     57      Parameters.Add(new LookupParameter<ResultCollection>(ResultCollectionParameterName));
     58    }
     59
     60    protected DiversificationStatisticsOperator(DiversificationStatisticsOperator original, Cloner cloner) : base(original, cloner) { }
     61
     62    public override IDeepCloneable Clone(Cloner cloner) { return new DiversificationStatisticsOperator(this, cloner); }
    3763
    3864    [StorableConstructor]
    39     protected SchemaCleanupOperator(bool deserializing) : base(deserializing) { }
     65    protected DiversificationStatisticsOperator(bool deserializing) : base(deserializing) { }
    4066
    4167    public override IOperation Apply() {
    42       foreach (var scope in ExecutionContext.Scope.SubScopes.Where(x => x.Name.Equals("Schema"))) {
    43         foreach (var subscope in scope.SubScopes) { subscope.Variables.Clear(); }
    44         scope.SubScopes.Clear();
    45         scope.Variables.Clear();
     68
     69      var results = ResultCollectionParameter.ActualValue;
     70      DataTable table;
     71      if (!results.ContainsKey("NumberOfChangedTrees")) {
     72        table = new DataTable();
     73        results.Add(new Result("NumberOfChangedTrees", table));
     74        var row = new DataRow("Changed trees");
     75        table.Rows.Add(row);
    4676      }
    47       ExecutionContext.Scope.SubScopes.RemoveAll(x => x.Name.Equals("Schema"));
     77      if (!results.ContainsKey("AverageSchemaLength")) {
     78        table = new DataTable();
     79        results.Add(new Result("AverageSchemaLength", table));
     80        var row = new DataRow("Average schema length");
     81        table.Rows.Add(row);
     82      }
     83      if (!results.ContainsKey("NumberOfSchemas")) {
     84        table = new DataTable();
     85        results.Add(new Result("NumberOfSchemas", table));
     86        var row = new DataRow("Number of schemas");
     87        table.Rows.Add(row);
     88      }
     89      ((DataTable)results["NumberOfChangedTrees"].Value).Rows["Changed trees"].Values.Add(NumberOfChangedTreesParameter.ActualValue.Value);
     90      ((DataTable)results["AverageSchemaLength"].Value).Rows["Average schema length"].Values.Add(AverageSchemaLengthParameter.ActualValue.Value);
     91      ((DataTable)results["NumberOfSchemas"].Value).Rows["Number of schemas"].Values.Add(NumberOfSchemasParameter.ActualValue.Value);
     92
    4893      return base.Apply();
    4994    }
  • 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
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs

    r12979 r12988  
    5050    private const string MutatorParameterName = "Mutator";
    5151    private const string RandomReplacementParameterName = "RandomReplacement";
    52     private const string ChangedTreesParameterName = "ChangedTrees";
     52    private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees";
    5353    private const string ExecuteInParallelParameterName = "ExecuteInParallel";
    5454    private const string MaxDegreeOfParalellismParameterName = "MaxDegreeOfParallelism";
     55    private const string ExclusiveMatchingParameterName = "ExclusiveMatching";
    5556    #endregion
    5657
    5758    #region parameters
     59    public ILookupParameter<BoolValue> ExclusiveMatchingParameter {
     60      get { return (ILookupParameter<BoolValue>)Parameters[ExclusiveMatchingParameterName]; }
     61    }
    5862    public ILookupParameter<BoolValue> ExecuteInParallelParameter {
    5963      get { return (ILookupParameter<BoolValue>)Parameters[ExecuteInParallelParameterName]; }
     
    101105      get { return (ILookupParameter<PercentValue>)Parameters[MinimumPhenotypicSimilarityParameterName]; }
    102106    }
    103     public LookupParameter<IntValue> ChangedTreesParameter {
    104       get { return (LookupParameter<IntValue>)Parameters[ChangedTreesParameterName]; }
     107    public LookupParameter<IntValue> NumberOfChangedTreesParameter {
     108      get { return (LookupParameter<IntValue>)Parameters[NumberOfChangedTreesParameterName]; }
    105109    }
    106110    #endregion
     
    111115    public PercentValue MinimumPhenotypicSimilarity { get { return MinimumPhenotypicSimilarityParameter.ActualValue; } }
    112116    public BoolValue RandomReplacement { get { return RandomReplacementParameter.ActualValue; } }
     117    public IntValue NumberOfChangedTrees { get { return NumberOfChangedTreesParameter.ActualValue; } }
    113118    #endregion
    114119
     
    122127    };
    123128
     129    public ISymbolicExpressionTree Schema { get; set; }
     130
    124131    [Storable]
    125132    private readonly UpdateEstimatedValuesOperator updateEstimatedValuesOperator;
     
    128135      qm = new QueryMatch(comp) { MatchParents = true };
    129136      this.updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
    130 
     137      #region add parameters
    131138      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SchemaParameterName, "The current schema to be evaluated"));
    132139      Parameters.Add(new LookupParameter<PercentValue>(MinimumSchemaFrequencyParameterName));
     
    142149      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName));
    143150      Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName));
    144       Parameters.Add(new LookupParameter<IntValue>(ChangedTreesParameterName));
     151      Parameters.Add(new LookupParameter<IntValue>(NumberOfChangedTreesParameterName));
    145152      Parameters.Add(new LookupParameter<BoolValue>(ExecuteInParallelParameterName));
    146153      Parameters.Add(new LookupParameter<IntValue>(MaxDegreeOfParalellismParameterName));
     154      Parameters.Add(new LookupParameter<BoolValue>(ExclusiveMatchingParameterName));
     155      #endregion
    147156    }
    148157
     
    170179      var mutator = MutatorParameter.ActualValue;
    171180
    172       var s = SchemaParameter.ActualValue;
     181      var s = Schema;
    173182      var sRoot = s.Root.GetSubtree(0).GetSubtree(0);
    174183      int countThreshold = (int)Math.Max(2, Math.Round(MinimumSchemaFrequency.Value * individuals.Count));
    175184
    176185      // first apply the length and root equality checks in order to filter the individuals
    177       var filtered = (from ind in individuals
    178                       let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
    179                       where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot)
    180                       select ind).ToList();
     186      var exclusiveMatching = ExclusiveMatchingParameter.ActualValue.Value;
     187      var filtered = exclusiveMatching ? (from ind in individuals
     188                                          where !ind.Variables.ContainsKey("AlreadyMatched")
     189                                          let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
     190                                          where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot)
     191                                          select ind).ToList()
     192                                       : (from ind in individuals
     193                                          let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
     194                                          where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot)
     195                                          select ind).ToList();
    181196
    182197      // if we don't have enough filtered individuals, then we are done
    183198      if (filtered.Count < countThreshold) {
    184         ChangedTreesParameter.ActualValue = new IntValue(0);
    185199        return base.Apply();
    186200      }
     
    210224      } else {
    211225        for (int i = 0; i < filtered.Count; ++i) {
    212 
    213226          // break early if it becomes impossible to reach the minimum threshold
    214227          if (matchingIndividuals.Count + filtered.Count - i < countThreshold)
     
    223236      }
    224237
     238      // additional condition: the average schema quality should be equal or greater than the population average quality
    225239      if (matchingIndividuals.Count < countThreshold) {
    226         ChangedTreesParameter.ActualValue = new IntValue(0);
    227240        return base.Apply();
    228241      }
     
    230243      var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator, executeInParallel, maxDegreeOfParallelism);
    231244      if (similarity < MinimumPhenotypicSimilarity.Value) {
    232         ChangedTreesParameter.ActualValue = new IntValue(0);
    233245        return base.Apply();
    234246      }
     
    244256        mutationOc.Add(mutatorOp);
    245257        updateEstimatedValues.Add(updateOp);
    246       }
    247       ChangedTreesParameter.ActualValue = new IntValue(individualsToReplace.Count);
     258        if (exclusiveMatching)
     259          ind.Variables.Add(new Core.Variable("AlreadyMatched"));
     260      }
     261
     262      NumberOfChangedTrees.Value += individualsToReplace.Count; // a lock is not necessary here because the SchemaEvaluators cannot be executed in parallel
     263
    248264      return new OperationCollection(mutationOc, updateEstimatedValues, base.Apply());
    249265    }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/UpdateEstimatedValuesOperator.cs

    r12979 r12988  
    3838    private const string EstimationLimitsParameterName = "EstimationLimits";
    3939    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
     40    private const string ScaleEstimatedValuesParameterName = "ScaleEstimatedValues";
    4041
    4142    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
     
    5152      get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
    5253    }
    53 
     54    public ILookupParameter<BoolValue> ScaleEstimatedValuesParameter {
     55      get { return (ILookupParameter<BoolValue>)Parameters[ScaleEstimatedValuesParameterName]; }
     56    }
    5457
    5558    public UpdateEstimatedValuesOperator() {
     
    5861      Parameters.Add(new LookupParameter<DoubleLimit>(EstimationLimitsParameterName));
    5962      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName));
     63      Parameters.Add(new LookupParameter<BoolValue>(ScaleEstimatedValuesParameterName));
    6064    }
    6165
     
    100104      OnlineCalculatorError error;
    101105      var r = OnlinePearsonsRCalculator.Calculate(targetValues, scaled, out error);
    102       if (error != OnlineCalculatorError.None) r = 0;
     106      if (error != OnlineCalculatorError.None) r = double.NaN;
    103107
    104108      var r2 = r * r;
    105       if (r2 > 1.0) r2 = 1.0;
    106109
    107110      var variables = ExecutionContext.Scope.Variables;
    108111      ((DoubleValue)variables["Quality"].Value).Value = r2;
     112
     113      var scaleEstimatedValues = ScaleEstimatedValuesParameter.ActualValue;
     114      if (!scaleEstimatedValues.Value)
     115        scaled = estimatedValues.LimitToRange(estimationLimits.Lower, estimationLimits.Upper).ToArray();
    109116
    110117      if (variables.ContainsKey("EstimatedValues")) {
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/QueryMatch.cs

    r12979 r12988  
    159159
    160160    internal static List<NodeInfo> InitializePostOrder(ISymbolicExpressionTreeNode node) {
    161       var list = node.IterateNodesPostfix().ToList();
    162       var nodes = list.Select((x, i) => new NodeInfo { Node = x, Index = i, Level = node.GetBranchLevel(x) }).ToList();
    163       var map = Enumerable.Range(0, list.Count).ToDictionary(i => list[i], i => nodes[i]);
     161      var nodes = node.IterateNodesPostfix().Select((x, i) => new NodeInfo { Node = x, Index = i, Level = 0 }).ToList();
     162      var map = nodes.ToDictionary(x => x.Node, x => x);
    164163
    165164      var inf = new NodeInfo { Node = null, Index = int.MaxValue, Previous = nodes.Last() };
    166165      var nil = new NodeInfo { Node = null, Index = -1, Next = nodes.First() };
    167166
    168       foreach (var n in nodes) {
     167      for (int i = nodes.Count - 1; i >= 0; --i) {
     168        var n = nodes[i];
    169169        n.Parent = n.Node.Parent != null && map.ContainsKey(n.Node.Parent) ? map[n.Node.Parent] : null;
     170        n.Level = n.Parent?.Level + 1 ?? -1;
    170171        n.Next = n == nodes.Last() ? inf : nodes[n.Index + 1];
    171172        n.Previous = n == nodes.First() ? nil : nodes[n.Index - 1];
Note: See TracChangeset for help on using the changeset viewer.