Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer.cs @ 5747

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

#1418 renamed bounded evaluator, added base classes for single objective and multi objective validation analzers, added overfitting analyzers for symbolic regression and classification.

File size: 8.4 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;
32using System;
33
34namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
35  /// <summary>
36  /// An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.
37  /// </summary>
38  [Item("SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
39  [StorableClass]
40  public abstract class SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisMultiObjectiveValidationAnalyzer<T, U>,
41    ISymbolicDataAnalysisMultiObjectiveAnalyzer
42    where S : class, ISymbolicDataAnalysisSolution
43    where T : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<U>
44    where U : class, IDataAnalysisProblemData {
45    private const string ValidationBestSolutionsParameterName = "Best validation solutions";
46    private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
47
48    #region parameter properties
49    public ILookupParameter<ItemList<S>> ValidationBestSolutionsParameter {
50      get { return (ILookupParameter<ItemList<S>>)Parameters[ValidationBestSolutionsParameterName]; }
51    }
52    public ILookupParameter<ItemList<DoubleArray>> ValidationBestSolutionQualitiesParameter {
53      get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
54    }
55    #endregion
56    #region properties
57    public ItemList<S> ValidationBestSolutions {
58      get { return ValidationBestSolutionsParameter.ActualValue; }
59      set { ValidationBestSolutionsParameter.ActualValue = value; }
60    }
61    public ItemList<DoubleArray> ValidationBestSolutionQualities {
62      get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
63      set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
64    }
65    #endregion
66
67    [StorableConstructor]
68    protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
69    protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
70    public SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer()
71      : base() {
72      Parameters.Add(new LookupParameter<ItemList<S>>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
73      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
74    }
75
76    public override IOperation Apply() {
77      var results = ResultCollection;
78      // create empty parameter and result values
79      if (ValidationBestSolutions == null) {
80        ValidationBestSolutions = new ItemList<S>();
81        ValidationBestSolutionQualities = new ItemList<DoubleArray>();
82        results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
83        results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
84      }
85
86      IList<double[]> trainingBestQualities = ValidationBestSolutionQualities
87        .Select(x => x.ToArray())
88        .ToList();
89
90      #region find best trees
91      IList<int> nonDominatedIndexes = new List<int>();
92      ISymbolicExpressionTree[] tree = SymbolicExpressionTrees.ToArray();
93      List<double[]> qualities = new List<double[]>();
94      bool[] maximization = Maximization.ToArray();
95      List<double[]> newNonDominatedQualities = new List<double[]>();
96      var evaluator = Evaluator;
97      int start = ValidationSamplesStart.Value;
98      int end = ValidationSamplesEnd.Value;
99      IEnumerable<int> rows = Enumerable.Range(start, end - start);
100      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
101      for (int i = 0; i < tree.Length; i++) {
102        qualities.Add(evaluator.Evaluate(childContext, tree[i], ProblemData, rows)); // qualities[i] = ...
103        if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
104          IsNonDominated(qualities[i], qualities, maximization)) {
105          if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
106            newNonDominatedQualities.Add(qualities[i]);
107            nonDominatedIndexes.Add(i);
108          }
109        }
110      }
111      #endregion
112      #region update Pareto-optimal solution archive
113      if (nonDominatedIndexes.Count > 0) {
114        ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
115        ItemList<S> nonDominatedSolutions = new ItemList<S>();
116        // add all new non-dominated solutions to the archive
117        foreach (var index in nonDominatedIndexes) {
118          S solution = CreateSolution(tree[index], qualities[index]);
119          nonDominatedSolutions.Add(solution);
120          nonDominatedQualities.Add(new DoubleArray(qualities[index]));
121        }
122        // add old non-dominated solutions only if they are not dominated by one of the new solutions
123        for (int i = 0; i < trainingBestQualities.Count; i++) {
124          if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
125            if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
126              nonDominatedSolutions.Add(ValidationBestSolutions[i]);
127              nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
128            }
129          }
130        }
131
132        results[ValidationBestSolutionsParameter.Name].Value = nonDominatedSolutions;
133        results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
134      }
135      #endregion
136      return base.Apply();
137    }
138
139    protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
140
141    private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
142      foreach (var refPoint in points) {
143        bool refPointDominatesPoint = true;
144        for (int i = 0; i < point.Length; i++) {
145          refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
146        }
147        if (refPointDominatesPoint) return false;
148      }
149      return true;
150    }
151    private bool IsBetter(double lhs, double rhs, bool maximization) {
152      if (maximization) return lhs > rhs;
153      else return lhs < rhs;
154    }
155
156    private class DoubleArrayComparer : IEqualityComparer<double[]> {
157      public bool Equals(double[] x, double[] y) {
158        if (y.Length != x.Length) throw new ArgumentException();
159        for (int i = 0; i < x.Length; i++) {
160          if (!x[i].IsAlmost(y[i])) return false;
161        }
162        return true;
163      }
164
165      public int GetHashCode(double[] obj) {
166        int c = obj.Length;
167        for (int i = 0; i < obj.Length; i++)
168          c ^= obj[i].GetHashCode();
169        return c;
170      }
171    }
172
173  }
174}
Note: See TracBrowser for help on using the repository browser.