- Timestamp:
- 01/11/11 15:03:46 (13 years ago)
- Location:
- branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic
- Files:
-
- 3 added
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/BestSymbolicRegressionSolutionAnalyzer.cs
r4475 r5275 22 22 using System.Linq; 23 23 using HeuristicLab.Analysis; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Data; … … 40 41 private const string VariableImpactsResultName = "Integrated variable frequencies"; 41 42 private const string BestSolutionParameterName = "BestSolution"; 42 private const string BestSolutionComplexity = "Best solution complexity";43 43 44 44 #region parameter properties … … 68 68 #endregion 69 69 70 [StorableConstructor] 71 private BestSymbolicRegressionSolutionAnalyzer(bool deserializing) : base(deserializing) { } 72 private BestSymbolicRegressionSolutionAnalyzer(BestSymbolicRegressionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } 70 73 public BestSymbolicRegressionSolutionAnalyzer() 71 74 : base() { … … 76 79 } 77 80 81 public override IDeepCloneable Clone(Cloner cloner) { 82 return new BestSymbolicRegressionSolutionAnalyzer(this, cloner); 83 } 84 78 85 [StorableHook(HookType.AfterDeserialization)] 79 private void Initialize() {86 private void AfterDeserialization() { 80 87 if (!Parameters.ContainsKey(VariableFrequenciesParameterName)) { 81 88 Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts")); … … 92 99 var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(), 93 100 SymbolicExpressionTree[i]); 94 var solution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), model, lowerEstimationLimit, upperEstimationLimit); 101 DataAnalysisProblemData problemDataClone = (DataAnalysisProblemData)ProblemData.Clone(); 102 var solution = new SymbolicRegressionSolution(problemDataClone, model, lowerEstimationLimit, upperEstimationLimit); 95 103 solution.Name = BestSolutionParameterName; 96 104 solution.Description = "Best solution on validation partition found over the whole run."; 97 105 BestSolutionParameter.ActualValue = solution; 98 106 BestSolutionQualityParameter.ActualValue = Quality[i]; 99 BestSymbolicRegressionSolutionAnalyzer.UpdateSymbolicRegressionBestSolutionResults(solution, ProblemData, Results, VariableFrequencies);107 BestSymbolicRegressionSolutionAnalyzer.UpdateSymbolicRegressionBestSolutionResults(solution, problemDataClone, Results, VariableFrequencies); 100 108 } 101 109 return BestSolutionParameter.ActualValue; 102 110 } 103 111 104 public static void UpdateBestSolutionResults(SymbolicRegressionSolution bestSolution, DataAnalysisProblemData problemData, ResultCollection results, IntValue currentGeneration, DataTable variableFrequencies) {105 RegressionSolutionAnalyzer.UpdateBestSolutionResults( bestSolution, problemData, results, currentGeneration);106 UpdateSymbolicRegressionBestSolutionResults( bestSolution, problemData, results, variableFrequencies);112 public static void UpdateBestSolutionResults(SymbolicRegressionSolution solution, DataAnalysisProblemData problemData, ResultCollection results, IntValue currentGeneration, DataTable variableFrequencies) { 113 RegressionSolutionAnalyzer.UpdateBestSolutionResults(solution, problemData, results, currentGeneration); 114 UpdateSymbolicRegressionBestSolutionResults(solution, problemData, results, variableFrequencies); 107 115 } 108 116 109 private static void UpdateSymbolicRegressionBestSolutionResults(SymbolicRegressionSolution bestSolution, DataAnalysisProblemData problemData, ResultCollection results, DataTable variableFrequencies) {117 private static void UpdateSymbolicRegressionBestSolutionResults(SymbolicRegressionSolution solution, DataAnalysisProblemData problemData, ResultCollection results, DataTable variableFrequencies) { 110 118 if (results.ContainsKey(BestSolutionInputvariableCountResultName)) { 111 results[BestSolutionInputvariableCountResultName].Value = new IntValue( bestSolution.Model.InputVariables.Count());119 results[BestSolutionInputvariableCountResultName].Value = new IntValue(solution.Model.InputVariables.Count()); 112 120 results[VariableImpactsResultName].Value = CalculateVariableImpacts(variableFrequencies); 113 var sizeTable = (DataTable)results[BestSolutionComplexity].Value;114 sizeTable.Rows["Best solution size"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Size);115 sizeTable.Rows["Best solution height"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Height);116 sizeTable.Rows["Best solution variables"].Values.Add(bestSolution.Model.InputVariables.Count());117 121 } else { 118 results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue( bestSolution.Model.InputVariables.Count())));122 results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(solution.Model.InputVariables.Count()))); 119 123 results.Add(new Result(VariableImpactsResultName, CalculateVariableImpacts(variableFrequencies))); 120 var sizeTable = new DataTable("Best solution complexity");121 sizeTable.Rows.Add(new DataRow("Best solution size"));122 sizeTable.Rows.Add(new DataRow("Best solution height"));123 sizeTable.Rows.Add(new DataRow("Best solution variables"));124 sizeTable.Rows["Best solution size"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Size);125 sizeTable.Rows["Best solution height"].Values.Add(bestSolution.Model.SymbolicExpressionTree.Height);126 sizeTable.Rows["Best solution variables"].Values.Add(bestSolution.Model.InputVariables.Count());127 results.Add(new Result(BestSolutionComplexity, sizeTable));128 124 } 129 125 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs
r4484 r5275 23 23 using System.Linq; 24 24 using HeuristicLab.Analysis; 25 using HeuristicLab.Common; 25 26 using HeuristicLab.Core; 26 27 using HeuristicLab.Data; … … 30 31 using HeuristicLab.Parameters; 31 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Problems.DataAnalysis.Evaluators;33 33 using HeuristicLab.Problems.DataAnalysis.Symbolic; 34 using System;35 34 36 35 namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers { … … 40 39 [Item("FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic regression solution.")] 41 40 [StorableClass] 42 public sealed class FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer : SingleSuccessorOperator, ISymbolicRegressionAnalyzer { 43 private const string RandomParameterName = "Random"; 44 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 45 private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 46 private const string ProblemDataParameterName = "ProblemData"; 47 private const string ValidationSamplesStartParameterName = "SamplesStart"; 48 private const string ValidationSamplesEndParameterName = "SamplesEnd"; 49 // private const string QualityParameterName = "Quality"; 50 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 51 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 52 private const string EvaluatorParameterName = "Evaluator"; 41 public sealed class FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer : SymbolicRegressionValidationAnalyzer, ISymbolicRegressionAnalyzer { 53 42 private const string MaximizationParameterName = "Maximization"; 43 private const string CalculateSolutionComplexityParameterName = "CalculateSolutionComplexity"; 54 44 private const string BestSolutionParameterName = "Best solution (validation)"; 55 45 private const string BestSolutionQualityParameterName = "Best solution quality (validation)"; 46 private const string BestSolutionLengthParameterName = "Best solution length (validation)"; 47 private const string BestSolutionHeightParameterName = "Best solution height (validiation)"; 56 48 private const string CurrentBestValidationQualityParameterName = "Current best validation quality"; 57 49 private const string BestSolutionQualityValuesParameterName = "Validation Quality"; … … 60 52 private const string BestKnownQualityParameterName = "BestKnownQuality"; 61 53 private const string GenerationsParameterName = "Generations"; 62 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";63 64 private const string TrainingMeanSquaredErrorQualityParameterName = "Mean squared error (training)";65 private const string MinTrainingMeanSquaredErrorQualityParameterName = "Min mean squared error (training)";66 private const string MaxTrainingMeanSquaredErrorQualityParameterName = "Max mean squared error (training)";67 private const string AverageTrainingMeanSquaredErrorQualityParameterName = "Average mean squared error (training)";68 private const string BestTrainingMeanSquaredErrorQualityParameterName = "Best mean squared error (training)";69 70 private const string TrainingAverageRelativeErrorQualityParameterName = "Average relative error (training)";71 private const string MinTrainingAverageRelativeErrorQualityParameterName = "Min average relative error (training)";72 private const string MaxTrainingAverageRelativeErrorQualityParameterName = "Max average relative error (training)";73 private const string AverageTrainingAverageRelativeErrorQualityParameterName = "Average average relative error (training)";74 private const string BestTrainingAverageRelativeErrorQualityParameterName = "Best average relative error (training)";75 76 private const string TrainingRSquaredQualityParameterName = "R² (training)";77 private const string MinTrainingRSquaredQualityParameterName = "Min R² (training)";78 private const string MaxTrainingRSquaredQualityParameterName = "Max R² (training)";79 private const string AverageTrainingRSquaredQualityParameterName = "Average R² (training)";80 private const string BestTrainingRSquaredQualityParameterName = "Best R² (training)";81 82 private const string TestMeanSquaredErrorQualityParameterName = "Mean squared error (test)";83 private const string MinTestMeanSquaredErrorQualityParameterName = "Min mean squared error (test)";84 private const string MaxTestMeanSquaredErrorQualityParameterName = "Max mean squared error (test)";85 private const string AverageTestMeanSquaredErrorQualityParameterName = "Average mean squared error (test)";86 private const string BestTestMeanSquaredErrorQualityParameterName = "Best mean squared error (test)";87 88 private const string TestAverageRelativeErrorQualityParameterName = "Average relative error (test)";89 private const string MinTestAverageRelativeErrorQualityParameterName = "Min average relative error (test)";90 private const string MaxTestAverageRelativeErrorQualityParameterName = "Max average relative error (test)";91 private const string AverageTestAverageRelativeErrorQualityParameterName = "Average average relative error (test)";92 private const string BestTestAverageRelativeErrorQualityParameterName = "Best average relative error (test)";93 94 private const string TestRSquaredQualityParameterName = "R² (test)";95 private const string MinTestRSquaredQualityParameterName = "Min R² (test)";96 private const string MaxTestRSquaredQualityParameterName = "Max R² (test)";97 private const string AverageTestRSquaredQualityParameterName = "Average R² (test)";98 private const string BestTestRSquaredQualityParameterName = "Best R² (test)";99 100 private const string RSquaredValuesParameterName = "R²";101 private const string MeanSquaredErrorValuesParameterName = "Mean squared error";102 private const string RelativeErrorValuesParameterName = "Average relative error";103 54 104 55 #region parameter properties 105 public ILookupParameter<IRandom> RandomParameter {106 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }107 }108 public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {109 get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }110 }111 public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {112 get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }113 }114 public ILookupParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {115 get { return (ILookupParameter<ISymbolicRegressionEvaluator>)Parameters[EvaluatorParameterName]; }116 }117 56 public ILookupParameter<BoolValue> MaximizationParameter { 118 57 get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; } 119 58 } 120 public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter { 121 get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; } 122 } 123 public IValueLookupParameter<IntValue> ValidationSamplesStartParameter { 124 get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesStartParameterName]; } 125 } 126 public IValueLookupParameter<IntValue> ValidationSamplesEndParameter { 127 get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesEndParameterName]; } 128 } 129 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter { 130 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; } 131 } 132 133 public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter { 134 get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; } 135 } 136 public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter { 137 get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; } 59 public IValueParameter<BoolValue> CalculateSolutionComplexityParameter { 60 get { return (IValueParameter<BoolValue>)Parameters[CalculateSolutionComplexityParameterName]; } 138 61 } 139 62 public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter { 140 63 get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; } 141 64 } 142 public ILookupParameter<SymbolicRegressionSolution> BestTrainingSolutionParameter {143 get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters["BestTrainingSolution"]; }144 }145 public ScopeTreeLookupParameter<DoubleValue> QualityParameter {146 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }147 }148 public ScopeTreeLookupParameter<DoubleValue> ValidationQualityParameter {149 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["ValidationQuality"]; }150 }151 152 65 public ILookupParameter<IntValue> GenerationsParameter { 153 66 get { return (ILookupParameter<IntValue>)Parameters[GenerationsParameterName]; } … … 156 69 get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; } 157 70 } 158 public ILookupParameter<DataTable> BestSolutionQualityValuesParameter { 159 get { return (ILookupParameter<DataTable>)Parameters[BestSolutionQualityValuesParameterName]; } 71 public ILookupParameter<IntValue> BestSolutionLengthParameter { 72 get { return (ILookupParameter<IntValue>)Parameters[BestSolutionLengthParameterName]; } 73 } 74 public ILookupParameter<IntValue> BestSolutionHeightParameter { 75 get { return (ILookupParameter<IntValue>)Parameters[BestSolutionHeightParameterName]; } 160 76 } 161 77 public ILookupParameter<ResultCollection> ResultsParameter { … … 165 81 get { return (ILookupParameter<DoubleValue>)Parameters[BestKnownQualityParameterName]; } 166 82 } 167 public ILookupParameter<DoubleValue> CurrentBestValidationQualityParameter {168 get { return (ILookupParameter<DoubleValue>)Parameters[CurrentBestValidationQualityParameterName]; }169 }170 171 83 public ILookupParameter<DataTable> VariableFrequenciesParameter { 172 84 get { return (ILookupParameter<DataTable>)Parameters[VariableFrequenciesParameterName]; } … … 175 87 #endregion 176 88 #region properties 177 public IRandom Random {178 get { return RandomParameter.ActualValue; }179 }180 public ItemArray<SymbolicExpressionTree> SymbolicExpressionTree {181 get { return SymbolicExpressionTreeParameter.ActualValue; }182 }183 public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {184 get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }185 }186 public ISymbolicRegressionEvaluator Evaluator {187 get { return EvaluatorParameter.ActualValue; }188 }189 89 public BoolValue Maximization { 190 90 get { return MaximizationParameter.ActualValue; } 191 91 } 192 public DataAnalysisProblemData ProblemData { 193 get { return ProblemDataParameter.ActualValue; } 194 } 195 public IntValue ValidationSamplesStart { 196 get { return ValidationSamplesStartParameter.ActualValue; } 197 } 198 public IntValue ValidationSamplesEnd { 199 get { return ValidationSamplesEndParameter.ActualValue; } 200 } 201 public PercentValue RelativeNumberOfEvaluatedSamples { 202 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; } 203 } 204 205 public DoubleValue UpperEstimationLimit { 206 get { return UpperEstimationLimitParameter.ActualValue; } 207 } 208 public DoubleValue LowerEstimationLimit { 209 get { return LowerEstimationLimitParameter.ActualValue; } 92 public BoolValue CalculateSolutionComplexity { 93 get { return CalculateSolutionComplexityParameter.Value; } 94 set { CalculateSolutionComplexityParameter.Value = value; } 210 95 } 211 96 public ResultCollection Results { … … 221 106 get { return BestSolutionQualityParameter.ActualValue; } 222 107 } 108 public IntValue BestSolutionLength { 109 get { return BestSolutionLengthParameter.ActualValue; } 110 set { BestSolutionLengthParameter.ActualValue = value; } 111 } 112 public IntValue BestSolutionHeight { 113 get { return BestSolutionHeightParameter.ActualValue; } 114 set { BestSolutionHeightParameter.ActualValue = value; } 115 } 223 116 224 117 #endregion 225 118 119 [StorableConstructor] 120 private FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(bool deserializing) : base(deserializing) { } 121 private FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { } 226 122 public FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer() 227 123 : base() { 228 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));229 Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>(EvaluatorParameterName, "The evaluator which should be used to evaluate the solution on the validation set."));230 Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));231 124 Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization.")); 232 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees.")); 233 Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution.")); 234 Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesStartParameterName, "The first index of the validation partition of the data set.")); 235 Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesEndParameterName, "The last index of the validation partition of the data set.")); 236 Parameters.Add(new ValueParameter<PercentValue>(RelativeNumberOfEvaluatedSamplesParameterName, "The relative number of samples of the dataset partition, which should be randomly chosen for evaluation between the start and end index.", new PercentValue(1))); 237 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees.")); 238 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees.")); 125 Parameters.Add(new ValueParameter<BoolValue>(CalculateSolutionComplexityParameterName, "Determines if the length and height of the validation best solution should be calculated.", new BoolValue(false))); 239 126 Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution.")); 240 Parameters.Add(new LookupParameter<SymbolicRegressionSolution>("BestTrainingSolution"));241 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));242 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ValidationQuality"));243 127 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations calculated so far.")); 244 128 Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best symbolic regression solution.")); 129 Parameters.Add(new LookupParameter<IntValue>(BestSolutionLengthParameterName, "The length of the best symbolic regression solution.")); 130 Parameters.Add(new LookupParameter<IntValue>(BestSolutionHeightParameterName, "The height of the best symbolic regression solution.")); 245 131 Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored.")); 246 132 Parameters.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameterName, "The best known (validation) quality achieved on the data set.")); 247 Parameters.Add(new LookupParameter<DoubleValue>(CurrentBestValidationQualityParameterName, "The quality of the best solution (on the validation set) of the current generation."));248 Parameters.Add(new LookupParameter<DataTable>(BestSolutionQualityValuesParameterName));249 133 Parameters.Add(new LookupParameter<DataTable>(VariableFrequenciesParameterName, "The variable frequencies table to use for the calculation of variable impacts")); 250 134 } 251 135 252 [StorableConstructor] 253 private FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(bool deserializing) : base(deserializing) { } 136 public override IDeepCloneable Clone(Cloner cloner) { 137 return new FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer(this, cloner); 138 } 254 139 255 140 [StorableHook(HookType.AfterDeserialization)] 256 141 private void AfterDeserialization() { 257 #region compatibility remove before releasing 3. 3.1258 if (!Parameters.ContainsKey( EvaluatorParameterName)) {259 Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>( EvaluatorParameterName, "The evaluator which should be used to evaluate the solution on the validation set."));142 #region compatibility remove before releasing 3.4 143 if (!Parameters.ContainsKey("Evaluator")) { 144 Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>("Evaluator", "The evaluator which should be used to evaluate the solution on the validation set.")); 260 145 } 261 146 if (!Parameters.ContainsKey(MaximizationParameterName)) { 262 147 Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization.")); 263 148 } 264 if (!Parameters.ContainsKey(BestSolutionQualityValuesParameterName)) { 265 Parameters.Add(new LookupParameter<DataTable>(BestSolutionQualityValuesParameterName)); 266 } 267 if (!Parameters.ContainsKey("BestTrainingSolution")) { 268 Parameters.Add(new LookupParameter<SymbolicRegressionSolution>("BestTrainingSolution")); 269 } 270 if (!Parameters.ContainsKey("Quality")) { 271 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality")); 272 } 273 if (!Parameters.ContainsKey("ValidationQuality")) { 274 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("ValidationQuality")); 149 if (!Parameters.ContainsKey(CalculateSolutionComplexityParameterName)) { 150 Parameters.Add(new ValueParameter<BoolValue>(CalculateSolutionComplexityParameterName, "Determines if the length and height of the validation best solution should be calculated.", new BoolValue(false))); 151 } 152 if (!Parameters.ContainsKey(BestSolutionLengthParameterName)) { 153 Parameters.Add(new LookupParameter<IntValue>(BestSolutionLengthParameterName, "The length of the best symbolic regression solution.")); 154 } 155 if (!Parameters.ContainsKey(BestSolutionHeightParameterName)) { 156 Parameters.Add(new LookupParameter<IntValue>(BestSolutionHeightParameterName, "The height of the best symbolic regression solution.")); 275 157 } 276 158 #endregion 277 159 } 278 160 279 public override IOperation Apply() { 280 ItemArray<SymbolicExpressionTree> trees = SymbolicExpressionTree; 281 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 282 283 string targetVariable = ProblemData.TargetVariable.Value; 284 285 // select a random subset of rows in the validation set 286 int validationStart = ValidationSamplesStart.Value; 287 int validationEnd = ValidationSamplesEnd.Value; 288 int seed = Random.Next(); 289 int count = (int)((validationEnd - validationStart) * RelativeNumberOfEvaluatedSamples.Value); 290 if (count == 0) count = 1; 291 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(seed, validationStart, validationEnd, count); 292 293 double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity; 294 double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity; 295 161 protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) { 296 162 double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity; 297 163 SymbolicExpressionTree bestTree = null; 298 SymbolicExpressionTree bestTrainingTree = trees[0]; 299 double bestTrainingQuality = qualities[0].Value; 300 ItemArray<DoubleValue> validationQualites = new ItemArray<DoubleValue>(qualities.Length); 164 301 165 for (int i = 0; i < trees.Length; i++) { 302 SymbolicExpressionTree tree = trees[i]; 303 double quality = Evaluator.Evaluate(SymbolicExpressionTreeInterpreter, tree, 304 lowerEstimationLimit, upperEstimationLimit, 305 ProblemData.Dataset, targetVariable, 306 rows); 307 validationQualites[i] = new DoubleValue(quality); 166 double quality = validationQuality[i]; 308 167 if ((Maximization.Value && quality > bestQuality) || 309 168 (!Maximization.Value && quality < bestQuality)) { 310 169 bestQuality = quality; 311 bestTree = tree ;170 bestTree = trees[i]; 312 171 } 313 if ((Maximization.Value && qualities[i].Value > bestTrainingQuality) || 314 (!Maximization.Value && qualities[i].Value < bestTrainingQuality)) { 315 bestTrainingQuality = qualities[i].Value; 316 bestTrainingTree = tree; 317 } 318 } 319 ValidationQualityParameter.ActualValue = validationQualites; 320 321 var scaledBestTrainingTree = GetScaledTree(bestTrainingTree); 322 323 SymbolicRegressionSolution bestTrainingSolution = new SymbolicRegressionSolution((DataAnalysisProblemData)ProblemData.Clone(), 324 new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(), scaledBestTrainingTree), 325 lowerEstimationLimit, upperEstimationLimit); 326 bestTrainingSolution.Name = "Best solution (training)"; 327 bestTrainingSolution.Description = "The solution of the population with the highest fitness"; 172 } 328 173 329 174 // if the best validation tree is better than the current best solution => update … … 333 178 (!Maximization.Value && bestQuality < BestSolutionQuality.Value); 334 179 if (newBest) { 335 var scaledTree = GetScaledTree(bestTree); 180 double lowerEstimationLimit = LowerEstimationLimit.Value; 181 double upperEstimationLimit = UpperEstimationLimit.Value; 182 string targetVariable = ProblemData.TargetVariable.Value; 183 184 // calculate scaling parameters and only for the best tree using the full training set 185 double alpha, beta; 186 SymbolicRegressionScaledMeanSquaredErrorEvaluator.Calculate(SymbolicExpressionTreeInterpreter, bestTree, 187 lowerEstimationLimit, upperEstimationLimit, 188 ProblemData.Dataset, targetVariable, 189 ProblemData.TrainingIndizes, out beta, out alpha); 190 191 // scale tree for solution 192 var scaledTree = SymbolicRegressionSolutionLinearScaler.Scale(bestTree, alpha, beta); 336 193 var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(), 337 194 scaledTree); … … 343 200 BestSolutionQualityParameter.ActualValue = new DoubleValue(bestQuality); 344 201 202 if (CalculateSolutionComplexity.Value) { 203 BestSolutionLength = new IntValue(solution.Model.SymbolicExpressionTree.Size); 204 BestSolutionHeight = new IntValue(solution.Model.SymbolicExpressionTree.Height); 205 if (!Results.ContainsKey(BestSolutionLengthParameterName)) { 206 Results.Add(new Result(BestSolutionLengthParameterName, "Length of the best solution on the validation set", new IntValue())); 207 Results.Add(new Result(BestSolutionHeightParameterName, "Height of the best solution on the validation set", new IntValue())); 208 } 209 Results[BestSolutionLengthParameterName].Value = BestSolutionLength; 210 Results[BestSolutionHeightParameterName].Value = BestSolutionHeight; 211 } 212 345 213 BestSymbolicRegressionSolutionAnalyzer.UpdateBestSolutionResults(solution, ProblemData, Results, Generations, VariableFrequencies); 346 214 } 347 348 CurrentBestValidationQualityParameter.ActualValue = new DoubleValue(bestQuality);349 215 350 216 if (!Results.ContainsKey(BestSolutionQualityValuesParameterName)) { … … 352 218 Results.Add(new Result(BestSolutionQualityParameterName, new DoubleValue())); 353 219 Results.Add(new Result(CurrentBestValidationQualityParameterName, new DoubleValue())); 354 Results.Add(new Result("Best solution (training)", bestTrainingSolution));355 220 } 356 221 Results[BestSolutionQualityParameterName].Value = new DoubleValue(BestSolutionQualityParameter.ActualValue.Value); 357 222 Results[CurrentBestValidationQualityParameterName].Value = new DoubleValue(bestQuality); 358 Results["Best solution (training)"].Value = bestTrainingSolution;359 223 360 224 DataTable validationValues = (DataTable)Results[BestSolutionQualityValuesParameterName].Value; 361 225 AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName); 362 226 AddValue(validationValues, bestQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName); 363 364 BestSolutionQualityValuesParameter.ActualValue = validationValues; 365 366 return base.Apply(); 367 } 368 369 private SymbolicExpressionTree GetScaledTree(SymbolicExpressionTree tree) { 370 // calculate scaling parameters and only for the best tree using the full training set 371 double alpha, beta; 372 int trainingStart = ProblemData.TrainingSamplesStart.Value; 373 int trainingEnd = ProblemData.TrainingSamplesEnd.Value; 374 IEnumerable<int> trainingRows = Enumerable.Range(trainingStart, trainingEnd - trainingStart); 375 IEnumerable<double> originalValues = ProblemData.Dataset.GetEnumeratedVariableValues(ProblemData.TargetVariable.Value, trainingRows); 376 IEnumerable<double> estimatedValues = SymbolicExpressionTreeInterpreter.GetSymbolicExpressionTreeValues(tree, ProblemData.Dataset, trainingRows); 377 378 SymbolicRegressionScaledMeanSquaredErrorEvaluator.CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha); 379 380 // scale tree for solution 381 return SymbolicRegressionSolutionLinearScaler.Scale(tree, alpha, beta); 382 } 383 384 [StorableHook(HookType.AfterDeserialization)] 385 private void Initialize() { } 227 } 386 228 387 229 private static void AddValue(DataTable table, double data, string name, string description) { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionModelQualityAnalyzer.cs
r4068 r5275 24 24 using System.Linq; 25 25 using HeuristicLab.Analysis; 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Data; … … 118 119 #endregion 119 120 121 [StorableConstructor] 122 private SymbolicRegressionModelQualityAnalyzer(bool deserializing) : base(deserializing) { } 123 private SymbolicRegressionModelQualityAnalyzer(SymbolicRegressionModelQualityAnalyzer original, Cloner cloner) : base(original, cloner) { } 120 124 public SymbolicRegressionModelQualityAnalyzer() 121 125 : base() { … … 131 135 } 132 136 133 [StorableConstructor] 134 private SymbolicRegressionModelQualityAnalyzer(bool deserializing) : base() { } 137 public override IDeepCloneable Clone(Cloner cloner) { 138 return new SymbolicRegressionModelQualityAnalyzer(this, cloner); 139 } 135 140 136 141 public override IOperation Apply() { 137 142 Analyze(SymbolicExpressionTreeParameter.ActualValue, SymbolicExpressionTreeInterpreterParameter.ActualValue, 138 143 UpperEstimationLimit.Value, LowerEstimationLimit.Value, ProblemDataParameter.ActualValue, 139 ProblemDataParameter.ActualValue.TrainingSamplesStart.Value, ProblemDataParameter.ActualValue.TrainingSamplesEnd.Value,140 ProblemDataParameter.ActualValue.TestSamplesStart.Value, ProblemDataParameter.ActualValue.TestSamplesEnd.Value,141 144 ResultsParameter.ActualValue); 142 145 return base.Apply(); … … 145 148 public static void Analyze(IEnumerable<SymbolicExpressionTree> trees, ISymbolicExpressionTreeInterpreter interpreter, 146 149 double upperEstimationLimit, double lowerEstimationLimit, 147 DataAnalysisProblemData problemData, int trainingStart, int trainingEnd, int testStart, int testEnd,ResultCollection results) {150 DataAnalysisProblemData problemData, ResultCollection results) { 148 151 int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value); 149 IEnumerable<double> originalTrainingValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariableIndex, trainingStart, trainingEnd);150 IEnumerable<double> originalTestValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariableIndex, testStart, testEnd);152 IEnumerable<double> originalTrainingValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariableIndex, problemData.TrainingIndizes); 153 IEnumerable<double> originalTestValues = problemData.Dataset.GetEnumeratedVariableValues(targetVariableIndex, problemData.TestIndizes); 151 154 List<double> trainingMse = new List<double>(); 152 155 List<double> trainingR2 = new List<double>(); … … 162 165 foreach (var tree in trees) { 163 166 #region training 164 var estimatedTrainingValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, Enumerable.Range(trainingStart, trainingEnd - trainingStart));167 var estimatedTrainingValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TrainingIndizes); 165 168 mseEvaluator.Reset(); 166 169 r2Evaluator.Reset(); … … 184 187 #endregion 185 188 #region test 186 var estimatedTestValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, Enumerable.Range(testStart, testEnd - testStart));189 var estimatedTestValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, problemData.TestIndizes); 187 190 188 191 mseEvaluator.Reset(); -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionModelQualityCalculator.cs
r4068 r5275 20 20 #endregion 21 21 22 using System; 23 using HeuristicLab.Common; 22 24 using HeuristicLab.Core; 23 25 using HeuristicLab.Data; … … 35 37 [Item("SymbolicRegressionModelQualityCalculator", "An operator to calculate the quality values of a symbolic regression solution symbolic expression tree encoding.")] 36 38 [StorableClass] 39 [Obsolete("This class should not be used anymore because of performance reasons and will therefore not be updated.")] 37 40 public sealed class SymbolicRegressionModelQualityCalculator : AlgorithmOperator { 38 41 private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; … … 81 84 #endregion 82 85 86 [StorableConstructor] 87 private SymbolicRegressionModelQualityCalculator(bool deserializing) : base(deserializing) { } 88 private SymbolicRegressionModelQualityCalculator(SymbolicRegressionModelQualityCalculator original, Cloner cloner) : base(original, cloner) { } 83 89 public SymbolicRegressionModelQualityCalculator() 84 90 : base() { … … 136 142 137 143 } 144 public override IDeepCloneable Clone(Cloner cloner) { 145 return new SymbolicRegressionModelQualityCalculator(this, cloner); 146 } 138 147 } 139 148 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionSolutionLinearScaler.cs
r4068 r5275 20 20 #endregion 21 21 22 using HeuristicLab.Common; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Data; … … 53 54 } 54 55 56 [StorableConstructor] 57 private SymbolicRegressionSolutionLinearScaler(bool deserializing) : base(deserializing) { } 58 private SymbolicRegressionSolutionLinearScaler(SymbolicRegressionSolutionLinearScaler original, Cloner cloner) : base(original, cloner) { } 55 59 public SymbolicRegressionSolutionLinearScaler() 56 60 : base() { … … 59 63 Parameters.Add(new LookupParameter<DoubleValue>(AlphaParameterName, "Alpha parameter for linear transformation.")); 60 64 Parameters.Add(new LookupParameter<DoubleValue>(BetaParameterName, "Beta parameter for linear transformation.")); 65 } 66 67 public override IDeepCloneable Clone(Cloner cloner) { 68 return new SymbolicRegressionSolutionLinearScaler(this, cloner); 61 69 } 62 70 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionTournamentPruning.cs
r4350 r5275 33 33 using System; 34 34 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols; 35 using HeuristicLab.Common; 35 36 36 37 namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers { … … 192 193 [StorableConstructor] 193 194 protected SymbolicRegressionTournamentPruning(bool deserializing) : base(deserializing) { } 195 protected SymbolicRegressionTournamentPruning(SymbolicRegressionTournamentPruning original, Cloner cloner) 196 : base(original, cloner) { 197 } 194 198 public SymbolicRegressionTournamentPruning() 195 199 : base() { … … 218 222 Parameters.Add(new ValueLookupParameter<PercentValue>("RelativeNumberOfEvaluatedRows", new PercentValue(1.0))); 219 223 Parameters.Add(new ValueLookupParameter<IntValue>("MinimalTreeSize", new IntValue(15))); 224 } 225 226 public override IDeepCloneable Clone(Cloner cloner) { 227 return new SymbolicRegressionTournamentPruning(this, cloner); 220 228 } 221 229 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/SymbolicRegressionVariableFrequencyAnalyzer.cs
r4068 r5275 22 22 using System.Linq; 23 23 using HeuristicLab.Analysis; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; … … 60 61 #endregion 61 62 63 [StorableConstructor] 64 private SymbolicRegressionVariableFrequencyAnalyzer(bool deserializing) : base(deserializing) { } 65 private SymbolicRegressionVariableFrequencyAnalyzer(SymbolicRegressionVariableFrequencyAnalyzer original, Cloner cloner) : base(original, cloner) { } 62 66 public SymbolicRegressionVariableFrequencyAnalyzer() 63 67 : base() { … … 68 72 } 69 73 74 public override IDeepCloneable Clone(Cloner cloner) { 75 return new SymbolicRegressionVariableFrequencyAnalyzer(this, cloner); 76 } 77 70 78 public override IOperation Apply() { 71 79 ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue; 72 80 DataAnalysisProblemData problemData = ProblemDataParameter.ActualValue; 73 var inputVariables = problemData.InputVariables. Select(x => x.Value);81 var inputVariables = problemData.InputVariables.CheckedItems.Select(x => x.Value.Value); 74 82 ResultCollection results = ResultsParameter.ActualValue; 75 83 76 84 if (VariableFrequencies == null) { 77 85 VariableFrequencies = new DataTable("Variable frequencies", "Relative frequency of variable references aggregated over the whole population."); 86 VariableFrequencies.VisualProperties.XAxisTitle = "Generation"; 87 VariableFrequencies.VisualProperties.YAxisTitle = "Relative Variable Frequency"; 78 88 // add a data row for each input variable 79 foreach (var inputVariable in inputVariables) 80 VariableFrequencies.Rows.Add(new DataRow(inputVariable)); 89 foreach (var inputVariable in inputVariables) { 90 DataRow row = new DataRow(inputVariable); 91 row.VisualProperties.StartIndexZero = true; 92 VariableFrequencies.Rows.Add(row); 93 } 81 94 results.Add(new Result("Variable frequencies", VariableFrequencies)); 82 95 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/ValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs
r4068 r5275 39 39 [Item("ValidationBestScaledSymbolicRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic regression solution.")] 40 40 [StorableClass] 41 [Obsolete("This class should not be used anymore because of performance reasons and will therefore not be updated.")] 41 42 public sealed class ValidationBestScaledSymbolicRegressionSolutionAnalyzer : AlgorithmOperator, ISymbolicRegressionAnalyzer { 42 43 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; … … 134 135 private ResultsCollector resultsCollector; 135 136 137 [StorableConstructor] 138 private ValidationBestScaledSymbolicRegressionSolutionAnalyzer(bool deserializing) : base(deserializing) { } 139 private ValidationBestScaledSymbolicRegressionSolutionAnalyzer(ValidationBestScaledSymbolicRegressionSolutionAnalyzer original, Cloner cloner) 140 : base(original, cloner) { 141 Initialize(); 142 } 136 143 public ValidationBestScaledSymbolicRegressionSolutionAnalyzer() 137 144 : base() { … … 248 255 } 249 256 250 [StorableConstructor] 251 private ValidationBestScaledSymbolicRegressionSolutionAnalyzer(bool deserializing) : base() { } 257 public override IDeepCloneable Clone(Cloner cloner) { 258 return new ValidationBestScaledSymbolicRegressionSolutionAnalyzer(this, cloner); 259 } 252 260 253 261 [StorableHook(HookType.AfterDeserialization)] 262 private void AfterDeserialization() { 263 Initialize(); 264 } 254 265 private void Initialize() { 255 266 SymbolicExpressionTreeParameter.DepthChanged += new EventHandler(SymbolicExpressionTreeParameter_DepthChanged); 256 267 } 257 268 258 public override IDeepCloneable Clone(Cloner cloner) {259 ValidationBestScaledSymbolicRegressionSolutionAnalyzer clone = (ValidationBestScaledSymbolicRegressionSolutionAnalyzer)base.Clone(cloner);260 clone.Initialize();261 return clone;262 }263 269 264 270 private void SymbolicExpressionTreeParameter_DepthChanged(object sender, EventArgs e) { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/MultiObjectiveSymbolicRegressionEvaluator.cs
r4244 r5275 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 23 using System.Linq; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Data; … … 102 103 #endregion 103 104 105 [StorableConstructor] 106 protected MultiObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { } 107 protected MultiObjectiveSymbolicRegressionEvaluator(MultiObjectiveSymbolicRegressionEvaluator original, Cloner cloner) : base(original, cloner) { } 104 108 public MultiObjectiveSymbolicRegressionEvaluator() 105 109 : base() { … … 114 118 } 115 119 116 [StorableConstructor]117 protected MultiObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { }118 [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]119 private void AfterDeserialization() {120 }121 122 120 public override IOperation Apply() { 123 121 int seed = Random.Next(); 124 IEnumerable<int> rows = SingleObjectiveSymbolicRegressionEvaluator.GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value); 122 IEnumerable<int> rows = SingleObjectiveSymbolicRegressionEvaluator.GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value) 123 .Where(i => i < RegressionProblemData.TestSamplesStart.Value || RegressionProblemData.TestSamplesEnd.Value <= i); 125 124 double[] qualities = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, RegressionProblemData.Dataset, 126 125 RegressionProblemData.TargetVariable, rows); -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator.cs
r4166 r5275 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Data; … … 27 27 using HeuristicLab.Parameters; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Problems.DataAnalysis.Evaluators;30 29 using HeuristicLab.Problems.DataAnalysis.Symbolic; 31 30 using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; … … 34 33 [Item("MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator", "Calculates the mean squared error and the number of variables of a symbolic regression solution.")] 35 34 [StorableClass] 36 public class MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator : MultiObjectiveSymbolicRegressionEvaluator {35 public sealed class MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator : MultiObjectiveSymbolicRegressionEvaluator { 37 36 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 38 37 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; … … 54 53 } 55 54 #endregion 55 [StorableConstructor] 56 private MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { } 57 private MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator(MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator original, Cloner cloner) 58 : base(original, cloner) { 59 } 56 60 public MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator() 57 61 : base() { 58 62 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees.")); 59 63 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees.")); 64 } 65 66 public override IDeepCloneable Clone(Cloner cloner) { 67 return new MultiObjectiveSymbolicRegressionMeanSquaredErrorEvaluator(this, cloner); 60 68 } 61 69 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator.cs
r4128 r5275 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 23 using HeuristicLab.Common; 24 24 using HeuristicLab.Core; 25 25 using HeuristicLab.Data; … … 27 27 using HeuristicLab.Parameters; 28 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 using HeuristicLab.Problems.DataAnalysis.Evaluators;30 29 using HeuristicLab.Problems.DataAnalysis.Symbolic; 31 30 using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols; … … 34 33 [Item("MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator", "Calculates the correlation coefficient r² and the number of variables of a symbolic regression solution.")] 35 34 [StorableClass] 36 public class MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator : MultiObjectiveSymbolicRegressionEvaluator {35 public sealed class MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator : MultiObjectiveSymbolicRegressionEvaluator { 37 36 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 38 37 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; … … 54 53 } 55 54 #endregion 55 [StorableConstructor] 56 private MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator(bool deserializing) : base(deserializing) { } 57 private MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator(MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator original, Cloner cloner) 58 : base(original, cloner) { 59 } 56 60 public MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator() 57 61 : base() { 58 62 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper limit that should be used as cut off value for the output values of symbolic expression trees.")); 59 63 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees.")); 64 } 65 66 public override IDeepCloneable Clone(Cloner cloner) { 67 return new MultiObjectiveSymbolicRegressionPearsonsRSquaredEvaluator(this, cloner); 60 68 } 61 69 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SingleObjectiveSymbolicRegressionEvaluator.cs
r4244 r5275 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; 25 27 using HeuristicLab.Data; … … 114 116 #endregion 115 117 118 [StorableConstructor] 119 protected SingleObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { } 120 protected SingleObjectiveSymbolicRegressionEvaluator(SingleObjectiveSymbolicRegressionEvaluator original, Cloner cloner) 121 : base(original, cloner) { 122 } 116 123 public SingleObjectiveSymbolicRegressionEvaluator() 117 124 : base() { … … 128 135 } 129 136 130 [StorableConstructor] 131 protected SingleObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { } 137 132 138 [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)] 133 139 private void AfterDeserialization() { … … 140 146 public override IOperation Apply() { 141 147 int seed = Random.Next(); 142 IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value); 148 IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value) 149 .Where(i => i < RegressionProblemData.TestSamplesStart.Value || RegressionProblemData.TestSamplesEnd.Value <= i); 143 150 double quality = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, LowerEstimationLimit.Value, UpperEstimationLimit.Value, 144 151 RegressionProblemData.Dataset, -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionMeanSquaredErrorEvaluator.cs
r4195 r5275 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Parameters;28 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 28 using HeuristicLab.Problems.DataAnalysis.Evaluators; … … 35 34 public class SymbolicRegressionMeanSquaredErrorEvaluator : SingleObjectiveSymbolicRegressionEvaluator { 36 35 37 public SymbolicRegressionMeanSquaredErrorEvaluator() 38 : base() { 36 [StorableConstructor] 37 protected SymbolicRegressionMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { } 38 protected SymbolicRegressionMeanSquaredErrorEvaluator(SymbolicRegressionMeanSquaredErrorEvaluator original, Cloner cloner) 39 : base(original, cloner) { 40 } 41 public SymbolicRegressionMeanSquaredErrorEvaluator() : base() { } 42 43 public override IDeepCloneable Clone(Cloner cloner) { 44 return new SymbolicRegressionMeanSquaredErrorEvaluator(this, cloner); 39 45 } 40 46 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionPearsonsRSquaredEvaluator.cs
r4225 r5275 22 22 using System; 23 23 using System.Collections.Generic; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 26 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; 27 using HeuristicLab.Parameters;28 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 28 using HeuristicLab.Problems.DataAnalysis.Evaluators; … … 34 33 [StorableClass] 35 34 public class SymbolicRegressionPearsonsRSquaredEvaluator : SingleObjectiveSymbolicRegressionEvaluator { 36 public SymbolicRegressionPearsonsRSquaredEvaluator() 37 : base() { 35 [StorableConstructor] 36 protected SymbolicRegressionPearsonsRSquaredEvaluator(bool deserializing) : base(deserializing) { } 37 protected SymbolicRegressionPearsonsRSquaredEvaluator(SymbolicRegressionPearsonsRSquaredEvaluator original, Cloner cloner) 38 : base(original, cloner) { 38 39 } 40 public SymbolicRegressionPearsonsRSquaredEvaluator() : base() { } 39 41 42 public override IDeepCloneable Clone(Cloner cloner) { 43 return new SymbolicRegressionPearsonsRSquaredEvaluator(this, cloner); 44 } 40 45 public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) { 41 double r2= Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows);42 return r2;46 double mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows); 47 return mse; 43 48 } 44 49 … … 54 59 double original = originalEnumerator.Current; 55 60 if (double.IsNaN(estimated)) 56 return 0.0;61 estimated = upperEstimationLimit; 57 62 else 58 63 estimated = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, estimated)); -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator.cs
r4555 r5275 34 34 [Item("SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator", "Calculates the mean and the variance of the squared errors of a linearly scaled symbolic regression solution.")] 35 35 [StorableClass] 36 public class SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator {36 public sealed class SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator { 37 37 private const string QualityVarianceParameterName = "QualityVariance"; 38 38 private const string QualitySamplesParameterName = "QualitySamples"; … … 90 90 } 91 91 #endregion 92 [StorableConstructor] 93 private SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator(bool deserializing) : base(deserializing) { } 94 private SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator(SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator original, Cloner cloner) : base(original, cloner) { } 92 95 public SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator() 93 96 : base() { … … 102 105 } 103 106 107 public override IDeepCloneable Clone(Cloner cloner) { 108 return new SymbolicRegressionScaledMeanAndVarianceSquaredErrorEvaluator(this, cloner); 109 } 110 104 111 public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) { 105 112 double alpha, beta; … … 108 115 double bias, variance, covariance; 109 116 double mse; 110 if (ApplyScaling.Value) { 111 mse = Calculate(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable, rows, out beta, out alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); 112 Alpha = new DoubleValue(alpha); 113 Beta = new DoubleValue(beta); 117 if (ExecutionContext != null) { 118 if (ApplyScaling.Value) { 119 mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, out beta, out alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); 120 Alpha = new DoubleValue(alpha); 121 Beta = new DoubleValue(beta); 122 } else { 123 mse = CalculateWithScaling(interpreter, solution,lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, 1, 0, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); 124 } 125 QualityVariance = new DoubleValue(varianceSE); 126 QualitySamples = new IntValue(count); 127 DecompositionBiasParameter.ActualValue = new DoubleValue(bias / meanSE); 128 DecompositionVarianceParameter.ActualValue = new DoubleValue(variance / meanSE); 129 DecompositionCovarianceParameter.ActualValue = new DoubleValue(covariance / meanSE); 114 130 } else { 115 mse = CalculateWithScaling(interpreter, solution, LowerEstimationLimit.Value, UpperEstimationLimit.Value, dataset, targetVariable, rows, 1, 0, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); 116 } 117 QualityVariance = new DoubleValue(varianceSE); 118 QualitySamples = new IntValue(count); 119 DecompositionBiasParameter.ActualValue = new DoubleValue(bias / meanSE); 120 DecompositionVarianceParameter.ActualValue = new DoubleValue(variance / meanSE); 121 DecompositionCovarianceParameter.ActualValue = new DoubleValue(covariance / meanSE); 131 if (ApplyScalingParameter.Value != null && ApplyScalingParameter.Value.Value) 132 mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, out beta, out alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); 133 else 134 mse = CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, 1, 0, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); 135 } 136 122 137 return mse; 123 138 } … … 126 141 IEnumerable<double> originalValues = dataset.GetEnumeratedVariableValues(targetVariable, rows); 127 142 IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, dataset, rows); 128 SymbolicRegressionScaledMeanSquaredErrorEvaluator.CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha);143 CalculateScalingParameters(originalValues, estimatedValues, out beta, out alpha); 129 144 130 145 return CalculateWithScaling(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, beta, alpha, out meanSE, out varianceSE, out count, out bias, out variance, out covariance); … … 174 189 } 175 190 } 191 192 /// <summary> 193 /// Calculates linear scaling parameters in one pass. 194 /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer. 195 /// http://www.springerlink.com/content/x035121165125175/ 196 /// </summary> 197 public static void CalculateScalingParameters(IEnumerable<double> original, IEnumerable<double> estimated, out double beta, out double alpha) { 198 IEnumerator<double> originalEnumerator = original.GetEnumerator(); 199 IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator(); 200 OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator(); 201 OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator(); 202 OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator(); 203 int cnt = 0; 204 205 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { 206 double y = estimatedEnumerator.Current; 207 double t = originalEnumerator.Current; 208 if (IsValidValue(t) && IsValidValue(y)) { 209 tMeanCalculator.Add(t); 210 yVarianceCalculator.Add(y); 211 ytCovarianceEvaluator.Add(y, t); 212 213 cnt++; 214 } 215 } 216 217 if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) 218 throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match."); 219 if (cnt < 2) { 220 alpha = 0; 221 beta = 1; 222 } else { 223 if (yVarianceCalculator.Variance.IsAlmost(0.0)) 224 beta = 1; 225 else 226 beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.Variance; 227 228 alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean; 229 } 230 } 231 232 private static bool IsValidValue(double d) { 233 return !double.IsInfinity(d) && !double.IsNaN(d) && d > -1.0E07 && d < 1.0E07; // don't consider very large or very small values for scaling 234 } 176 235 } 177 236 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SymbolicRegressionScaledMeanSquaredErrorEvaluator.cs
r4555 r5275 34 34 [Item("SymbolicRegressionScaledMeanSquaredErrorEvaluator", "Calculates the mean squared error of a linearly scaled symbolic regression solution.")] 35 35 [StorableClass] 36 public class SymbolicRegressionScaledMeanSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator {36 public sealed class SymbolicRegressionScaledMeanSquaredErrorEvaluator : SymbolicRegressionMeanSquaredErrorEvaluator { 37 37 38 38 #region parameter properties … … 54 54 } 55 55 #endregion 56 [StorableConstructor] 57 private SymbolicRegressionScaledMeanSquaredErrorEvaluator(bool deserializing) : base(deserializing) { } 58 private SymbolicRegressionScaledMeanSquaredErrorEvaluator(SymbolicRegressionScaledMeanSquaredErrorEvaluator original, Cloner cloner) : base(original, cloner) { } 56 59 public SymbolicRegressionScaledMeanSquaredErrorEvaluator() 57 60 : base() { … … 60 63 } 61 64 65 public override IDeepCloneable Clone(Cloner cloner) { 66 return new SymbolicRegressionScaledMeanSquaredErrorEvaluator(this, cloner); 67 } 68 62 69 public override double Evaluate(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, Dataset dataset, string targetVariable, IEnumerable<int> rows) { 63 70 double alpha, beta; 64 71 double mse = Calculate(interpreter, solution, lowerEstimationLimit, upperEstimationLimit, dataset, targetVariable, rows, out beta, out alpha); 65 AlphaParameter.ActualValue = new DoubleValue(alpha); 66 BetaParameter.ActualValue = new DoubleValue(beta); 72 if (ExecutionContext != null) { 73 AlphaParameter.ActualValue = new DoubleValue(alpha); 74 BetaParameter.ActualValue = new DoubleValue(beta); 75 } 67 76 return mse; 68 77 } … … 100 109 } 101 110 111 /// <summary> 112 /// Calculates linear scaling parameters in one pass. 113 /// The formulas to calculate the scaling parameters were taken from Scaled Symblic Regression by Maarten Keijzer. 114 /// http://www.springerlink.com/content/x035121165125175/ 115 /// </summary> 102 116 public static void CalculateScalingParameters(IEnumerable<double> original, IEnumerable<double> estimated, out double beta, out double alpha) { 103 117 IEnumerator<double> originalEnumerator = original.GetEnumerator(); 104 118 IEnumerator<double> estimatedEnumerator = estimated.GetEnumerator(); 105 OnlineLinearScalingCalculator linearScalingCalculator = new OnlineLinearScalingCalculator(); 119 OnlineMeanAndVarianceCalculator yVarianceCalculator = new OnlineMeanAndVarianceCalculator(); 120 OnlineMeanAndVarianceCalculator tMeanCalculator = new OnlineMeanAndVarianceCalculator(); 121 OnlineCovarianceEvaluator ytCovarianceEvaluator = new OnlineCovarianceEvaluator(); 122 int cnt = 0; 106 123 107 124 while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) { … … 109 126 double t = originalEnumerator.Current; 110 127 if (IsValidValue(t) && IsValidValue(y)) { 111 linearScalingCalculator.Add(t, y); 128 tMeanCalculator.Add(t); 129 yVarianceCalculator.Add(y); 130 ytCovarianceEvaluator.Add(y, t); 131 132 cnt++; 112 133 } 113 134 } … … 115 136 if (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext()) 116 137 throw new ArgumentException("Number of elements in original and estimated enumeration doesn't match."); 117 else { 118 beta = linearScalingCalculator.Beta; 119 alpha = linearScalingCalculator.Alpha; 138 if (cnt < 2) { 139 alpha = 0; 140 beta = 1; 141 } else { 142 if (yVarianceCalculator.PopulationVariance.IsAlmost(0.0)) 143 beta = 1; 144 else 145 beta = ytCovarianceEvaluator.Covariance / yVarianceCalculator.PopulationVariance; 146 147 alpha = tMeanCalculator.Mean - beta * yVarianceCalculator.Mean; 120 148 } 121 149 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Interfaces/ISymbolicRegressionEvaluator.cs
r4195 r5275 20 20 #endregion 21 21 22 using System.Collections.Generic; 22 23 using HeuristicLab.Core; 23 24 using HeuristicLab.Data; … … 25 26 using HeuristicLab.Optimization; 26 27 using HeuristicLab.Problems.DataAnalysis.Symbolic; 27 using System.Collections.Generic;28 28 29 29 namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/MultiObjectiveSymbolicRegressionProblem.cs
r4166 r5275 21 21 22 22 using System; 23 using System.Collections.Generic;24 using System.Linq;25 23 using HeuristicLab.Common; 26 24 using HeuristicLab.Core; 27 25 using HeuristicLab.Data; 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;29 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;30 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;31 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;32 26 using HeuristicLab.Optimization; 33 27 using HeuristicLab.Parameters; 34 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 35 using HeuristicLab.PluginInfrastructure;36 using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;37 using HeuristicLab.Problems.DataAnalysis.Symbolic;38 29 39 30 namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic { … … 71 62 #endregion 72 63 73 74 64 [StorableConstructor] 75 65 protected MultiObjectiveSymbolicRegressionProblem(bool deserializing) : base(deserializing) { } 66 protected MultiObjectiveSymbolicRegressionProblem(MultiObjectiveSymbolicRegressionProblem original, Cloner cloner) 67 : base(original, cloner) { 68 RegisterParameterEvents(); 69 RegisterParameterValueEvents(); 70 } 76 71 public MultiObjectiveSymbolicRegressionProblem() 77 72 : base() { … … 89 84 90 85 public override IDeepCloneable Clone(Cloner cloner) { 91 MultiObjectiveSymbolicRegressionProblem clone = (MultiObjectiveSymbolicRegressionProblem)base.Clone(cloner); 92 clone.RegisterParameterEvents(); 93 clone.RegisterParameterValueEvents(); 94 return clone; 86 return new MultiObjectiveSymbolicRegressionProblem(this, cloner); 95 87 } 96 88 … … 112 104 } 113 105 114 protected virtual void OnEvaluatorChanged(EventArgs e) { 106 protected override void OnEvaluatorChanged(EventArgs e) { 107 base.OnEvaluatorChanged(e); 115 108 ParameterizeEvaluator(); 116 109 RaiseEvaluatorChanged(e); -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SimpleSymbolicRegressionEvaluator.cs
r4068 r5275 22 22 using System; 23 23 using System.Linq; 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Data; … … 33 34 [Item("SimpleSymbolicRegressionEvaluator", "Evaluates a symbolic regression solution and outputs a matrix of target and estimated values.")] 34 35 [StorableClass] 35 public class SimpleSymbolicRegressionEvaluator : SingleSuccessorOperator {36 public sealed class SimpleSymbolicRegressionEvaluator : SingleSuccessorOperator { 36 37 private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 37 38 private const string FunctionTreeParameterName = "FunctionTree"; … … 100 101 #endregion 101 102 103 [StorableConstructor] 104 private SimpleSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { } 105 private SimpleSymbolicRegressionEvaluator(SimpleSymbolicRegressionEvaluator original, Cloner cloner) : base(original, cloner) { } 102 106 public SimpleSymbolicRegressionEvaluator() 103 107 : base() { … … 110 114 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower limit that should be used as cut off value for the output values of symbolic expression trees.")); 111 115 Parameters.Add(new LookupParameter<DoubleMatrix>(ValuesParameterName, "The matrix of target and estimated values as generated by the symbolic regression solution.")); 116 } 117 118 public override IDeepCloneable Clone(Cloner cloner) { 119 return new SimpleSymbolicRegressionEvaluator(this, cloner); 112 120 } 113 121 -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionModel.cs
r4068 r5275 32 32 [StorableClass] 33 33 [Item("SymbolicRegressionModel", "A symbolic regression model represents an entity that provides estimated values based on input values.")] 34 public class SymbolicRegressionModel : NamedItem, IDataAnalysisModel { 35 private SymbolicRegressionModel() : base() { } // for cloning 34 public sealed class SymbolicRegressionModel : NamedItem, IDataAnalysisModel { 36 35 [StorableConstructor] 37 protected SymbolicRegressionModel(bool deserializing) 38 : base(deserializing) { 36 private SymbolicRegressionModel(bool deserializing) : base(deserializing) { } 37 private SymbolicRegressionModel(SymbolicRegressionModel original, Cloner cloner) 38 : base(original, cloner) { 39 tree = (SymbolicExpressionTree)cloner.Clone(original.tree); 40 interpreter = (ISymbolicExpressionTreeInterpreter)cloner.Clone(original.interpreter); 41 inputVariables = new List<string>(original.inputVariables); 39 42 } 43 40 44 public SymbolicRegressionModel(ISymbolicExpressionTreeInterpreter interpreter, SymbolicExpressionTree tree) 41 45 : base() { … … 43 47 this.interpreter = interpreter; 44 48 this.inputVariables = tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(var => var.VariableName).Distinct().ToList(); 49 } 50 51 public override IDeepCloneable Clone(Cloner cloner) { 52 return new SymbolicRegressionModel(this, cloner); 53 } 54 55 [StorableHook(HookType.AfterDeserialization)] 56 private void AfterDeserialization() { 57 if (inputVariables == null) 58 this.inputVariables = tree.IterateNodesPrefix().OfType<VariableTreeNode>().Select(var => var.VariableName).Distinct().ToList(); 45 59 } 46 60 … … 62 76 63 77 public IEnumerable<double> GetEstimatedValues(DataAnalysisProblemData problemData, int start, int end) { 64 return interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, Enumerable.Range(start, end - start));78 return GetEstimatedValues(problemData, Enumerable.Range(start, end - start)); 65 79 } 66 67 public override IDeepCloneable Clone(Cloner cloner) { 68 var clone = (SymbolicRegressionModel)base.Clone(cloner); 69 clone.tree = (SymbolicExpressionTree)cloner.Clone(tree); 70 clone.interpreter = (ISymbolicExpressionTreeInterpreter)cloner.Clone(interpreter); 71 clone.inputVariables = new List<string>(inputVariables); 72 return clone; 80 public IEnumerable<double> GetEstimatedValues(DataAnalysisProblemData problemData, IEnumerable<int> rows) { 81 return interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows); 73 82 } 74 83 } -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs
r4341 r5275 34 34 [Creatable("Problems")] 35 35 [StorableClass] 36 public class SymbolicRegressionProblem : SymbolicRegressionProblemBase, ISingleObjectiveProblem {36 public sealed class SymbolicRegressionProblem : SymbolicRegressionProblemBase, ISingleObjectiveDataAnalysisProblem { 37 37 38 38 #region Parameter Properties … … 74 74 75 75 [StorableConstructor] 76 protected SymbolicRegressionProblem(bool deserializing) : base(deserializing) { } 76 private SymbolicRegressionProblem(bool deserializing) : base(deserializing) { } 77 private SymbolicRegressionProblem(SymbolicRegressionProblem original, Cloner cloner) 78 : base(original, cloner) { 79 RegisterParameterEvents(); 80 RegisterParameterValueEvents(); 81 } 82 77 83 public SymbolicRegressionProblem() 78 84 : base() { … … 81 87 Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator)); 82 88 Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem.")); 83 84 evaluator.QualityParameter.ActualName = "TrainingPearsonR2";85 89 86 90 InitializeOperators(); … … 92 96 93 97 public override IDeepCloneable Clone(Cloner cloner) { 94 SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner); 95 clone.RegisterParameterEvents(); 96 clone.RegisterParameterValueEvents(); 97 return clone; 98 return new SymbolicRegressionProblem(this, cloner); 98 99 } 99 100 … … 146 147 private void InitializeOperators() { 147 148 AddOperator(new FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer()); 149 AddOperator(new SymbolicRegressionOverfittingAnalyzer()); 148 150 ParameterizeAnalyzers(); 149 151 } … … 159 161 foreach (var analyzer in Analyzers) { 160 162 analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 163 var validationSolutionAnalyzer = analyzer as SymbolicRegressionValidationAnalyzer; 164 if (validationSolutionAnalyzer != null) { 165 validationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name; 166 validationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name; 167 validationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name; 168 validationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name; 169 validationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName; 170 validationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart; 171 validationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd; 172 } 173 161 174 var fixedBestValidationSolutionAnalyzer = analyzer as FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer; 162 175 if (fixedBestValidationSolutionAnalyzer != null) { 163 fixedBestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;164 fixedBestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;165 fixedBestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;166 fixedBestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;167 fixedBestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;168 fixedBestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;169 fixedBestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;170 176 fixedBestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name; 171 177 } 178 172 179 var bestValidationSolutionAnalyzer = analyzer as ValidationBestScaledSymbolicRegressionSolutionAnalyzer; 173 180 if (bestValidationSolutionAnalyzer != null) { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblemBase.cs
r4341 r5275 125 125 } 126 126 public IntValue TrainingSamplesStart { 127 get { return new IntValue(DataAnalysisProblemData.Training SamplesStart.Value); }127 get { return new IntValue(DataAnalysisProblemData.TrainingIndizes.First()); } 128 128 } 129 129 public IntValue TrainingSamplesEnd { 130 130 get { 131 return new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + 132 DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2); 131 int endIndex = (int)(DataAnalysisProblemData.TrainingIndizes.Count() * (1.0 - DataAnalysisProblemData.ValidationPercentage.Value) - 1); 132 if (endIndex < 0) endIndex = 0; 133 return new IntValue(DataAnalysisProblemData.TrainingIndizes.ElementAt(endIndex)); 133 134 } 134 135 } … … 137 138 } 138 139 public IntValue ValidationSamplesEnd { 139 get { return new IntValue(DataAnalysisProblemData.Training SamplesEnd.Value); }140 get { return new IntValue(DataAnalysisProblemData.TrainingIndizes.Last() + 1); } 140 141 } 141 142 public IntValue TestSamplesStart { … … 152 153 [StorableConstructor] 153 154 protected SymbolicRegressionProblemBase(bool deserializing) : base(deserializing) { } 155 protected SymbolicRegressionProblemBase(SymbolicRegressionProblemBase original, Cloner cloner) 156 : base(original, cloner) { 157 operators = original.operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 158 RegisterParameterValueEvents(); 159 RegisterParameterEvents(); 160 } 154 161 public SymbolicRegressionProblemBase() 155 162 : base() { … … 164 171 Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar)); 165 172 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100))); 166 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression. ", new IntValue(10)));173 Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression. The minimum depth needed for the algorithm is 3 because two levels are reserved for the ProgramRoot and the Start symbol.", new IntValue(10))); 167 174 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly())); 168 175 Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly())); 169 170 creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";171 176 172 177 ParameterizeSolutionCreator(); … … 175 180 UpdateEstimationLimits(); 176 181 InitializeOperators(); 182 RegisterParameterValueEvents(); 177 183 RegisterParameterEvents(); 178 RegisterParameterValueEvents(); 179 } 180 181 public override IDeepCloneable Clone(Cloner cloner) { 182 SymbolicRegressionProblemBase clone = (SymbolicRegressionProblemBase)base.Clone(cloner); 183 clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList(); 184 clone.RegisterParameterEvents(); 185 clone.RegisterParameterValueEvents(); 186 return clone; 187 } 188 189 private void RegisterParameterValueEvents() { 184 } 185 186 private void RegisterParameterEvents() { 190 187 MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged); 191 188 MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged); 189 MaxExpressionDepthParameter.ValueChanged += new EventHandler(MaxExpressionDepthParameter_ValueChanged); 192 190 SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged); 193 191 FunctionTreeGrammarParameter.ValueChanged += new EventHandler(FunctionTreeGrammarParameter_ValueChanged); 194 } 195 196 private void RegisterParameterEvents() { 192 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged); 193 } 194 195 private void RegisterParameterValueEvents() { 197 196 MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged); 198 197 MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged); 199 SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);198 MaxExpressionDepthParameter.Value.ValueChanged += new EventHandler(MaxExpressionDepthParameterValue_ValueChanged); 200 199 } 201 200 … … 253 252 OnArchitectureParameterChanged(e); 254 253 } 254 255 private void MaxExpressionDepthParameter_ValueChanged(object sender, EventArgs e) { 256 MaxExpressionDepthParameterValue_ValueChanged(sender, e); 257 MaxExpressionDepthParameter.Value.ValueChanged += MaxExpressionDepthParameterValue_ValueChanged; 258 } 259 private void MaxExpressionDepthParameterValue_ValueChanged(object sender, EventArgs e) { 260 if (MaxExpressionDepth != null && MaxExpressionDepth.Value < 3) 261 MaxExpressionDepth.Value = 3; 262 } 255 263 #endregion 256 264 … … 262 270 if (operators == null) InitializeOperators(); 263 271 #endregion 272 RegisterParameterValueEvents(); 264 273 RegisterParameterEvents(); 265 RegisterParameterValueEvents();266 274 } 267 275 … … 325 333 varFreqAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name; 326 334 } 327 var pruningOperator = analyzer as SymbolicRegressionTournamentPruning;328 if (pruningOperator != null) {329 pruningOperator.SamplesStartParameter.Value = TrainingSamplesStart;330 pruningOperator.SamplesEndParameter.Value = TrainingSamplesEnd;331 pruningOperator.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;332 pruningOperator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;333 pruningOperator.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;334 pruningOperator.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;335 pruningOperator.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;336 }337 335 } 338 336 foreach (ISymbolicExpressionTreeAnalyzer analyzer in Operators.OfType<ISymbolicExpressionTreeAnalyzer>()) { -
branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionSolution.cs
r5010 r5275 24 24 using System.Drawing; 25 25 using System.Linq; 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 34 35 [Item("SymbolicRegressionSolution", "Represents a solution for a symbolic regression problem which can be visualized in the GUI.")] 35 36 [StorableClass] 36 public sealed class SymbolicRegressionSolution : DataAnalysisSolution { 37 public SymbolicRegressionSolution() : base() { } // for cloning 38 [StorableConstructor] 39 public SymbolicRegressionSolution(bool deserializing) : base(deserializing) { } 40 public SymbolicRegressionSolution(DataAnalysisProblemData problemData, SymbolicRegressionModel model, double lowerEstimationLimit, double upperEstimationLimit) 41 : base(problemData, lowerEstimationLimit, upperEstimationLimit) { 42 this.Model = model; 43 } 44 37 public class SymbolicRegressionSolution : DataAnalysisSolution { 45 38 public override Image ItemImage { 46 39 get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; } … … 50 43 get { return (SymbolicRegressionModel)base.Model; } 51 44 set { base.Model = value; } 45 } 46 47 protected List<double> estimatedValues; 48 public override IEnumerable<double> EstimatedValues { 49 get { 50 if (estimatedValues == null) RecalculateEstimatedValues(); 51 return estimatedValues; 52 } 53 } 54 55 public override IEnumerable<double> EstimatedTrainingValues { 56 get { return GetEstimatedValues(ProblemData.TrainingIndizes); } 57 } 58 59 public override IEnumerable<double> EstimatedTestValues { 60 get { return GetEstimatedValues(ProblemData.TestIndizes); } 61 } 62 63 [StorableConstructor] 64 protected SymbolicRegressionSolution(bool deserializing) : base(deserializing) { } 65 protected SymbolicRegressionSolution(SymbolicRegressionSolution original, Cloner cloner) 66 : base(original, cloner) { 67 } 68 public SymbolicRegressionSolution(DataAnalysisProblemData problemData, SymbolicRegressionModel model, double lowerEstimationLimit, double upperEstimationLimit) 69 : base(problemData, lowerEstimationLimit, upperEstimationLimit) { 70 this.Model = model; 71 } 72 73 public override IDeepCloneable Clone(Cloner cloner) { 74 return new SymbolicRegressionSolution(this, cloner); 52 75 } 53 76 … … 65 88 } 66 89 67 private List<double> estimatedValues; 68 public override IEnumerable<double> EstimatedValues { 69 get { 70 if (estimatedValues == null) RecalculateEstimatedValues(); 71 return estimatedValues.AsEnumerable(); 72 } 73 } 74 75 public override IEnumerable<double> EstimatedTrainingValues { 76 get { 77 if (estimatedValues == null) RecalculateEstimatedValues(); 78 int start = ProblemData.TrainingSamplesStart.Value; 79 int n = ProblemData.TrainingSamplesEnd.Value - start; 80 return estimatedValues.Skip(start).Take(n).ToList(); 81 } 82 } 83 84 public override IEnumerable<double> EstimatedTestValues { 85 get { 86 if (estimatedValues == null) RecalculateEstimatedValues(); 87 int start = ProblemData.TestSamplesStart.Value; 88 int n = ProblemData.TestSamplesEnd.Value - start; 89 return estimatedValues.Skip(start).Take(n).ToList(); 90 } 90 public virtual IEnumerable<double> GetEstimatedValues(IEnumerable<int> rows) { 91 if (estimatedValues == null) RecalculateEstimatedValues(); 92 foreach (int row in rows) 93 yield return estimatedValues[row]; 91 94 } 92 95 }
Note: See TracChangeset
for help on using the changeset viewer.