Changeset 5275 for branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers
- Timestamp:
- 01/11/11 15:03:46 (14 years ago)
- Location:
- branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers
- Files:
-
- 3 added
- 8 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) {
Note: See TracChangeset
for help on using the changeset viewer.