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