Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/Analyzers/PopulationBestSymbolicRegressionSolutionAnalyzer.cs @ 3662

Last change on this file since 3662 was 3659, checked in by swagner, 15 years ago

Worked on refactoring of algorithm analysis and tracing (#999)

  • removed SubScopesSubScopesLookupParameter
  • adapted SubScopesLookupParameter and renamed it into ScopeTreeLookupParameter
File size: 7.9 KB
Line 
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
22using System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
31using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic;
32using HeuristicLab.Problems.DataAnalysis.Symbolic;
33using System.Collections.Generic;
34using HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols;
35using HeuristicLab.Problems.DataAnalysis;
36
37namespace 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}
Note: See TracBrowser for help on using the repository browser.