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 | using HeuristicLab.Problems.DataAnalysis.Evaluators;
|
---|
37 |
|
---|
38 | namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers {
|
---|
39 | [Item("BestSymbolicRegressionSolutionAnalyzer", "An operator for analyzing the best solution of symbolic regression problems given in symbolic expression tree encoding.")]
|
---|
40 | [StorableClass]
|
---|
41 | public sealed class BestSymbolicRegressionSolutionAnalyzer : RegressionSolutionAnalyzer, ISymbolicRegressionAnalyzer {
|
---|
42 | private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
|
---|
43 | private const string SymbolicExpressionTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
|
---|
44 | private const string BestSolutionInputvariableCountResultName = "Variables used by best solution";
|
---|
45 | private const string BestSolutionParameterName = "BestSolution";
|
---|
46 |
|
---|
47 | #region parameter properties
|
---|
48 | public ScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
49 | get { return (ScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
|
---|
50 | }
|
---|
51 | public IValueLookupParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
|
---|
52 | get { return (IValueLookupParameter<ISymbolicExpressionTreeInterpreter>)Parameters[SymbolicExpressionTreeInterpreterParameterName]; }
|
---|
53 | }
|
---|
54 | public ILookupParameter<SymbolicRegressionSolution> BestSolutionParameter {
|
---|
55 | get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestSolutionParameterName]; }
|
---|
56 | }
|
---|
57 | #endregion
|
---|
58 | #region properties
|
---|
59 | public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
|
---|
60 | get { return SymbolicExpressionTreeInterpreterParameter.ActualValue; }
|
---|
61 | }
|
---|
62 | public ItemArray<SymbolicExpressionTree> SymbolicExpressionTree {
|
---|
63 | get { return SymbolicExpressionTreeParameter.ActualValue; }
|
---|
64 | }
|
---|
65 | #endregion
|
---|
66 |
|
---|
67 | public BestSymbolicRegressionSolutionAnalyzer()
|
---|
68 | : base() {
|
---|
69 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
|
---|
70 | Parameters.Add(new ValueLookupParameter<ISymbolicExpressionTreeInterpreter>(SymbolicExpressionTreeInterpreterParameterName, "The interpreter that should be used for the analysis of symbolic expression trees."));
|
---|
71 | Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestSolutionParameterName, "The best symbolic regression solution."));
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override DataAnalysisSolution UpdateBestSolution() {
|
---|
75 | double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity;
|
---|
76 | double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity;
|
---|
77 |
|
---|
78 | int i = Quality.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
79 |
|
---|
80 | if (BestSolutionQualityParameter.ActualValue == null || BestSolutionQualityParameter.ActualValue.Value > Quality[i].Value) {
|
---|
81 | var model = new SymbolicRegressionModel((ISymbolicExpressionTreeInterpreter)SymbolicExpressionTreeInterpreter.Clone(),
|
---|
82 | SymbolicExpressionTree[i],
|
---|
83 | GetInputVariables(SymbolicExpressionTree[i]));
|
---|
84 | var solution = new SymbolicRegressionSolution(ProblemData, model, lowerEstimationLimit, upperEstimationLimit);
|
---|
85 |
|
---|
86 | BestSolutionParameter.ActualValue = solution;
|
---|
87 | BestSolutionQualityParameter.ActualValue = Quality[i];
|
---|
88 |
|
---|
89 | if (Results.ContainsKey(BestSolutionInputvariableCountResultName)) {
|
---|
90 | Results[BestSolutionInputvariableCountResultName].Value = new IntValue(model.InputVariables.Count());
|
---|
91 | } else {
|
---|
92 | Results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(model.InputVariables.Count())));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | return BestSolutionParameter.ActualValue;
|
---|
96 | }
|
---|
97 |
|
---|
98 | private IEnumerable<string> GetInputVariables(SymbolicExpressionTree tree) {
|
---|
99 | return (from varNode in tree.IterateNodesPrefix().OfType<VariableTreeNode>()
|
---|
100 | select varNode.VariableName).Distinct();
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|