[5557] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 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]
|
---|
[14803] | 37 | public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer, IIterationBasedOperator where T : class, ISymbolicDataAnalysisSolution {
|
---|
[5557] | 38 | private const string TrainingBestSolutionParameterName = "Best training solution";
|
---|
| 39 | private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
|
---|
[10906] | 40 | private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
|
---|
[14803] | 41 | private const string TrainingBestSolutionsHistoryParameterName = "Best training solutions history";
|
---|
[9152] | 42 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
[10906] | 43 | private const string IterationsParameterName = "Iterations";
|
---|
| 44 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
[14803] | 45 | private const string StoreHistoryParameterName = "Store History";
|
---|
[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 | }
|
---|
[10906] | 54 | public ILookupParameter<IntValue> TrainingBestSolutionGenerationParameter {
|
---|
| 55 | get { return (ILookupParameter<IntValue>)Parameters[TrainingBestSolutionGenerationParameterName]; }
|
---|
| 56 | }
|
---|
[14803] | 57 | public ILookupParameter<ItemList<T>> TrainingBestSolutionsHistoryParameter {
|
---|
| 58 | get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsHistoryParameterName]; }
|
---|
| 59 | }
|
---|
[9152] | 60 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
| 61 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
| 62 | }
|
---|
[10906] | 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 | }
|
---|
[14803] | 69 |
|
---|
| 70 | public IFixedValueParameter<BoolValue> StoreHistoryParameter {
|
---|
| 71 | get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
|
---|
| 72 | }
|
---|
[5557] | 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 | }
|
---|
[14803] | 83 | public bool UpdateAlways {
|
---|
| 84 | get { return UpdateAlwaysParameter.Value.Value; }
|
---|
| 85 | set { UpdateAlwaysParameter.Value.Value = value; }
|
---|
[9152] | 86 | }
|
---|
[14803] | 87 |
|
---|
| 88 | public bool StoreHistory {
|
---|
| 89 | get { return StoreHistoryParameter.Value.Value; }
|
---|
| 90 | set { StoreHistoryParameter.Value.Value = value; }
|
---|
| 91 | }
|
---|
[5557] | 92 | #endregion
|
---|
| 93 |
|
---|
[14803] | 94 |
|
---|
[5557] | 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() {
|
---|
[14803] | 100 | Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The best training symbolic data analyis solution."));
|
---|
[5607] | 101 | Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
|
---|
[10906] | 102 | Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
|
---|
[9152] | 103 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
[10906] | 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 });
|
---|
[14803] | 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."));
|
---|
[9152] | 108 | UpdateAlwaysParameter.Hidden = true;
|
---|
[5557] | 109 | }
|
---|
| 110 |
|
---|
[9152] | 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 | }
|
---|
[10906] | 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 });
|
---|
[14803] | 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."));
|
---|
[9152] | 127 | }
|
---|
| 128 |
|
---|
[5557] | 129 | public override IOperation Apply() {
|
---|
[14803] | 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 |
|
---|
[5557] | 146 | #region find best tree
|
---|
| 147 | double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
| 148 | ISymbolicExpressionTree bestTree = null;
|
---|
[5882] | 149 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5557] | 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 |
|
---|
[14803] | 159 | if (bestTree != null && (UpdateAlways || TrainingBestSolutionQuality == null ||
|
---|
[8798] | 160 | IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
|
---|
[5557] | 161 | TrainingBestSolution = CreateSolution(bestTree, bestQuality);
|
---|
| 162 | TrainingBestSolutionQuality = new DoubleValue(bestQuality);
|
---|
[10906] | 163 | if (IterationsParameter.ActualValue != null)
|
---|
| 164 | TrainingBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
|
---|
[5557] | 165 |
|
---|
[14803] | 166 | results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
|
---|
| 167 | results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
|
---|
| 168 | if (TrainingBestSolutionGenerationParameter.ActualValue != null)
|
---|
| 169 | results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
|
---|
[10906] | 170 |
|
---|
[14803] | 171 | if (StoreHistory) {
|
---|
| 172 | TrainingBestSolutionsHistoryParameter.ActualValue.Add(TrainingBestSolution);
|
---|
[5557] | 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 | }
|
---|