Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/09/18 15:32:44 (7 years ago)
Author:
lkammere
Message:

#2886: Refactor grammar enumeration alg.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/Test/GrammarEnumerationTest.cs

    r15734 r15746  
    11using System;
     2using System.Diagnostics;
    23using System.Linq;
    34using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
     
    2425    [TestInitialize]
    2526    public void InitTest() {
    26       Console.Write("init called... ");
    2727      rand = new FastRandom(Seed);
    2828
     
    3333    }
    3434
    35     [TestCleanup()]
     35    [TestCleanup]
    3636    public void Cleanup() {
    3737      if (alg.BestTrainingSentence != null) {
    3838        Console.WriteLine("Training: " + alg.Grammar.PostfixToInfixParser(alg.BestTrainingSentence));
    3939      }
    40       if (alg.BestTestSentence != null) {
    41         Console.WriteLine("Test: " + alg.Grammar.PostfixToInfixParser(alg.BestTestSentence));
    42       }
    4340    }
    4441
     
    4946
    5047      // Check if algorithm terminated correctly
    51       Assert.IsTrue(alg.Results.ContainsKey("Best solution quality (training)"), "No training solution returned!");
    52       Assert.IsTrue(alg.Results.ContainsKey("Best solution quality (test)"), "No test solution returned!");
     48      Assert.IsTrue(alg.Results.ContainsKey("Best solution (Training)"), "No training solution returned!");
    5349
    5450      // Check resultss
    55       Assert.AreEqual(1.0, ((DoubleValue)alg.Results["Best solution quality (training)"].Value).Value, eps, "Training quality too low!");
    56       Assert.AreEqual(1.0, ((DoubleValue)alg.Results["Best solution quality (test)"].Value).Value, eps, "Test quality too low!");
    57 
    58       // Check overfitting
    59       Assert.AreEqual(alg.Grammar.CalcHashCode(alg.BestTrainingSentence),
    60                     alg.Grammar.CalcHashCode(alg.BestTestSentence),
    61                     "Training and Test solution are not equal!");
     51      Assert.AreEqual(1.0, ((IRegressionSolution)alg.Results["Best solution (Training)"].Value).TestRSquared, eps, "Test quality too low!");
    6252    }
    6353
     
    6858    public void MctsSymbReg_NoConstants_Nguyen1() {
    6959      // x³ + x² + x
    70       alg.MaxTreeSize = 3;
     60      alg.MaxTreeSize = 15;
    7161      Console.WriteLine(alg.MaxTreeSize);
    7262      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
     
    7565
    7666      alg.Start();
    77 
    78       // Evaluate
    79       // EvaluateGrammarEnumeration();
    8067
    8168      TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First();
     
    9077
    9178      int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution);
    92       int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTestSentence);
    93 
    94       Assert.IsTrue(alg.distinctSentences.Select(tuple => tuple.Item2).Contains(actualSolutionHash));
    95 
    96       // last because long sentences are overwritten by short ones.
    97       Console.WriteLine(alg.Grammar.PostfixToInfixParser(alg.distinctSentences.Last(tuple => tuple.Item2 == targetSolutionHash).Item1));
    98 
    99 
    100       Assert.AreEqual(targetSolutionHash, actualSolutionHash);
     79      int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence);
     80
     81      Assert.IsTrue(alg.DistinctSentences.ContainsKey(actualSolutionHash), "Actual solution was not generated!");
     82
     83      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
     84
     85      // Evaluate
     86      EvaluateGrammarEnumeration();
    10187    }
    10288
     
    10591    public void MctsSymbReg_NoConstants_Nguyen2() {
    10692      // x^4 + x³ + x² + x
    107       alg.MaxTreeSize = 30;
     93      alg.MaxTreeSize = 25;
    10894      var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(Seed);
    10995      var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F2 ")));
     
    11197
    11298      alg.Start();
     99
     100      TerminalSymbol varSymbol = alg.Grammar.Var.VariableTerminalSymbols.First();
     101      TerminalSymbol mulSymbol = alg.Grammar.Multiplication;
     102      TerminalSymbol addSymbol = alg.Grammar.Addition;
     103
     104      SymbolString targetSolution = new SymbolString(new[] {
     105        varSymbol, varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, mulSymbol,
     106        varSymbol, varSymbol, varSymbol, mulSymbol, mulSymbol, addSymbol,
     107        varSymbol, varSymbol, mulSymbol, addSymbol,
     108        varSymbol, addSymbol
     109      });
     110
     111      int targetSolutionHash = alg.Grammar.CalcHashCode(targetSolution);
     112      int actualSolutionHash = alg.Grammar.CalcHashCode(alg.BestTrainingSentence);
     113
     114      var targetSolutionStr = alg.Grammar.PostfixToInfixParser(targetSolution).ToString();
     115      var foundTargetSolutionStr = alg.Grammar.PostfixToInfixParser(alg.DistinctSentences[targetSolutionHash]).ToString();
     116      var actualSolutionStr = alg.Grammar.PostfixToInfixParser(alg.DistinctSentences[targetSolutionHash]).ToString();
     117
     118      Assert.IsTrue(alg.DistinctSentences.ContainsKey(targetSolutionHash), "Target solution was not generated!");
     119
     120      Assert.AreEqual(targetSolutionHash, actualSolutionHash, "Actual solution was not recognized as best one.");
    113121
    114122      // Evaluate
     
    126134      alg.Start();
    127135
    128       Assert.AreEqual(alg.distinctSentences.Count(), 110);
     136      Assert.AreEqual(alg.DistinctSentences.Count(), 110);
    129137
    130138      // Evaluate
     
    142150      alg.Start();
    143151
    144       Assert.AreEqual(alg.distinctSentences.Count(), 16);
     152      Assert.AreEqual(alg.DistinctSentences.Count(), 16);
    145153
    146154      // Evaluate
Note: See TracChangeset for help on using the changeset viewer.