Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1418 worked on data analysis solutions and validation best analyzers.

File size: 5.4 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    private const string TrainingBestSolutionResultName = TrainingBestSolutionParameterName;
44    private const string TrainingBestSolutionQualityResultName = TrainingBestSolutionQualityParameterName;
45
46    #region parameter properties
47    public ILookupParameter<T> TrainingBestSolutionParameter {
48      get { return (ILookupParameter<T>)Parameters[TrainingBestSolutionParameterName]; }
49    }
50    public ILookupParameter<DoubleValue> TrainingBestSolutionQualityParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingBestSolutionQualityParameterName]; }
52    }
53    #endregion
54    #region properties
55    public T TrainingBestSolution {
56      get { return TrainingBestSolutionParameter.ActualValue; }
57      set { TrainingBestSolutionParameter.ActualValue = value; }
58    }
59    public DoubleValue TrainingBestSolutionQuality {
60      get { return TrainingBestSolutionQualityParameter.ActualValue; }
61      set { TrainingBestSolutionQualityParameter.ActualValue = value; }
62    }
63    #endregion
64
65    [StorableConstructor]
66    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
67    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
68    public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer()
69      : base() {
70      Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The training best symbolic data analyis solution."));
71      Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training 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 = SymbolicExpressionTrees.ToArray();
79      double[] quality = Quality.Select(x => x.Value).ToArray();
80      for (int i = 0; i < tree.Length; i++) {
81        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
82          bestQuality = quality[i];
83          bestTree = tree[i];
84        }
85      }
86      #endregion
87
88      var results = ResultCollection;
89      if (TrainingBestSolutionQuality == null ||
90        IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value)) {
91        TrainingBestSolution = CreateSolution(bestTree, bestQuality);
92        TrainingBestSolutionQuality = new DoubleValue(bestQuality);
93
94        if (!results.ContainsKey(TrainingBestSolutionParameterName)) {
95          results.Add(new Result(TrainingBestSolutionResultName, TrainingBestSolution));
96          results.Add(new Result(TrainingBestSolutionQualityResultName, TrainingBestSolutionQuality));
97        } else {
98          results[TrainingBestSolutionResultName].Value = TrainingBestSolution;
99          results[TrainingBestSolutionQualityResultName].Value = TrainingBestSolutionQuality;
100        }
101      }
102      return base.Apply();
103    }
104
105    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
106
107    private bool IsBetter(double lhs, double rhs, bool maximization) {
108      if (maximization) return lhs > rhs;
109      else return lhs < rhs;
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.