Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SingleObjectiveSymbolicRegressionEvaluator.cs @ 15034

Last change on this file since 15034 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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