Changeset 15974 for branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis
- Timestamp:
- 06/28/18 11:35:13 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Analysis/RSquaredEvaluator.cs
r15949 r15974 16 16 public class RSquaredEvaluator : Item, IGrammarEnumerationAnalyzer { 17 17 public static readonly string BestTrainingQualityResultName = "Best R² (Training)"; 18 public static readonly string BestTestQualityResultName = "Best R² (Test)"; 18 19 public static readonly string BestTrainingModelResultName = "Best model (Training)"; 19 20 public static readonly string BestTrainingSolutionResultName = "Best solution (Training)"; 21 public static readonly string BestComplexityResultName = "Best solution complexity"; 20 22 21 23 private static readonly ISymbolicDataAnalysisExpressionTreeInterpreter expressionTreeLinearInterpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter(); … … 42 44 algorithm.Started -= OnStarted; 43 45 algorithm.Stopped -= OnStopped; 44 45 46 algorithm.DistinctSentenceGenerated -= AlgorithmOnDistinctSentenceGenerated; 46 47 } … … 57 58 } 58 59 59 private void OnStopped(object sender, EventArgs eventArgs) { 60 GrammarEnumerationAlgorithm algorithm = (GrammarEnumerationAlgorithm)sender; 61 if (algorithm.Results.ContainsKey(BestTrainingModelResultName)) { 62 SymbolicRegressionModel model = (SymbolicRegressionModel)algorithm.Results[BestTrainingModelResultName].Value; 63 IRegressionSolution bestTrainingSolution = new RegressionSolution(model, algorithm.Problem.ProblemData); 60 private void OnStopped(object sender, EventArgs eventArgs) { } 64 61 65 algorithm.Results.AddOrUpdateResult(BestTrainingSolutionResultName, bestTrainingSolution); 66 } 62 private T GetValue<T>(IItem value) where T : struct { 63 var v = value as ValueTypeValue<T>; 64 if (v == null) 65 throw new ArgumentException(string.Format("Item is not of type {0}", typeof(ValueTypeValue<T>))); 66 return v.Value; 67 67 } 68 68 69 69 private void EvaluateSentence(GrammarEnumerationAlgorithm algorithm, SymbolString symbolString) { 70 var results = algorithm.Results; 71 var grammar = algorithm.Grammar; 70 72 var problemData = algorithm.Problem.ProblemData; 71 73 … … 74 76 75 77 double r2 = Evaluate(problemData, tree, OptimizeConstants); 78 double bestR2 = results.ContainsKey(BestTrainingQualityResultName) ? GetValue<double>(results[BestTrainingQualityResultName].Value) : 0.0; 79 if (r2 < bestR2) 80 return; 76 81 77 double bestR2 = 0.0; 78 if (algorithm.Results.ContainsKey(BestTrainingQualityResultName)) 79 bestR2 = ((DoubleValue)algorithm.Results[BestTrainingQualityResultName].Value).Value; 80 bool better = r2 > bestR2; 81 bool equallyGood = r2.IsAlmost(bestR2); 82 bool shorter = false; 82 var bestComplexity = int.MaxValue; 83 if (results.ContainsKey(BestComplexityResultName)) { 84 bestComplexity = GetValue<int>(results[BestComplexityResultName].Value); 85 } else if (algorithm.BestTrainingSentence != null) { 86 bestComplexity = grammar.GetComplexity(algorithm.BestTrainingSentence); 87 results.AddOrUpdateResult(BestComplexityResultName, new IntValue(bestComplexity)); 88 } 89 var complexity = grammar.GetComplexity(symbolString); 83 90 84 if (!better && equallyGood) { 85 shorter = algorithm.BestTrainingSentence != null && 86 algorithm.Grammar.GetComplexity(algorithm.BestTrainingSentence) > algorithm.Grammar.GetComplexity(symbolString); 87 } 88 if (better || (equallyGood && shorter)) { 89 algorithm.Results.AddOrUpdateResult(BestTrainingQualityResultName, new DoubleValue(r2)); 90 91 SymbolicRegressionModel model = new SymbolicRegressionModel( 92 problemData.TargetVariable, 93 tree, 94 expressionTreeLinearInterpreter); 95 96 algorithm.Results.AddOrUpdateResult(BestTrainingModelResultName, model); 97 91 if (r2 > bestR2 || (r2.IsAlmost(bestR2) && complexity < bestComplexity)) { 92 results.AddOrUpdateResult(BestTrainingQualityResultName, new DoubleValue(r2)); 93 results.AddOrUpdateResult(BestComplexityResultName, new IntValue(complexity)); 98 94 algorithm.BestTrainingSentence = symbolString; 99 95 } … … 121 117 } 122 118 } 123 124 r2 = double.IsNaN(r2) ? 0.0 : r2;125 126 119 } else { 127 var target = problemData.TargetVariableTrainingValues; 128 129 SymbolicRegressionModel model = new SymbolicRegressionModel( 130 problemData.TargetVariable, 120 r2 = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(expressionTreeLinearInterpreter, 131 121 tree, 132 expressionTreeLinearInterpreter); 133 134 var estVals = model.GetEstimatedValues(problemData.Dataset, problemData.TrainingIndices); 135 OnlineCalculatorError error; 136 r2 = OnlinePearsonsRCalculator.Calculate(target, estVals, out error); 137 if (error != OnlineCalculatorError.None) r2 = 0.0; 122 double.MinValue, 123 double.MaxValue, 124 problemData, 125 problemData.TrainingIndices, 126 applyLinearScaling: true); 138 127 } 139 140 return r2; 128 return double.IsNaN(r2) ? 0.0 : r2; 141 129 } 142 130 }
Note: See TracChangeset
for help on using the changeset viewer.