source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/BestValidationSymbolicRegressionSolutionVisualizer.cs @ 3442

Last change on this file since 3442 was 3442, checked in by gkronber, 11 years ago

Implemented views for DataAnalysisProblems and DataAnalysisSolutions. #938 (Data types and operators for regression problems)

File size: 5.4 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;
31
32namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
33  /// <summary>
34  /// An operator for visualizing the best symbolic regression solution based on the validation set.
35  /// </summary>
36  [Item("BestSymbolicExpressionTreeVisualizer", "An operator for visualizing the best symbolic regression solution based on the validation set.")]
37  [StorableClass]
38  public sealed class BestValidationSymbolicRegressionSolutionVisualizer : SingleSuccessorOperator, ISingleObjectiveSolutionsVisualizer, ISolutionsVisualizer {
39    private const string SymbolicRegressionModelParameterName = "SymbolicRegressionModel";
40    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
41    private const string BestValidationSolutionParameterName = "BestValidationSolution";
42    private const string QualityParameterName = "Quality";
43    public ILookupParameter<ItemArray<SymbolicExpressionTree>> SymbolicExpressionTreeParameter {
44      get { return (ILookupParameter<ItemArray<SymbolicExpressionTree>>)Parameters[SymbolicRegressionModelParameterName]; }
45    }
46    public ILookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
47      get { return (ILookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
48    }
49    public ILookupParameter<SymbolicRegressionSolution> BestValidationSolutionParameter {
50      get { return (ILookupParameter<SymbolicRegressionSolution>)Parameters[BestValidationSolutionParameterName]; }
51    }
52    ILookupParameter ISolutionsVisualizer.VisualizationParameter {
53      get { return BestValidationSolutionParameter; }
54    }
55
56    public ILookupParameter<ItemArray<DoubleValue>> QualityParameter {
57      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters[QualityParameterName]; }
58    }
59
60    public BestValidationSymbolicRegressionSolutionVisualizer()
61      : base() {
62      Parameters.Add(new SubScopesLookupParameter<SymbolicExpressionTree>(SymbolicRegressionModelParameterName, "The symbolic regression solutions from which the best solution should be visualized."));
63      Parameters.Add(new SubScopesLookupParameter<DoubleValue>(QualityParameterName, "The quality of the symbolic regression solutions."));
64      Parameters.Add(new LookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The symbolic regression problme data on which the best solution should be evaluated."));
65      Parameters.Add(new LookupParameter<SymbolicRegressionSolution>(BestValidationSolutionParameterName, "The best symbolic expression tree based on the validation data for the symbolic regression problem."));
66      Parameters.Add(new LookupParameter<ResultCollection>("Results"));
67    }
68
69    public override IOperation Apply() {
70      ItemArray<SymbolicExpressionTree> expressions = SymbolicExpressionTreeParameter.ActualValue;
71      DataAnalysisProblemData problemData = DataAnalysisProblemDataParameter.ActualValue;
72      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
73
74      var bestExpressionIndex = (from index in Enumerable.Range(0, qualities.Count())
75                                 select new { Index = index, Quality = qualities[index] }).OrderBy(x => x.Quality).Select(x => x.Index).First();
76
77      var bestExpression = expressions[bestExpressionIndex];
78      SymbolicRegressionSolution bestSolution = BestValidationSolutionParameter.ActualValue;
79      if (bestSolution == null) BestValidationSolutionParameter.ActualValue = CreateDataAnalysisSolution(problemData, bestExpression);
80      else {
81        bestSolution.Model = CreateModel(problemData, bestExpression);
82      }
83      // ((ResultCollection)Parameters["Results"].ActualValue).Add(new Result("ValidationMSE", new DoubleValue(3.15)));
84      return base.Apply();
85    }
86
87    private SymbolicRegressionModel CreateModel(DataAnalysisProblemData problemData, SymbolicExpressionTree expression) {
88      return new SymbolicRegressionModel(expression, problemData.InputVariables.Select(x => x.Value));
89    }
90
91    private SymbolicRegressionSolution CreateDataAnalysisSolution(DataAnalysisProblemData problemData, SymbolicExpressionTree expression) {
92      return new SymbolicRegressionSolution(problemData, CreateModel(problemData, expression));
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.