Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer.cs @ 6760

Last change on this file since 6760 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 8.3 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;
32
33namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
34  /// <summary>
35  /// An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.
36  /// </summary>
37  [Item("SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
38  [StorableClass]
39  public abstract class SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisMultiObjectiveValidationAnalyzer<T, U>,
40    ISymbolicDataAnalysisMultiObjectiveAnalyzer
41    where S : class, ISymbolicDataAnalysisSolution
42    where T : class, ISymbolicDataAnalysisMultiObjectiveEvaluator<U>
43    where U : class, IDataAnalysisProblemData {
44    private const string ValidationBestSolutionsParameterName = "Best validation solutions";
45    private const string ValidationBestSolutionQualitiesParameterName = "Best validation solution qualities";
46
47    #region parameter properties
48    public ILookupParameter<ItemList<S>> ValidationBestSolutionsParameter {
49      get { return (ILookupParameter<ItemList<S>>)Parameters[ValidationBestSolutionsParameterName]; }
50    }
51    public ILookupParameter<ItemList<DoubleArray>> ValidationBestSolutionQualitiesParameter {
52      get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[ValidationBestSolutionQualitiesParameterName]; }
53    }
54    #endregion
55    #region properties
56    public ItemList<S> ValidationBestSolutions {
57      get { return ValidationBestSolutionsParameter.ActualValue; }
58      set { ValidationBestSolutionsParameter.ActualValue = value; }
59    }
60    public ItemList<DoubleArray> ValidationBestSolutionQualities {
61      get { return ValidationBestSolutionQualitiesParameter.ActualValue; }
62      set { ValidationBestSolutionQualitiesParameter.ActualValue = value; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
68    protected SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
69    public SymbolicDataAnalysisMultiObjectiveValidationBestSolutionAnalyzer()
70      : base() {
71      Parameters.Add(new LookupParameter<ItemList<S>>(ValidationBestSolutionsParameterName, "The validation best (Pareto-optimal) symbolic data analysis solutions."));
72      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(ValidationBestSolutionQualitiesParameterName, "The qualities of the validation best (Pareto-optimal) solutions."));
73    }
74
75    public override IOperation Apply() {
76      IEnumerable<int> rows = GenerateRowsToEvaluate();
77      if (!rows.Any()) return base.Apply();
78
79      var results = ResultCollection;
80      // create empty parameter and result values
81      if (ValidationBestSolutions == null) {
82        ValidationBestSolutions = new ItemList<S>();
83        ValidationBestSolutionQualities = new ItemList<DoubleArray>();
84        results.Add(new Result(ValidationBestSolutionQualitiesParameter.Name, ValidationBestSolutionQualitiesParameter.Description, ValidationBestSolutionQualities));
85        results.Add(new Result(ValidationBestSolutionsParameter.Name, ValidationBestSolutionsParameter.Description, ValidationBestSolutions));
86      }
87
88      IList<double[]> trainingBestQualities = ValidationBestSolutionQualities
89        .Select(x => x.ToArray())
90        .ToList();
91
92      #region find best trees
93      IList<int> nonDominatedIndexes = new List<int>();
94      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
95      bool[] maximization = Maximization.ToArray();
96      List<double[]> newNonDominatedQualities = new List<double[]>();
97      var evaluator = EvaluatorParameter.ActualValue;
98      var problemData = ProblemDataParameter.ActualValue;
99      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
100
101      var qualities = tree
102        .AsParallel()
103        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
104        .ToArray();
105      for (int i = 0; i < tree.Length; 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.