[5685] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5685] | 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 |
|
---|
[5759] | 22 | using System;
|
---|
[5685] | 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;
|
---|
[16565] | 31 | using HEAL.Attic;
|
---|
[5685] | 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
|
---|
[16565] | 38 | [StorableType("ABDCE5CB-E177-4382-B478-90372FE6D47F")]
|
---|
[5747] | 39 | public abstract class SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisMultiObjectiveValidationAnalyzer<T, U>,
|
---|
[5685] | 40 | ISymbolicDataAnalysisMultiObjectiveAnalyzer
|
---|
| 41 | where S : class, ISymbolicDataAnalysisSolution
|
---|
| 42 | where T : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<U>
|
---|
| 43 | where U : class, IDataAnalysisProblemData {
|
---|
| 44 | private const string ValidationBestSolutionsParameterName = "Best validation solutions";
|
---|
| 45 | private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
|
---|
[9152] | 46 | private const string UpdateAlwaysParameterName = "Always update best solutions";
|
---|
[5685] | 47 |
|
---|
| 48 | #region parameter properties
|
---|
| 49 | public ILookupParameter<ItemList<S>> ValidationBestSolutionsParameter {
|
---|
| 50 | get { return (ILookupParameter<ItemList<S>>)Parameters[ValidationBestSolutionsParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | public ILookupParameter<ItemList<DoubleArray>> ValidationBestSolutionQualitiesParameter {
|
---|
| 53 | get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
|
---|
| 54 | }
|
---|
[9152] | 55 | public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
|
---|
| 56 | get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
|
---|
| 57 | }
|
---|
[5685] | 58 | #endregion
|
---|
| 59 | #region properties
|
---|
| 60 | public ItemList<S> ValidationBestSolutions {
|
---|
| 61 | get { return ValidationBestSolutionsParameter.ActualValue; }
|
---|
| 62 | set { ValidationBestSolutionsParameter.ActualValue = value; }
|
---|
| 63 | }
|
---|
| 64 | public ItemList<DoubleArray> ValidationBestSolutionQualities {
|
---|
| 65 | get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
|
---|
| 66 | set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
|
---|
| 67 | }
|
---|
[9152] | 68 | public BoolValue UpdateAlways {
|
---|
| 69 | get { return UpdateAlwaysParameter.Value; }
|
---|
| 70 | }
|
---|
[5685] | 71 | #endregion
|
---|
| 72 |
|
---|
| 73 | [StorableConstructor]
|
---|
[16565] | 74 | protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(StorableConstructorFlag _) : base(_) { }
|
---|
[5685] | 75 | protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 76 | public SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer()
|
---|
| 77 | : base() {
|
---|
| 78 | Parameters.Add(new LookupParameter<ItemList<S>>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
|
---|
| 79 | Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
|
---|
[9152] | 80 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solutions should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
| 81 | UpdateAlwaysParameter.Hidden = true;
|
---|
[5685] | 82 | }
|
---|
| 83 |
|
---|
[9152] | 84 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 85 | private void AfterDeserialization() {
|
---|
| 86 | if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
|
---|
| 87 | Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
|
---|
| 88 | UpdateAlwaysParameter.Hidden = true;
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[5685] | 92 | public override IOperation Apply() {
|
---|
[5882] | 93 | IEnumerable<int> rows = GenerateRowsToEvaluate();
|
---|
[5907] | 94 | if (!rows.Any()) return base.Apply();
|
---|
[5759] | 95 |
|
---|
[5685] | 96 | var results = ResultCollection;
|
---|
| 97 | // create empty parameter and result values
|
---|
| 98 | if (ValidationBestSolutions == null) {
|
---|
| 99 | ValidationBestSolutions = new ItemList<S>();
|
---|
| 100 | ValidationBestSolutionQualities = new ItemList<DoubleArray>();
|
---|
[5747] | 101 | results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
|
---|
| 102 | results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
|
---|
[5685] | 103 | }
|
---|
| 104 |
|
---|
[9152] | 105 | //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
|
---|
| 106 | IList<double[]> trainingBestQualities;
|
---|
| 107 | if (UpdateAlways.Value) {
|
---|
| 108 | trainingBestQualities = new List<double[]>();
|
---|
| 109 | } else {
|
---|
| 110 | trainingBestQualities = ValidationBestSolutionQualities.Select(x => x.ToArray()).ToList();
|
---|
| 111 | }
|
---|
[5685] | 112 |
|
---|
| 113 | #region find best trees
|
---|
| 114 | IList<int> nonDominatedIndexes = new List<int>();
|
---|
[5882] | 115 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5685] | 116 | bool[] maximization = Maximization.ToArray();
|
---|
| 117 | List<double[]> newNonDominatedQualities = new List<double[]>();
|
---|
[5759] | 118 | var evaluator = EvaluatorParameter.ActualValue;
|
---|
[6728] | 119 | var problemData = ProblemDataParameter.ActualValue;
|
---|
[5722] | 120 | IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
|
---|
[6728] | 121 |
|
---|
| 122 | var qualities = tree
|
---|
| 123 | .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
|
---|
| 124 | .ToArray();
|
---|
[5685] | 125 | for (int i = 0; i < tree.Length; i++) {
|
---|
| 126 | if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
|
---|
| 127 | IsNonDominated(qualities[i], qualities, maximization)) {
|
---|
[5742] | 128 | if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
|
---|
| 129 | newNonDominatedQualities.Add(qualities[i]);
|
---|
| 130 | nonDominatedIndexes.Add(i);
|
---|
| 131 | }
|
---|
[5685] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | #endregion
|
---|
| 135 | #region update Pareto-optimal solution archive
|
---|
| 136 | if (nonDominatedIndexes.Count > 0) {
|
---|
| 137 | ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
|
---|
| 138 | ItemList<S> nonDominatedSolutions = new ItemList<S>();
|
---|
| 139 | // add all new non-dominated solutions to the archive
|
---|
| 140 | foreach (var index in nonDominatedIndexes) {
|
---|
| 141 | S solution = CreateSolution(tree[index], qualities[index]);
|
---|
| 142 | nonDominatedSolutions.Add(solution);
|
---|
| 143 | nonDominatedQualities.Add(new DoubleArray(qualities[index]));
|
---|
| 144 | }
|
---|
| 145 | // add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
| 146 | for (int i = 0; i < trainingBestQualities.Count; i++) {
|
---|
| 147 | if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
|
---|
[5742] | 148 | if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
|
---|
| 149 | nonDominatedSolutions.Add(ValidationBestSolutions[i]);
|
---|
| 150 | nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
|
---|
| 151 | }
|
---|
[5685] | 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[5747] | 155 | results[ValidationBestSolutionsParameter.Name].Value = nonDominatedSolutions;
|
---|
| 156 | results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
|
---|
[5685] | 157 | }
|
---|
| 158 | #endregion
|
---|
| 159 | return base.Apply();
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
|
---|
| 163 |
|
---|
| 164 | private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
|
---|
| 165 | foreach (var refPoint in points) {
|
---|
| 166 | bool refPointDominatesPoint = true;
|
---|
| 167 | for (int i = 0; i < point.Length; i++) {
|
---|
| 168 | refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
|
---|
| 169 | }
|
---|
| 170 | if (refPointDominatesPoint) return false;
|
---|
| 171 | }
|
---|
| 172 | return true;
|
---|
| 173 | }
|
---|
| 174 | private bool IsBetter(double lhs, double rhs, bool maximization) {
|
---|
| 175 | if (maximization) return lhs > rhs;
|
---|
| 176 | else return lhs < rhs;
|
---|
| 177 | }
|
---|
[5742] | 178 |
|
---|
[16565] | 179 | [StorableType("69cfeefe-8654-4d07-93f1-fdca6fe02338")]
|
---|
[5742] | 180 | private class DoubleArrayComparer : IEqualityComparer<double[]> {
|
---|
| 181 | public bool Equals(double[] x, double[] y) {
|
---|
| 182 | if (y.Length != x.Length) throw new ArgumentException();
|
---|
| 183 | for (int i = 0; i < x.Length; i++) {
|
---|
| 184 | if (!x[i].IsAlmost(y[i])) return false;
|
---|
| 185 | }
|
---|
| 186 | return true;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | public int GetHashCode(double[] obj) {
|
---|
| 190 | int c = obj.Length;
|
---|
| 191 | for (int i = 0; i < obj.Length; i++)
|
---|
| 192 | c ^= obj[i].GetHashCode();
|
---|
| 193 | return c;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[5685] | 197 | }
|
---|
| 198 | }
|
---|