Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer.cs @ 8126

Last change on this file since 8126 was 8126, checked in by gkronber, 12 years ago

#1823 fixed a bug in the Pareto-best solution analyzers for symbolic data analysis. Fixed a minor bug in the calculation of classification thresholds, made a sneaky change in SymbolicExpressionTree

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using System;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
35  /// <summary>
36  /// An operator that collects the Pareto-best symbolic data analysis solutions for single objective symbolic data analysis problems.
37  /// </summary>
38  [Item("SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer", "An operator that analyzes the Pareto-best symbolic data analysis solution for single objective symbolic data analysis problems.")]
39  [StorableClass]
40  public abstract class SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer
41    where T : class, ISymbolicDataAnalysisSolution {
42    private const string TrainingBestSolutionsParameterName = "Best training solutions";
43    private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities";
44    private const string ComplexityParameterName = "Complexity";
45
46    public override bool EnabledByDefault {
47      get { return false; }
48    }
49
50    #region parameter properties
51    public ILookupParameter<ItemList<T>> TrainingBestSolutionsParameter {
52      get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsParameterName]; }
53    }
54    public ILookupParameter<ItemList<DoubleArray>> TrainingBestSolutionQualitiesParameter {
55      get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[TrainingBestSolutionQualitiesParameterName]; }
56    }
57    public IScopeTreeLookupParameter<DoubleValue> ComplexityParameter {
58      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[ComplexityParameterName]; }
59    }
60    #endregion
61    #region properties
62    public ItemList<T> TrainingBestSolutions {
63      get { return TrainingBestSolutionsParameter.ActualValue; }
64      set { TrainingBestSolutionsParameter.ActualValue = value; }
65    }
66    public ItemList<DoubleArray> TrainingBestSolutionQualities {
67      get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
68      set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
69    }
70    #endregion
71
72    [StorableConstructor]
73    protected SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
74    protected SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
75    public SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer()
76      : base() {
77      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions."));
78      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
79      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(ComplexityParameterName, "The complexity of each tree."));
80    }
81
82    public override IOperation Apply() {
83      var results = ResultCollection;
84      // create empty parameter and result values
85      if (TrainingBestSolutions == null) {
86        TrainingBestSolutions = new ItemList<T>();
87        TrainingBestSolutionQualities = new ItemList<DoubleArray>();
88        results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
89        results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
90      }
91
92      IList<Tuple<double, double>> trainingBestQualities = TrainingBestSolutionQualities
93        .Select(x => Tuple.Create(x[0], x[1]))
94        .ToList();
95
96      #region find best trees
97      IList<int> nonDominatedIndexes = new List<int>();
98      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
99      List<double> qualities = Quality.Select(x => x.Value).ToList();
100
101      List<double> complexities;
102      if (ComplexityParameter.ActualValue != null && ComplexityParameter.ActualValue.Length == qualities.Count) {
103        complexities = ComplexityParameter.ActualValue.Select(x => x.Value).ToList();
104      } else {
105        complexities = tree.Select(t => (double)t.Length).ToList();
106      }
107      List<Tuple<double, double>> fitness = new List<Tuple<double, double>>();
108      for (int i = 0; i < qualities.Count; i++)
109        fitness.Add(Tuple.Create(qualities[i], complexities[i]));
110      var maximization = Tuple.Create(Maximization.Value, false);// complexity must be minimized
111      List<Tuple<double, double>> newNonDominatedQualities = new List<Tuple<double, double>>();
112      for (int i = 0; i < tree.Length; i++) {
113        if (IsNonDominated(fitness[i], trainingBestQualities, maximization) &&
114          IsNonDominated(fitness[i], newNonDominatedQualities, maximization) &&
115          IsNonDominated(fitness[i], fitness.Skip(i + 1), maximization)) {
116          if (!newNonDominatedQualities.Contains(fitness[i])) {
117            newNonDominatedQualities.Add(fitness[i]);
118            nonDominatedIndexes.Add(i);
119          }
120        }
121      }
122      #endregion
123
124      #region update Pareto-optimal solution archive
125      if (nonDominatedIndexes.Count > 0) {
126        ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
127        ItemList<T> nonDominatedSolutions = new ItemList<T>();
128        // add all new non-dominated solutions to the archive
129        foreach (var index in nonDominatedIndexes) {
130          T solution = CreateSolution(tree[index]);
131          nonDominatedSolutions.Add(solution);
132          nonDominatedQualities.Add(new DoubleArray(new double[] { fitness[index].Item1, fitness[index].Item2 }));
133        }
134        // add old non-dominated solutions only if they are not dominated by one of the new solutions
135        for (int i = 0; i < trainingBestQualities.Count; i++) {
136          if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
137            if (!newNonDominatedQualities.Contains(trainingBestQualities[i])) {
138              nonDominatedSolutions.Add(TrainingBestSolutions[i]);
139              nonDominatedQualities.Add(TrainingBestSolutionQualities[i]);
140            }
141          }
142        }
143
144        // make sure solutions and qualities are ordered in the results
145        var orderedIndexes =
146          nonDominatedSolutions.Select((s, i) => i).OrderBy(i => nonDominatedQualities[i][0]).ToArray();
147
148        var orderedNonDominatedSolutions = new ItemList<T>();
149        var orderedNonDominatedQualities = new ItemList<DoubleArray>();
150        foreach (var i in orderedIndexes) {
151          orderedNonDominatedQualities.Add(nonDominatedQualities[i]);
152          orderedNonDominatedSolutions.Add(nonDominatedSolutions[i]);
153        }
154
155        TrainingBestSolutions = orderedNonDominatedSolutions;
156        TrainingBestSolutionQualities = orderedNonDominatedQualities;
157
158        results[TrainingBestSolutionsParameter.Name].Value = orderedNonDominatedSolutions;
159        results[TrainingBestSolutionQualitiesParameter.Name].Value = orderedNonDominatedQualities;
160      }
161      #endregion
162      return base.Apply();
163    }
164
165    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree);
166
167    private bool IsNonDominated(Tuple<double, double> point, IEnumerable<Tuple<double, double>> points, Tuple<bool, bool> maximization) {
168      return !points.Any(p => IsBetterOrEqual(p.Item1, point.Item1, maximization.Item1) &&
169                             IsBetterOrEqual(p.Item2, point.Item2, maximization.Item2));
170    }
171    private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
172      if (maximization) return lhs >= rhs;
173      else return lhs <= rhs;
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.