Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2893_BNLR/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer.cs @ 15747

Last change on this file since 15747 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 11.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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, IIterationBasedOperator where T : class, ISymbolicDataAnalysisSolution {
38    private const string TrainingBestSolutionParameterName = "Best training solution";
39    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
40    private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
41    private const string TrainingBestSolutionsHistoryParameterName = "Best training solutions history";
42    private const string UpdateAlwaysParameterName = "Always update best solution";
43    private const string IterationsParameterName = "Iterations";
44    private const string MaximumIterationsParameterName = "Maximum Iterations";
45    private const string StoreHistoryParameterName = "Store History";
46
47    #region parameter properties
48    public ILookupParameter<T> TrainingBestSolutionParameter {
49      get { return (ILookupParameter<T>)Parameters[TrainingBestSolutionParameterName]; }
50    }
51    public ILookupParameter<DoubleValue> TrainingBestSolutionQualityParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters[TrainingBestSolutionQualityParameterName]; }
53    }
54    public ILookupParameter<IntValue> TrainingBestSolutionGenerationParameter {
55      get { return (ILookupParameter<IntValue>)Parameters[TrainingBestSolutionGenerationParameterName]; }
56    }
57    public ILookupParameter<ItemList<T>> TrainingBestSolutionsHistoryParameter {
58      get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsHistoryParameterName]; }
59    }
60    public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
61      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
62    }
63    public ILookupParameter<IntValue> IterationsParameter {
64      get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
65    }
66    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
67      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
68    }
69
70    public IFixedValueParameter<BoolValue> StoreHistoryParameter {
71      get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
72    }
73    #endregion
74    #region properties
75    public T TrainingBestSolution {
76      get { return TrainingBestSolutionParameter.ActualValue; }
77      set { TrainingBestSolutionParameter.ActualValue = value; }
78    }
79    public DoubleValue TrainingBestSolutionQuality {
80      get { return TrainingBestSolutionQualityParameter.ActualValue; }
81      set { TrainingBestSolutionQualityParameter.ActualValue = value; }
82    }
83    public bool UpdateAlways {
84      get { return UpdateAlwaysParameter.Value.Value; }
85      set { UpdateAlwaysParameter.Value.Value = value; }
86    }
87
88    public bool StoreHistory {
89      get { return StoreHistoryParameter.Value.Value; }
90      set { StoreHistoryParameter.Value.Value = value; }
91    }
92    #endregion
93
94
95    [StorableConstructor]
96    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
97    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
98    public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer()
99      : base() {
100      Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The best training symbolic data analyis solution."));
101      Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
102      Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
103      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
104      Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
105      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
106      Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, "Flag that determines whether all encountered best solutions should be stored as results.", new BoolValue(false)));
107      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsHistoryParameterName, "The history of the best training symbolic data analysis solutions."));
108      UpdateAlwaysParameter.Hidden = true;
109    }
110
111    [StorableHook(HookType.AfterDeserialization)]
112    private void AfterDeserialization() {
113      if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
114        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
115        UpdateAlwaysParameter.Hidden = true;
116      }
117      if (!Parameters.ContainsKey(TrainingBestSolutionGenerationParameterName))
118        Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
119      if (!Parameters.ContainsKey(IterationsParameterName))
120        Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
121      if (!Parameters.ContainsKey(MaximumIterationsParameterName))
122        Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
123      if (!Parameters.ContainsKey(StoreHistoryParameterName))
124        Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, "Flag that determines whether all encountered best solutions should be stored as results.", new BoolValue(false)));
125      if (!Parameters.ContainsKey(TrainingBestSolutionsHistoryParameterName))
126        Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsHistoryParameterName, "The history of the best training symbolic data analysis solutions."));
127    }
128
129    public override IOperation Apply() {
130      var results = ResultCollection;
131      #region create results
132      if (!results.ContainsKey(TrainingBestSolutionParameter.Name))
133        results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, typeof(T)));
134      if (!results.ContainsKey(TrainingBestSolutionQualityParameter.Name))
135        results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, typeof(DoubleValue)));
136      if (!results.ContainsKey(TrainingBestSolutionGenerationParameter.Name) && IterationsParameter.ActualValue != null)
137        results.Add(new Result(TrainingBestSolutionGenerationParameter.Name, TrainingBestSolutionGenerationParameter.Description, typeof(IntValue)));
138      if (StoreHistory && !results.ContainsKey(TrainingBestSolutionsHistoryParameter.Name)) {
139
140        results.Add(new Result(TrainingBestSolutionsHistoryParameter.Name, TrainingBestSolutionsHistoryParameter.Description, typeof(ItemList<T>)));
141        TrainingBestSolutionsHistoryParameter.ActualValue = new ItemList<T>();
142        results[TrainingBestSolutionsHistoryParameter.Name].Value = TrainingBestSolutionsHistoryParameter.ActualValue;
143      }
144      #endregion
145
146      #region find best tree
147      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
148      ISymbolicExpressionTree bestTree = null;
149      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
150      double[] quality = Quality.Select(x => x.Value).ToArray();
151      for (int i = 0; i < tree.Length; i++) {
152        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
153          bestQuality = quality[i];
154          bestTree = tree[i];
155        }
156      }
157      #endregion
158
159      if (bestTree != null && (UpdateAlways || TrainingBestSolutionQuality == null ||
160        IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
161        TrainingBestSolution = CreateSolution(bestTree, bestQuality);
162        TrainingBestSolutionQuality = new DoubleValue(bestQuality);
163        if (IterationsParameter.ActualValue != null)
164          TrainingBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
165
166        results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
167        results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
168        if (TrainingBestSolutionGenerationParameter.ActualValue != null)
169          results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
170
171        if (StoreHistory) {
172          TrainingBestSolutionsHistoryParameter.ActualValue.Add(TrainingBestSolution);
173        }
174      }
175      return base.Apply();
176    }
177
178    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
179
180    private bool IsBetter(double lhs, double rhs, bool maximization) {
181      if (maximization) return lhs > rhs;
182      else return lhs < rhs;
183    }
184  }
185}
Note: See TracBrowser for help on using the repository browser.