[5607] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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]
|
---|
[5747] | 39 | public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>
|
---|
[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";
|
---|
[9241] | 45 | private const string UpdateAlwaysParameterName = "Always update best solution";
|
---|
[5607] | 46 |
|
---|
| 47 | #region parameter properties
|
---|
| 48 | public ILookupParameter<S> ValidationBestSolutionParameter {
|
---|
| 49 | get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
|
---|
| 50 | }
|
---|
| 51 | public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
|
---|
| 52 | get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
|
---|
| 53 | }
|
---|
[9241] | 54 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
| 55 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
| 56 | }
|
---|
[5607] | 57 | #endregion
|
---|
| 58 | #region properties
|
---|
| 59 | public S ValidationBestSolution {
|
---|
| 60 | get { return ValidationBestSolutionParameter.ActualValue; }
|
---|
| 61 | set { ValidationBestSolutionParameter.ActualValue = value; }
|
---|
| 62 | }
|
---|
| 63 | public DoubleValue ValidationBestSolutionQuality {
|
---|
| 64 | get { return ValidationBestSolutionQualityParameter.ActualValue; }
|
---|
| 65 | set { ValidationBestSolutionQualityParameter.ActualValue = value; }
|
---|
| 66 | }
|
---|
[9241] | 67 | public BoolValue UpdateAlways {
|
---|
| 68 | get { return UpdateAlwaysParameter.Value; }
|
---|
| 69 | }
|
---|
[5607] | 70 | #endregion
|
---|
| 71 |
|
---|
| 72 | [StorableConstructor]
|
---|
| 73 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 74 | protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 75 | public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
|
---|
| 76 | : base() {
|
---|
[5722] | 77 | Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
|
---|
[5607] | 78 | Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
|
---|
[9241] | 79 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
| 80 | UpdateAlwaysParameter.Hidden = true;
|
---|
[5607] | 81 | }
|
---|
| 82 |
|
---|
[9241] | 83 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 84 | private void AfterDeserialization() {
|
---|
| 85 | if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
|
---|
| 86 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
| 87 | UpdateAlwaysParameter.Hidden = true;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[5607] | 91 | public override IOperation Apply() {
|
---|
[7721] | 92 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
| 93 | if (!rows.Any()) return base.Apply();
|
---|
| 94 |
|
---|
[5607] | 95 | #region find best tree
|
---|
[7721] | 96 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
| 97 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 98 | double bestValidationQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
[5607] | 99 | ISymbolicExpressionTree bestTree = null;
|
---|
[5882] | 100 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5759] | 101 |
|
---|
[7721] | 102 | // sort is ascending and we take the first n% => order so that best solutions are smallest
|
---|
| 103 | // sort order is determined by maximization parameter
|
---|
| 104 | double[] trainingQuality;
|
---|
| 105 | if (Maximization.Value) {
|
---|
| 106 | // largest values must be sorted first
|
---|
| 107 | trainingQuality = Quality.Select(x => -x.Value).ToArray();
|
---|
| 108 | } else {
|
---|
| 109 | // smallest values must be sorted first
|
---|
| 110 | trainingQuality = Quality.Select(x => x.Value).ToArray();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // sort trees by training qualities
|
---|
| 114 | Array.Sort(trainingQuality, tree);
|
---|
| 115 |
|
---|
| 116 | // number of best training solutions to validate (at least 1)
|
---|
| 117 | int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1);
|
---|
| 118 |
|
---|
[5722] | 119 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
[7721] | 120 | // evaluate best n training trees on validiation set
|
---|
[6728] | 121 | var quality = tree
|
---|
[7721] | 122 | .Take(topN)
|
---|
[6728] | 123 | .AsParallel()
|
---|
| 124 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
| 125 | .ToArray();
|
---|
| 126 |
|
---|
[7721] | 127 | for (int i = 0; i < quality.Length; i++) {
|
---|
| 128 | if (IsBetter(quality[i], bestValidationQuality, Maximization.Value)) {
|
---|
| 129 | bestValidationQuality = quality[i];
|
---|
[5607] | 130 | bestTree = tree[i];
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | #endregion
|
---|
| 134 |
|
---|
| 135 | var results = ResultCollection;
|
---|
[9241] | 136 | if (UpdateAlways.Value || ValidationBestSolutionQuality == null ||
|
---|
[7721] | 137 | IsBetter(bestValidationQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
|
---|
| 138 | ValidationBestSolution = CreateSolution(bestTree, bestValidationQuality);
|
---|
| 139 | ValidationBestSolutionQuality = new DoubleValue(bestValidationQuality);
|
---|
[5607] | 140 |
|
---|
[5747] | 141 | if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
|
---|
| 142 | results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
|
---|
| 143 | results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
|
---|
[5607] | 144 | } else {
|
---|
[5747] | 145 | results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
|
---|
| 146 | results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
|
---|
[5607] | 147 | }
|
---|
| 148 | }
|
---|
| 149 | return base.Apply();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
|
---|
| 153 |
|
---|
| 154 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
| 155 | if (maximization) return lhs > rhs;
|
---|
| 156 | else return lhs < rhs;
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 | }
|
---|