Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer.cs @ 10669

Last change on this file since 10669 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
32  /// <summary>
33  /// An operator that analyzes the training best symbolic data analysis solution for single objective symbolic data analysis problems.
34  /// </summary>
35  [Item("SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for single objective symbolic data analysis problems.")]
36  [StorableClass]
37  public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer
38    where T : class, ISymbolicDataAnalysisSolution {
39    private const string TrainingBestSolutionParameterName = "Best training solution";
40    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
41    private const string UpdateAlwaysParameterName = "Always update best solution";
42
43    #region parameter properties
44    public ILookupParameter<T> TrainingBestSolutionParameter {
45      get { return (ILookupParameter<T>)Parameters[TrainingBestSolutionParameterName]; }
46    }
47    public ILookupParameter<DoubleValue> TrainingBestSolutionQualityParameter {
48      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingBestSolutionQualityParameterName]; }
49    }
50    public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
51      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
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    public BoolValue UpdateAlways {
64      get { return UpdateAlwaysParameter.Value; }
65    }
66    #endregion
67
68    [StorableConstructor]
69    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
70    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
71    public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer()
72      : base() {
73      Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The training best symbolic data analyis solution."));
74      Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
75      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
76      UpdateAlwaysParameter.Hidden = true;
77    }
78
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81      if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
82        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
83        UpdateAlwaysParameter.Hidden = true;
84      }
85    }
86
87    public override IOperation Apply() {
88      #region find best tree
89      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
90      ISymbolicExpressionTree bestTree = null;
91      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
92      double[] quality = Quality.Select(x => x.Value).ToArray();
93      for (int i = 0; i < tree.Length; i++) {
94        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
95          bestQuality = quality[i];
96          bestTree = tree[i];
97        }
98      }
99      #endregion
100
101      var results = ResultCollection;
102      if (bestTree != null && (UpdateAlways.Value || TrainingBestSolutionQuality == null ||
103        IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
104        TrainingBestSolution = CreateSolution(bestTree, bestQuality);
105        TrainingBestSolutionQuality = new DoubleValue(bestQuality);
106
107        if (!results.ContainsKey(TrainingBestSolutionParameter.Name)) {
108          results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, TrainingBestSolution));
109          results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, TrainingBestSolutionQuality));
110        } else {
111          results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
112          results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
113        }
114      }
115      return base.Apply();
116    }
117
118    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
119
120    private bool IsBetter(double lhs, double rhs, bool maximization) {
121      if (maximization) return lhs > rhs;
122      else return lhs < rhs;
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.