Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer.cs @ 11303

Last change on this file since 11303 was 9363, checked in by spimming, 12 years ago

#1888:

  • Merged revisions from trunk
File size: 7.8 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 validation best symbolic data analysis solution for single objective symbolic data analysis problems.
36  /// </summary>
37  [Item("SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer", "An operator that analyzes the validation best symbolic data analysis solution for single objective symbolic data analysis problems.")]
38  [StorableClass]
39  public abstract class SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> : SymbolicDataAnalysisSingleObjectiveValidationAnalyzer<T, U>
40    where S : class, ISymbolicDataAnalysisSolution
41    where T : class, ISymbolicDataAnalysisSingleObjectiveEvaluator<U>
42    where U : class, IDataAnalysisProblemData {
43    private const string ValidationBestSolutionParameterName = "Best validation solution";
44    private const string ValidationBestSolutionQualityParameterName = "Best validation solution quality";
45    private const string UpdateAlwaysParameterName = "Always update best solution";
46
47    #region parameter properties
48    public ILookupParameter<S> ValidationBestSolutionParameter {
49      get { return (ILookupParameter<S>)Parameters[ValidationBestSolutionParameterName]; }
50    }
51    public ILookupParameter<DoubleValue> ValidationBestSolutionQualityParameter {
52      get { return (ILookupParameter<DoubleValue>)Parameters[ValidationBestSolutionQualityParameterName]; }
53    }
54    public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
55      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
56    }
57    #endregion
58    #region properties
59    public S ValidationBestSolution {
60      get { return ValidationBestSolutionParameter.ActualValue; }
61      set { ValidationBestSolutionParameter.ActualValue = value; }
62    }
63    public DoubleValue ValidationBestSolutionQuality {
64      get { return ValidationBestSolutionQualityParameter.ActualValue; }
65      set { ValidationBestSolutionQualityParameter.ActualValue = value; }
66    }
67    public BoolValue UpdateAlways {
68      get { return UpdateAlwaysParameter.Value; }
69    }
70    #endregion
71
72    [StorableConstructor]
73    protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
74    protected SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer(SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer<S, T, U> original, Cloner cloner) : base(original, cloner) { }
75    public SymbolicDataAnalysisSingleObjectiveValidationBestSolutionAnalyzer()
76      : base() {
77      Parameters.Add(new LookupParameter<S>(ValidationBestSolutionParameterName, "The validation best symbolic data analyis solution."));
78      Parameters.Add(new LookupParameter<DoubleValue>(ValidationBestSolutionQualityParameterName, "The quality of the validation best symbolic data analysis solution."));
79      Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best validation solution should always be updated regardless of its quality.", new BoolValue(false)));
80      UpdateAlwaysParameter.Hidden = true;
81    }
82
83    [StorableHook(HookType.AfterDeserialization)]
84    private void AfterDeserialization() {
85      if (!Parameters.ContainsKey(UpdateAlwaysParameterName)) {
86        Parameters.Add(new FixedValueParameter<BoolValue>(UpdateAlwaysParameterName, "Determines if the best training solution should always be updated regardless of its quality.", new BoolValue(false)));
87        UpdateAlwaysParameter.Hidden = true;
88      }
89    }
90
91    public override IOperation Apply() {
92      IEnumerable<int> rows = GenerateRowsToEvaluate();
93      if (!rows.Any()) return base.Apply();
94
95      #region find best tree
96      var evaluator = EvaluatorParameter.ActualValue;
97      var problemData = ProblemDataParameter.ActualValue;
98      double bestValidationQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
99      ISymbolicExpressionTree bestTree = null;
100      ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
101
102      // sort is ascending and we take the first n% => order so that best solutions are smallest
103      // sort order is determined by maximization parameter
104      double[] trainingQuality;
105      if (Maximization.Value) {
106        // largest values must be sorted first
107        trainingQuality = Quality.Select(x => -x.Value).ToArray();
108      } else {
109        // smallest values must be sorted first
110        trainingQuality = Quality.Select(x => x.Value).ToArray();
111      }
112
113      // sort trees by training qualities
114      Array.Sort(trainingQuality, tree);
115
116      // number of best training solutions to validate (at least 1)
117      int topN = (int)Math.Max(tree.Length * PercentageOfBestSolutionsParameter.ActualValue.Value, 1);
118
119      IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
120      // evaluate best n training trees on validiation set
121      var quality = tree
122        .Take(topN)
123        .AsParallel()
124        .Select(t => evaluator.Evaluate(childContext, t, problemData, rows))
125        .ToArray();
126
127      for (int i = 0; i < quality.Length; i++) {
128        if (IsBetter(quality[i], bestValidationQuality, Maximization.Value)) {
129          bestValidationQuality = quality[i];
130          bestTree = tree[i];
131        }
132      }
133      #endregion
134
135      var results = ResultCollection;
136      if (UpdateAlways.Value || ValidationBestSolutionQuality == null ||
137        IsBetter(bestValidationQuality, ValidationBestSolutionQuality.Value, Maximization.Value)) {
138        ValidationBestSolution = CreateSolution(bestTree, bestValidationQuality);
139        ValidationBestSolutionQuality = new DoubleValue(bestValidationQuality);
140
141        if (!results.ContainsKey(ValidationBestSolutionParameter.Name)) {
142          results.Add(new Result(ValidationBestSolutionParameter.Name, ValidationBestSolutionParameter.Description, ValidationBestSolution));
143          results.Add(new Result(ValidationBestSolutionQualityParameter.Name, ValidationBestSolutionQualityParameter.Description, ValidationBestSolutionQuality));
144        } else {
145          results[ValidationBestSolutionParameter.Name].Value = ValidationBestSolution;
146          results[ValidationBestSolutionQualityParameter.Name].Value = ValidationBestSolutionQuality;
147        }
148      }
149      return base.Apply();
150    }
151
152    protected abstract S CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality);
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}
Note: See TracBrowser for help on using the repository browser.