Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer.cs @ 11883

Last change on this file since 11883 was 11407, checked in by mkommend, 10 years ago

#2175: Added nested tree size evaluator and simplification operator.

File size: 9.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
33  /// <summary>
34  /// An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.
35  /// </summary>
36  [Item("SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for multi objective symbolic data analysis problems.")]
37  [StorableClass]
38  public abstract class SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisMultiObjectiveAnalyzer
39    where T : class, ISymbolicDataAnalysisSolution {
40    private const string TrainingBestSolutionsParameterName = "Best training solutions";
41    private const string TrainingBestSolutionQualitiesParameterName = "Best training solution qualities";
42    private const string UpdateAlwaysParameterName = "Always update best solutions";
43
44    private const string TrainingBestSolutionParameterName = "Best training solution";
45    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
46    private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
47
48    #region parameter properties
49    public ILookupParameter<ItemList<T>> TrainingBestSolutionsParameter {
50      get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsParameterName]; }
51    }
52    public ILookupParameter<ItemList<DoubleArray>> TrainingBestSolutionQualitiesParameter {
53      get { return (ILookupParameter<ItemList<DoubleArray>>)Parameters[TrainingBestSolutionQualitiesParameterName]; }
54    }
55    public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
56      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
57    }
58    #endregion
59    #region properties
60    public ItemList<T> TrainingBestSolutions {
61      get { return TrainingBestSolutionsParameter.ActualValue; }
62      set { TrainingBestSolutionsParameter.ActualValue = value; }
63    }
64    public ItemList<DoubleArray> TrainingBestSolutionQualities {
65      get { return TrainingBestSolutionQualitiesParameter.ActualValue; }
66      set { TrainingBestSolutionQualitiesParameter.ActualValue = value; }
67    }
68    public BoolValue UpdateAlways {
69      get { return UpdateAlwaysParameter.Value; }
70    }
71    #endregion
72
73    [StorableConstructor]
74    protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
75    protected SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer(SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer<T> original, Cloner cloner) : base(original, cloner) { }
76    public SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer()
77      : base() {
78      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsParameterName, "The training best (Pareto-optimal) symbolic data analysis solutions."));
79      Parameters.Add(new LookupParameter<ItemList<DoubleArray>>(TrainingBestSolutionQualitiesParameterName, "The qualities of the training best (Pareto-optimal) solutions."));
80      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
81      UpdateAlwaysParameter.Hidden = true;
82    }
83
84    [StorableHook(HookType.AfterDeserialization)]
85    private void AfterDeserialization() {
86      if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
87        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solutions should always be updated regardless of its quality.", new BoolValue(false)));
88        UpdateAlwaysParameter.Hidden = true;
89      }
90    }
91
92    public override IOperation Apply() {
93      var results = ResultCollection;
94      // create empty parameter and result values
95      if (TrainingBestSolutions == null) {
96        TrainingBestSolutions = new ItemList<T>();
97        TrainingBestSolutionQualities = new ItemList<DoubleArray>();
98        results.Add(new Result(TrainingBestSolutionQualitiesParameter.Name, TrainingBestSolutionQualitiesParameter.Description, TrainingBestSolutionQualities));
99        results.Add(new Result(TrainingBestSolutionsParameter.Name, TrainingBestSolutionsParameter.Description, TrainingBestSolutions));
100      }
101
102      if (!results.ContainsKey(TrainingBestSolutionParameterName)) {
103        results.Add(new Result(TrainingBestSolutionParameterName, "", typeof(ISymbolicDataAnalysisSolution)));
104      }
105
106      //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
107      List<double[]> trainingBestQualities;
108      if (UpdateAlways.Value) {
109        trainingBestQualities = new List<double[]>();
110      } else {
111        trainingBestQualities = TrainingBestSolutionQualities.Select(x => x.ToArray()).ToList();
112      }
113
114      ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray();
115      List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList();
116      bool[] maximization = Maximization.ToArray();
117
118      var nonDominatedInvididuals = new[] { new { Tree = default(ISymbolicExpressionTree), Qualities = default(double[]) } }.ToList();
119      nonDominatedInvididuals.Clear();
120
121      // build list of new non-dominated solutions
122      for (int i = 0; i < trees.Length; i++) {
123        if (IsNonDominated(qualities[i], nonDominatedInvididuals.Select(ind => ind.Qualities), maximization) &&
124            IsNonDominated(qualities[i], trainingBestQualities, maximization)) {
125          for (int j = nonDominatedInvididuals.Count - 1; j >= 0; j--) {
126            if (IsBetterOrEqual(qualities[i], nonDominatedInvididuals[j].Qualities, maximization)) {
127              nonDominatedInvididuals.RemoveAt(j);
128            }
129          }
130          nonDominatedInvididuals.Add(new { Tree = trees[i], Qualities = qualities[i] });
131        }
132      }
133
134      var nonDominatedSolutions = nonDominatedInvididuals.Select(x => new { Solution = CreateSolution(x.Tree, x.Qualities), Qualities = x.Qualities }).ToList();
135      nonDominatedSolutions.ForEach(s => s.Solution.Name = string.Join(",", s.Qualities.Select(q => q.ToString())));
136
137      #region update Pareto-optimal solution archive
138      if (nonDominatedSolutions.Count > 0) {
139        //add old non-dominated solutions only if they are not dominated by one of the new solutions
140        for (int i = 0; i < trainingBestQualities.Count; i++) {
141          if (IsNonDominated(trainingBestQualities[i], nonDominatedSolutions.Select(x => x.Qualities), maximization)) {
142            nonDominatedSolutions.Add(new { Solution = TrainingBestSolutions[i], Qualities = TrainingBestSolutionQualities[i].ToArray() });
143          }
144        }
145
146        var sortedNonDominatedSolutions = nonDominatedSolutions.OrderByDescending(x => x.Qualities[0]);
147        var trainingBestSolution = sortedNonDominatedSolutions.Select(s => s.Solution).First();
148        results[TrainingBestSolutionParameterName].Value = trainingBestSolution;
149        TrainingBestSolutions = new ItemList<T>(sortedNonDominatedSolutions.Select(x => x.Solution));
150        results[TrainingBestSolutionsParameter.Name].Value = TrainingBestSolutions;
151        TrainingBestSolutionQualities = new ItemList<DoubleArray>(sortedNonDominatedSolutions.Select(x => new DoubleArray(x.Qualities)));
152        results[TrainingBestSolutionQualitiesParameter.Name].Value = TrainingBestSolutionQualities;
153      }
154      #endregion
155      return base.Apply();
156    }
157
158    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
159
160    private bool IsNonDominated(double[] point, IEnumerable<double[]> points, bool[] maximization) {
161      foreach (var refPoint in points) {
162        bool refPointDominatesPoint = IsBetterOrEqual(refPoint, point, maximization);
163        if (refPointDominatesPoint) return false;
164      }
165      return true;
166    }
167
168    private bool IsBetterOrEqual(double[] lhs, double[] rhs, bool[] maximization) {
169      for (int i = 0; i < lhs.Length; i++) {
170        var result = IsBetterOrEqual(lhs[i], rhs[i], maximization[i]);
171        if (!result) return false;
172      }
173      return true;
174    }
175
176    private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
177      if (maximization) return lhs >= rhs;
178      else return lhs <= rhs;
179    }
180  }
181}
Note: See TracBrowser for help on using the repository browser.