Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15724 for branches


Ignore:
Timestamp:
02/06/18 13:52:00 (7 years ago)
Author:
lkammere
Message:

#2886: Add parsing to infix form for debugging purpose.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15723 r15724  
    155155    #endregion
    156156    */
    157    
     157
    158158    #region Hashing
    159159    public int CalcHashCode(SymbolString sentence) {
     
    282282    }
    283283    #endregion
     284
     285    #region Parse to Infix string
     286
     287    public SymbolString PostfixToInfixParser(SymbolString phrase) {
     288      Stack<Symbol> parseStack = new Stack<Symbol>(phrase);
     289
     290      return PostfixToInfixSubtreeParser(parseStack);
     291    }
     292
     293    private SymbolString PostfixToInfixSubtreeParser(Stack<Symbol> parseStack) {
     294      Symbol head = parseStack.Pop();
     295
     296      SymbolString result = new SymbolString();
     297
     298      if (ReferenceEquals(head, Addition) || ReferenceEquals(head, Multiplication)) {
     299        result.AddRange(PostfixToInfixSubtreeParser(parseStack));
     300        result.Add(head);
     301        result.AddRange(PostfixToInfixSubtreeParser(parseStack));
     302      } else {
     303        result.Add(head);
     304      }
     305      return result;
     306    }
     307
     308    #endregion
    284309  }
    285310}
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15723 r15724  
    2020  public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> {
    2121    private readonly string BestTrainingSolution = "Best solution (training)";
    22     private readonly string BestTrainingSolutionString = "Best solution string (training)";
    2322    private readonly string BestTrainingSolutionQuality = "Best solution quality (training)";
    2423    private readonly string BestTestSolution = "Best solution (test)";
    25     private readonly string BestTestSolutionString = "Best solution string (test)";
    2624    private readonly string BestTestSolutionQuality = "Best solution quality (test)";
    2725
     
    5654    }
    5755
     56    public SymbolString BestTrainingSentence;
     57    public SymbolString BestTestSentence;
     58
    5859    #endregion
    5960
    60     private Grammar grammar;
     61    public Grammar Grammar;
    6162
    6263
     
    8586
    8687    protected override void Run(CancellationToken cancellationToken) {
     88      BestTrainingSentence = null;
     89      BestTrainingSentence = null;
     90
    8791      List<SymbolString> allGenerated = new List<SymbolString>();
    8892      List<SymbolString> distinctGenerated = new List<SymbolString>();
     
    9296      HashSet<int> evaluatedHashes = new HashSet<int>();
    9397
    94       grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray());
     98      Grammar = new Grammar(Problem.ProblemData.AllowedInputVariables.ToArray());
    9599
    96100      Stack<SymbolString> remainingTrees = new Stack<SymbolString>();
    97       remainingTrees.Push(new SymbolString(new[] { grammar.StartSymbol }));
     101      remainingTrees.Push(new SymbolString(new[] { Grammar.StartSymbol }));
    98102
    99103      while (remainingTrees.Any()) {
     
    103107
    104108        if (currSymbolString.IsSentence()) {
    105           allGenerated.Add(currSymbolString);
     109          allGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));
    106110
    107111          //if (evaluatedHashes.Add(grammar.CalcHashCode(currSymbolString))) {
    108112          EvaluateSentence(currSymbolString);
    109           //distinctGenerated.Add(currSymbolString);
     113          distinctGenerated.Add(Grammar.PostfixToInfixParser(currSymbolString));
    110114          //}
    111115
     
    155159
    156160    private void EvaluateSentence(SymbolString symbolString) {
    157       SymbolicExpressionTree tree = grammar.ParseSymbolicExpressionTree(symbolString);
     161      SymbolicExpressionTree tree = Grammar.ParseSymbolicExpressionTree(symbolString);
    158162      SymbolicRegressionModel model = new SymbolicRegressionModel(
    159163        Problem.ProblemData.TargetVariable,
     
    168172           || !Results.TryGetValue(BestTestSolution, out currBestTestSolutionResult)) {
    169173
     174        BestTrainingSentence = symbolString;
    170175        Results.Add(new Result(BestTrainingSolution, newSolution));
    171         Results.Add(new Result(BestTrainingSolutionString, new StringValue(symbolString.ToString()).AsReadOnly()));
    172176        Results.Add(new Result(BestTrainingSolutionQuality, new DoubleValue(newSolution.TrainingRSquared).AsReadOnly()));
     177
     178        BestTestSentence = symbolString;
    173179        Results.Add(new Result(BestTestSolution, newSolution));
    174         Results.Add(new Result(BestTestSolutionString, new StringValue(symbolString.ToString()).AsReadOnly()));
    175180        Results.Add(new Result(BestTestSolutionQuality, new DoubleValue(newSolution.TestRSquared).AsReadOnly()));
    176181
     
    178183        IRegressionSolution currBestTrainingSolution = (IRegressionSolution)currBestTrainingSolutionResult.Value;
    179184        if (currBestTrainingSolution.TrainingRSquared < newSolution.TrainingRSquared) {
     185          BestTrainingSentence = symbolString;
    180186          currBestTrainingSolutionResult.Value = newSolution;
    181           Results.AddOrUpdateResult(BestTrainingSolutionString, new StringValue(symbolString.ToString()));
    182187          Results.AddOrUpdateResult(BestTrainingSolutionQuality, new DoubleValue(newSolution.TrainingRSquared).AsReadOnly());
    183188        }
     
    185190        IRegressionSolution currBestTestSolution = (IRegressionSolution)currBestTestSolutionResult.Value;
    186191        if (currBestTestSolution.TestRSquared < newSolution.TestRSquared) {
     192          BestTestSentence = symbolString;
    187193          currBestTestSolutionResult.Value = newSolution;
    188           Results.AddOrUpdateResult(BestTestSolutionString, new StringValue(symbolString.ToString()));
    189194          Results.AddOrUpdateResult(BestTestSolutionQuality, new DoubleValue(newSolution.TestRSquared).AsReadOnly());
    190195        }
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15723 r15724  
    22using System.Linq;
    33using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
     4using HeuristicLab.Common;
    45using HeuristicLab.Core;
    56using HeuristicLab.Data;
     7using HeuristicLab.Optimization;
    68using HeuristicLab.Problems.DataAnalysis;
    79using HeuristicLab.Random;
     
    911
    1012namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
    11   [TestClass()]
     13  [TestClass]
    1214  public class MctsSymbolicRegressionTest {
    1315    private const int Seed = 1234;
     
    3032    }
    3133
     34    [TestCleanup()]
     35    public void Cleanup() {
     36      if (alg.BestTrainingSentence != null) {
     37        Console.WriteLine("Training: " + alg.Grammar.PostfixToInfixParser(alg.BestTrainingSentence));
     38      }
     39      if (alg.BestTestSentence != null) {
     40        Console.WriteLine("Test: " + alg.Grammar.PostfixToInfixParser(alg.BestTestSentence));
     41      }
     42    }
     43
    3244
    3345    private void TestGrammarEnumeration(IRegressionProblemData problemData) {
     
    4961
    5062      // Check overfitting
    51       Assert.AreEqual(alg.Results["Best solution string (training)"].Value.ToString(),
    52                       alg.Results["Best solution string (test)"].Value.ToString());
     63      Assert.AreEqual(alg.Grammar.CalcHashCode(alg.BestTrainingSentence),
     64                    alg.Grammar.CalcHashCode(alg.BestTestSentence),
     65                    "Training and Test solution are not equal!");
    5366    }
    5467
     
    6376      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F1 ")));
    6477      TestGrammarEnumeration(regProblem);
    65 
    66       Console.WriteLine("Nguyen1: " + alg.Results["Best solution string (training)"].Value);
    6778    }
    6879
     
    7586      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 ")));
    7687      TestGrammarEnumeration(regProblem);
    77 
    78       Console.WriteLine("Nguyen2: " + alg.Results["Best solution string (training)"].Value);
    7988    }
    8089
Note: See TracChangeset for help on using the changeset viewer.