[8623] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8623] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Operators;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Parameters;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 32 | [StorableClass]
|
---|
| 33 | [Item(Name = "GaussianProcessClassificationSolutionCreator",
|
---|
| 34 | Description = "Creates a Gaussian process solution from a trained model.")]
|
---|
| 35 | public sealed class GaussianProcessClassificationSolutionCreator : SingleSuccessorOperator {
|
---|
| 36 | private const string ProblemDataParameterName = "ProblemData";
|
---|
| 37 | private const string ModelParameterName = "GaussianProcessClassificationModel";
|
---|
| 38 | private const string SolutionParameterName = "Solution";
|
---|
| 39 | private const string ResultsParameterName = "Results";
|
---|
| 40 | private const string TrainingAccuracyResultName = "Accuracy (training)";
|
---|
| 41 | private const string TestAccuracyResultName = "Accuracy (test)";
|
---|
[13283] | 42 | private const string CreateSolutionParameterName = "CreateSolution";
|
---|
[8623] | 43 |
|
---|
| 44 | #region Parameter Properties
|
---|
| 45 | public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
|
---|
| 46 | get { return (ILookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
|
---|
| 47 | }
|
---|
| 48 | public ILookupParameter<IDiscriminantFunctionClassificationSolution> SolutionParameter {
|
---|
| 49 | get { return (ILookupParameter<IDiscriminantFunctionClassificationSolution>)Parameters[SolutionParameterName]; }
|
---|
| 50 | }
|
---|
| 51 | public ILookupParameter<IGaussianProcessModel> ModelParameter {
|
---|
| 52 | get { return (ILookupParameter<IGaussianProcessModel>)Parameters[ModelParameterName]; }
|
---|
| 53 | }
|
---|
| 54 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 55 | get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
|
---|
| 56 | }
|
---|
[13283] | 57 | public ILookupParameter<BoolValue> CreateSolutionParameter {
|
---|
| 58 | get { return (ILookupParameter<BoolValue>)Parameters[CreateSolutionParameterName]; }
|
---|
| 59 | }
|
---|
[8623] | 60 | #endregion
|
---|
| 61 |
|
---|
| 62 | [StorableConstructor]
|
---|
| 63 | private GaussianProcessClassificationSolutionCreator(bool deserializing) : base(deserializing) { }
|
---|
| 64 | private GaussianProcessClassificationSolutionCreator(GaussianProcessClassificationSolutionCreator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 65 | public GaussianProcessClassificationSolutionCreator()
|
---|
| 66 | : base() {
|
---|
| 67 | // in
|
---|
| 68 | Parameters.Add(new LookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The classification problem data for the Gaussian process solution."));
|
---|
| 69 | Parameters.Add(new LookupParameter<IGaussianProcessModel>(ModelParameterName, "The Gaussian process classification model to use for the solution."));
|
---|
[13283] | 70 | Parameters.Add(new LookupParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run"));
|
---|
| 71 |
|
---|
[8623] | 72 | // in & out
|
---|
| 73 | Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
|
---|
| 74 | // out
|
---|
| 75 | Parameters.Add(new LookupParameter<IDiscriminantFunctionClassificationSolution>(SolutionParameterName, "The produced Gaussian process solution."));
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[13283] | 78 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 79 | private void AfterDeserialization() {
|
---|
| 80 | // BackwardsCompatibility3.3
|
---|
| 81 | #region Backwards compatible code, remove with 3.4
|
---|
| 82 | if (!Parameters.ContainsKey(CreateSolutionParameterName)) {
|
---|
| 83 | Parameters.Add(new LookupParameter<BoolValue>(CreateSolutionParameterName, "Flag that indicates if a solution should be produced at the end of the run"));
|
---|
| 84 | }
|
---|
| 85 | #endregion
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[8623] | 88 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 89 | return new GaussianProcessClassificationSolutionCreator(this, cloner);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public override IOperation Apply() {
|
---|
[13283] | 93 | if (ModelParameter.ActualValue != null && CreateSolutionParameter.ActualValue.Value == true) {
|
---|
[8623] | 94 | var m = (IGaussianProcessModel)ModelParameter.ActualValue.Clone();
|
---|
[8982] | 95 | m.FixParameters();
|
---|
[8623] | 96 | var data = (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone();
|
---|
[8679] | 97 | var model = new DiscriminantFunctionClassificationModel(m, new NormalDistributionCutPointsThresholdCalculator());
|
---|
[8623] | 98 | model.RecalculateModelParameters(data, data.TrainingIndices);
|
---|
| 99 | var s = model.CreateDiscriminantFunctionClassificationSolution(data);
|
---|
| 100 |
|
---|
| 101 | SolutionParameter.ActualValue = s;
|
---|
| 102 | var results = ResultsParameter.ActualValue;
|
---|
| 103 | if (!results.ContainsKey(SolutionParameterName)) {
|
---|
| 104 | results.Add(new Result(SolutionParameterName, "The Gaussian process classification solution", s));
|
---|
| 105 | results.Add(new Result(TrainingAccuracyResultName,
|
---|
| 106 | "The accuracy of the Gaussian process solution on the training partition.",
|
---|
| 107 | new DoubleValue(s.TrainingAccuracy)));
|
---|
| 108 | results.Add(new Result(TestAccuracyResultName,
|
---|
| 109 | "The accuracy of the Gaussian process solution on the test partition.",
|
---|
| 110 | new DoubleValue(s.TestAccuracy)));
|
---|
| 111 | } else {
|
---|
| 112 | results[SolutionParameterName].Value = s;
|
---|
| 113 | results[TrainingAccuracyResultName].Value = new DoubleValue(s.TrainingAccuracy);
|
---|
| 114 | results[TestAccuracyResultName].Value = new DoubleValue(s.TestAccuracy);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|
| 117 | return base.Apply();
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|