Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GaussianProcessEvolution/HeuristicLab.Problems.GaussianProcessTuning/BestSolutionAnalyzer.cs @ 8753

Last change on this file since 8753 was 8753, checked in by gkronber, 12 years ago

#1967 initial import of Gaussian process evolution plugin

File size: 5.6 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 BestSolutionParameterName = "Best solution";
20    private const string ResultsParameterName = "Results";
21    private const string ProblemDataParameterName = "ProblemData";
22    private const string InterpreterParameterName = "Interpreter";
23    private const string SolutionParameterName = "Solution";
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<Solution> BestSolutionParameter {
35      get { return (ILookupParameter<Solution>)Parameters[BestSolutionParameterName]; }
36    }
37    public ILookupParameter<ResultCollection> ResultParameter {
38      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
39    }
40    public ILookupParameter<IRegressionProblemData> ProblemDataParameter {
41      get { return (ILookupParameter<IRegressionProblemData>)Parameters[ProblemDataParameterName]; }
42    }
43    public ILookupParameter<Interpreter> InterpreterParameter {
44      get { return (ILookupParameter<Interpreter>)Parameters[InterpreterParameterName]; }
45    }
46
47    [StorableConstructor]
48    protected BestSolutionAnalyzer(bool deserializing) : base(deserializing) { }
49    protected BestSolutionAnalyzer(BestSolutionAnalyzer original, Cloner cloner)
50      : base(original, cloner) {
51    }
52
53    public BestSolutionAnalyzer() {
54      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The solution quality of the Gaussian process configuration."));
55      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The Gaussian process configuration to evaluate represented as symbolic expression tree."));
56      Parameters.Add(new ScopeTreeLookupParameter<IGaussianProcessSolution>(SolutionParameterName, "The solution of the Gaussian process algorithm."));
57      Parameters.Add(new LookupParameter<Solution>(BestSolutionParameterName, "The best Gaussian process configuration."));
58      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
59      Parameters.Add(new LookupParameter<IRegressionProblemData>(ProblemDataParameterName, ""));
60      Parameters.Add(new LookupParameter<Interpreter>(InterpreterParameterName, ""));
61    }
62
63    [StorableHook(HookType.AfterDeserialization)]
64    private void AfterDeserialization() {
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      return new BestSolutionAnalyzer(this, cloner);
69    }
70
71    public override IOperation Apply() {
72      var trees = SymbolicExpressionTreeParameter.ActualValue;
73      var qualities = QualityParameter.ActualValue;
74      var solutions = SolutionParameter.ActualValue;
75      // find max tree
76      double maxQuality = double.PositiveInfinity;
77      ISymbolicExpressionTree bestTree = null;
78      IGaussianProcessSolution bestSolution = null;
79      for (int i = 0; i < qualities.Length; i++) {
80        if (qualities[i].Value < maxQuality) {
81          maxQuality = qualities[i].Value;
82          bestTree = trees[i];
83          bestSolution = solutions[i];
84        }
85      }
86      var cloner = new Cloner();
87      var problemData = cloner.Clone(ProblemDataParameter.ActualValue);
88      var interpreter = cloner.Clone(InterpreterParameter.ActualValue);
89      bestTree = cloner.Clone(bestTree);
90      var bestModel = new GaussianProcessRegressionModel(bestTree, problemData, interpreter);
91      var bestSolutionConfiguration = new Solution(bestModel, problemData);
92
93      BestSolutionParameter.ActualValue = bestSolutionConfiguration;
94
95      var resultCollection = ResultParameter.ActualValue;
96      if (!resultCollection.ContainsKey(BestSolutionParameterName)) {
97        resultCollection.Add(new Result(BestSolutionParameterName, "The best Gaussian process solution", bestSolutionConfiguration));
98        resultCollection.Add(new Result(SolutionParameterName, "The Gaussian process algorithm solution", bestSolution));
99      } else {
100        resultCollection[BestSolutionParameterName].Value = bestSolutionConfiguration;
101        resultCollection[SolutionParameterName].Value = bestSolution;
102      }
103
104      return base.Apply();
105    }
106    public bool EnabledByDefault {
107      get { return true; }
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.