Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer.cs @ 7952

Last change on this file since 7952 was 7721, checked in by gkronber, 13 years ago

#1817 added parameter to symbolic data analysis validation analyzers to determine the percentage of best solutions that should be evaluated on the validation set

File size: 6.8 KB
RevLine 
[5607]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5607]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
[7721]22using System;
[5607]23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
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]
[5747]39  public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>
[5607]40    where S : class, ISymbolicDataAnalysisSolution
41    where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
42    where U : class, IDataAnalysisProblemData {
43    private const string ValidationBestSolutionParameterName = "Best validation solution";
44    private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality";
45
46    #region parameter properties
47    public ILookupParameter<S> ValidationBestSolutionParameter {
48      get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
49    }
50    public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
52    }
53    #endregion
54    #region properties
55    public S ValidationBestSolution {
56      get { return ValidationBestSolutionParameter.ActualValue; }
57      set { ValidationBestSolutionParameter.ActualValue = value; }
58    }
59    public DoubleValue ValidationBestSolutionQuality {
60      get { return ValidationBestSolutionQualityParameter.ActualValue; }
61      set { ValidationBestSolutionQualityParameter.ActualValue = value; }
62    }
63    #endregion
64
65    [StorableConstructor]
66    protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
67    protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
68    public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
69      : base() {
[5722]70      Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
[5607]71      Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
72    }
73
74    public override IOperation Apply() {
[7721]75      IEnumerable<int> rows = GenerateRowsToEvaluate();
76      if (!rows.Any()) return base.Apply();
77
[5607]78      #region find best tree
[7721]79      var evaluator = EvaluatorParameter.ActualValue;
80      var problemData = ProblemDataParameter.ActualValue;
81      double bestValidationQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
[5607]82      ISymbolicExpressionTree bestTree = null;
[5882]83      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
[5759]84
[7721]85      // sort is ascending and we take the first n% => order so that best solutions are smallest
86      // sort order is determined by maximization parameter
87      double[] trainingQuality;
88      if (Maximization.Value) {
89        // largest values must be sorted first
90        trainingQuality = Quality.Select(x => -x.Value).ToArray();
91      } else {
92        // smallest values must be sorted first
93        trainingQuality = Quality.Select(x => x.Value).ToArray();
94      }
95
96      // sort trees by training qualities
97      Array.Sort(trainingQuality, tree);
98
99      // number of best training solutions to validate (at least 1)
100      int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1);
101
[5722]102      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
[7721]103      // evaluate best n training trees on validiation set
[6728]104      var quality = tree
[7721]105        .Take(topN)
[6728]106        .AsParallel()
107        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
108        .ToArray();
109
[7721]110      for (int i = 0; i < quality.Length; i++) {
111        if (IsBetter(quality[i], bestValidationQuality, Maximization.Value)) {
112          bestValidationQuality = quality[i];
[5607]113          bestTree = tree[i];
114        }
115      }
116      #endregion
117
118      var results = ResultCollection;
119      if (ValidationBestSolutionQuality == null ||
[7721]120        IsBetter(bestValidationQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
121        ValidationBestSolution = CreateSolution(bestTree, bestValidationQuality);
122        ValidationBestSolutionQuality = new DoubleValue(bestValidationQuality);
[5607]123
[5747]124        if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
125          results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
126          results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
[5607]127        } else {
[5747]128          results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
129          results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
[5607]130        }
131      }
132      return base.Apply();
133    }
134
135    protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
136
137    private bool IsBetter(double lhs, double rhs, bool maximization) {
138      if (maximization) return lhs > rhs;
139      else return lhs < rhs;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.