source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SingleObjectiveSymbolicRegressionEvaluator.cs @ 4468

Last change on this file since 4468 was 4468, checked in by mkommend, 9 years ago

Preparation for cross validation - removed the test samples from the trainining samples and added ValidationPercentage parameter (ticket #1199).

File size: 9.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32
33namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
34  [Item("SingleObjectiveSymbolicRegressionEvaluator", "Evaluates a symbolic regression solution.")]
35  [StorableClass]
36  public abstract class SingleObjectiveSymbolicRegressionEvaluator : SingleSuccessorOperator, ISymbolicRegressionEvaluator {
37    private const string RandomParameterName = "Random";
38    private const string QualityParameterName = "Quality";
39    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
40    private const string FunctionTreeParameterName = "FunctionTree";
41    private const string RegressionProblemDataParameterName = "RegressionProblemData";
42    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
43    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
44    private const string SamplesStartParameterName = "SamplesStart";
45    private const string SamplesEndParameterName = "SamplesEnd";
46    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
47    #region ISymbolicRegressionEvaluator Members
48
49    public ILookupParameter<IRandom> RandomParameter {
50      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
51    }
52    public ILookupParameter<DoubleValue> QualityParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
54    }
55
56    public ILookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
57      get { return (ILookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
58    }
59
60    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
61      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[FunctionTreeParameterName]; }
62    }
63
64    public ILookupParameter<DataAnalysisProblemData> RegressionProblemDataParameter {
65      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[RegressionProblemDataParameterName]; }
66    }
67
68    public IValueLookupParameter<IntValue> SamplesStartParameter {
69      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
70    }
71
72    public IValueLookupParameter<IntValue> SamplesEndParameter {
73      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
74    }
75    public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
76      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
77    }
78    public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
79      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
80    }
81    public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
82      get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
83    }
84
85
86    #endregion
87    #region properties
88    public IRandom Random {
89      get { return RandomParameter.ActualValue; }
90    }
91    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
92      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
93    }
94    public SymbolicExpressionTree SymbolicExpressionTree {
95      get { return SymbolicExpressionTreeParameter.ActualValue; }
96    }
97    public DataAnalysisProblemData RegressionProblemData {
98      get { return RegressionProblemDataParameter.ActualValue; }
99    }
100    public IntValue SamplesStart {
101      get { return SamplesStartParameter.ActualValue; }
102    }
103    public IntValue SamplesEnd {
104      get { return SamplesEndParameter.ActualValue; }
105    }
106    public DoubleValue UpperEstimationLimit {
107      get { return UpperEstimationLimitParameter.ActualValue; }
108    }
109    public DoubleValue LowerEstimationLimit {
110      get { return LowerEstimationLimitParameter.ActualValue; }
111    }
112    public PercentValue RelativeNumberOfEvaluatedSamples {
113      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
114    }
115    #endregion
116
117    public SingleObjectiveSymbolicRegressionEvaluator()
118      : base() {
119      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
120      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the evaluated symbolic regression solution."));
121      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
122      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(FunctionTreeParameterName, "The symbolic regression solution encoded as a symbolic expression tree."));
123      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(RegressionProblemDataParameterName, "The problem data on which the symbolic regression solution should be evaluated."));
124      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The start index of the dataset partition on which the symbolic regression solution should be evaluated."));
125      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The end index of the dataset partition on which the symbolic regression solution should be evaluated."));
126      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."));
127      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."));
128      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)));
129    }
130
131    [StorableConstructor]
132    protected SingleObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { }
133    [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
134    private void AfterDeserialization() {
135      if (!Parameters.ContainsKey(RelativeNumberOfEvaluatedSamplesParameterName))
136        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)));
137      if (!Parameters.ContainsKey(RandomParameterName))
138        Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
139    }
140
141    public override IOperation Apply() {
142      int seed = Random.Next();
143      IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value)
144          .Where(i => i < RegressionProblemData.TestSamplesStart.Value || RegressionProblemData.TestSamplesEnd.Value <= i);
145      double quality = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, LowerEstimationLimit.Value, UpperEstimationLimit.Value,
146        RegressionProblemData.Dataset,
147        RegressionProblemData.TargetVariable.Value, rows);
148      QualityParameter.ActualValue = new DoubleValue(quality);
149      return base.Apply();
150    }
151
152
153    internal static IEnumerable<int> GenerateRowsToEvaluate(int seed, double relativeAmount, int start, int end) {
154      if (end < start) throw new ArgumentException("Start value is larger than end value.");
155      int count = (int)((end - start) * relativeAmount);
156      if (count == 0) count = 1;
157      return RandomEnumerable.SampleRandomNumbers(seed, start, end, count);
158    }
159
160    public abstract double Evaluate(ISymbolicExpressionTreeInterpreter interpreter,
161      SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit,
162      Dataset dataset,
163      string targetVariable,
164      IEnumerable<int> rows);
165  }
166}
Note: See TracBrowser for help on using the repository browser.