Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessClassification.cs @ 12708

Last change on this file since 12708 was 12708, checked in by mkommend, 9 years ago

#2025:Merged all changes regarding the new item dialog into stable.
#2387: Merged all changes regarding the type selector into stable.

File size: 6.2 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  /// Gaussian process least-squares classification data analysis algorithm.
36  /// </summary>
37  [Item("Gaussian Process Least-Squares Classification", "Gaussian process least-squares classification data analysis algorithm.")]
38  [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 160)]
39  [StorableClass]
40  public sealed class GaussianProcessClassification : GaussianProcessBase, IStorableContent {
41    public string Filename { get; set; }
42
43    public override Type ProblemType { get { return typeof(IClassificationProblem); } }
44    public new IClassificationProblem Problem {
45      get { return (IClassificationProblem)base.Problem; }
46      set { base.Problem = value; }
47    }
48
49    private const string ModelParameterName = "Model";
50
51    #region parameter properties
52    public IConstrainedValueParameter<IGaussianProcessClassificationModelCreator> GaussianProcessModelCreatorParameter {
53      get { return (IConstrainedValueParameter<IGaussianProcessClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
54    }
55    public IFixedValueParameter<GaussianProcessClassificationSolutionCreator> GaussianProcessSolutionCreatorParameter {
56      get { return (IFixedValueParameter<GaussianProcessClassificationSolutionCreator>)Parameters[SolutionCreatorParameterName]; }
57    }
58    #endregion
59
60    [StorableConstructor]
61    private GaussianProcessClassification(bool deserializing) : base(deserializing) { }
62    private GaussianProcessClassification(GaussianProcessClassification original, Cloner cloner)
63      : base(original, cloner) {
64      RegisterEventHandlers();
65    }
66    public GaussianProcessClassification()
67      : base(new ClassificationProblem()) {
68      this.name = ItemName;
69      this.description = ItemDescription;
70
71      var modelCreators = ApplicationManager.Manager.GetInstances<IGaussianProcessClassificationModelCreator>();
72      var defaultModelCreator = modelCreators.First(c => c is GaussianProcessClassificationModelCreator);
73
74      // GP regression and classification algorithms only differ in the model and solution creators,
75      // thus we use a common base class and use operator parameters to implement the specific versions.
76      // Different model creators can be implemented,
77      // but the solution creator is implemented in a generic fashion already and we don't allow derived solution creators
78      Parameters.Add(new ConstrainedValueParameter<IGaussianProcessClassificationModelCreator>(ModelCreatorParameterName, "The operator to create the Gaussian process model.",
79        new ItemSet<IGaussianProcessClassificationModelCreator>(modelCreators), defaultModelCreator));
80      // this parameter is not intended to be changed,
81      Parameters.Add(new FixedValueParameter<GaussianProcessClassificationSolutionCreator>(SolutionCreatorParameterName, "The solution creator for the algorithm",
82        new GaussianProcessClassificationSolutionCreator()));
83      Parameters[SolutionCreatorParameterName].Hidden = true;
84
85      ParameterizedModelCreators();
86      ParameterizeSolutionCreator(GaussianProcessSolutionCreatorParameter.Value);
87      RegisterEventHandlers();
88    }
89
90
91    [StorableHook(HookType.AfterDeserialization)]
92    private void AfterDeserialization() {
93      RegisterEventHandlers();
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new GaussianProcessClassification(this, cloner);
98    }
99
100    #region events
101    private void RegisterEventHandlers() {
102      GaussianProcessModelCreatorParameter.ValueChanged += ModelCreatorParameter_ValueChanged;
103    }
104
105    private void ModelCreatorParameter_ValueChanged(object sender, EventArgs e) {
106      ParameterizedModelCreator(GaussianProcessModelCreatorParameter.Value);
107    }
108    #endregion
109
110    private void ParameterizedModelCreators() {
111      foreach (var creator in GaussianProcessModelCreatorParameter.ValidValues) {
112        ParameterizedModelCreator(creator);
113      }
114    }
115
116    private void ParameterizedModelCreator(IGaussianProcessClassificationModelCreator modelCreator) {
117      modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
118      modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
119      modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
120
121      // parameter names fixed by the algorithm
122      modelCreator.ModelParameter.ActualName = ModelParameterName;
123      modelCreator.HyperparameterParameter.ActualName = HyperparameterParameterName;
124      modelCreator.HyperparameterGradientsParameter.ActualName = HyperparameterGradientsParameterName;
125      modelCreator.NegativeLogLikelihoodParameter.ActualName = NegativeLogLikelihoodParameterName;
126    }
127
128    private void ParameterizeSolutionCreator(GaussianProcessClassificationSolutionCreator solutionCreator) {
129      solutionCreator.ModelParameter.ActualName = ModelParameterName;
130      solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
131    }
132  }
133}
Note: See TracBrowser for help on using the repository browser.