Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer.cs @ 5722

Last change on this file since 5722 was 5722, checked in by gkronber, 14 years ago

#1418 fixed evaluator call from validation analyzers, fixed bugs in interactive simplifier view and added apply linear scaling flag to analyzers.

File size: 6.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  /// <summary>
35  /// An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.
36  /// </summary>
37  [Item("SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.")]
38  [StorableClass]
39  public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisValidationAnalyzer<T, U>,
40    ISymbolicDataAnalysisSingleObjectiveAnalyzer
41    where S : class, ISymbolicDataAnalysisSolution
42    where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
43    where U : class, IDataAnalysisProblemData {
44    private const string QualityParameterName = "Quality";
45    private const string MaximizationParameterName = "Maximization";
46    private const string ValidationBestSolutionParameterName = "Best validation solution";
47    private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality";
48    private const string ValidationBestSolutionResultName = ValidationBestSolutionParameterName;
49    private const string ValidationBestSolutionQualityResultName = ValidationBestSolutionQualityParameterName;
50
51    #region parameter properties
52    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
53      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
54    }
55    public ILookupParameter<BoolValue> MaximizationParameter {
56      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
57    }
58    public ILookupParameter<S> ValidationBestSolutionParameter {
59      get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
60    }
61    public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
62      get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
63    }
64    #endregion
65    #region properties
66    public ItemArray<DoubleValue> Quality {
67      get { return QualityParameter.ActualValue; }
68    }
69    public BoolValue Maximization {
70      get { return MaximizationParameter.ActualValue; }
71    }
72    public S ValidationBestSolution {
73      get { return ValidationBestSolutionParameter.ActualValue; }
74      set { ValidationBestSolutionParameter.ActualValue = value; }
75    }
76    public DoubleValue ValidationBestSolutionQuality {
77      get { return ValidationBestSolutionQualityParameter.ActualValue; }
78      set { ValidationBestSolutionQualityParameter.ActualValue = value; }
79    }
80    #endregion
81
82    [StorableConstructor]
83    protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
84    protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
85    public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
86      : base() {
87      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the trees that should be analyzed."));
88      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "The direction of optimization."));
89      Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
90      Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
91    }
92
93    public override IOperation Apply() {
94      #region find best tree
95      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
96      ISymbolicExpressionTree bestTree = null;
97      ISymbolicExpressionTree[] tree = SymbolicExpressionTrees.ToArray();
98      double[] quality = new double[tree.Length];
99      var evaluator = Evaluator;
100      int start = ValidationSamplesStart.Value;
101      int end = ValidationSamplesEnd.Value;
102      IEnumerable<int> rows = Enumerable.Range(start, end - start);
103      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
104      for (int i = 0; i < tree.Length; i++) {
105        quality[i] = evaluator.Evaluate(childContext, tree[i], ProblemData, rows);
106        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
107          bestQuality = quality[i];
108          bestTree = tree[i];
109        }
110      }
111      #endregion
112
113      var results = ResultCollection;
114      if (ValidationBestSolutionQuality == null ||
115        IsBetter(bestQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
116        ValidationBestSolution = CreateSolution(bestTree, bestQuality);
117        ValidationBestSolutionQuality = new DoubleValue(bestQuality);
118
119        if (!results.ContainsKey(ValidationBestSolutionParameterName)) {
120          results.Add(new Result(ValidationBestSolutionResultName, ValidationBestSolution));
121          results.Add(new Result(ValidationBestSolutionQualityResultName, ValidationBestSolutionQuality));
122        } else {
123          results[ValidationBestSolutionResultName].Value = ValidationBestSolution;
124          results[ValidationBestSolutionQualityResultName].Value = ValidationBestSolutionQuality;
125        }
126      }
127      return base.Apply();
128    }
129
130    protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
131
132    private bool IsBetter(double lhs, double rhs, bool maximization) {
133      if (maximization) return lhs > rhs;
134      else return lhs < rhs;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.