Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17352 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.4 KB
Line 
1
2#region License Information
3/* HeuristicLab
4 * Copyright (C) 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.Data;
28using HeuristicLab.Parameters;
29using HEAL.Attic;
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  [StorableType("5D8711E4-1A3F-45E7-83A5-E9BBAC239793")]
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    private const string CreateSolutionParameterName = "CreateSolution";
51
52    #region parameter properties
53    public IConstrainedValueParameter<IGaussianProcessClassificationModelCreator> GaussianProcessModelCreatorParameter {
54      get { return (IConstrainedValueParameter<IGaussianProcessClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
55    }
56    public IFixedValueParameter<GaussianProcessClassificationSolutionCreator> GaussianProcessSolutionCreatorParameter {
57      get { return (IFixedValueParameter<GaussianProcessClassificationSolutionCreator>)Parameters[SolutionCreatorParameterName]; }
58    }
59    public IFixedValueParameter<BoolValue> CreateSolutionParameter {
60      get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
61    }
62    #endregion
63    #region properties
64    public bool CreateSolution {
65      get { return CreateSolutionParameter.Value.Value; }
66      set { CreateSolutionParameter.Value.Value = value; }
67    }
68    #endregion
69
70    [StorableConstructor]
71    private GaussianProcessClassification(StorableConstructorFlag _) : base(_) { }
72    private GaussianProcessClassification(GaussianProcessClassification original, Cloner cloner)
73      : base(original, cloner) {
74      RegisterEventHandlers();
75    }
76    public GaussianProcessClassification()
77      : base(new ClassificationProblem()) {
78      this.name = ItemName;
79      this.description = ItemDescription;
80
81      var modelCreators = ApplicationManager.Manager.GetInstances<IGaussianProcessClassificationModelCreator>();
82      var defaultModelCreator = modelCreators.First(c => c is GaussianProcessClassificationModelCreator);
83
84      // GP regression and classification algorithms only differ in the model and solution creators,
85      // thus we use a common base class and use operator parameters to implement the specific versions.
86      // Different model creators can be implemented,
87      // but the solution creator is implemented in a generic fashion already and we don't allow derived solution creators
88      Parameters.Add(new ConstrainedValueParameter<IGaussianProcessClassificationModelCreator>(ModelCreatorParameterName, "The operator to create the Gaussian process model.",
89        new ItemSet<IGaussianProcessClassificationModelCreator>(modelCreators), defaultModelCreator));
90      // this parameter is not intended to be changed,
91      Parameters.Add(new FixedValueParameter<GaussianProcessClassificationSolutionCreator>(SolutionCreatorParameterName, "The solution creator for the algorithm",
92        new GaussianProcessClassificationSolutionCreator()));
93      Parameters[SolutionCreatorParameterName].Hidden = true;
94      // TODO: it would be better to deactivate the solution creator when this parameter is changed
95      Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
96      Parameters[CreateSolutionParameterName].Hidden = true;
97
98      ParameterizedModelCreators();
99      ParameterizeSolutionCreator(GaussianProcessSolutionCreatorParameter.Value);
100      RegisterEventHandlers();
101    }
102
103
104    [StorableHook(HookType.AfterDeserialization)]
105    private void AfterDeserialization() {
106      // BackwardsCompatibility3.3
107      #region Backwards compatible code, remove with 3.4
108      if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
109        Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
110        Parameters[CreateSolutionParameterName].Hidden = true;
111      }
112      #endregion
113      RegisterEventHandlers();
114    }
115
116    public override IDeepCloneable Clone(Cloner cloner) {
117      return new GaussianProcessClassification(this, cloner);
118    }
119
120    #region events
121    private void RegisterEventHandlers() {
122      GaussianProcessModelCreatorParameter.ValueChanged += ModelCreatorParameter_ValueChanged;
123    }
124
125    private void ModelCreatorParameter_ValueChanged(object sender, EventArgs e) {
126      ParameterizedModelCreator(GaussianProcessModelCreatorParameter.Value);
127    }
128    #endregion
129
130    private void ParameterizedModelCreators() {
131      foreach (var creator in GaussianProcessModelCreatorParameter.ValidValues) {
132        ParameterizedModelCreator(creator);
133      }
134    }
135
136    private void ParameterizedModelCreator(IGaussianProcessClassificationModelCreator modelCreator) {
137      modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
138      modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
139      modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
140
141      // parameter names fixed by the algorithm
142      modelCreator.ModelParameter.ActualName = ModelParameterName;
143      modelCreator.HyperparameterParameter.ActualName = HyperparameterParameterName;
144      modelCreator.HyperparameterGradientsParameter.ActualName = HyperparameterGradientsParameterName;
145      modelCreator.NegativeLogLikelihoodParameter.ActualName = NegativeLogLikelihoodParameterName;
146    }
147
148    private void ParameterizeSolutionCreator(GaussianProcessClassificationSolutionCreator solutionCreator) {
149      solutionCreator.ModelParameter.ActualName = ModelParameterName;
150      solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.