[4128] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 31 |
|
---|
| 32 | namespace 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";
|
---|
[4190] | 41 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
| 42 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
[4128] | 43 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
| 44 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
| 45 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
| 46 | #region ISymbolicRegressionEvaluator Members
|
---|
| 47 |
|
---|
[4190] | 48 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 49 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
| 50 | }
|
---|
[4128] | 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 | }
|
---|
[4190] | 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 | }
|
---|
[4128] | 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 | }
|
---|
[4190] | 105 | public DoubleValue UpperEstimationLimit {
|
---|
| 106 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
| 107 | }
|
---|
| 108 | public DoubleValue LowerEstimationLimit {
|
---|
| 109 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
| 110 | }
|
---|
[4128] | 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."));
|
---|
[4190] | 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."));
|
---|
[4128] | 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() {
|
---|
[4246] | 141 | int seed = Random.Next();
|
---|
[4128] | 142 | IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value);
|
---|
[4190] | 143 | double quality = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, LowerEstimationLimit.Value, UpperEstimationLimit.Value,
|
---|
| 144 | RegressionProblemData.Dataset,
|
---|
| 145 | RegressionProblemData.TargetVariable.Value, rows);
|
---|
[4128] | 146 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 147 | return base.Apply();
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
[4246] | 151 | internal static IEnumerable<int> GenerateRowsToEvaluate(int seed, double relativeAmount, int start, int end) {
|
---|
[4128] | 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 |
|
---|
[4190] | 158 | public abstract double Evaluate(ISymbolicExpressionTreeInterpreter interpreter,
|
---|
| 159 | SymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit,
|
---|
[4128] | 160 | Dataset dataset,
|
---|
[4190] | 161 | string targetVariable,
|
---|
[4128] | 162 | IEnumerable<int> rows);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|