[5557] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9708] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5557] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 |
|
---|
| 31 | namespace 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
|
---|
[5607] | 38 | where T : class, ISymbolicDataAnalysisSolution {
|
---|
[5557] | 39 | private const string TrainingBestSolutionParameterName = "Best training solution";
|
---|
| 40 | private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
|
---|
[9163] | 41 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
[5557] | 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 | }
|
---|
[9163] | 50 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
| 51 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
| 52 | }
|
---|
[5557] | 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 | }
|
---|
[9163] | 63 | public BoolValue UpdateAlways {
|
---|
| 64 | get { return UpdateAlwaysParameter.Value; }
|
---|
| 65 | }
|
---|
[5557] | 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."));
|
---|
[5607] | 74 | Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
|
---|
[9163] | 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;
|
---|
[5557] | 77 | }
|
---|
| 78 |
|
---|
[9163] | 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 |
|
---|
[5557] | 87 | public override IOperation Apply() {
|
---|
| 88 | #region find best tree
|
---|
| 89 | double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
| 90 | ISymbolicExpressionTree bestTree = null;
|
---|
[5882] | 91 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5557] | 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;
|
---|
[9163] | 102 | if (bestTree != null && (UpdateAlways.Value || TrainingBestSolutionQuality == null ||
|
---|
[9126] | 103 | IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
|
---|
[5557] | 104 | TrainingBestSolution = CreateSolution(bestTree, bestQuality);
|
---|
| 105 | TrainingBestSolutionQuality = new DoubleValue(bestQuality);
|
---|
| 106 |
|
---|
[5747] | 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));
|
---|
[5557] | 110 | } else {
|
---|
[5747] | 111 | results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
|
---|
| 112 | results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
|
---|
[5557] | 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 | }
|
---|