Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Algorithms.DataAnalysis/3.4/GaussianProcess/GaussianProcessClassificationSolutionCreator.cs @ 11642

Last change on this file since 11642 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 5.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace 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)";
42
43    #region Parameter Properties
44    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
45      get { return (ILookupParameter<IClassificationProblemData>)Parameters[ProblemDataParameterName]; }
46    }
47    public ILookupParameter<IDiscriminantFunctionClassificationSolution> SolutionParameter {
48      get { return (ILookupParameter<IDiscriminantFunctionClassificationSolution>)Parameters[SolutionParameterName]; }
49    }
50    public ILookupParameter<IGaussianProcessModel> ModelParameter {
51      get { return (ILookupParameter<IGaussianProcessModel>)Parameters[ModelParameterName]; }
52    }
53    public ILookupParameter<ResultCollection> ResultsParameter {
54      get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
55    }
56    #endregion
57
58    [StorableConstructor]
59    private GaussianProcessClassificationSolutionCreator(bool deserializing) : base(deserializing) { }
60    private GaussianProcessClassificationSolutionCreator(GaussianProcessClassificationSolutionCreator original, Cloner cloner) : base(original, cloner) { }
61    public GaussianProcessClassificationSolutionCreator()
62      : base() {
63      // in
64      Parameters.Add(new LookupParameter<IClassificationProblemData>(ProblemDataParameterName, "The classification problem data for the Gaussian process solution."));
65      Parameters.Add(new LookupParameter<IGaussianProcessModel>(ModelParameterName, "The Gaussian process classification model to use for the solution."));
66      // in & out
67      Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection of the algorithm."));
68      // out
69      Parameters.Add(new LookupParameter<IDiscriminantFunctionClassificationSolution>(SolutionParameterName, "The produced Gaussian process solution."));
70    }
71
72    public override IDeepCloneable Clone(Cloner cloner) {
73      return new GaussianProcessClassificationSolutionCreator(this, cloner);
74    }
75
76    public override IOperation Apply() {
77      if (ModelParameter.ActualValue != null) {
78        var m = (IGaussianProcessModel)ModelParameter.ActualValue.Clone();
79        m.FixParameters();
80        var data = (IClassificationProblemData)ProblemDataParameter.ActualValue.Clone();
81        var model = new DiscriminantFunctionClassificationModel(m, new NormalDistributionCutPointsThresholdCalculator());
82        model.RecalculateModelParameters(data, data.TrainingIndices);
83        var s = model.CreateDiscriminantFunctionClassificationSolution(data);
84
85        SolutionParameter.ActualValue = s;
86        var results = ResultsParameter.ActualValue;
87        if (!results.ContainsKey(SolutionParameterName)) {
88          results.Add(new Result(SolutionParameterName, "The Gaussian process classification solution", s));
89          results.Add(new Result(TrainingAccuracyResultName,
90                                 "The accuracy of the Gaussian process solution on the training partition.",
91                                 new DoubleValue(s.TrainingAccuracy)));
92          results.Add(new Result(TestAccuracyResultName,
93                                 "The accuracy of the Gaussian process solution on the test partition.",
94                                 new DoubleValue(s.TestAccuracy)));
95        } else {
96          results[SolutionParameterName].Value = s;
97          results[TrainingAccuracyResultName].Value = new DoubleValue(s.TrainingAccuracy);
98          results[TestAccuracyResultName].Value = new DoubleValue(s.TestAccuracy);
99        }
100      }
101      return base.Apply();
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.