#region License Information /* HeuristicLab * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.DataAnalysis { /// /// Gaussian process least-squares classification data analysis algorithm. /// [Item("Gaussian Process Least-Squares Classification", "Gaussian process least-squares classification data analysis algorithm.")] [Creatable(CreatableAttribute.Categories.DataAnalysisClassification, Priority = 160)] [StorableType("cf258d4e-2155-4613-8fdb-ea54c371949a")] public sealed class GaussianProcessClassification : GaussianProcessBase, IStorableContent { public string Filename { get; set; } public override Type ProblemType { get { return typeof(IClassificationProblem); } } public new IClassificationProblem Problem { get { return (IClassificationProblem)base.Problem; } set { base.Problem = value; } } private const string ModelParameterName = "Model"; private const string CreateSolutionParameterName = "CreateSolution"; #region parameter properties public IConstrainedValueParameter GaussianProcessModelCreatorParameter { get { return (IConstrainedValueParameter)Parameters[ModelCreatorParameterName]; } } public IFixedValueParameter GaussianProcessSolutionCreatorParameter { get { return (IFixedValueParameter)Parameters[SolutionCreatorParameterName]; } } public IFixedValueParameter CreateSolutionParameter { get { return (IFixedValueParameter)Parameters[CreateSolutionParameterName]; } } #endregion #region properties public bool CreateSolution { get { return CreateSolutionParameter.Value.Value; } set { CreateSolutionParameter.Value.Value = value; } } #endregion [StorableConstructor] private GaussianProcessClassification(StorableConstructorFlag deserializing) : base(deserializing) { } private GaussianProcessClassification(GaussianProcessClassification original, Cloner cloner) : base(original, cloner) { RegisterEventHandlers(); } public GaussianProcessClassification() : base(new ClassificationProblem()) { this.name = ItemName; this.description = ItemDescription; var modelCreators = ApplicationManager.Manager.GetInstances(); var defaultModelCreator = modelCreators.First(c => c is GaussianProcessClassificationModelCreator); // GP regression and classification algorithms only differ in the model and solution creators, // thus we use a common base class and use operator parameters to implement the specific versions. // Different model creators can be implemented, // but the solution creator is implemented in a generic fashion already and we don't allow derived solution creators Parameters.Add(new ConstrainedValueParameter(ModelCreatorParameterName, "The operator to create the Gaussian process model.", new ItemSet(modelCreators), defaultModelCreator)); // this parameter is not intended to be changed, Parameters.Add(new FixedValueParameter(SolutionCreatorParameterName, "The solution creator for the algorithm", new GaussianProcessClassificationSolutionCreator())); Parameters[SolutionCreatorParameterName].Hidden = true; // TODO: it would be better to deactivate the solution creator when this parameter is changed Parameters.Add(new FixedValueParameter(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true))); Parameters[CreateSolutionParameterName].Hidden = true; ParameterizedModelCreators(); ParameterizeSolutionCreator(GaussianProcessSolutionCreatorParameter.Value); RegisterEventHandlers(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { // BackwardsCompatibility3.3 #region Backwards compatible code, remove with 3.4 if (!Parameters.ContainsKey(CreateSolutionParameterName)) { Parameters.Add(new FixedValueParameter(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run", new BoolValue(true))); Parameters[CreateSolutionParameterName].Hidden = true; } #endregion RegisterEventHandlers(); } public override IDeepCloneable Clone(Cloner cloner) { return new GaussianProcessClassification(this, cloner); } #region events private void RegisterEventHandlers() { GaussianProcessModelCreatorParameter.ValueChanged += ModelCreatorParameter_ValueChanged; } private void ModelCreatorParameter_ValueChanged(object sender, EventArgs e) { ParameterizedModelCreator(GaussianProcessModelCreatorParameter.Value); } #endregion private void ParameterizedModelCreators() { foreach (var creator in GaussianProcessModelCreatorParameter.ValidValues) { ParameterizedModelCreator(creator); } } private void ParameterizedModelCreator(IGaussianProcessClassificationModelCreator modelCreator) { modelCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name; modelCreator.MeanFunctionParameter.ActualName = MeanFunctionParameterName; modelCreator.CovarianceFunctionParameter.ActualName = CovarianceFunctionParameterName; // parameter names fixed by the algorithm modelCreator.ModelParameter.ActualName = ModelParameterName; modelCreator.HyperparameterParameter.ActualName = HyperparameterParameterName; modelCreator.HyperparameterGradientsParameter.ActualName = HyperparameterGradientsParameterName; modelCreator.NegativeLogLikelihoodParameter.ActualName = NegativeLogLikelihoodParameterName; } private void ParameterizeSolutionCreator(GaussianProcessClassificationSolutionCreator solutionCreator) { solutionCreator.ModelParameter.ActualName = ModelParameterName; solutionCreator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name; } } }