[4056] | 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;
|
---|
[4068] | 23 | using System.Collections.Generic;
|
---|
[4056] | 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
[4068] | 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
[4056] | 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[4068] | 31 | using HeuristicLab.Problems.DataAnalysis.Regression;
|
---|
[4056] | 32 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic.Evaluators {
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public abstract class SymbolicVectorRegressionEvaluator : SingleSuccessorOperator, IMultiVariateDataAnalysisEvaluator {
|
---|
| 37 | private const string RandomParameterName = "Random";
|
---|
| 38 | private const string MultiVariateDataAnalysisProblemDataParameterName = "MultiVariateDataAnalysisProblemData";
|
---|
| 39 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
| 40 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
| 41 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
| 42 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
| 43 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
| 44 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
| 45 | private const string RelativeNumberOfEvaluatedSamplesParameterName = "RelativeNumberOfEvaluatedSamples";
|
---|
| 46 |
|
---|
| 47 | #region parameter properties
|
---|
| 48 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 49 | get { return (ILookupParameter<IRandom>)Parameters[RandomParameterName]; }
|
---|
| 50 | }
|
---|
| 51 | public ILookupParameter<MultiVariateDataAnalysisProblemData> MultiVariateDataAnalysisProblemDataParameter {
|
---|
| 52 | get { return (ILookupParameter<MultiVariateDataAnalysisProblemData>)Parameters[MultiVariateDataAnalysisProblemDataParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | public IValueLookupParameter<IntValue> SamplesStartParameter {
|
---|
| 55 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
|
---|
| 56 | }
|
---|
| 57 | public IValueLookupParameter<IntValue> SamplesEndParameter {
|
---|
| 58 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
|
---|
| 59 | }
|
---|
| 60 | public IValueLookupParameter<DoubleArray> LowerEstimationLimitParameter {
|
---|
| 61 | get { return (IValueLookupParameter<DoubleArray>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
| 62 | }
|
---|
| 63 | public IValueLookupParameter<DoubleArray> UpperEstimationLimitParameter {
|
---|
| 64 | get { return (IValueLookupParameter<DoubleArray>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
| 65 | }
|
---|
| 66 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 67 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
| 68 | }
|
---|
| 69 | public ILookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
| 70 | get { return (ILookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
| 71 | }
|
---|
| 72 | public IValueParameter<PercentValue> RelativeNumberOfEvaluatedSamplesParameter {
|
---|
| 73 | get { return (IValueParameter<PercentValue>)Parameters[RelativeNumberOfEvaluatedSamplesParameterName]; }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | #endregion
|
---|
| 77 | #region properties
|
---|
| 78 | public IRandom Random {
|
---|
| 79 | get { return RandomParameter.ActualValue; }
|
---|
| 80 | }
|
---|
| 81 | public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
| 82 | get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
|
---|
| 83 | }
|
---|
| 84 | public SymbolicExpressionTree SymbolicExpressionTree {
|
---|
| 85 | get { return SymbolicExpressionTreeParameter.ActualValue; }
|
---|
| 86 | }
|
---|
| 87 | public MultiVariateDataAnalysisProblemData MultiVariateDataAnalysisProblemData {
|
---|
| 88 | get { return MultiVariateDataAnalysisProblemDataParameter.ActualValue; }
|
---|
| 89 | }
|
---|
| 90 | public IntValue SamplesStart {
|
---|
| 91 | get { return SamplesStartParameter.ActualValue; }
|
---|
| 92 | }
|
---|
| 93 | public IntValue SamplesEnd {
|
---|
| 94 | get { return SamplesEndParameter.ActualValue; }
|
---|
| 95 | }
|
---|
| 96 | public DoubleArray LowerEstimationLimit {
|
---|
| 97 | get { return LowerEstimationLimitParameter.ActualValue; }
|
---|
| 98 | }
|
---|
| 99 | public DoubleArray UpperEstimationLimit {
|
---|
| 100 | get { return UpperEstimationLimitParameter.ActualValue; }
|
---|
| 101 | }
|
---|
| 102 | public PercentValue RelativeNumberOfEvaluatedSamples {
|
---|
| 103 | get { return RelativeNumberOfEvaluatedSamplesParameter.Value; }
|
---|
| 104 | }
|
---|
| 105 | #endregion
|
---|
| 106 |
|
---|
| 107 | public SymbolicVectorRegressionEvaluator()
|
---|
| 108 | : base() {
|
---|
| 109 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "A random number generator."));
|
---|
| 110 | Parameters.Add(new LookupParameter<MultiVariateDataAnalysisProblemData>(MultiVariateDataAnalysisProblemDataParameterName, "The multi-variate data analysis problem data to use for training."));
|
---|
| 111 | Parameters.Add(new LookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The tree interpreter that should be used to evaluate the symbolic expression tree."));
|
---|
| 112 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition to use for training."));
|
---|
| 113 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition to use for training."));
|
---|
| 114 | Parameters.Add(new ValueLookupParameter<DoubleArray>(UpperEstimationLimitParameterName, "The upper limit for the estimated values for each component."));
|
---|
| 115 | Parameters.Add(new ValueLookupParameter<DoubleArray>(LowerEstimationLimitParameterName, "The lower limit for the estimated values for each component."));
|
---|
| 116 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic vector regression solution encoded as a symbolic expression tree."));
|
---|
| 117 | 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)));
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | public override IOperation Apply() {
|
---|
| 121 | var interpreter = SymbolicExpressionTreeInterpreter;
|
---|
| 122 | var tree = SymbolicExpressionTree;
|
---|
| 123 | var problemData = MultiVariateDataAnalysisProblemData;
|
---|
| 124 |
|
---|
| 125 | IEnumerable<string> selectedTargetVariables =
|
---|
| 126 | problemData.TargetVariables.CheckedItems
|
---|
| 127 | .Select(x => x.Value.Value);
|
---|
| 128 |
|
---|
| 129 | // check if there is a vector component for each target variable
|
---|
| 130 | if (selectedTargetVariables.Count() != tree.Root.SubTrees[0].SubTrees.Count)
|
---|
| 131 | throw new ArgumentException("The dimension of the output-vector of the tree doesn't match the number of selected target variables.");
|
---|
| 132 | int start = SamplesStart.Value;
|
---|
| 133 | int end = SamplesEnd.Value;
|
---|
| 134 |
|
---|
| 135 | IEnumerable<int> rows = GenerateRowsToEvaluate((uint)Random.Next(), RelativeNumberOfEvaluatedSamples.Value, start, end);
|
---|
| 136 |
|
---|
| 137 | Evaluate(tree, interpreter, problemData, selectedTargetVariables, rows, LowerEstimationLimit, UpperEstimationLimit);
|
---|
| 138 |
|
---|
| 139 | return base.Apply();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public abstract void Evaluate(SymbolicExpressionTree tree, ISymbolicExpressionTreeInterpreter interpreter, MultiVariateDataAnalysisProblemData problemData, IEnumerable<string> targetVariables, IEnumerable<int> rows, DoubleArray lowerEstimationBound, DoubleArray upperEstimationBound);
|
---|
| 143 |
|
---|
| 144 | private static IEnumerable<int> GenerateRowsToEvaluate(uint seed, double relativeAmount, int start, int end) {
|
---|
| 145 | if (end < start) throw new ArgumentException("Start value is larger than end value.");
|
---|
| 146 | int count = (int)((end - start) * relativeAmount);
|
---|
| 147 | if (count == 0) count = 1;
|
---|
| 148 | return RandomEnumerable.SampleRandomNumbers(seed, start, end, count);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|