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

Last change on this file since 4246 was 4246, checked in by gkronber, 9 years ago

Merged r4244 into trunk. #1082

File size: 9.2 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 HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Symbolic;
31
32namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
33  [Item("SingleObjectiveSymbolicRegressionEvaluator", "Evaluates a symbolic regression solution.")]
34  [StorableClass]
35  public abstract class SingleObjectiveSymbolicRegressionEvaluator : SingleSuccessorOperator, ISymbolicRegressionEvaluator {
36    private const string RandomParameterName = "Random";
37    private const string QualityParameterName = "Quality";
38    private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
39    private const string FunctionTreeParameterName = "FunctionTree";
40    private const string RegressionProblemDataParameterName = "RegressionProblemData";
41    private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
42    private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
43    private const string SamplesStartParameterName = "SamplesStart";
44    private const string SamplesEndParameterName = "SamplesEnd";
45    private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
46    #region ISymbolicRegressionEvaluator Members
47
48    public ILookupParameter<IRandom> RandomParameter {
49      get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
50    }
51    public ILookupParameter<DoubleValue> QualityParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
53    }
54
55    public ILookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
56      get { return (ILookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
57    }
58
59    public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
60      get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[FunctionTreeParameterName]; }
61    }
62
63    public ILookupParameter<DataAnalysisProblemData> RegressionProblemDataParameter {
64      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[RegressionProblemDataParameterName]; }
65    }
66
67    public IValueLookupParameter<IntValue> SamplesStartParameter {
68      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
69    }
70
71    public IValueLookupParameter<IntValue> SamplesEndParameter {
72      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
73    }
74    public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
75      get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
76    }
77    public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
78      get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
79    }
80    public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
81      get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
82    }
83
84
85    #endregion
86    #region properties
87    public IRandom Random {
88      get { return RandomParameter.ActualValue; }
89    }
90    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
91      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
92    }
93    public SymbolicExpressionTree SymbolicExpressionTree {
94      get { return SymbolicExpressionTreeParameter.ActualValue; }
95    }
96    public DataAnalysisProblemData RegressionProblemData {
97      get { return RegressionProblemDataParameter.ActualValue; }
98    }
99    public IntValue SamplesStart {
100      get { return SamplesStartParameter.ActualValue; }
101    }
102    public IntValue SamplesEnd {
103      get { return SamplesEndParameter.ActualValue; }
104    }
105    public DoubleValue UpperEstimationLimit {
106      get { return UpperEstimationLimitParameter.ActualValue; }
107    }
108    public DoubleValue LowerEstimationLimit {
109      get { return LowerEstimationLimitParameter.ActualValue; }
110    }
111    public PercentValue RelativeNumberOfEvaluatedSamples {
112      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
113    }
114    #endregion
115
116    public SingleObjectiveSymbolicRegressionEvaluator()
117      : base() {
118      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
119      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the evaluated symbolic regression solution."));
120      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
121      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(FunctionTreeParameterName, "The symbolic regression solution encoded as a symbolic expression tree."));
122      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(RegressionProblemDataParameterName, "The problem data on which the symbolic regression solution should be evaluated."));
123      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The start index of the dataset partition on which the symbolic regression solution should be evaluated."));
124      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The end index of the dataset partition on which the symbolic regression solution should be evaluated."));
125      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."));
126      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."));
127      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)));
128    }
129
130    [StorableConstructor]
131    protected SingleObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { }
132    [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
133    private void AfterDeserialization() {
134      if (!Parameters.ContainsKey(RelativeNumberOfEvaluatedSamplesParameterName))
135        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)));
136      if (!Parameters.ContainsKey(RandomParameterName))
137        Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
138    }
139
140    public override IOperation Apply() {
141      int seed = Random.Next();
142      IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value);
143      double quality = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, LowerEstimationLimit.Value, UpperEstimationLimit.Value,
144        RegressionProblemData.Dataset,
145        RegressionProblemData.TargetVariable.Value, rows);
146      QualityParameter.ActualValue = new DoubleValue(quality);
147      return base.Apply();
148    }
149
150
151    internal static IEnumerable<int> GenerateRowsToEvaluate(int seed, double relativeAmount, int start, int end) {
152      if (end < start) throw new ArgumentException("Start value is larger than end value.");
153      int count = (int)((end - start) * relativeAmount);
154      if (count == 0) count = 1;
155      return RandomEnumerable.SampleRandomNumbers(seed, start, end, count);
156    }
157
158    public abstract double Evaluate(ISymbolicExpressionTreeInterpreter interpreter,
159      SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit,
160      Dataset dataset,
161      string targetVariable,
162      IEnumerable<int> rows);
163  }
164}
Note: See TracBrowser for help on using the repository browser.