Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5198


Ignore:
Timestamp:
01/03/11 15:05:36 (14 years ago)
Author:
gkronber
Message:

Changed FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer to extend from SymbolicRegressionValidationAnalyzer. #1356

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer.cs

    r4889 r5198  
    3939  [Item("FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer", "An operator that analyzes the validation best scaled symbolic regression solution.")]
    4040  [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 {
    5242    private const string MaximizationParameterName = "Maximization";
    5343    private const string BestSolutionParameterName = "Best solution (validation)";
     
    5949    private const string BestKnownQualityParameterName = "BestKnownQuality";
    6050    private const string GenerationsParameterName = "Generations";
    61     private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
    6251
    6352    #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     }
    7653    public ILookupParameter<BoolValue> MaximizationParameter {
    7754      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]; }
    9755    }
    9856    public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
     
    11775    #endregion
    11876    #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     }
    13177    public BoolValue Maximization {
    13278      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; }
    15279    }
    15380    public ResultCollection Results {
     
    17198    public FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer()
    17299      : 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."));
    176100      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."));
    184101      Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
    185102      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations calculated so far."));
     
    197114    private void AfterDeserialization() {
    198115      #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."));
    201118      }
    202119      if (!Parameters.ContainsKey(MaximizationParameterName)) {
     
    205122      #endregion
    206123    }
    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) {
    225126      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
    226127      SymbolicExpressionTree bestTree = null;
    227128
    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];
    234131        if ((Maximization.Value && quality > bestQuality) ||
    235132            (!Maximization.Value && quality < bestQuality)) {
    236133          bestQuality = quality;
    237           bestTree = tree;
     134          bestTree = trees[i];
    238135        }
    239136      }
     
    245142        (!Maximization.Value && bestQuality < BestSolutionQuality.Value);
    246143      if (newBest) {
     144        double lowerEstimationLimit = LowerEstimationLimit.Value;
     145        double upperEstimationLimit = UpperEstimationLimit.Value;
     146        string targetVariable = ProblemData.TargetVariable.Value;
     147
    247148        // calculate scaling parameters and only for the best tree using the full training set
    248149        double alpha, beta;
     
    278179      AddValue(validationValues, BestSolutionQualityParameter.ActualValue.Value, BestSolutionQualityParameterName, BestSolutionQualityParameterName);
    279180      AddValue(validationValues, bestQuality, CurrentBestValidationQualityParameterName, CurrentBestValidationQualityParameterName);
    280       return base.Apply();
    281181    }
    282 
    283     [StorableHook(HookType.AfterDeserialization)]
    284     private void Initialize() { }
    285182
    286183    private static void AddValue(DataTable table, double data, string name, string description) {
Note: See TracChangeset for help on using the changeset viewer.