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

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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        }
Note: See TracChangeset for help on using the changeset viewer.