Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.PopulationDiversityAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Evaluators/SingleObjectiveSymbolicRegressionEvaluator.cs @ 12940

Last change on this file since 12940 was 4877, checked in by swinkler, 14 years ago

Created branch for population diversity analysis for symbolic regression. (#1278)

File size: 9.5 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.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 IRandom Random {
90      get { return RandomParameter.ActualValue; }
91    }
92    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
93      get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
94    }
95    public SymbolicExpressionTree SymbolicExpressionTree {
96      get { return SymbolicExpressionTreeParameter.ActualValue; }
97    }
98    public DataAnalysisProblemData RegressionProblemData {
99      get { return RegressionProblemDataParameter.ActualValue; }
100    }
101    public IntValue SamplesStart {
102      get { return SamplesStartParameter.ActualValue; }
103    }
104    public IntValue SamplesEnd {
105      get { return SamplesEndParameter.ActualValue; }
106    }
107    public DoubleValue UpperEstimationLimit {
108      get { return UpperEstimationLimitParameter.ActualValue; }
109    }
110    public DoubleValue LowerEstimationLimit {
111      get { return LowerEstimationLimitParameter.ActualValue; }
112    }
113    public PercentValue RelativeNumberOfEvaluatedSamples {
114      get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
115    }
116    #endregion
117
118    [StorableConstructor]
119    protected SingleObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { }
120    protected SingleObjectiveSymbolicRegressionEvaluator(SingleObjectiveSymbolicRegressionEvaluator original, Cloner cloner)
121      : base(original, cloner) {
122    }
123    public SingleObjectiveSymbolicRegressionEvaluator()
124      : base() {
125      Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
126      Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the evaluated symbolic regression solution."));
127      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
128      Parameters.Add(new LookupParameter<SymbolicExpressionTree>(FunctionTreeParameterName, "The symbolic regression solution encoded as a symbolic expression tree."));
129      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(RegressionProblemDataParameterName, "The problem data on which the symbolic regression solution should be evaluated."));
130      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The start index of the dataset partition on which the symbolic regression solution should be evaluated."));
131      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The end index of the dataset partition on which the symbolic regression solution should be evaluated."));
132      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."));
133      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."));
134      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)));
135    }
136
137
138    [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
139    private void AfterDeserialization() {
140      if (!Parameters.ContainsKey(RelativeNumberOfEvaluatedSamplesParameterName))
141        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)));
142      if (!Parameters.ContainsKey(RandomParameterName))
143        Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
144    }
145
146    public override IOperation Apply() {
147      int seed = Random.Next();
148      IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value)
149          .Where(i => i < RegressionProblemData.TestSamplesStart.Value || RegressionProblemData.TestSamplesEnd.Value <= i);
150      double quality = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, LowerEstimationLimit.Value, UpperEstimationLimit.Value,
151        RegressionProblemData.Dataset,
152        RegressionProblemData.TargetVariable.Value, rows);
153      QualityParameter.ActualValue = new DoubleValue(quality);
154      return base.Apply();
155    }
156
157
158    internal static IEnumerable<int> GenerateRowsToEvaluate(int seed, double relativeAmount, int start, int end) {
159      if (end < start) throw new ArgumentException("Start value is larger than end value.");
160      int count = (int)((end - start) * relativeAmount);
161      if (count == 0) count = 1;
162      return RandomEnumerable.SampleRandomNumbers(seed, start, end, count);
163    }
164
165    public abstract double Evaluate(ISymbolicExpressionTreeInterpreter interpreter,
166      SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit,
167      Dataset dataset,
168      string targetVariable,
169      IEnumerable<int> rows);
170  }
171}
Note: See TracBrowser for help on using the repository browser.