Free cookie consent management tool by TermsFeed Policy Generator

source: branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCA.cs @ 8425

Last change on this file since 8425 was 8425, checked in by abeham, 12 years ago

#1913: Added several initialization methods (LDA, PCA, and Random)

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 System.Linq;
23using HeuristicLab.Algorithms.DataAnalysis;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.PluginInfrastructure;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.NCA {
34  /// <summary>
35  /// Neighborhood Components Analysis
36  /// </summary>
37  [Item("NCA", "Neihborhood Components Analysis is described in J. Goldberger, S. Roweis, G. Hinton, R. Salakhutdinov. 2005. Neighbourhood Component Analysis. Advances in Neural Information Processing Systems, 17. pp. 513-520.")]
38  [Creatable("Data Analysis")]
39  [StorableClass]
40  public sealed class NCA : FixedDataAnalysisAlgorithm<IClassificationProblem> {
41    #region Parameter Properties
42    public IValueLookupParameter<IntValue> KParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters["k"]; }
44    }
45    public IValueLookupParameter<IntValue> ReduceDimensionsParameter {
46      get { return (IValueLookupParameter<IntValue>)Parameters["ReduceDimensions"]; }
47    }
48    private IConstrainedValueParameter<INCAInitializer> InitializationParameter {
49      get { return (IConstrainedValueParameter<INCAInitializer>)Parameters["Initialization"]; }
50    }
51    #endregion
52
53    #region Properties
54    public IntValue K {
55      get { return KParameter.Value; }
56    }
57    public IntValue ReduceDimensions {
58      get { return ReduceDimensionsParameter.Value; }
59    }
60    #endregion
61
62    [StorableConstructor]
63    private NCA(bool deserializing) : base(deserializing) { }
64    private NCA(NCA original, Cloner cloner) : base(original, cloner) { }
65    public NCA()
66      : base() {
67      Parameters.Add(new ValueLookupParameter<IntValue>("k", "The k for the nearest neighbor.", new IntValue(1)));
68      Parameters.Add(new ValueLookupParameter<IntValue>("ReduceDimensions", "The number of dimensions that NCA should reduce the data to.", new IntValue(2)));
69      Parameters.Add(new ConstrainedValueParameter<INCAInitializer>("Initialization", "Which method should be used to initialize the matrix. Typically LDA (linear discriminant analysis) should provide a good estimate."));
70
71      INCAInitializer defaultInitializer = null;
72      foreach (var initializer in ApplicationManager.Manager.GetInstances<INCAInitializer>().OrderBy(x => x.ItemName)) {
73        if (initializer is LDAInitializer) defaultInitializer = initializer;
74        InitializationParameter.ValidValues.Add(initializer);
75      }
76      if (defaultInitializer != null) InitializationParameter.Value = defaultInitializer;
77
78      Problem = new ClassificationProblem();
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new NCA(this, cloner);
83    }
84
85    public override void Prepare() {
86      if (Problem != null) base.Prepare();
87    }
88
89    protected override void Run() {
90      var classification = NeighborhoodComponentsAnalysis.CreateNCASolution(Problem.ProblemData, K.Value, ReduceDimensions.Value, InitializationParameter.Value);
91      Results.Add(new Result("ClassificationSolution", "The classification solution.", classification));
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.