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.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
|
---|
32 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
|
---|
35 | using HeuristicLab.Problems.DataAnalysis;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
38 | /// <summary>
|
---|
39 | /// An operator that calculates the mean squared error of a symbolic regression solution encoded as a symbolic expression tree.
|
---|
40 | /// </summary>
|
---|
41 | [Item("SymbolicRegressionMeanSquaredErrorCalculator", "An operator that calculates the mean squared error of a symbolic regression solution encoded as a symbolic expression tree.")]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class SymbolicRegressionMeanSquaredErrorCalculator : AlgorithmOperator {
|
---|
44 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
45 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
46 | private const string ProblemDataParameterName = "ProblemData";
|
---|
47 | private const string QualityParameterName = "Mean Squared Error";
|
---|
48 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
49 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
50 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
51 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
52 |
|
---|
53 | public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
54 | get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
55 | }
|
---|
56 | public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
57 | get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
58 | }
|
---|
59 | public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
|
---|
60 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
61 | }
|
---|
62 | public IValueLookupParameter<IntValue> SamplesStartParameter {
|
---|
63 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
|
---|
64 | }
|
---|
65 | public IValueLookupParameter<IntValue> SamplesEndParameter {
|
---|
66 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
|
---|
67 | }
|
---|
68 | public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
|
---|
69 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
70 | }
|
---|
71 | public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
|
---|
72 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
73 | }
|
---|
74 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
75 | get { return (ILookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
76 | }
|
---|
77 |
|
---|
78 | public SymbolicRegressionMeanSquaredErrorCalculator()
|
---|
79 | : base() {
|
---|
80 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic expression tree."));
|
---|
81 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree to analyze."));
|
---|
82 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data containing the input varaibles for the symbolic regression problem."));
|
---|
83 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition on which the model quality values should be calculated."));
|
---|
84 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The first index of the data set partition on which the model quality values should be calculated."));
|
---|
85 | 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."));
|
---|
86 | 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."));
|
---|
87 | Parameters.Add(new ValueLookupParameter<DoubleValue>(QualityParameterName, "The mean squared error value of the output of the model."));
|
---|
88 |
|
---|
89 |
|
---|
90 | SymbolicRegressionMeanSquaredErrorEvaluator evaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
|
---|
91 | evaluator.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
|
---|
92 | evaluator.QualityParameter.ActualName = QualityParameter.Name;
|
---|
93 | evaluator.RegressionProblemDataParameter.ActualName = ProblemDataParameter.Name;
|
---|
94 | evaluator.SamplesEndParameter.ActualName = SamplesEndParameter.Name;
|
---|
95 | evaluator.SamplesStartParameter.ActualName = SamplesStartParameter.Name;
|
---|
96 | evaluator.SymbolicExpressionTreeParameter.ActualName = SymbolicExpressionTreeParameter.Name;
|
---|
97 | evaluator.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
|
---|
98 |
|
---|
99 | OperatorGraph.InitialOperator = evaluator;
|
---|
100 | evaluator.Successor = null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | // need to create custom operations for each solution scope (this has to be adapted on basis of the depth value of SymbolicExpressionTreeParameter)
|
---|
104 | public override IOperation Apply() {
|
---|
105 | var scopes = GetScopesOnLevel(ExecutionContext.Scope, SymbolicExpressionTreeParameter.Depth);
|
---|
106 | OperationCollection operations = new OperationCollection();
|
---|
107 | foreach (IScope treeScopes in scopes) {
|
---|
108 | operations.Add(ExecutionContext.CreateChildOperation(OperatorGraph.InitialOperator, treeScopes));
|
---|
109 | }
|
---|
110 | if (Successor != null) operations.Add(ExecutionContext.CreateOperation(Successor));
|
---|
111 | return operations;
|
---|
112 | }
|
---|
113 |
|
---|
114 | private IEnumerable<IScope> GetScopesOnLevel(IScope scope, int d) {
|
---|
115 | if (d == 0) yield return scope;
|
---|
116 | else {
|
---|
117 | foreach (IScope subScope in scope.SubScopes) {
|
---|
118 | foreach (IScope scopesOfSubScope in GetScopesOnLevel(subScope, d - 1)) {
|
---|
119 | yield return scopesOfSubScope;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|