Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer.cs @ 5984

Last change on this file since 5984 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.3 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 training best symbolic data analysis solution for single objective symbolic data analysis problems.
36  /// </summary>
37  [Item("SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for single objective symbolic data analysis problems.")]
38  [StorableClass]
39  public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer
40    where T : class, ISymbolicDataAnalysisSolution {
41    private const string TrainingBestSolutionParameterName = "Best training solution";
42    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
43
44    #region parameter properties
45    public ILookupParameter<T> TrainingBestSolutionParameter {
46      get { return (ILookupParameter<T>)Parameters[TrainingBestSolutionParameterName]; }
47    }
48    public ILookupParameter<DoubleValue> TrainingBestSolutionQualityParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingBestSolutionQualityParameterName]; }
50    }
51    #endregion
52    #region properties
53    public T TrainingBestSolution {
54      get { return TrainingBestSolutionParameter.ActualValue; }
55      set { TrainingBestSolutionParameter.ActualValue = value; }
56    }
57    public DoubleValue TrainingBestSolutionQuality {
58      get { return TrainingBestSolutionQualityParameter.ActualValue; }
59      set { TrainingBestSolutionQualityParameter.ActualValue = value; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
65    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
66    public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer()
67      : base() {
68      Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The training best symbolic data analyis solution."));
69      Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
70    }
71
72    public override IOperation Apply() {
73      #region find best tree
74      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
75      ISymbolicExpressionTree bestTree = null;
76      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
77      double[] quality = Quality.Select(x => x.Value).ToArray();
78      for (int i = 0; i < tree.Length; i++) {
79        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
80          bestQuality = quality[i];
81          bestTree = tree[i];
82        }
83      }
84      #endregion
85
86      var results = ResultCollection;
87      if (TrainingBestSolutionQuality == null ||
88        IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value)) {
89        TrainingBestSolution = CreateSolution(bestTree, bestQuality);
90        TrainingBestSolutionQuality = new DoubleValue(bestQuality);
91
92        if (!results.ContainsKey(TrainingBestSolutionParameter.Name)) {
93          results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, TrainingBestSolution));
94          results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, TrainingBestSolutionQuality));
95        } else {
96          results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
97          results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
98        }
99      }
100      return base.Apply();
101    }
102
103    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
104
105    private bool IsBetter(double lhs, double rhs, bool maximization) {
106      if (maximization) return lhs > rhs;
107      else return lhs < rhs;
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.