[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";
|
---|
| 41 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
| 42 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
| 43 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
| 44 | #region ISymbolicRegressionEvaluator Members
|
---|
| 45 |
|
---|
| 46 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 47 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | public ILookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
| 51 | get { return (ILookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 55 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[FunctionTreeParameterName]; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | public ILookupParameter<DataAnalysisProblemData> RegressionProblemDataParameter {
|
---|
| 59 | get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[RegressionProblemDataParameterName]; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public IValueLookupParameter<IntValue> SamplesStartParameter {
|
---|
| 63 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | public IValueLookupParameter<IntValue> SamplesEndParameter {
|
---|
| 67 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
|
---|
| 71 | get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 75 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | #endregion
|
---|
| 79 | #region properties
|
---|
| 80 | public IRandom Random {
|
---|
| 81 | get { return RandomParameter.ActualValue; }
|
---|
| 82 | }
|
---|
| 83 | public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
| 84 | get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
|
---|
| 85 | }
|
---|
| 86 | public SymbolicExpressionTree SymbolicExpressionTree {
|
---|
| 87 | get { return SymbolicExpressionTreeParameter.ActualValue; }
|
---|
| 88 | }
|
---|
| 89 | public DataAnalysisProblemData RegressionProblemData {
|
---|
| 90 | get { return RegressionProblemDataParameter.ActualValue; }
|
---|
| 91 | }
|
---|
| 92 | public IntValue SamplesStart {
|
---|
| 93 | get { return SamplesStartParameter.ActualValue; }
|
---|
| 94 | }
|
---|
| 95 | public IntValue SamplesEnd {
|
---|
| 96 | get { return SamplesEndParameter.ActualValue; }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public PercentValue RelativeNumberOfEvaluatedSamples {
|
---|
| 100 | get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
|
---|
| 101 | }
|
---|
| 102 | #endregion
|
---|
| 103 |
|
---|
| 104 | public SingleObjectiveSymbolicRegressionEvaluator()
|
---|
| 105 | : base() {
|
---|
| 106 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
| 107 | Parameters.Add(new LookupParameter<DoubleValue>(QualityParameterName, "The quality of the evaluated symbolic regression solution."));
|
---|
| 108 | Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
|
---|
| 109 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(FunctionTreeParameterName, "The symbolic regression solution encoded as a symbolic expression tree."));
|
---|
| 110 | Parameters.Add(new LookupParameter<DataAnalysisProblemData>(RegressionProblemDataParameterName, "The problem data on which the symbolic regression solution should be evaluated."));
|
---|
| 111 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The start index of the dataset partition on which the symbolic regression solution should be evaluated."));
|
---|
| 112 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The end index of the dataset partition on which the symbolic regression solution should be evaluated."));
|
---|
| 113 | 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)));
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | [StorableConstructor]
|
---|
| 117 | protected SingleObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 118 | [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
|
---|
| 119 | private void AfterDeserialization() {
|
---|
| 120 | if (!Parameters.ContainsKey(RelativeNumberOfEvaluatedSamplesParameterName))
|
---|
| 121 | 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)));
|
---|
| 122 | if (!Parameters.ContainsKey(RandomParameterName))
|
---|
| 123 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | public override IOperation Apply() {
|
---|
| 127 | uint seed = (uint)Random.Next();
|
---|
| 128 | IEnumerable<int> rows = GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value);
|
---|
| 129 | double quality = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, RegressionProblemData.Dataset,
|
---|
| 130 | RegressionProblemData.TargetVariable, rows);
|
---|
| 131 | QualityParameter.ActualValue = new DoubleValue(quality);
|
---|
| 132 | return base.Apply();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | internal static IEnumerable<int> GenerateRowsToEvaluate(uint seed, double relativeAmount, int start, int end) {
|
---|
| 137 | if (end < start) throw new ArgumentException("Start value is larger than end value.");
|
---|
| 138 | int count = (int)((end - start) * relativeAmount);
|
---|
| 139 | if (count == 0) count = 1;
|
---|
| 140 | return RandomEnumerable.SampleRandomNumbers(seed, start, end, count);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | protected abstract double Evaluate(ISymbolicExpressionTreeInterpreter interpreter,
|
---|
| 144 | SymbolicExpressionTree solution,
|
---|
| 145 | Dataset dataset,
|
---|
| 146 | StringValue targetVariable,
|
---|
| 147 | IEnumerable<int> rows);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|