Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer.cs @ 13869

Last change on this file since 13869 was 13368, checked in by ascheibe, 9 years ago

#2520 added guids to storable classes

File size: 8.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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("5559C311-31E2-4040-9DB8-094293A96180")]
37  public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer, IIterationBasedOperator
38
39    where T : class, ISymbolicDataAnalysisSolution {
40    private const string TrainingBestSolutionParameterName = "Best training solution";
41    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
42    private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
43    private const string UpdateAlwaysParameterName = "Always update best solution";
44    private const string IterationsParameterName = "Iterations";
45    private const string MaximumIterationsParameterName = "Maximum Iterations";
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 IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
58      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
59    }
60    public ILookupParameter<IntValue> IterationsParameter {
61      get { return (ILookupParameter<IntValue>)Parameters[IterationsParameterName]; }
62    }
63    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
64      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
65    }
66    #endregion
67    #region properties
68    public T TrainingBestSolution {
69      get { return TrainingBestSolutionParameter.ActualValue; }
70      set { TrainingBestSolutionParameter.ActualValue = value; }
71    }
72    public DoubleValue TrainingBestSolutionQuality {
73      get { return TrainingBestSolutionQualityParameter.ActualValue; }
74      set { TrainingBestSolutionQualityParameter.ActualValue = value; }
75    }
76    public BoolValue UpdateAlways {
77      get { return UpdateAlwaysParameter.Value; }
78    }
79    #endregion
80
81    [StorableConstructor]
82    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
83    protected SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
84    public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer()
85      : base() {
86      Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The training best symbolic data analyis solution."));
87      Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
88      Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
89      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
90      Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
91      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
92      UpdateAlwaysParameter.Hidden = true;
93    }
94
95    [StorableHook(HookType.AfterDeserialization)]
96    private void AfterDeserialization() {
97      if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
98        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
99        UpdateAlwaysParameter.Hidden = true;
100      }
101      if (!Parameters.ContainsKey(TrainingBestSolutionGenerationParameterName))
102        Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
103      if (!Parameters.ContainsKey(IterationsParameterName))
104        Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
105      if (!Parameters.ContainsKey(MaximumIterationsParameterName))
106        Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
107    }
108
109    public override IOperation Apply() {
110      #region find best tree
111      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
112      ISymbolicExpressionTree bestTree = null;
113      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
114      double[] quality = Quality.Select(x => x.Value).ToArray();
115      for (int i = 0; i < tree.Length; i++) {
116        if (IsBetter(quality[i], bestQuality, Maximization.Value)) {
117          bestQuality = quality[i];
118          bestTree = tree[i];
119        }
120      }
121      #endregion
122
123      var results = ResultCollection;
124      if (bestTree != null && (UpdateAlways.Value || TrainingBestSolutionQuality == null ||
125        IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
126        TrainingBestSolution = CreateSolution(bestTree, bestQuality);
127        TrainingBestSolutionQuality = new DoubleValue(bestQuality);
128        if (IterationsParameter.ActualValue != null)
129          TrainingBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
130
131        if (!results.ContainsKey(TrainingBestSolutionParameter.Name)) {
132          results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, TrainingBestSolution));
133          results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, TrainingBestSolutionQuality));
134          if (TrainingBestSolutionGenerationParameter.ActualValue != null)
135            results.Add(new Result(TrainingBestSolutionGenerationParameter.Name, TrainingBestSolutionGenerationParameter.Description, TrainingBestSolutionGenerationParameter.ActualValue));
136        } else {
137          results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
138          results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
139          if (TrainingBestSolutionGenerationParameter.ActualValue != null)
140            results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
141
142        }
143      }
144      return base.Apply();
145    }
146
147    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
148
149    private bool IsBetter(double lhs, double rhs, bool maximization) {
150      if (maximization) return lhs > rhs;
151      else return lhs < rhs;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.