Changeset 15861


Ignore:
Timestamp:
03/23/18 18:57:36 (3 years ago)
Author:
lkammere
Message:

#2886: Make constant optimization toggleable in algorithm.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/RSquaredEvaluator.cs

    r15860 r15861  
    1919    private readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    2020
     21    public bool OptimizeConstants { get; set; }
     22
    2123    public RSquaredEvaluator() { }
    2224
    23     protected RSquaredEvaluator(RSquaredEvaluator original, Cloner cloner) { }
     25    protected RSquaredEvaluator(RSquaredEvaluator original, Cloner cloner) {
     26      this.OptimizeConstants = original.OptimizeConstants;
     27    }
    2428
    2529    public override IDeepCloneable Clone(Cloner cloner) {
     
    7276      // TODO: Restarts
    7377
    74       double r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter,
    75         tree,
    76         problemData,
    77         problemData.TrainingIndices,
    78         applyLinearScaling: false,
    79         maxIterations: 50,
    80         updateVariableWeights: true,
    81         updateConstantsInTree: true);
    82 
    83       foreach (var symbolicExpressionTreeNode in tree.IterateNodesPostfix()) {
    84         ConstantTreeNode constTreeNode = symbolicExpressionTreeNode as ConstantTreeNode;
    85         if (constTreeNode != null && constTreeNode.Value.IsAlmost(0.0)) {
    86           constTreeNode.Value = 0.0;
    87         }
    88       }
     78      double r2;
    8979
    9080      SymbolicRegressionModel model = new SymbolicRegressionModel(
    91         problemData.TargetVariable,
    92         tree,
    93         expressionTreeLinearInterpreter);
     81          problemData.TargetVariable,
     82          tree,
     83          expressionTreeLinearInterpreter);
    9484
    95       //var target = problemData.TargetVariableTrainingValues;
    96       //var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
    97       //OnlineCalculatorError error;
    98       //double r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
    99       //if (error != OnlineCalculatorError.None) r2 = 0.0;
     85      if (OptimizeConstants) {
     86        r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter,
     87          tree,
     88          problemData,
     89          problemData.TrainingIndices,
     90          applyLinearScaling: false,
     91          maxIterations: 50,
     92          updateVariableWeights: true,
     93          updateConstantsInTree: true);
     94
     95        foreach (var symbolicExpressionTreeNode in tree.IterateNodesPostfix()) {
     96          ConstantTreeNode constTreeNode = symbolicExpressionTreeNode as ConstantTreeNode;
     97          if (constTreeNode != null && constTreeNode.Value.IsAlmost(0.0)) {
     98            constTreeNode.Value = 0.0;
     99          }
     100        }
     101
     102
     103      } else {
     104        var target = problemData.TargetVariableTrainingValues;
     105        var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
     106        OnlineCalculatorError error;
     107        r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
     108        if (error != OnlineCalculatorError.None) r2 = 0.0;
     109      }
    100110
    101111      var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value;
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15851 r15861  
    165165    }
    166166
     167    public int GetComplexity(SymbolString s) {
     168      int c = 0;
     169      int length = s.Count();
     170      for (int i = 0; i < length; i++) {
     171        if (s[i] is NonterminalSymbol || s[i] is VariableTerminalSymbol) c++;
     172      }
     173      return c;
     174    }
     175
    167176    #region Parse to SymbolicExpressionTree
    168177
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15860 r15861  
    22using System.Collections.Generic;
    33using System.Linq;
     4using System.Security.Cryptography;
    45using System.Threading;
    56using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
     
    3031
    3132
     33    private readonly string OptimizeConstantsParameterName = "Optimize Constants";
    3234    private readonly string SearchDataStructureParameterName = "Search Data Structure";
    3335    private readonly string MaxComplexityParameterName = "Max. Complexity";
     
    3537
    3638    public override bool SupportsPause { get { return false; } }
     39
     40    protected IValueParameter<BoolValue> OptimizeConstantsParameter {
     41      get { return (IValueParameter<BoolValue>)Parameters[OptimizeConstantsParameterName]; }
     42    }
     43
     44    public bool OptimizeConstants {
     45      get { return OptimizeConstantsParameter.Value.Value; }
     46      set { OptimizeConstantsParameter.Value.Value = value; }
     47    }
    3748
    3849    protected IValueParameter<IntValue> MaxComplexityParameter {
     
    97108      };
    98109
     110      Parameters.Add(new ValueParameter<BoolValue>(OptimizeConstantsParameterName, "Run constant optimization in sentence evaluation.", new BoolValue(true)));
    99111      Parameters.Add(new ValueParameter<IntValue>(MaxComplexityParameterName, "The maximum number of variable symbols in a sentence.", new IntValue(5)));
    100112      Parameters.Add(new ValueParameter<IntValue>(GuiUpdateIntervalParameterName, "Number of generated sentences, until GUI is refreshed.", new IntValue(100000)));
     
    124136      #region init
    125137      InitResults();
     138
     139      Analyzers.OfType<RSquaredEvaluator>().First().OptimizeConstants = OptimizeConstants;
    126140
    127141      ArchivedPhrases = new HashSet<int>();
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15860 r15861  
    6060    public void NoConstants_Nguyen1() {
    6161      // x³ + x² + x
     62      alg.OptimizeConstants = false;
    6263      alg.MaxComplexity = 6;
    6364      alg.Problem.ProblemData = new NguyenFunctionOne(Seed).GenerateRegressionData();
     
    117118    public void NoConstants_Nguyen6() {
    118119      // sin(x) + sin(x + x²)
     120      alg.OptimizeConstants = false;
    119121      alg.MaxComplexity = 4;
    120122      alg.Problem.ProblemData = new NguyenFunctionSix(Seed).GenerateRegressionData();
     
    147149    [TestProperty("Goal", "structure search")]
    148150    public void NoConstants_Nguyen9() {
    149       // sin(x) + sin(y²)
     151      // sin(x) + sin(y²)   
     152      alg.OptimizeConstants = false;
    150153      alg.MaxComplexity = 3;
    151154      alg.Problem.ProblemData = new NguyenFunctionNine(Seed).GenerateRegressionData();
     
    190193    [TestProperty("Goal", "structure search")]
    191194    public void NoConstants_Inverse() {
    192       // x / (log(x)*x + x)
     195      // x / (log(x)*x + x)
     196      alg.OptimizeConstants = false;
    193197      alg.MaxComplexity = 4;
    194198
Note: See TracChangeset for help on using the changeset viewer.