Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis SolutionEnsembles/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer.cs @ 6004

Last change on this file since 6004 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 8.6 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;
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;
32using HeuristicLab.Random;
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      int start = ValidationPartitionParameter.ActualValue.Start;
78      int end = ValidationPartitionParameter.ActualValue.End;
79      int count = (int)((end - start) * RelativeNumberOfEvaluatedSamplesParameter.ActualValue.Value);
80      if (count <= 0) return base.Apply();
81
82      var results = ResultCollection;
83      // create empty parameter and result values
84      if (ValidationBestSolutions == null) {
85        ValidationBestSolutions = new ItemList<S>();
86        ValidationBestSolutionQualities = new ItemList<DoubleArray>();
87        results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
88        results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
89      }
90
91      IList<double[]> trainingBestQualities = ValidationBestSolutionQualities
92        .Select(x => x.ToArray())
93        .ToList();
94
95      #region find best trees
96      IList<int> nonDominatedIndexes = new List<int>();
97      ISymbolicExpressionTree[] tree = SymbolicExpressionTrees.ToArray();
98      List<double[]> qualities = new List<double[]>();
99      bool[] maximization = Maximization.ToArray();
100      List<double[]> newNonDominatedQualities = new List<double[]>();
101      var evaluator = EvaluatorParameter.ActualValue;
102      IEnumerable<int> rows = RandomEnumerable.SampleRandomNumbers(start, end, count);
103      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
104      for (int i = 0; i < tree.Length; i++) {
105        qualities.Add(evaluator.Evaluate(childContext, tree[i], ProblemDataParameter.ActualValue, rows)); // qualities[i] = ...
106        if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
107          IsNonDominated(qualities[i], qualities, maximization)) {
108          if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
109            newNonDominatedQualities.Add(qualities[i]);
110            nonDominatedIndexes.Add(i);
111          }
112        }
113      }
114      #endregion
115      #region update Pareto-optimal solution archive
116      if (nonDominatedIndexes.Count > 0) {
117        ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
118        ItemList<S> nonDominatedSolutions = new ItemList<S>();
119        // add all new non-dominated solutions to the archive
120        foreach (var index in nonDominatedIndexes) {
121          S solution = CreateSolution(tree[index], qualities[index]);
122          nonDominatedSolutions.Add(solution);
123          nonDominatedQualities.Add(new DoubleArray(qualities[index]));
124        }
125        // add old non-dominated solutions only if they are not dominated by one of the new solutions
126        for (int i = 0; i < trainingBestQualities.Count; i++) {
127          if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
128            if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
129              nonDominatedSolutions.Add(ValidationBestSolutions[i]);
130              nonDominatedQualities.Add(ValidationBestSolutionQualities[i]);
131            }
132          }
133        }
134
135        results[ValidationBestSolutionsParameter.Name].Value = nonDominatedSolutions;
136        results[ValidationBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
137      }
138      #endregion
139      return base.Apply();
140    }
141
142    protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
143
144    private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
145      foreach (var refPoint in points) {
146        bool refPointDominatesPoint = true;
147        for (int i = 0; i < point.Length; i++) {
148          refPointDominatesPoint &= IsBetter(refPoint[i], point[i], maximization[i]);
149        }
150        if (refPointDominatesPoint) return false;
151      }
152      return true;
153    }
154    private bool IsBetter(double lhs, double rhs, bool maximization) {
155      if (maximization) return lhs > rhs;
156      else return lhs < rhs;
157    }
158
159    private class DoubleArrayComparer : IEqualityComparer<double[]> {
160      public bool Equals(double[] x, double[] y) {
161        if (y.Length != x.Length) throw new ArgumentException();
162        for (int i = 0; i < x.Length; i++) {
163          if (!x[i].IsAlmost(y[i])) return false;
164        }
165        return true;
166      }
167
168      public int GetHashCode(double[] obj) {
169        int c = obj.Length;
170        for (int i = 0; i < obj.Length; i++)
171          c ^= obj[i].GetHashCode();
172        return c;
173      }
174    }
175
176  }
177}
Note: See TracBrowser for help on using the repository browser.