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 | [Item("BestSymbolicRegressionSolutionAnalyzer", "An operator for analyzing the best solution of symbolic regression problems given in symbolic expression tree encoding.")]
|
---|
39 | [StorableClass]
|
---|
40 | public sealed class BestSymbolicRegressionSolutionAnalyzer : SingleSuccessorOperator, ISymbolicRegressionAnalyzer {
|
---|
41 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
42 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
43 | private const string ProblemDataParameterName = "ProblemData";
|
---|
44 | private const string QualityParameterName = "Quality";
|
---|
45 | private const string UpperEstimationLimitParameterName = "UpperEstimationLimit";
|
---|
46 | private const string LowerEstimationLimitParameterName = "LowerEstimationLimit";
|
---|
47 | private const string BestSolutionParameterName = "BestSolution";
|
---|
48 | private const string BestSolutionQualityParameterName = "BestSolutionQuality";
|
---|
49 | private const string ResultsParameterName = "Results";
|
---|
50 |
|
---|
51 | private const string BestSolutionResultName = "Best Solution (on validiation set)";
|
---|
52 | private const string BestSolutionInputvariableCountResultName = "Variables Used by Best Solution";
|
---|
53 |
|
---|
54 | public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
55 | get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
56 | }
|
---|
57 | public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
58 | get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
59 | }
|
---|
60 | public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {
|
---|
61 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
62 | }
|
---|
63 | public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
64 | get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
|
---|
65 | }
|
---|
66 | public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {
|
---|
67 | get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }
|
---|
68 | }
|
---|
69 | public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {
|
---|
70 | get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }
|
---|
71 | }
|
---|
72 | public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
|
---|
73 | get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; }
|
---|
74 | }
|
---|
75 | public ILookupParameter<DoubleValue> BestSolutionQualityParameter {
|
---|
76 | get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; }
|
---|
77 | }
|
---|
78 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
79 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
80 | }
|
---|
81 |
|
---|
82 | public BestSymbolicRegressionSolutionAnalyzer()
|
---|
83 | : base() {
|
---|
84 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
85 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
|
---|
86 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the symbolic expression tree is a solution."));
|
---|
87 | Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the symbolic expression trees."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the symbolic expression trees."));
|
---|
89 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the symbolic regression trees which should be analyzed."));
|
---|
90 | Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
|
---|
91 | Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best symbolic regression solution."));
|
---|
92 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best symbolic regression solution should be stored."));
|
---|
93 | }
|
---|
94 |
|
---|
95 | public override IOperation Apply() {
|
---|
96 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
97 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
98 | ISymbolicExpressionTreeInterpreter interpreter = SymbolicExpressionTreeInterpreterParameter.ActualValue;
|
---|
99 | ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
|
---|
100 | DataAnalysisProblemData problemData = ProblemDataParameter.ActualValue;
|
---|
101 | DoubleValue upperEstimationLimit = UpperEstimationLimitParameter.ActualValue;
|
---|
102 | DoubleValue lowerEstimationLimit = LowerEstimationLimitParameter.ActualValue;
|
---|
103 |
|
---|
104 | int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
105 |
|
---|
106 | SymbolicRegressionSolution solution = BestSolutionParameter.ActualValue;
|
---|
107 | if (solution == null) {
|
---|
108 | var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)interpreter.Clone(), expressions[i], GetInputVariables(expressions[i]));
|
---|
109 | solution = new SymbolicRegressionSolution(problemData, model, lowerEstimationLimit.Value, upperEstimationLimit.Value);
|
---|
110 | BestSolutionParameter.ActualValue = solution;
|
---|
111 | BestSolutionQualityParameter.ActualValue = qualities[i];
|
---|
112 | results.Add(new Result(BestSolutionResultName, solution));
|
---|
113 | results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(model.InputVariables.Count())));
|
---|
114 | } else {
|
---|
115 | if (BestSolutionQualityParameter.ActualValue.Value > qualities[i].Value) {
|
---|
116 | var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)interpreter.Clone(), expressions[i], GetInputVariables(expressions[i]));
|
---|
117 | solution = new SymbolicRegressionSolution(problemData, model, lowerEstimationLimit.Value, upperEstimationLimit.Value);
|
---|
118 | BestSolutionParameter.ActualValue = solution;
|
---|
119 | BestSolutionQualityParameter.ActualValue = qualities[i];
|
---|
120 | results[BestSolutionResultName].Value = solution;
|
---|
121 | results[BestSolutionInputvariableCountResultName].Value = new IntValue(model.InputVariables.Count());
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return base.Apply();
|
---|
126 | }
|
---|
127 |
|
---|
128 | private IEnumerable<string> GetInputVariables(SymbolicExpressionTree tree) {
|
---|
129 | return (from varNode in tree.IterateNodesPrefix().OfType<VariableTreeNode>()
|
---|
130 | select varNode.VariableName).Distinct();
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|