Changeset 16691


Ignore:
Timestamp:
03/16/19 13:13:39 (3 months ago)
Author:
gkronber
Message:

#2974: improved code for benchmarking (previous code failed to account for update of trees after call of Optimize as well as early stopping in Optimize for trees with invalid outputs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2974_Constants_Optimization/Benchmarks/Benchmarks/ConstantsOptimizationPerformance.cs

    r16525 r16691  
    1616    private static readonly int seed = 1234;
    1717    private static readonly int maxTreeSize = 50;
    18     private static readonly int nTrees = 1000;
     18    private static readonly int nTrees = 10;
    1919
    20     private ISymbolicExpressionTree[] trees;
     20    private List<ISymbolicExpressionTree> globalTrees = new List<ISymbolicExpressionTree>();
     21    private List<ISymbolicExpressionTree> iterationTrees = new List<ISymbolicExpressionTree>();
    2122    private IRegressionProblemData problemData;
    2223    private TypeCoherentExpressionGrammar grammar;
     
    3435      grammar.ConfigureAsDefaultRegressionGrammar();
    3536
    36       trees = Util.CreateRandomTrees(twister, problemData.Dataset, grammar, nTrees, 1, maxTreeSize, 0, 0);
    37       foreach (SymbolicExpressionTree tree in trees) {
    38         Util.InitTree(tree, twister, problemData.AllowedInputVariables.ToList());
     37      // generate random trees and only keep trees which are ok
     38      while (globalTrees.Count < nTrees) {
     39        var t = Util.CreateRandomTrees(twister, problemData.Dataset, grammar, 1, 1, maxTreeSize, 0, 0).Single();
     40        Util.InitTree(t, twister, problemData.AllowedInputVariables.ToList());
     41        var q = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, t, double.MinValue, double.MaxValue, problemData, rows, applyLinearScaling: true);
     42        if (q > 0) globalTrees.Add(t);
     43      }
     44    }
     45
     46    [IterationSetup]
     47    public void IterationSetup() {
     48      iterationTrees.Clear();
     49      for (int i = 0; i < globalTrees.Count; i++) {
     50        var t = (ISymbolicExpressionTree)globalTrees[i].Clone(); // trees are changed in OptimizeConstants, so we need to clone the original trees for each iteration
     51        iterationTrees.Add(t);
    3952      }
    4053    }
     
    5770
    5871      if (Iterations > 0) {
    59         for (int i = 0; i < trees.Length; i++) {
    60           avgQuality += LMConstantsOptimizer.OptimizeConstants(trees[i], problemData.Dataset, problemData.TargetVariable, rows, true, Iterations);
     72        for (int i = 0; i < iterationTrees.Count; i++) {
     73          avgQuality += LMConstantsOptimizer.OptimizeConstants(iterationTrees[i], problemData.Dataset, problemData.TargetVariable, rows, applyLinearScaling: true, maxIterations: Iterations);
    6174        }
    6275      } else {
    63         for (int i = 0; i < trees.Length; i++) {
    64           avgQuality += SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, trees[i], double.MinValue, double.MaxValue, problemData, rows, true);
     76        for (int i = 0; i < iterationTrees.Count; i++) {
     77          avgQuality += SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, iterationTrees[i], double.MinValue, double.MaxValue, problemData, rows, applyLinearScaling: true);
    6578        }
    6679      }
    67       return avgQuality / trees.Length;
     80      return avgQuality / iterationTrees.Count;
    6881    }
    6982  }
Note: See TracChangeset for help on using the changeset viewer.