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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
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("MultiObjectiveSymbolicRegressionEvaluator", "Evaluates a symbolic regression solution.")]
|
---|
34 | [StorableClass]
|
---|
35 | public abstract class MultiObjectiveSymbolicRegressionEvaluator : SingleSuccessorOperator, IMultiObjectiveSymbolicRegressionEvaluator {
|
---|
36 | private const string RandomParameterName = "Random";
|
---|
37 | private const string QualitiesParameterName = "Qualities";
|
---|
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<DoubleArray> QualitiesParameter {
|
---|
47 | get { return (ILookupParameter<DoubleArray>)Parameters[QualitiesParameterName]; }
|
---|
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 MultiObjectiveSymbolicRegressionEvaluator()
|
---|
105 | : base() {
|
---|
106 | Parameters.Add(new LookupParameter<IRandom>(RandomParameterName, "The random generator to use."));
|
---|
107 | Parameters.Add(new LookupParameter<DoubleArray>(QualitiesParameterName, "The qualities 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 MultiObjectiveSymbolicRegressionEvaluator(bool deserializing) : base(deserializing) { }
|
---|
118 | [StorableHook(Persistence.Default.CompositeSerializers.Storable.HookType.AfterDeserialization)]
|
---|
119 | private void AfterDeserialization() {
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override IOperation Apply() {
|
---|
123 | int seed = Random.Next();
|
---|
124 | IEnumerable<int> rows = SingleObjectiveSymbolicRegressionEvaluator.GenerateRowsToEvaluate(seed, RelativeNumberOfEvaluatedSamples.Value, SamplesStart.Value, SamplesEnd.Value)
|
---|
125 | .Where(i => i < RegressionProblemData.TestSamplesStart.Value || RegressionProblemData.TestSamplesEnd.Value <= i);
|
---|
126 | double[] qualities = Evaluate(SymbolicExpressionTreeInterpreter, SymbolicExpressionTree, RegressionProblemData.Dataset,
|
---|
127 | RegressionProblemData.TargetVariable, rows);
|
---|
128 | QualitiesParameter.ActualValue = new DoubleArray(qualities);
|
---|
129 | return base.Apply();
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | protected abstract double[] Evaluate(ISymbolicExpressionTreeInterpreter interpreter,
|
---|
134 | SymbolicExpressionTree solution,
|
---|
135 | Dataset dataset,
|
---|
136 | StringValue targetVariable,
|
---|
137 | IEnumerable<int> rows);
|
---|
138 | }
|
---|
139 | }
|
---|