Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 5593

Last change on this file since 5593 was 5557, checked in by gkronber, 14 years ago

#1418 implemented single-objective and multi-objective training best solution analyzers for symbolic data analysis plugins.

File size: 7.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  /// <summary>
35  /// An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.
36  /// </summary>
37  [Item("SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
38  [StorableClass]
39  public abstract class SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisMultiObjectiveAnalyzer
40    where T : class, IDataAnalysisSolution {
41    private const string TrainingBestSolutionsParameterName = "Best training solutions";
42    private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities";
43    private const string TrainingBestSolutionsResultName = TrainingBestSolutionsParameterName;
44    private const string TrainingBestSolutionQualitiesResultName = TrainingBestSolutionQualitiesParameterName;
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    #endregion
54    #region properties
55    public ItemList<T> TrainingBestSolutions {
56      get { return TrainingBestSolutionsParameter.ActualValue; }
57      set { TrainingBestSolutionsParameter.ActualValue = value; }
58    }
59    public ItemList<DoubleArray> TrainingBestSolutionQualities {
60      get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
61      set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
62    }
63    #endregion
64
65    [StorableConstructor]
66    protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
67    protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
68    public SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer()
69      : base() {
70      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analyis solutions."));
71      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
72    }
73
74    public override IOperation Apply() {
75      var results = ResultCollection;
76      // create empty parameter and result values
77      if (TrainingBestSolutions == null) {
78        TrainingBestSolutions = new ItemList<T>();
79        TrainingBestSolutionQualities = new ItemList<DoubleArray>();
80        results.Add(new Result(TrainingBestSolutionQualitiesResultName, TrainingBestSolutionQualities));
81        results.Add(new Result(TrainingBestSolutionsResultName, TrainingBestSolutions));
82      }
83
84      IList<double[]> trainingBestQualities = TrainingBestSolutionQualities
85        .Select(x => x.ToArray())
86        .ToList();
87
88      #region find best trees
89      IList<int> nonDominatedIndexes = new List<int>();
90      ISymbolicExpressionTree[] tree = SymbolicExpressionTrees.ToArray();
91      List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList();
92      bool[] maximization = Maximization.ToArray();
93      List<double[]> newNonDominatedQualities = new List<double[]>();
94      for (int i = 0; i < tree.Length; i++) {
95        if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
96          IsNonDominated(qualities[i], qualities, maximization)) {
97          newNonDominatedQualities.Add(qualities[i]);
98          nonDominatedIndexes.Add(i);
99        }
100      }
101      #endregion
102      #region update Pareto-optimal solution archive
103      if (nonDominatedIndexes.Count > 0) {
104        ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
105        ItemList<T> nonDominatedSolutions = new ItemList<T>();
106        // add all new non-dominated solutions to the archive
107        foreach (var index in nonDominatedIndexes) {
108          T solution = CreateSolution(tree[index], qualities[index]);
109          nonDominatedSolutions.Add(solution);
110          nonDominatedQualities.Add(new DoubleArray(qualities[index]));
111        }
112        // add old non-dominated solutions only if they are not dominated by one of the new solutions
113        for (int i = 0; i < trainingBestQualities.Count; i++) {
114          if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
115            nonDominatedSolutions.Add(TrainingBestSolutions[i]);
116            nonDominatedQualities.Add(TrainingBestSolutionQualities[i]);
117          }
118        }
119
120        results[TrainingBestSolutionsResultName].Value = nonDominatedSolutions;
121        results[TrainingBestSolutionQualitiesResultName].Value = nonDominatedQualities;
122      }
123      #endregion
124      return base.Apply();
125    }
126
127    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
128
129    private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
130      foreach (var refPoint in points) {
131        bool refPointDominatesPoint = true;
132        for (int i = 0; i < point.Length; i++) {
133          refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
134        }
135        if (refPointDominatesPoint) return false;
136      }
137      return true;
138    }
139    private bool IsBetter(double lhs, double rhs, bool maximization) {
140      if (maximization) return lhs > rhs;
141      else return lhs < rhs;
142    }
143  }
144}
Note: See TracBrowser for help on using the repository browser.