Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15824


Ignore:
Timestamp:
02/28/18 19:09:34 (6 years ago)
Author:
lkammere
Message:

#2886: Move R² calculation of sentences to separate class and allow its deactivation.

Location:
branches/2886_SymRegGrammarEnumeration
Files:
1 added
4 edited

Legend:

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

    • Property svn:ignore set to
      Plugin.cs
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15821 r15824  
    88using HeuristicLab.Core;
    99using HeuristicLab.Data;
    10 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    1110using HeuristicLab.Optimization;
    1211using HeuristicLab.Parameters;
    1312using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    1413using HeuristicLab.Problems.DataAnalysis;
    15 using HeuristicLab.Problems.DataAnalysis.Symbolic;
    16 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    1714
    1815namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration {
     
    2219  public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> {
    2320    #region properties and result names
    24     private readonly string BestTrainingQualityName = "Best R² (Training)";
    25     private readonly string BestTrainingSolutionName = "Best solution (Training)";
    2621    private readonly string SearchStructureSizeName = "Search Structure Size";
    2722    private readonly string GeneratedPhrasesName = "Generated/Archived Phrases";
     
    3227    private readonly string OverwrittenSentencesName = "Sentences overwritten";
    3328    private readonly string AnalyzersParameterName = "Analyzers";
     29    private readonly string ExpansionsPerSecondName = "Expansions per second";
    3430
    3531
     
    7268    }
    7369
    74     public SymbolString BestTrainingSentence;
    75 
     70    public SymbolString BestTrainingSentence { get; set; }     // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now...
    7671    #endregion
    7772
     
    9186    public Grammar Grammar { get; private set; }
    9287
    93     private readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
    9488
    9589    #region ctors
     
    109103      var availableAnalyzers = new IGrammarEnumerationAnalyzer[] {
    110104        new SearchGraphVisualizer(),
    111         new SentenceLogger()
     105        new SentenceLogger(),
     106        new RSquaredEvaluator()
    112107      };
    113108      Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>>(
     
    119114      }
    120115      Analyzers.CheckedItemsChanged += AnalyzersOnCheckedItemsChanged;
     116      Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true);
    121117    }
    122118
     
    179175
    180176                DistinctSentencesLength[phraseHash] = newPhrase.Count;
    181                 EvaluateSentence(newPhrase);
    182 
    183177                OnDistinctSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, productionAlternative);
    184178              }
     
    191185        }
    192186      }
    193 
    194187      UpdateView(force: true);
    195       UpdateFinalResults();
    196     }
    197 
    198     #region Evaluation of generated models.
    199 
    200     // Evaluate sentence within an algorithm run.
    201     private void EvaluateSentence(SymbolString symbolString) {
    202       SymbolicExpressionTree tree = Grammar.ParseSymbolicExpressionTree(symbolString);
    203       SymbolicRegressionModel model = new SymbolicRegressionModel(
    204         Problem.ProblemData.TargetVariable,
    205         tree,
    206         expressionTreeLinearInterpreter);
    207 
    208       var probData = Problem.ProblemData;
    209       var target = probData.TargetVariableTrainingValues;
    210       var estVals = model.GetEstimatedValues(probData.Dataset, probData.TrainingIndices);
    211       OnlineCalculatorError error;
    212       var r2 = OnlinePearsonsRSquaredCalculator.Calculate(target, estVals, out error);
    213       if (error != OnlineCalculatorError.None) r2 = 0.0;
    214 
    215       var bestR2 = ((DoubleValue)Results[BestTrainingQualityName].Value).Value;
    216       if (r2 > bestR2) {
    217         ((DoubleValue)Results[BestTrainingQualityName].Value).Value = r2;
    218         BestTrainingSentence = symbolString;
    219       }
    220     }
    221 
    222     #endregion
     188    }
    223189
    224190    #region Visualization in HL
    225191    // Initialize entries in result set.
    226192    private void InitResults() {
    227       BestTrainingSentence = null;
    228 
    229       Results.Add(new Result(BestTrainingQualityName, new DoubleValue(-1.0)));
    230 
    231193      Results.Add(new Result(GeneratedPhrasesName, new IntValue(0)));
    232194      Results.Add(new Result(SearchStructureSizeName, new IntValue(0)));
     
    236198      Results.Add(new Result(OverwrittenSentencesName, new IntValue(0)));
    237199      Results.Add(new Result(AverageSentenceLengthName, new DoubleValue(1.0)));
     200      Results.Add(new Result(ExpansionsPerSecondName, "In Thousand expansions per second", new IntValue(0)));
    238201    }
    239202
     
    251214        ((DoubleValue)Results[AverageSentenceLengthName].Value).Value = DistinctSentencesLength.Select(pair => pair.Value).Average();
    252215        ((IntValue)Results[OverwrittenSentencesName].Value).Value = OverwrittenSentencesCount;
    253       }
    254     }
    255 
    256     // Generate all Results after an algorithm run.
    257     private void UpdateFinalResults() {
    258       SymbolicExpressionTree tree = Grammar.ParseSymbolicExpressionTree(BestTrainingSentence);
    259       SymbolicRegressionModel model = new SymbolicRegressionModel(
    260         Problem.ProblemData.TargetVariable,
    261         tree,
    262         new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
    263 
    264       IRegressionSolution bestTrainingSolution = new RegressionSolution(model, Problem.ProblemData);
    265       Results.AddOrUpdateResult(BestTrainingSolutionName, bestTrainingSolution);
     216        ((IntValue)Results[ExpansionsPerSecondName].Value).Value = (int)((PhraseExpansionCount /
     217                                                                          ExecutionTime.TotalSeconds) / 1000.0);
     218      }
    266219    }
    267220    #endregion
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.csproj

    r15823 r15824  
    4949    <Reference Include="HeuristicLab.Common-3.3">
    5050      <HintPath>..\..\..\trunk\bin\HeuristicLab.Common-3.3.dll</HintPath>
    51     <Private>False</Private>
     51      <Private>False</Private>
    5252    </Reference>
    5353    <Reference Include="HeuristicLab.Core-3.3">
    5454      <HintPath>..\..\..\trunk\bin\HeuristicLab.Core-3.3.dll</HintPath>
    55     <Private>False</Private>
     55      <Private>False</Private>
    5656    </Reference>
    5757    <Reference Include="HeuristicLab.Data-3.3">
    5858      <HintPath>..\..\..\trunk\bin\HeuristicLab.Data-3.3.dll</HintPath>
    59     <Private>False</Private>
     59      <Private>False</Private>
    6060    </Reference>
    6161    <Reference Include="HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4">
    6262      <HintPath>..\..\..\trunk\bin\HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.dll</HintPath>
    6363      <Private>False</Private>
    64   </Reference>
     64    </Reference>
    6565    <Reference Include="HeuristicLab.Operators-3.3">
    6666      <HintPath>..\..\..\trunk\bin\HeuristicLab.Operators-3.3.dll</HintPath>
    6767      <Private>False</Private>
    68   </Reference>
     68    </Reference>
    6969    <Reference Include="HeuristicLab.Optimization-3.3">
    7070      <HintPath>..\..\..\trunk\bin\HeuristicLab.Optimization-3.3.dll</HintPath>
    71     <Private>False</Private>
     71      <Private>False</Private>
    7272    </Reference>
    7373    <Reference Include="HeuristicLab.Parameters-3.3">
    7474      <HintPath>..\..\..\trunk\bin\HeuristicLab.Parameters-3.3.dll</HintPath>
    75     <Private>False</Private>
     75      <Private>False</Private>
    7676    </Reference>
    7777    <Reference Include="HeuristicLab.Persistence-3.3">
    7878      <HintPath>..\..\..\trunk\bin\HeuristicLab.Persistence-3.3.dll</HintPath>
    79     <Private>False</Private>
     79      <Private>False</Private>
    8080    </Reference>
    8181    <Reference Include="HeuristicLab.PluginInfrastructure-3.3">
    8282      <HintPath>..\..\..\trunk\bin\HeuristicLab.PluginInfrastructure-3.3.dll</HintPath>
    83     <Private>False</Private>
     83      <Private>False</Private>
    8484    </Reference>
    8585    <Reference Include="HeuristicLab.Problems.DataAnalysis-3.4">
    8686      <HintPath>..\..\..\trunk\bin\HeuristicLab.Problems.DataAnalysis-3.4.dll</HintPath>
    87     <Private>False</Private>
     87      <Private>False</Private>
    8888    </Reference>
    8989    <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic-3.4">
    9090      <HintPath>..\..\..\trunk\bin\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.dll</HintPath>
    9191      <Private>False</Private>
    92   </Reference>
     92    </Reference>
    9393    <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4">
    9494      <HintPath>..\..\..\trunk\bin\HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4.dll</HintPath>
    95     <Private>False</Private>
     95      <Private>False</Private>
    9696    </Reference>
    9797    <Reference Include="HeuristicLab.Problems.Instances-3.3">
    9898      <HintPath>..\..\..\trunk\bin\HeuristicLab.Problems.Instances-3.3.dll</HintPath>
    99     <Private>False</Private>
     99      <Private>False</Private>
    100100    </Reference>
    101101    <Reference Include="HeuristicLab.Random-3.3">
    102102      <HintPath>..\..\..\trunk\bin\HeuristicLab.Random-3.3.dll</HintPath>
    103     <Private>False</Private>
     103      <Private>False</Private>
    104104    </Reference>
    105105    <Reference Include="HeuristicLab.Problems.Instances.DataAnalysis-3.3">
    106106      <HintPath>..\..\..\trunk\bin\HeuristicLab.Problems.Instances.DataAnalysis-3.3.dll</HintPath>
    107     <Private>False</Private>
     107      <Private>False</Private>
    108108    </Reference>
    109109    <Reference Include="System" />
     
    111111  <ItemGroup>
    112112    <Compile Include="Analysis\IGrammarEnumerationAnalyzer.cs" />
     113    <Compile Include="Analysis\RSquaredEvaluator.cs" />
    113114    <Compile Include="Analysis\SentenceLogger.cs" />
    114115    <Compile Include="Analysis\SearchGraphVisualizer.cs" />
     
    119120    <Compile Include="Properties\AssemblyInfo.cs" />
    120121    <Compile Include="GrammarEnumeration\Symbol.cs" />
     122    <Compile Include="Plugin.cs" />
    121123  </ItemGroup>
    122124  <ItemGroup>
  • branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs

    r15821 r15824  
    170170      Assert.AreNotEqual(hash1, hash2);
    171171    }*/
    172 
    173     #region parser
    174 
    175     [TestMethod]
    176     [TestCategory("Parser")]
    177     public void InfixParserSimpleTest() {
    178       SymbolString postfix = new SymbolString(new[] { varA, varB, varC, grammar.Addition, grammar.Addition });
    179       SymbolString infix = new SymbolString(new[] { varA, grammar.Addition, varB, grammar.Addition, varC });
    180 
    181       Assert.AreEqual(infix.ToString(), grammar.ToInfixString(postfix));
    182     }
    183 
    184     [TestMethod]
    185     [TestCategory("Parser")]
    186     public void InfixParserComplexTest() {
    187       SymbolString postfix = new SymbolString(new[] { varB, varA, varB, varC, grammar.Multiplication, grammar.Addition, grammar.Addition });
    188       SymbolString infix = new SymbolString(new[] { varB, grammar.Addition, varA, grammar.Addition, varB, grammar.Multiplication, varC });
    189 
    190       Assert.AreEqual(infix.ToString(), grammar.ToInfixString(postfix));
    191     }
    192 
    193     #endregion
    194172  }
    195173}
Note: See TracChangeset for help on using the changeset viewer.