Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GaussianProcessTuning/HeuristicLab.Problems.GaussianProcessTuning/BestSolutionAnalyzer.cs @ 10757

Last change on this file since 10757 was 10757, checked in by gkronber, 10 years ago

#1967 removed obsolete classes and cleaned up the implementation. Added the ProblemInstanceProvider interface to allow loading of CSV files.

File size: 4.8 KB
Line 
1using HeuristicLab.Algorithms.DataAnalysis;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Data;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Operators;
7using HeuristicLab.Optimization;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.Problems.DataAnalysis;
11
12namespace HeuristicLab.Problems.GaussianProcessTuning {
13  [StorableClass]
14  [Item("Gaussian Process Solution Analyzer", "Analyzer that stores the best Gaussian process tuning solution.")]
15  public class BestSolutionAnalyzer : SingleSuccessorOperator, ISymbolicExpressionTreeAnalyzer {
16
17    private const string QualityParameterName = "Quality";
18    private const string SymbolicExpressionTreeParameterName = "GaussianProcessConfiguration";
19    private const string ResultsParameterName = "Results";
20    private const string ProblemDataParameterName = "ProblemData";
21    private const string InterpreterParameterName = "Interpreter";
22    private const string SolutionParameterName = "Solution";
23    private const string TreeParameterName = "Covariance function";
24
25    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
26      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }
27    }
28    public IScopeTreeLookupParameter<IGaussianProcessSolution> SolutionParameter {
29      get { return (IScopeTreeLookupParameter<IGaussianProcessSolution>)Parameters[SolutionParameterName]; }
30    }
31    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
32      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
33    }
34    public ILookupParameter<ResultCollection> ResultParameter {
35      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
36    }
37    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
38      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
39    }
40    public ILookupParameter<Interpreter> InterpreterParameter {
41      get { return (ILookupParameter<Interpreter>)Parameters[InterpreterParameterName]; }
42    }
43
44    [StorableConstructor]
45    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
46    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
47      : base(original, cloner) {
48    }
49
50    public BestSolutionAnalyzer() {
51      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the Gaussian process configuration."));
52      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The Gaussian process configuration to evaluate represented as symbolic expression tree."));
53      Parameters.Add(new ScopeTreeLookupParameter<IGaussianProcessSolution>(SolutionParameterName, "The solution of the Gaussian process algorithm."));
54      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
55      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, ""));
56      Parameters.Add(new LookupParameter<Interpreter>(InterpreterParameterName, ""));
57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new BestSolutionAnalyzer(this, cloner);
65    }
66
67    public override IOperation Apply() {
68      var trees = SymbolicExpressionTreeParameter.ActualValue;
69      var qualities = QualityParameter.ActualValue;
70      var solutions = SolutionParameter.ActualValue;
71      // find max tree
72      double maxQuality = double.PositiveInfinity;
73      ISymbolicExpressionTree bestTree = null;
74      IGaussianProcessSolution bestSolution = null;
75      for (int i = 0; i < qualities.Length; i++) {
76        if (qualities[i].Value < maxQuality) {
77          maxQuality = qualities[i].Value;
78          bestTree = trees[i];
79          bestSolution = solutions[i];
80        }
81      }
82      var cloner = new Cloner();
83      bestTree = cloner.Clone(bestTree);
84
85      var resultCollection = ResultParameter.ActualValue;
86      if (!resultCollection.ContainsKey(SolutionParameterName)) {
87        resultCollection.Add(new Result(SolutionParameterName, "The Gaussian process algorithm solution", bestSolution));
88        resultCollection.Add(new Result(TreeParameterName, "The best covariance function", bestTree));
89      } else {
90        resultCollection[SolutionParameterName].Value = bestSolution;
91        resultCollection[TreeParameterName].Value = bestTree;
92      }
93
94      return base.Apply();
95    }
96    public bool EnabledByDefault {
97      get { return true; }
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.