[5557] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5742] | 32 | using System;
|
---|
[5557] | 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.
|
---|
| 37 | /// </summary>
|
---|
| 38 | [Item("SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public abstract class SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisMultiObjectiveAnalyzer
|
---|
[5607] | 41 | where T : class, ISymbolicDataAnalysisSolution {
|
---|
[5557] | 42 | private const string TrainingBestSolutionsParameterName = "Best training solutions";
|
---|
| 43 | private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities";
|
---|
| 44 |
|
---|
| 45 | #region parameter properties
|
---|
| 46 | public ILookupParameter<ItemList<T>> TrainingBestSolutionsParameter {
|
---|
| 47 | get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsParameterName]; }
|
---|
| 48 | }
|
---|
| 49 | public ILookupParameter<ItemList<DoubleArray>> TrainingBestSolutionQualitiesParameter {
|
---|
| 50 | get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[TrainingBestSolutionQualitiesParameterName]; }
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 | #region properties
|
---|
| 54 | public ItemList<T> TrainingBestSolutions {
|
---|
| 55 | get { return TrainingBestSolutionsParameter.ActualValue; }
|
---|
| 56 | set { TrainingBestSolutionsParameter.ActualValue = value; }
|
---|
| 57 | }
|
---|
| 58 | public ItemList<DoubleArray> TrainingBestSolutionQualities {
|
---|
| 59 | get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
|
---|
| 60 | set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
|
---|
| 61 | }
|
---|
| 62 | #endregion
|
---|
| 63 |
|
---|
| 64 | [StorableConstructor]
|
---|
| 65 | protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 66 | protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 67 | public SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer()
|
---|
| 68 | : base() {
|
---|
[5607] | 69 | Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions."));
|
---|
[5557] | 70 | Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public override IOperation Apply() {
|
---|
| 74 | var results = ResultCollection;
|
---|
| 75 | // create empty parameter and result values
|
---|
| 76 | if (TrainingBestSolutions == null) {
|
---|
| 77 | TrainingBestSolutions = new ItemList<T>();
|
---|
| 78 | TrainingBestSolutionQualities = new ItemList<DoubleArray>();
|
---|
[5747] | 79 | results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
|
---|
| 80 | results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
|
---|
[5557] | 81 | }
|
---|
| 82 |
|
---|
| 83 | IList<double[]> trainingBestQualities = TrainingBestSolutionQualities
|
---|
| 84 | .Select(x => x.ToArray())
|
---|
| 85 | .ToList();
|
---|
| 86 |
|
---|
| 87 | #region find best trees
|
---|
| 88 | IList<int> nonDominatedIndexes = new List<int>();
|
---|
[5882] | 89 | ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
|
---|
[5557] | 90 | List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList();
|
---|
| 91 | bool[] maximization = Maximization.ToArray();
|
---|
| 92 | List<double[]> newNonDominatedQualities = new List<double[]>();
|
---|
| 93 | for (int i = 0; i < tree.Length; i++) {
|
---|
| 94 | if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
|
---|
| 95 | IsNonDominated(qualities[i], qualities, maximization)) {
|
---|
[5742] | 96 | if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
|
---|
| 97 | newNonDominatedQualities.Add(qualities[i]);
|
---|
| 98 | nonDominatedIndexes.Add(i);
|
---|
| 99 | }
|
---|
[5557] | 100 | }
|
---|
| 101 | }
|
---|
| 102 | #endregion
|
---|
| 103 | #region update Pareto-optimal solution archive
|
---|
| 104 | if (nonDominatedIndexes.Count > 0) {
|
---|
| 105 | ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
|
---|
| 106 | ItemList<T> nonDominatedSolutions = new ItemList<T>();
|
---|
| 107 | // add all new non-dominated solutions to the archive
|
---|
| 108 | foreach (var index in nonDominatedIndexes) {
|
---|
| 109 | T solution = CreateSolution(tree[index], qualities[index]);
|
---|
| 110 | nonDominatedSolutions.Add(solution);
|
---|
| 111 | nonDominatedQualities.Add(new DoubleArray(qualities[index]));
|
---|
| 112 | }
|
---|
| 113 | // add old non-dominated solutions only if they are not dominated by one of the new solutions
|
---|
| 114 | for (int i = 0; i < trainingBestQualities.Count; i++) {
|
---|
| 115 | if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
|
---|
[5742] | 116 | if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
|
---|
| 117 | nonDominatedSolutions.Add(TrainingBestSolutions[i]);
|
---|
| 118 | nonDominatedQualities.Add(TrainingBestSolutionQualities[i]);
|
---|
| 119 | }
|
---|
[5557] | 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[5747] | 123 | results[TrainingBestSolutionsParameter.Name].Value = nonDominatedSolutions;
|
---|
| 124 | results[TrainingBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
|
---|
[5557] | 125 | }
|
---|
| 126 | #endregion
|
---|
| 127 | return base.Apply();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[5742] | 130 | private class DoubleArrayComparer : IEqualityComparer<double[]> {
|
---|
| 131 | public bool Equals(double[] x, double[] y) {
|
---|
| 132 | if (y.Length != x.Length) throw new ArgumentException();
|
---|
| 133 | for (int i = 0; i < x.Length;i++ ) {
|
---|
| 134 | if (!x[i].IsAlmost(y[i])) return false;
|
---|
| 135 | }
|
---|
| 136 | return true;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | public int GetHashCode(double[] obj) {
|
---|
| 140 | int c = obj.Length;
|
---|
| 141 | for (int i = 0; i < obj.Length; i++)
|
---|
| 142 | c ^= obj[i].GetHashCode();
|
---|
| 143 | return c;
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[5557] | 147 | protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
|
---|
| 148 |
|
---|
| 149 | private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
|
---|
| 150 | foreach (var refPoint in points) {
|
---|
| 151 | bool refPointDominatesPoint = true;
|
---|
| 152 | for (int i = 0; i < point.Length; i++) {
|
---|
[5742] | 153 | refPointDominatesPoint &= IsBetterOrEqual(refPoint[i], point[i], maximization[i]);
|
---|
[5557] | 154 | }
|
---|
| 155 | if (refPointDominatesPoint) return false;
|
---|
| 156 | }
|
---|
| 157 | return true;
|
---|
| 158 | }
|
---|
[5742] | 159 | private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
|
---|
[5557] | 160 | if (maximization) return lhs > rhs;
|
---|
| 161 | else return lhs < rhs;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|