[8623] | 1 |
|
---|
| 2 | #region License Information
|
---|
| 3 | /* HeuristicLab
|
---|
[15583] | 4 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8623] | 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 |
|
---|
| 23 | using System;
|
---|
[9096] | 24 | using System.Linq;
|
---|
[8623] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[13205] | 27 | using HeuristicLab.Data;
|
---|
[8623] | 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[9096] | 31 | using HeuristicLab.PluginInfrastructure;
|
---|
[8623] | 32 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// Gaussian process least-squares classification data analysis algorithm.
|
---|
| 37 | /// </summary>
|
---|
| 38 | [Item("Gaussian Process Least-Squares Classification", "Gaussian process least-squares classification data analysis algorithm.")]
|
---|
[12504] | 39 | [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 160)]
|
---|
[8623] | 40 | [StorableClass]
|
---|
[9096] | 41 | public sealed class GaussianProcessClassification : GaussianProcessBase, IStorableContent {
|
---|
[8623] | 42 | public string Filename { get; set; }
|
---|
| 43 |
|
---|
| 44 | public override Type ProblemType { get { return typeof(IClassificationProblem); } }
|
---|
| 45 | public new IClassificationProblem Problem {
|
---|
| 46 | get { return (IClassificationProblem)base.Problem; }
|
---|
| 47 | set { base.Problem = value; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[9096] | 50 | private const string ModelParameterName = "Model";
|
---|
[13205] | 51 | private const string CreateSolutionParameterName = "CreateSolution";
|
---|
[8623] | 52 |
|
---|
| 53 | #region parameter properties
|
---|
[9098] | 54 | public IConstrainedValueParameter<IGaussianProcessClassificationModelCreator> GaussianProcessModelCreatorParameter {
|
---|
[9096] | 55 | get { return (IConstrainedValueParameter<IGaussianProcessClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
|
---|
[8623] | 56 | }
|
---|
[9098] | 57 | public IFixedValueParameter<GaussianProcessClassificationSolutionCreator> GaussianProcessSolutionCreatorParameter {
|
---|
[9096] | 58 | get { return (IFixedValueParameter<GaussianProcessClassificationSolutionCreator>)Parameters[SolutionCreatorParameterName]; }
|
---|
[8623] | 59 | }
|
---|
[13205] | 60 | public IFixedValueParameter<BoolValue> CreateSolutionParameter {
|
---|
| 61 | get { return (IFixedValueParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
|
---|
| 62 | }
|
---|
[8623] | 63 | #endregion
|
---|
[13205] | 64 | #region properties
|
---|
| 65 | public bool CreateSolution {
|
---|
| 66 | get { return CreateSolutionParameter.Value.Value; }
|
---|
| 67 | set { CreateSolutionParameter.Value.Value = value; }
|
---|
| 68 | }
|
---|
| 69 | #endregion
|
---|
[8623] | 70 |
|
---|
| 71 | [StorableConstructor]
|
---|
| 72 | private GaussianProcessClassification(bool deserializing) : base(deserializing) { }
|
---|
| 73 | private GaussianProcessClassification(GaussianProcessClassification original, Cloner cloner)
|
---|
| 74 | : base(original, cloner) {
|
---|
[9096] | 75 | RegisterEventHandlers();
|
---|
[8623] | 76 | }
|
---|
| 77 | public GaussianProcessClassification()
|
---|
[9096] | 78 | : base(new ClassificationProblem()) {
|
---|
[8623] | 79 | this.name = ItemName;
|
---|
| 80 | this.description = ItemDescription;
|
---|
| 81 |
|
---|
[9096] | 82 | var modelCreators = ApplicationManager.Manager.GetInstances<IGaussianProcessClassificationModelCreator>();
|
---|
| 83 | var defaultModelCreator = modelCreators.First(c => c is GaussianProcessClassificationModelCreator);
|
---|
[8623] | 84 |
|
---|
[9096] | 85 | // GP regression and classification algorithms only differ in the model and solution creators,
|
---|
| 86 | // thus we use a common base class and use operator parameters to implement the specific versions.
|
---|
| 87 | // Different model creators can be implemented,
|
---|
| 88 | // but the solution creator is implemented in a generic fashion already and we don't allow derived solution creators
|
---|
| 89 | Parameters.Add(new ConstrainedValueParameter<IGaussianProcessClassificationModelCreator>(ModelCreatorParameterName, "The operator to create the Gaussian process model.",
|
---|
| 90 | new ItemSet<IGaussianProcessClassificationModelCreator>(modelCreators), defaultModelCreator));
|
---|
| 91 | // this parameter is not intended to be changed,
|
---|
| 92 | Parameters.Add(new FixedValueParameter<GaussianProcessClassificationSolutionCreator>(SolutionCreatorParameterName, "The solution creator for the algorithm",
|
---|
| 93 | new GaussianProcessClassificationSolutionCreator()));
|
---|
| 94 | Parameters[SolutionCreatorParameterName].Hidden = true;
|
---|
[13205] | 95 | // TODO: it would be better to deactivate the solution creator when this parameter is changed
|
---|
| 96 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 97 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
[8623] | 98 |
|
---|
[9096] | 99 | ParameterizedModelCreators();
|
---|
[9098] | 100 | ParameterizeSolutionCreator(GaussianProcessSolutionCreatorParameter.Value);
|
---|
[9096] | 101 | RegisterEventHandlers();
|
---|
| 102 | }
|
---|
[8623] | 103 |
|
---|
| 104 |
|
---|
[9096] | 105 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 106 | private void AfterDeserialization() {
|
---|
[13205] | 107 | // BackwardsCompatibility3.3
|
---|
| 108 | #region Backwards compatible code, remove with 3.4
|
---|
| 109 | if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
|
---|
| 110 | Parameters.Add(new FixedValueParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true)));
|
---|
| 111 | Parameters[CreateSolutionParameterName].Hidden = true;
|
---|
| 112 | }
|
---|
| 113 | #endregion
|
---|
[9096] | 114 | RegisterEventHandlers();
|
---|
| 115 | }
|
---|
[8623] | 116 |
|
---|
[9096] | 117 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 118 | return new GaussianProcessClassification(this, cloner);
|
---|
| 119 | }
|
---|
[8623] | 120 |
|
---|
[9096] | 121 | #region events
|
---|
| 122 | private void RegisterEventHandlers() {
|
---|
[9098] | 123 | GaussianProcessModelCreatorParameter.ValueChanged += ModelCreatorParameter_ValueChanged;
|
---|
[9096] | 124 | }
|
---|
[8623] | 125 |
|
---|
[9096] | 126 | private void ModelCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[9098] | 127 | ParameterizedModelCreator(GaussianProcessModelCreatorParameter.Value);
|
---|
[9096] | 128 | }
|
---|
| 129 | #endregion
|
---|
[8623] | 130 |
|
---|
[9096] | 131 | private void ParameterizedModelCreators() {
|
---|
[9098] | 132 | foreach (var creator in GaussianProcessModelCreatorParameter.ValidValues) {
|
---|
[9096] | 133 | ParameterizedModelCreator(creator);
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
[8623] | 136 |
|
---|
[9096] | 137 | private void ParameterizedModelCreator(IGaussianProcessClassificationModelCreator modelCreator) {
|
---|
[8623] | 138 | modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
|
---|
| 139 | modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName;
|
---|
| 140 | modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName;
|
---|
| 141 |
|
---|
[9096] | 142 | // parameter names fixed by the algorithm
|
---|
| 143 | modelCreator.ModelParameter.ActualName = ModelParameterName;
|
---|
| 144 | modelCreator.HyperparameterParameter.ActualName = HyperparameterParameterName;
|
---|
| 145 | modelCreator.HyperparameterGradientsParameter.ActualName = HyperparameterGradientsParameterName;
|
---|
| 146 | modelCreator.NegativeLogLikelihoodParameter.ActualName = NegativeLogLikelihoodParameterName;
|
---|
| 147 | }
|
---|
[8623] | 148 |
|
---|
[9096] | 149 | private void ParameterizeSolutionCreator(GaussianProcessClassificationSolutionCreator solutionCreator) {
|
---|
| 150 | solutionCreator.ModelParameter.ActualName = ModelParameterName;
|
---|
[8623] | 151 | solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|