[7734] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7734] | 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 |
|
---|
[8169] | 22 | using System;
|
---|
[7734] | 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;
|
---|
[17097] | 31 | using HEAL.Attic;
|
---|
[7734] | 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// An operator that collects the Pareto-best symbolic data analysis solutions for single objective symbolic data analysis problems.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer", "An operator that analyzes the Pareto-best symbolic data analysis solution for single objective symbolic data analysis problems.")]
|
---|
[17097] | 38 | [StorableType("892CE424-FAB0-4E78-8BC2-40BFD1F4A78A")]
|
---|
[8169] | 39 | public abstract class SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>, ISymbolicDataAnalysisBoundedOperator
|
---|
[7734] | 40 | where S : class, ISymbolicDataAnalysisSolution
|
---|
| 41 | where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
|
---|
| 42 | where U : class, IDataAnalysisProblemData {
|
---|
| 43 | private const string ValidationBestSolutionsParameterName = "Best validation solutions";
|
---|
| 44 | private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
|
---|
| 45 | private const string ComplexityParameterName = "Complexity";
|
---|
[8169] | 46 | private const string EstimationLimitsParameterName = "EstimationLimits";
|
---|
[7734] | 47 |
|
---|
| 48 | public override bool EnabledByDefault {
|
---|
| 49 | get { return false; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | #region parameter properties
|
---|
| 53 | public ILookupParameter<ItemList<S>> ValidationBestSolutionsParameter {
|
---|
| 54 | get { return (ILookupParameter<ItemList<S>>)Parameters[ValidationBestSolutionsParameterName]; }
|
---|
| 55 | }
|
---|
| 56 | public ILookupParameter<ItemList<DoubleArray>> ValidationBestSolutionQualitiesParameter {
|
---|
| 57 | get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
|
---|
| 58 | }
|
---|
| 59 | public IScopeTreeLookupParameter<DoubleValue> ComplexityParameter {
|
---|
| 60 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[ComplexityParameterName]; }
|
---|
| 61 | }
|
---|
[8169] | 62 | public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
|
---|
| 63 | get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[7734] | 66 | #endregion
|
---|
| 67 | #region properties
|
---|
| 68 | public ItemList<S> ValidationBestSolutions {
|
---|
| 69 | get { return ValidationBestSolutionsParameter.ActualValue; }
|
---|
| 70 | set { ValidationBestSolutionsParameter.ActualValue = value; }
|
---|
| 71 | }
|
---|
| 72 | public ItemList<DoubleArray> ValidationBestSolutionQualities {
|
---|
| 73 | get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
|
---|
| 74 | set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
|
---|
| 75 | }
|
---|
| 76 | #endregion
|
---|
| 77 |
|
---|
| 78 | [StorableConstructor]
|
---|
[17097] | 79 | protected SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
[7734] | 80 | protected SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 81 | public SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer()
|
---|
| 82 | : base() {
|
---|
| 83 | Parameters.Add(new LookupParameter<ItemList<S>>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
|
---|
| 84 | Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
|
---|
| 85 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(ComplexityParameterName, "The complexity of each tree."));
|
---|
[8169] | 86 | Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The lower and upper limit for the estimated values produced by the symbolic classification model."));
|
---|
[7734] | 87 | }
|
---|
| 88 |
|
---|
| 89 | public override IOperation Apply() {
|
---|
| 90 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
| 91 | if (!rows.Any()) return base.Apply();
|
---|
| 92 |
|
---|
| 93 | #region find best tree
|
---|
| 94 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
| 95 | var problemData = ProblemDataParameter.ActualValue;
|
---|
| 96 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
| 97 |
|
---|
| 98 | // sort is ascending and we take the first n% => order so that best solutions are smallest
|
---|
| 99 | // sort order is determined by maximization parameter
|
---|
| 100 | double[] trainingQuality;
|
---|
| 101 | if (Maximization.Value) {
|
---|
| 102 | // largest values must be sorted first
|
---|
| 103 | trainingQuality = Quality.Select(x => -x.Value).ToArray();
|
---|
| 104 | } else {
|
---|
| 105 | // smallest values must be sorted first
|
---|
| 106 | trainingQuality = Quality.Select(x => x.Value).ToArray();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | int[] treeIndexes = Enumerable.Range(0, tree.Length).ToArray();
|
---|
| 110 |
|
---|
| 111 | // sort trees by training qualities
|
---|
| 112 | Array.Sort(trainingQuality, treeIndexes);
|
---|
| 113 |
|
---|
| 114 | // number of best training solutions to validate (at least 1)
|
---|
| 115 | int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1);
|
---|
| 116 |
|
---|
| 117 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
| 118 | // evaluate best n training trees on validiation set
|
---|
| 119 | var qualities = treeIndexes
|
---|
| 120 | .Select(i => tree[i])
|
---|
| 121 | .Take(topN)
|
---|
| 122 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
| 123 | .ToArray();
|
---|
| 124 | #endregion
|
---|
| 125 |
|
---|
| 126 | var results = ResultCollection;
|
---|
| 127 | // create empty parameter and result values
|
---|
| 128 | if (ValidationBestSolutions == null) {
|
---|
| 129 | ValidationBestSolutions = new ItemList<S>();
|
---|
| 130 | ValidationBestSolutionQualities = new ItemList<DoubleArray>();
|
---|
| 131 | results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
|
---|
| 132 | results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | IList<Tuple<double, double>> validationBestQualities = ValidationBestSolutionQualities
|
---|
| 136 | .Select(x => Tuple.Create(x[0], x[1]))
|
---|
| 137 | .ToList();
|
---|
| 138 |
|
---|
| 139 | #region find best trees
|
---|
| 140 | IList<int> nonDominatedIndexes = new List<int>();
|
---|
| 141 |
|
---|
| 142 | List<double> complexities;
|
---|
[8126] | 143 | if (ComplexityParameter.ActualValue != null && ComplexityParameter.ActualValue.Length == trainingQuality.Length) {
|
---|
[7734] | 144 | complexities = ComplexityParameter.ActualValue.Select(x => x.Value).ToList();
|
---|
| 145 | } else {
|
---|
| 146 | complexities = tree.Select(t => (double)t.Length).ToList();
|
---|
| 147 | }
|
---|
| 148 | List<Tuple<double, double>> fitness = new List<Tuple<double, double>>();
|
---|
| 149 | for (int i = 0; i < qualities.Length; i++)
|
---|
| 150 | fitness.Add(Tuple.Create(qualities[i], complexities[treeIndexes[i]]));
|
---|
| 151 |
|
---|
| 152 | var maximization = Tuple.Create(Maximization.Value, false); // complexity must be minimized
|
---|
| 153 | List<Tuple<double, double>> newNonDominatedQualities = new List<Tuple<double, double>>();
|
---|
| 154 | for (int i = 0; i < fitness.Count; i++) {
|
---|
| 155 | if (IsNonDominated(fitness[i], validationBestQualities, maximization) &&
|
---|
| 156 | IsNonDominated(fitness[i], newNonDominatedQualities, maximization) &&
|
---|
| 157 | IsNonDominated(fitness[i], fitness.Skip(i + 1), maximization)) {
|
---|
| 158 | if (!newNonDominatedQualities.Contains(fitness[i])) {
|
---|
| 159 | newNonDominatedQualities.Add(fitness[i]);
|
---|
| 160 | nonDominatedIndexes.Add(i);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | #endregion
|
---|
| 165 |
|
---|
| 166 | #region update Pareto-optimal solution archive
|
---|
| 167 | if (nonDominatedIndexes.Count > 0) {
|
---|
| 168 | ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
|
---|
| 169 | ItemList<S> nonDominatedSolutions = new ItemList<S>();
|
---|
| 170 | // add all new non-dominated solutions to the archive
|
---|
| 171 | foreach (var index in nonDominatedIndexes) {
|
---|
| 172 | S solution = CreateSolution(tree[treeIndexes[index]]);
|
---|
| 173 | nonDominatedSolutions.Add(solution);
|
---|
| 174 | nonDominatedQualities.Add(new DoubleArray(new double[] { fitness[index].Item1, fitness[index].Item2 }));
|
---|
| 175 | }
|
---|
| 176 | // add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
| 177 | for (int i = 0; i < validationBestQualities.Count; i++) {
|
---|
| 178 | if (IsNonDominated(validationBestQualities[i], newNonDominatedQualities, maximization)) {
|
---|
| 179 | if (!newNonDominatedQualities.Contains(validationBestQualities[i])) {
|
---|
| 180 | nonDominatedSolutions.Add(ValidationBestSolutions[i]);
|
---|
| 181 | nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | // make sure solutions and qualities are ordered in the results
|
---|
| 187 | var orderedIndexes =
|
---|
| 188 | nonDominatedSolutions.Select((s, i) => i).OrderBy(i => nonDominatedQualities[i][0]).ToArray();
|
---|
| 189 |
|
---|
| 190 | var orderedNonDominatedSolutions = new ItemList<S>();
|
---|
| 191 | var orderedNonDominatedQualities = new ItemList<DoubleArray>();
|
---|
| 192 | foreach (var i in orderedIndexes) {
|
---|
| 193 | orderedNonDominatedQualities.Add(nonDominatedQualities[i]);
|
---|
| 194 | orderedNonDominatedSolutions.Add(nonDominatedSolutions[i]);
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | ValidationBestSolutions = orderedNonDominatedSolutions;
|
---|
| 198 | ValidationBestSolutionQualities = orderedNonDominatedQualities;
|
---|
| 199 |
|
---|
| 200 | results[ValidationBestSolutionsParameter.Name].Value = orderedNonDominatedSolutions;
|
---|
| 201 | results[ValidationBestSolutionQualitiesParameter.Name].Value = orderedNonDominatedQualities;
|
---|
| 202 | }
|
---|
| 203 | #endregion
|
---|
| 204 | return base.Apply();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree);
|
---|
| 208 |
|
---|
| 209 | private bool IsNonDominated(Tuple<double, double> point, IEnumerable<Tuple<double, double>> points, Tuple<bool, bool> maximization) {
|
---|
| 210 | return !points.Any(p => IsBetterOrEqual(p.Item1, point.Item1, maximization.Item1) &&
|
---|
| 211 | IsBetterOrEqual(p.Item2, point.Item2, maximization.Item2));
|
---|
| 212 | }
|
---|
| 213 | private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
|
---|
| 214 | if (maximization) return lhs >= rhs;
|
---|
| 215 | else return lhs <= rhs;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | }
|
---|