Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418 renamed parameter and updated all validation analyzers to leave out test samples if the validation partition overlaps with the test partition.

File size: 5.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.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Random;
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> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>
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() {
70      Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
71      Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
72    }
73
74    public override IOperation Apply() {
75      #region find best tree
76      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
77      ISymbolicExpressionTree bestTree = null;
78      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
79      double[] quality = new double[tree.Length];
80      var evaluator = EvaluatorParameter.ActualValue;
81      IEnumerable<int> rows = GenerateRowsToEvaluate();
82      if (rows.Count() <= 0) return base.Apply();
83
84      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
85      for (int i = 0; i < tree.Length; i++) {
86        quality[i] = evaluator.Evaluate(childContext, tree[i], ProblemDataParameter.ActualValue, rows);
87        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
88          bestQuality = quality[i];
89          bestTree = tree[i];
90        }
91      }
92      #endregion
93
94      var results = ResultCollection;
95      if (ValidationBestSolutionQuality == null ||
96        IsBetter(bestQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
97        ValidationBestSolution = CreateSolution(bestTree, bestQuality);
98        ValidationBestSolutionQuality = new DoubleValue(bestQuality);
99
100        if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
101          results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
102          results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
103        } else {
104          results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
105          results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
106        }
107      }
108      return base.Apply();
109    }
110
111    protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
112
113    private bool IsBetter(double lhs, double rhs, bool maximization) {
114      if (maximization) return lhs > rhs;
115      else return lhs < rhs;
116    }
117  }
118}
Note: See TracBrowser for help on using the repository browser.