[5557] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11646] | 3 | * Copyright (C) 2002-2014 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]
|
---|
[10598] | 37 | public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer, IIterationBasedOperator
|
---|
| 38 |
|
---|
[5607] | 39 | where T : class, ISymbolicDataAnalysisSolution {
|
---|
[5557] | 40 | private const string TrainingBestSolutionParameterName = "Best training solution";
|
---|
| 41 | private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
|
---|
[10598] | 42 | private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
|
---|
[9152] | 43 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
[10598] | 44 | private const string IterationsParameterName = "Iterations";
|
---|
| 45 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
[5557] | 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 | }
|
---|
[10598] | 54 | public ILookupParameter<IntValue> TrainingBestSolutionGenerationParameter {
|
---|
| 55 | get { return (ILookupParameter<IntValue>)Parameters[TrainingBestSolutionGenerationParameterName]; }
|
---|
| 56 | }
|
---|
[9152] | 57 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
| 58 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
| 59 | }
|
---|
[10598] | 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 | }
|
---|
[5557] | 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 | }
|
---|
[9152] | 76 | public BoolValue UpdateAlways {
|
---|
| 77 | get { return UpdateAlwaysParameter.Value; }
|
---|
| 78 | }
|
---|
[5557] | 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."));
|
---|
[5607] | 87 | Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
|
---|
[10598] | 88 | Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
|
---|
[9152] | 89 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
[10598] | 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 });
|
---|
[9152] | 92 | UpdateAlwaysParameter.Hidden = true;
|
---|
[5557] | 93 | }
|
---|
| 94 |
|
---|
[9152] | 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 | }
|
---|
[10598] | 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 });
|
---|
[9152] | 107 | }
|
---|
| 108 |
|
---|
[5557] | 109 | public override IOperation Apply() {
|
---|
| 110 | #region find best tree
|
---|
| 111 | double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
| 112 | ISymbolicExpressionTree bestTree = null;
|
---|
[5882] | 113 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5557] | 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;
|
---|
[9152] | 124 | if (bestTree != null && (UpdateAlways.Value || TrainingBestSolutionQuality == null ||
|
---|
[8798] | 125 | IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
|
---|
[5557] | 126 | TrainingBestSolution = CreateSolution(bestTree, bestQuality);
|
---|
| 127 | TrainingBestSolutionQuality = new DoubleValue(bestQuality);
|
---|
[10598] | 128 | if (IterationsParameter.ActualValue != null)
|
---|
| 129 | TrainingBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
|
---|
[5557] | 130 |
|
---|
[5747] | 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));
|
---|
[10598] | 134 | if (TrainingBestSolutionGenerationParameter.ActualValue != null)
|
---|
| 135 | results.Add(new Result(TrainingBestSolutionGenerationParameter.Name, TrainingBestSolutionGenerationParameter.Description, TrainingBestSolutionGenerationParameter.ActualValue));
|
---|
[5557] | 136 | } else {
|
---|
[5747] | 137 | results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
|
---|
| 138 | results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
|
---|
[10598] | 139 | if (TrainingBestSolutionGenerationParameter.ActualValue != null)
|
---|
| 140 | results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
|
---|
| 141 |
|
---|
[5557] | 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 | }
|
---|