Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RegressionBenchmarks/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 7485

Last change on this file since 7485 was 7485, checked in by sforsten, 12 years ago

#1708:

  • changes according to mkommend's reviewing comments have been made
File size: 7.7 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
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, ISymbolicDataAnalysisSolution {
41    private const string TrainingBestSolutionsParameterName = "Best training solutions";
42    private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities";
43
44    #region parameter properties
45    public ILookupParameter<ItemList<T>> TrainingBestSolutionsParameter {
46      get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsParameterName]; }
47    }
48    public ILookupParameter<ItemList<DoubleArray>> TrainingBestSolutionQualitiesParameter {
49      get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[TrainingBestSolutionQualitiesParameterName]; }
50    }
51    #endregion
52    #region properties
53    public ItemList<T> TrainingBestSolutions {
54      get { return TrainingBestSolutionsParameter.ActualValue; }
55      set { TrainingBestSolutionsParameter.ActualValue = value; }
56    }
57    public ItemList<DoubleArray> TrainingBestSolutionQualities {
58      get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
59      set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
60    }
61    #endregion
62
63    [StorableConstructor]
64    protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
65    protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
66    public SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer()
67      : base() {
68      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions."));
69      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
70    }
71
72    public override IOperation Apply() {
73      var results = ResultCollection;
74      // create empty parameter and result values
75      if (TrainingBestSolutions == null) {
76        TrainingBestSolutions = new ItemList<T>();
77        TrainingBestSolutionQualities = new ItemList<DoubleArray>();
78        results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
79        results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
80      }
81
82      IList<double[]> trainingBestQualities = TrainingBestSolutionQualities
83        .Select(x => x.ToArray())
84        .ToList();
85
86      #region find best trees
87      IList<int> nonDominatedIndexes = new List<int>();
88      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
89      List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList();
90      bool[] maximization = Maximization.ToArray();
91      List<double[]> newNonDominatedQualities = new List<double[]>();
92      for (int i = 0; i < tree.Length; i++) {
93        if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
94          IsNonDominated(qualities[i], qualities, maximization)) {
95          if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
96            newNonDominatedQualities.Add(qualities[i]);
97            nonDominatedIndexes.Add(i);
98          }
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            if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
116              nonDominatedSolutions.Add(TrainingBestSolutions[i]);
117              nonDominatedQualities.Add(TrainingBestSolutionQualities[i]);
118            }
119          }
120        }
121
122        results[TrainingBestSolutionsParameter.Name].Value = nonDominatedSolutions;
123        results[TrainingBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
124      }
125      #endregion
126      return base.Apply();
127    }
128
129    private class DoubleArrayComparer : IEqualityComparer<double[]> {
130      public bool Equals(double[] x, double[] y) {
131        if (y.Length != x.Length) throw new ArgumentException();
132        for (int i = 0; i < x.Length;i++ ) {
133          if (!x[i].IsAlmost(y[i])) return false;
134        }
135        return true;
136      }
137
138      public int GetHashCode(double[] obj) {
139        int c = obj.Length;
140        for (int i = 0; i < obj.Length; i++)
141          c ^= obj[i].GetHashCode();
142        return c;
143      }
144    }
145
146    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
147
148    private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
149      foreach (var refPoint in points) {
150        bool refPointDominatesPoint = true;
151        for (int i = 0; i < point.Length; i++) {
152          refPointDominatesPoint &= IsBetterOrEqual(refPoint[i], point[i], maximization[i]);
153        }
154        if (refPointDominatesPoint) return false;
155      }
156      return true;
157    }
158    private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
159      if (maximization) return lhs > rhs;
160      else return lhs < rhs;
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.