Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7726 was 7726, checked in by gkronber, 13 years ago

#1823

  • added analyzer that calculates the complexity of symbolic data analysis trees (weighted symbols)
  • added analyzer that collects the Pareto-optimal solutions regarding complexity and accuracy
File size: 8.8 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("SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for multi 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    #region parameter properties
47    public ILookupParameter<ItemList<T>> TrainingBestSolutionsParameter {
48      get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsParameterName]; }
49    }
50    public ILookupParameter<ItemList<DoubleArray>> TrainingBestSolutionQualitiesParameter {
51      get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[TrainingBestSolutionQualitiesParameterName]; }
52    }
53    public IScopeTreeLookupParameter<DoubleValue> ComplexityParameter {
54      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[ComplexityParameterName]; }
55    }
56    #endregion
57    #region properties
58    public ItemList<T> TrainingBestSolutions {
59      get { return TrainingBestSolutionsParameter.ActualValue; }
60      set { TrainingBestSolutionsParameter.ActualValue = value; }
61    }
62    public ItemList<DoubleArray> TrainingBestSolutionQualities {
63      get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
64      set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
65    }
66    #endregion
67
68    [StorableConstructor]
69    protected SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
70    protected SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
71    public SymbolicDataAnalysisSingleObjectiveTrainingParetoBestSolutionAnalyzer()
72      : base() {
73      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions."));
74      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
75      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(ComplexityParameterName, "The complexity of each tree."));
76    }
77
78    public override IOperation Apply() {
79      var results = ResultCollection;
80      // create empty parameter and result values
81      if (TrainingBestSolutions == null) {
82        TrainingBestSolutions = new ItemList<T>();
83        TrainingBestSolutionQualities = new ItemList<DoubleArray>();
84        results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
85        results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
86      }
87
88      IList<Tuple<double, double>> trainingBestQualities = TrainingBestSolutionQualities
89        .Select(x => Tuple.Create(x[0], x[1]))
90        .ToList();
91
92      #region find best trees
93      IList<int> nonDominatedIndexes = new List<int>();
94      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
95      List<double> qualities = Quality.Select(x => x.Value).ToList();
96
97      List<double> complexities;
98      if (ComplexityParameter.ActualValue != null) {
99        complexities = ComplexityParameter.ActualValue.Select(x => x.Value).ToList();
100      } else {
101        complexities = tree.Select(t => (double)t.Length).ToList();
102      }
103      List<Tuple<double, double>> fitness = new List<Tuple<double, double>>();
104      for (int i = 0; i < qualities.Count; i++)
105        fitness.Add(Tuple.Create(qualities[i], complexities[i]));
106      var maximization = Tuple.Create(Maximization.Value, false);// complexity must be minimized
107      List<Tuple<double, double>> newNonDominatedQualities = new List<Tuple<double, double>>();
108      for (int i = 0; i < tree.Length; i++) {
109        if (IsNonDominated(fitness[i], trainingBestQualities, maximization) &&
110          IsNonDominated(fitness[i], newNonDominatedQualities, maximization) &&
111          IsNonDominated(fitness[i], fitness.Skip(i + 1), maximization)) {
112          if (!newNonDominatedQualities.Contains(fitness[i])) {
113            newNonDominatedQualities.Add(fitness[i]);
114            nonDominatedIndexes.Add(i);
115          }
116        }
117      }
118      #endregion
119
120      #region update Pareto-optimal solution archive
121      if (nonDominatedIndexes.Count > 0) {
122        ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
123        ItemList<T> nonDominatedSolutions = new ItemList<T>();
124        // add all new non-dominated solutions to the archive
125        foreach (var index in nonDominatedIndexes) {
126          T solution = CreateSolution(tree[index]);
127          nonDominatedSolutions.Add(solution);
128          nonDominatedQualities.Add(new DoubleArray(new double[] { fitness[index].Item1, fitness[index].Item2 }));
129        }
130        // add old non-dominated solutions only if they are not dominated by one of the new solutions
131        for (int i = 0; i < trainingBestQualities.Count; i++) {
132          if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
133            if (!newNonDominatedQualities.Contains(trainingBestQualities[i])) {
134              nonDominatedSolutions.Add(TrainingBestSolutions[i]);
135              nonDominatedQualities.Add(TrainingBestSolutionQualities[i]);
136            }
137          }
138        }
139
140        // make sure solutions and qualities are ordered in the results
141        var orderedIndexes =
142          nonDominatedSolutions.Select((s, i) => i).OrderBy(i => nonDominatedQualities[i][0]).ToArray();
143
144        var orderedNonDominatedSolutions = new ItemList<T>();
145        var orderedNonDominatedQualities = new ItemList<DoubleArray>();
146        foreach (var i in orderedIndexes) {
147          orderedNonDominatedQualities.Add(nonDominatedQualities[i]);
148          orderedNonDominatedSolutions.Add(nonDominatedSolutions[i]);
149        }
150
151        TrainingBestSolutions = orderedNonDominatedSolutions;
152        TrainingBestSolutionQualities = orderedNonDominatedQualities;
153
154        results[TrainingBestSolutionsParameter.Name].Value = orderedNonDominatedSolutions;
155        results[TrainingBestSolutionQualitiesParameter.Name].Value = orderedNonDominatedQualities;
156      }
157      #endregion
158      return base.Apply();
159    }
160
161    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree);
162
163    private bool IsNonDominated(Tuple<double, double> point, IEnumerable<Tuple<double, double>> points, Tuple<bool, bool> maximization) {
164      return !points.Any(p => IsBetterOrEqual(p.Item1, point.Item1, maximization.Item1) &&
165                             IsBetterOrEqual(p.Item2, point.Item2, maximization.Item2));
166    }
167    private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
168      if (maximization) return lhs >= rhs;
169      else return lhs <= rhs;
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.