[5607] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14312] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5607] | 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 |
|
---|
[7721] | 22 | using System;
|
---|
[5607] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.")]
|
---|
| 38 | [StorableClass]
|
---|
[11208] | 39 | public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>, IIterationBasedOperator
|
---|
[5607] | 40 | where S : class, ISymbolicDataAnalysisSolution
|
---|
| 41 | where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
|
---|
| 42 | where U : class, IDataAnalysisProblemData {
|
---|
| 43 | private const string ValidationBestSolutionParameterName = "Best validation solution";
|
---|
| 44 | private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality";
|
---|
[11208] | 45 | private const string ValidationBestSolutionGenerationParameterName = "Best validation solution generation";
|
---|
[9152] | 46 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
[11208] | 47 | private const string IterationsParameterName = "Iterations";
|
---|
| 48 | private const string MaximumIterationsParameterName = "Maximum Iterations";
|
---|
[5607] | 49 |
|
---|
| 50 | #region parameter properties
|
---|
| 51 | public ILookupParameter<S> ValidationBestSolutionParameter {
|
---|
| 52 | get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
|
---|
| 55 | get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
|
---|
| 56 | }
|
---|
[11208] | 57 | public ILookupParameter<IntValue> ValidationBestSolutionGenerationParameter {
|
---|
| 58 | get { return (ILookupParameter<IntValue>)Parameters[ValidationBestSolutionGenerationParameterName]; }
|
---|
| 59 | }
|
---|
[9152] | 60 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
| 61 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
| 62 | }
|
---|
[11208] | 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 | }
|
---|
[5607] | 69 | #endregion
|
---|
| 70 | #region properties
|
---|
| 71 | public S ValidationBestSolution {
|
---|
| 72 | get { return ValidationBestSolutionParameter.ActualValue; }
|
---|
| 73 | set { ValidationBestSolutionParameter.ActualValue = value; }
|
---|
| 74 | }
|
---|
| 75 | public DoubleValue ValidationBestSolutionQuality {
|
---|
| 76 | get { return ValidationBestSolutionQualityParameter.ActualValue; }
|
---|
| 77 | set { ValidationBestSolutionQualityParameter.ActualValue = value; }
|
---|
| 78 | }
|
---|
[9152] | 79 | public BoolValue UpdateAlways {
|
---|
| 80 | get { return UpdateAlwaysParameter.Value; }
|
---|
| 81 | }
|
---|
[5607] | 82 | #endregion
|
---|
| 83 |
|
---|
| 84 | [StorableConstructor]
|
---|
| 85 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 86 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 87 | public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
|
---|
| 88 | : base() {
|
---|
[5722] | 89 | Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
|
---|
[5607] | 90 | Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
|
---|
[11208] | 91 | Parameters.Add(new LookupParameter<IntValue>(ValidationBestSolutionGenerationParameterName, "The generation in which the best validation solution was found."));
|
---|
[9152] | 92 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
[11208] | 93 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
|
---|
| 94 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
|
---|
[9152] | 95 | UpdateAlwaysParameter.Hidden = true;
|
---|
[5607] | 96 | }
|
---|
| 97 |
|
---|
[9152] | 98 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 99 | private void AfterDeserialization() {
|
---|
| 100 | if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
|
---|
[11208] | 101 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
[9152] | 102 | UpdateAlwaysParameter.Hidden = true;
|
---|
| 103 | }
|
---|
[11208] | 104 | if (!Parameters.ContainsKey(ValidationBestSolutionGenerationParameterName))
|
---|
| 105 | Parameters.Add(new LookupParameter<IntValue>(ValidationBestSolutionGenerationParameterName, "The generation in which the best validation solution was found."));
|
---|
| 106 | if (!Parameters.ContainsKey(IterationsParameterName))
|
---|
| 107 | Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
|
---|
| 108 | if (!Parameters.ContainsKey(MaximumIterationsParameterName))
|
---|
| 109 | Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
|
---|
[9152] | 110 | }
|
---|
| 111 |
|
---|
[5607] | 112 | public override IOperation Apply() {
|
---|
[7721] | 113 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
| 114 | if (!rows.Any()) return base.Apply();
|
---|
| 115 |
|
---|
[5607] | 116 | #region find best tree
|
---|
[7721] | 117 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
| 118 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 119 | double bestValidationQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
[5607] | 120 | ISymbolicExpressionTree bestTree = null;
|
---|
[5882] | 121 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5759] | 122 |
|
---|
[7721] | 123 | // sort is ascending and we take the first n% => order so that best solutions are smallest
|
---|
| 124 | // sort order is determined by maximization parameter
|
---|
| 125 | double[] trainingQuality;
|
---|
| 126 | if (Maximization.Value) {
|
---|
| 127 | // largest values must be sorted first
|
---|
| 128 | trainingQuality = Quality.Select(x => -x.Value).ToArray();
|
---|
| 129 | } else {
|
---|
| 130 | // smallest values must be sorted first
|
---|
| 131 | trainingQuality = Quality.Select(x => x.Value).ToArray();
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | // sort trees by training qualities
|
---|
| 135 | Array.Sort(trainingQuality, tree);
|
---|
| 136 |
|
---|
| 137 | // number of best training solutions to validate (at least 1)
|
---|
| 138 | int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1);
|
---|
| 139 |
|
---|
[5722] | 140 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
[7721] | 141 | // evaluate best n training trees on validiation set
|
---|
[6728] | 142 | var quality = tree
|
---|
[7721] | 143 | .Take(topN)
|
---|
[6728] | 144 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
| 145 | .ToArray();
|
---|
| 146 |
|
---|
[7721] | 147 | for (int i = 0; i < quality.Length; i++) {
|
---|
| 148 | if (IsBetter(quality[i], bestValidationQuality, Maximization.Value)) {
|
---|
| 149 | bestValidationQuality = quality[i];
|
---|
[5607] | 150 | bestTree = tree[i];
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | #endregion
|
---|
| 154 |
|
---|
| 155 | var results = ResultCollection;
|
---|
[9152] | 156 | if (UpdateAlways.Value || ValidationBestSolutionQuality == null ||
|
---|
[7721] | 157 | IsBetter(bestValidationQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
|
---|
| 158 | ValidationBestSolution = CreateSolution(bestTree, bestValidationQuality);
|
---|
| 159 | ValidationBestSolutionQuality = new DoubleValue(bestValidationQuality);
|
---|
[11208] | 160 | if (IterationsParameter.ActualValue != null)
|
---|
| 161 | ValidationBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
|
---|
[5607] | 162 |
|
---|
[5747] | 163 | if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
|
---|
| 164 | results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
|
---|
| 165 | results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
|
---|
[11208] | 166 | if (ValidationBestSolutionGenerationParameter.ActualValue != null)
|
---|
| 167 | results.Add(new Result(ValidationBestSolutionGenerationParameter.Name, ValidationBestSolutionGenerationParameter.Description, ValidationBestSolutionGenerationParameter.ActualValue));
|
---|
[5607] | 168 | } else {
|
---|
[5747] | 169 | results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
|
---|
| 170 | results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
|
---|
[11208] | 171 | if (ValidationBestSolutionGenerationParameter.ActualValue != null)
|
---|
| 172 | results[ValidationBestSolutionGenerationParameter.Name].Value = ValidationBestSolutionGenerationParameter.ActualValue;
|
---|
[5607] | 173 | }
|
---|
| 174 | }
|
---|
| 175 | return base.Apply();
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | protected abstract S 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 | }
|
---|