Changeset 15824 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration
- Timestamp:
- 02/28/18 19:09:34 (6 years ago)
- Location:
- branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration
-
Property
svn:ignore
set to
Plugin.cs
-
Property
svn:ignore
set to
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs
r15821 r15824 8 8 using HeuristicLab.Core; 9 9 using HeuristicLab.Data; 10 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;11 10 using HeuristicLab.Optimization; 12 11 using HeuristicLab.Parameters; 13 12 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 14 13 using HeuristicLab.Problems.DataAnalysis; 15 using HeuristicLab.Problems.DataAnalysis.Symbolic;16 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;17 14 18 15 namespace HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration { … … 22 19 public class GrammarEnumerationAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> { 23 20 #region properties and result names 24 private readonly string BestTrainingQualityName = "Best R² (Training)";25 private readonly string BestTrainingSolutionName = "Best solution (Training)";26 21 private readonly string SearchStructureSizeName = "Search Structure Size"; 27 22 private readonly string GeneratedPhrasesName = "Generated/Archived Phrases"; … … 32 27 private readonly string OverwrittenSentencesName = "Sentences overwritten"; 33 28 private readonly string AnalyzersParameterName = "Analyzers"; 29 private readonly string ExpansionsPerSecondName = "Expansions per second"; 34 30 35 31 … … 72 68 } 73 69 74 public SymbolString BestTrainingSentence; 75 70 public SymbolString BestTrainingSentence { get; set; } // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now... 76 71 #endregion 77 72 … … 91 86 public Grammar Grammar { get; private set; } 92 87 93 private readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();94 88 95 89 #region ctors … … 109 103 var availableAnalyzers = new IGrammarEnumerationAnalyzer[] { 110 104 new SearchGraphVisualizer(), 111 new SentenceLogger() 105 new SentenceLogger(), 106 new RSquaredEvaluator() 112 107 }; 113 108 Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemCollection<IGrammarEnumerationAnalyzer>>( … … 119 114 } 120 115 Analyzers.CheckedItemsChanged += AnalyzersOnCheckedItemsChanged; 116 Analyzers.SetItemCheckedState(Analyzers.First(analyzer => analyzer is RSquaredEvaluator), true); 121 117 } 122 118 … … 179 175 180 176 DistinctSentencesLength[phraseHash] = newPhrase.Count; 181 EvaluateSentence(newPhrase);182 183 177 OnDistinctSentenceGenerated(fetchedPhrase.Hash, fetchedPhrase.SymbolString, phraseHash, newPhrase, expandedSymbol, productionAlternative); 184 178 } … … 191 185 } 192 186 } 193 194 187 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 } 223 189 224 190 #region Visualization in HL 225 191 // Initialize entries in result set. 226 192 private void InitResults() { 227 BestTrainingSentence = null;228 229 Results.Add(new Result(BestTrainingQualityName, new DoubleValue(-1.0)));230 231 193 Results.Add(new Result(GeneratedPhrasesName, new IntValue(0))); 232 194 Results.Add(new Result(SearchStructureSizeName, new IntValue(0))); … … 236 198 Results.Add(new Result(OverwrittenSentencesName, new IntValue(0))); 237 199 Results.Add(new Result(AverageSentenceLengthName, new DoubleValue(1.0))); 200 Results.Add(new Result(ExpansionsPerSecondName, "In Thousand expansions per second", new IntValue(0))); 238 201 } 239 202 … … 251 214 ((DoubleValue)Results[AverageSentenceLengthName].Value).Value = DistinctSentencesLength.Select(pair => pair.Value).Average(); 252 215 ((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 } 266 219 } 267 220 #endregion
Note: See TracChangeset
for help on using the changeset viewer.