Changeset 5198 for trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers
- Timestamp:
- 01/03/11 15:05:36 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs
r4889 r5198 39 39 [Item("FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic regression solution.")] 40 40 [StorableClass] 41 public sealed class FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer : SingleSuccessorOperator, ISymbolicRegressionAnalyzer { 42 private const string RandomParameterName = "Random"; 43 private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree"; 44 private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter"; 45 private const string ProblemDataParameterName = "ProblemData"; 46 private const string ValidationSamplesStartParameterName = "SamplesStart"; 47 private const string ValidationSamplesEndParameterName = "SamplesEnd"; 48 // private const string QualityParameterName = "Quality"; 49 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 50 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 51 private const string EvaluatorParameterName = "Evaluator"; 41 public sealed class FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer : SymbolicRegressionValidationAnalyzer, ISymbolicRegressionAnalyzer { 52 42 private const string MaximizationParameterName = "Maximization"; 53 43 private const string BestSolutionParameterName = "Best solution (validation)"; … … 59 49 private const string BestKnownQualityParameterName = "BestKnownQuality"; 60 50 private const string GenerationsParameterName = "Generations"; 61 private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";62 51 63 52 #region parameter properties 64 public ILookupParameter<IRandom> RandomParameter {65 get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }66 }67 public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {68 get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }69 }70 public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {71 get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }72 }73 public ILookupParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {74 get { return (ILookupParameter<ISymbolicRegressionEvaluator>)Parameters[EvaluatorParameterName]; }75 }76 53 public ILookupParameter<BoolValue> MaximizationParameter { 77 54 get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; } 78 }79 public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {80 get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }81 }82 public IValueLookupParameter<IntValue> ValidationSamplesStartParameter {83 get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesStartParameterName]; }84 }85 public IValueLookupParameter<IntValue> ValidationSamplesEndParameter {86 get { return (IValueLookupParameter<IntValue>)Parameters[ValidationSamplesEndParameterName]; }87 }88 public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {89 get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }90 }91 92 public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {93 get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }94 }95 public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {96 get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }97 55 } 98 56 public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter { … … 117 75 #endregion 118 76 #region properties 119 public IRandom Random {120 get { return RandomParameter.ActualValue; }121 }122 public ItemArray<SymbolicExpressionTree> SymbolicExpressionTree {123 get { return SymbolicExpressionTreeParameter.ActualValue; }124 }125 public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {126 get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }127 }128 public ISymbolicRegressionEvaluator Evaluator {129 get { return EvaluatorParameter.ActualValue; }130 }131 77 public BoolValue Maximization { 132 78 get { return MaximizationParameter.ActualValue; } 133 }134 public DataAnalysisProblemData ProblemData {135 get { return ProblemDataParameter.ActualValue; }136 }137 public IntValue ValidiationSamplesStart {138 get { return ValidationSamplesStartParameter.ActualValue; }139 }140 public IntValue ValidationSamplesEnd {141 get { return ValidationSamplesEndParameter.ActualValue; }142 }143 public PercentValue RelativeNumberOfEvaluatedSamples {144 get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }145 }146 147 public DoubleValue UpperEstimationLimit {148 get { return UpperEstimationLimitParameter.ActualValue; }149 }150 public DoubleValue LowerEstimationLimit {151 get { return LowerEstimationLimitParameter.ActualValue; }152 79 } 153 80 public ResultCollection Results { … … 171 98 public FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer() 172 99 : base() { 173 Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));174 Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>(EvaluatorParameterName, "The evaluator which should be used to evaluate the solution on the validation set."));175 Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));176 100 Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization.")); 177 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));178 Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));179 Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesStartParameterName, "The first index of the validation partition of the data set."));180 Parameters.Add(new ValueLookupParameter<IntValue>(ValidationSamplesEndParameterName, "The last index of the validation partition of the data set."));181 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)));182 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));183 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));184 101 Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution.")); 185 102 Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations calculated so far.")); … … 197 114 private void AfterDeserialization() { 198 115 #region compatibility remove before releasing 3.4 199 if (!Parameters.ContainsKey( EvaluatorParameterName)) {200 Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>( EvaluatorParameterName, "The evaluator which should be used to evaluate the solution on the validation set."));116 if (!Parameters.ContainsKey("Evaluator")) { 117 Parameters.Add(new LookupParameter<ISymbolicRegressionEvaluator>("Evaluator", "The evaluator which should be used to evaluate the solution on the validation set.")); 201 118 } 202 119 if (!Parameters.ContainsKey(MaximizationParameterName)) { … … 205 122 #endregion 206 123 } 207 208 public override IOperation Apply() { 209 var trees = SymbolicExpressionTree; 210 211 string targetVariable = ProblemData.TargetVariable.Value; 212 213 // select a random subset of rows in the validation set 214 int validationStart = ValidiationSamplesStart.Value; 215 int validationEnd = ValidationSamplesEnd.Value; 216 int seed = Random.Next(); 217 int count = (int)((validationEnd - validationStart) * RelativeNumberOfEvaluatedSamples.Value); 218 if (count == 0) count = 1; 219 IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(seed, validationStart, validationEnd, count) 220 .Where(row => row < ProblemData.TestSamplesStart.Value || ProblemData.TestSamplesEnd.Value <= row); 221 222 double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity; 223 double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity; 224 124 125 protected override void Analyze(SymbolicExpressionTree[] trees, double[] validationQuality) { 225 126 double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity; 226 127 SymbolicExpressionTree bestTree = null; 227 128 228 foreach (var tree in trees) { 229 double quality = Evaluator.Evaluate(SymbolicExpressionTreeInterpreter, tree, 230 lowerEstimationLimit, upperEstimationLimit, 231 ProblemData.Dataset, targetVariable, 232 rows); 233 129 for(int i=0;i<trees.Length;i++) { 130 double quality = validationQuality[i]; 234 131 if ((Maximization.Value && quality > bestQuality) || 235 132 (!Maximization.Value && quality < bestQuality)) { 236 133 bestQuality = quality; 237 bestTree = tree ;134 bestTree = trees[i]; 238 135 } 239 136 } … … 245 142 (!Maximization.Value && bestQuality < BestSolutionQuality.Value); 246 143 if (newBest) { 144 double lowerEstimationLimit = LowerEstimationLimit.Value; 145 double upperEstimationLimit = UpperEstimationLimit.Value; 146 string targetVariable = ProblemData.TargetVariable.Value; 147 247 148 // calculate scaling parameters and only for the best tree using the full training set 248 149 double alpha, beta; … … 278 179 AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName); 279 180 AddValue(validationValues, bestQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName); 280 return base.Apply();281 181 } 282 283 [StorableHook(HookType.AfterDeserialization)]284 private void Initialize() { }285 182 286 183 private static void AddValue(DataTable table, double data, string name, string description) {
Note: See TracChangeset
for help on using the changeset viewer.