Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15859 for branches


Ignore:
Timestamp:
03/23/18 15:41:02 (6 years ago)
Author:
lkammere
Message:

#2886: Add simple constant optimization to evaluation of solutions.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
2 edited

Legend:

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

    r15824 r15859  
    11using System;
     2using System.Diagnostics;
    23using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
    34using HeuristicLab.Common;
     
    6364
    6465    private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString) {
     66      var problemData = algorithm.Problem.ProblemData;
     67
    6568      SymbolicExpressionTree tree = algorithm.Grammar.ParseSymbolicExpressionTree(symbolString);
     69      Debug.Assert(SymbolicRegressionConstantOptimizationEvaluator.CanOptimizeConstants(tree));
    6670
    67       var problemData = algorithm.Problem.ProblemData;
     71      // TODO: Initialize constant values randomly
     72      // TODO: Restarts
     73
     74      double r2 = SymbolicRegressionConstantOptimizationEvaluator.OptimizeConstants(expressionTreeLinearInterpreter,
     75        tree,
     76        problemData,
     77        problemData.TrainingIndices,
     78        applyLinearScaling: false,
     79        maxIterations: 200,
     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      }
     89
    6890      SymbolicRegressionModel model = new SymbolicRegressionModel(
    6991        problemData.TargetVariable,
     
    7193        expressionTreeLinearInterpreter);
    7294
    73       var target = problemData.TargetVariableTrainingValues;
    74       var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
    75       OnlineCalculatorError error;
    76       var r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
    77       if (error != OnlineCalculatorError.None) r2 = 0.0;
     95      //var target = problemData.TargetVariableTrainingValues;
     96      //var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices);
     97      //OnlineCalculatorError error;
     98      //r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error);
     99      //if (error != OnlineCalculatorError.None) r2 = 0.0;
    78100
    79101      var bestR2Result = (DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value;
    80       if (r2 > bestR2Result.Value) {
     102      bool better = r2 > bestR2Result.Value;
     103      bool equallyGood = r2.IsAlmost(bestR2Result.Value);
     104      bool shorter = algorithm.BestTrainingSentence != null && symbolString.Count() < algorithm.BestTrainingSentence.Count();
     105      if (better || (equallyGood && shorter)) {
    81106        bestR2Result.Value = r2;
    82107        algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model);
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15849 r15859  
    200200      EvaluateGrammarEnumeration();
    201201    }
     202
     203    [TestMethod]
     204    [TestProperty("Goal", "structure search + const op")]
     205    public void Constants_Nguyen7() {
     206      // log(x+1) + log(x*x + 1)
     207      alg.MaxTreeSize = 22;
     208      alg.Problem.ProblemData = new NguyenFunctionSeven().GenerateRegressionData();
     209
     210      alg.Start();
     211
     212      TerminalSymbol constSymbol = alg.Grammar.Const;
     213      TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
     214      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     215      TerminalSymbol addSymbol = alg.Grammar.Addition;
     216      TerminalSymbol logSymbol = alg.Grammar.Log;
     217
     218      SymbolString targetSolution = new SymbolString(new[] {
     219        varSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol,
     220        varSymbol, varSymbol, mulSymbol, constSymbol, mulSymbol, constSymbol, addSymbol, logSymbol, constSymbol, mulSymbol, addSymbol,
     221        constSymbol, addSymbol
     222      });
     223
     224      int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
     225      int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
     226
     227      Assert.IsTrue(alg.DistinctSentencesLength.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
     228      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     229
     230
     231      // Evaluate
     232      EvaluateGrammarEnumeration();
     233    }
     234
     235    [TestMethod]
     236    [TestProperty("Goal", "structure search + const op")]
     237    public void Constants_Nguyen12() {
     238      // x*x*x*x - x*x*x + y*y/2 -y
     239      alg.MaxTreeSize = 28;
     240      alg.Problem.ProblemData = new NguyenFunctionTwelve().GenerateRegressionData();
     241
     242      alg.Start();
     243
     244      // Evaluate
     245      EvaluateGrammarEnumeration();
     246    }
     247
     248    [TestMethod]
     249    [TestProperty("Goal", "structure search + const op")]
     250    public void Constants_Keijzer3() {
     251      // 0.3*x*sin(2*pi*x)
     252      alg.MaxTreeSize = 20;
     253      alg.Problem.ProblemData = new KeijzerFunctionThree().GenerateRegressionData();
     254
     255      alg.Start();
     256
     257      TerminalSymbol constSymbol = alg.Grammar.Const;
     258      TerminalSymbol varSymbol = alg.Grammar.VarTerminals.First();
     259      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     260      TerminalSymbol addSymbol = alg.Grammar.Addition;
     261
     262      SymbolString targetSolution = new SymbolString(new[] {
     263        constSymbol, varSymbol, mulSymbol, constSymbol, addSymbol, alg.Grammar.Sin,
     264        varSymbol, mulSymbol,
     265        constSymbol, mulSymbol,
     266        constSymbol, addSymbol
     267      });
     268
     269      int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
     270      int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
     271
     272      Assert.IsTrue(alg.DistinctSentencesLength.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
     273
     274      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     275
     276      // Evaluate
     277      EvaluateGrammarEnumeration();
     278    }
     279
     280    [TestMethod]
     281    [TestProperty("Goal", "structure search + const op")]
     282    public void Constants_Keijzer5() {
     283      // (30*x*z) / ((x - 10)*y*y)
     284      alg.MaxTreeSize = 24;
     285      alg.Problem.ProblemData = new KeijzerFunctionFive().GenerateRegressionData();
     286
     287      alg.Start();
     288
     289      TerminalSymbol constSymbol = alg.Grammar.Const;
     290      TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First(s => s.StringRepresentation == "X");
     291      TerminalSymbol ySymbol = alg.Grammar.VarTerminals.First(s => s.StringRepresentation == "Y");
     292      TerminalSymbol zSymbol = alg.Grammar.VarTerminals.First(s => s.StringRepresentation == "Z");
     293      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     294      TerminalSymbol addSymbol = alg.Grammar.Addition;
     295      TerminalSymbol invSymbol = alg.Grammar.Inv;
     296
     297      // 30 * x * z * 1/(x*y*y - 10*y*y)
     298      // --> x z * c *     x y * y * c *  y y * c * + c + inv c +
     299      SymbolString targetSolution = new SymbolString(new[] {
     300        xSymbol, zSymbol, mulSymbol, constSymbol, mulSymbol,
     301        xSymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol,
     302        ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol, constSymbol, addSymbol, invSymbol,
     303        constSymbol, addSymbol
     304      });
     305
     306      int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
     307      int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
     308
     309      Assert.IsTrue(alg.DistinctSentencesLength.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
     310
     311      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     312
     313      // Evaluate
     314      EvaluateGrammarEnumeration();
     315    }
     316
     317
     318    [TestMethod]
     319    [TestProperty("Goal", "structure search + const op")]
     320    public void Constants_Keijzer12() {
     321      // x*x*x*x - x*x*x + y*y/2 - y
     322      alg.MaxTreeSize = 29;
     323      alg.Problem.ProblemData = new KeijzerFunctionTwelve().GenerateRegressionData();
     324
     325      alg.Start();
     326
     327      TerminalSymbol constSymbol = alg.Grammar.Const;
     328      TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First();
     329      TerminalSymbol ySymbol = alg.Grammar.VarTerminals.Last();
     330      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     331      TerminalSymbol addSymbol = alg.Grammar.Addition;
     332
     333      SymbolString targetSolution = new SymbolString(new[] {
     334        xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
     335        xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
     336        ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
     337        ySymbol, constSymbol, mulSymbol, addSymbol,
     338        constSymbol, addSymbol
     339      });
     340
     341      var x = alg.Grammar.ToInfixString(targetSolution);
     342
     343      int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
     344      int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
     345
     346      Assert.IsTrue(alg.DistinctSentencesLength.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
     347      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     348
     349      // Evaluate
     350      EvaluateGrammarEnumeration();
     351    }
     352
     353    [TestMethod]
     354    [TestProperty("Goal", "structure search + const op")]
     355    public void Constants_Keijzer14() {
     356      // 8 / (2 + x*x + y*y
     357      alg.MaxTreeSize = 19;
     358      alg.Problem.ProblemData = new KeijzerFunctionFourteen().GenerateRegressionData();
     359
     360      alg.Start();
     361     
     362      TerminalSymbol constSymbol = alg.Grammar.Const;
     363      TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First();
     364      TerminalSymbol ySymbol = alg.Grammar.VarTerminals.Last();
     365      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     366      TerminalSymbol addSymbol = alg.Grammar.Addition;
     367      TerminalSymbol divSymbol = alg.Grammar.Inv;
     368
     369     
     370      // x x mul c mul y y mul c mul add const add inv const mul const add
     371      SymbolString targetSolution = new SymbolString(new[] {
     372        xSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol, 
     373        ySymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
     374        constSymbol, addSymbol, divSymbol,
     375        constSymbol, mulSymbol,
     376        constSymbol, addSymbol
     377      });
     378
     379      int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
     380      int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
     381
     382      Assert.IsTrue(alg.DistinctSentencesLength.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
     383      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     384
     385      // Evaluate
     386      EvaluateGrammarEnumeration();
     387    }
     388
     389
     390    [TestMethod]
     391    [TestProperty("Goal", "structure search + const op")]
     392    public void Constants_Keijzer15() {
     393      // x*x*x / 5 + y*y*y / 2 - y - x
     394      alg.MaxTreeSize = 25;
     395      alg.Problem.ProblemData = new KeijzerFunctionFifteen().GenerateRegressionData();
     396
     397      alg.Start();
     398
     399      TerminalSymbol constSymbol = alg.Grammar.Const;
     400      TerminalSymbol xSymbol = alg.Grammar.VarTerminals.First();
     401      TerminalSymbol ySymbol = alg.Grammar.VarTerminals.Last();
     402      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     403      TerminalSymbol addSymbol = alg.Grammar.Addition;
     404
     405      // x x * x * const * y y * y * const * + y const * + x const * const +
     406      SymbolString targetSolution = new SymbolString(new[] {
     407        xSymbol, xSymbol, mulSymbol, xSymbol, mulSymbol, constSymbol, mulSymbol,
     408        ySymbol, ySymbol, mulSymbol, ySymbol, mulSymbol, constSymbol, mulSymbol, addSymbol,
     409        ySymbol, constSymbol, mulSymbol, addSymbol,
     410        xSymbol, constSymbol, mulSymbol, addSymbol,
     411        constSymbol, addSymbol
     412      });
     413
     414      int targetSolutionHash = alg.Grammar.Hasher.CalcHashCode(targetSolution);
     415      int actualSolutionHash = alg.Grammar.Hasher.CalcHashCode(alg.BestTrainingSentence);
     416
     417      Assert.IsTrue(alg.DistinctSentencesLength.ContainsKey(targetSolutionHash), "Actual solution was not generated!");
     418      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     419
     420      // Evaluate
     421      EvaluateGrammarEnumeration();
     422    }
     423
     424
    202425
    203426
Note: See TracChangeset for help on using the changeset viewer.