#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Linq; using HeuristicLab.Algorithms.DataAnalysis; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.PluginInfrastructure; using HeuristicLab.Problems.DataAnalysis; namespace HeuristicLab.Algorithms.NCA { /// /// Neighborhood Components Analysis /// [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.")] [Creatable("Data Analysis")] [StorableClass] public sealed class NCA : FixedDataAnalysisAlgorithm { #region Parameter Properties public IValueLookupParameter KParameter { get { return (IValueLookupParameter)Parameters["k"]; } } public IValueLookupParameter ReduceDimensionsParameter { get { return (IValueLookupParameter)Parameters["ReduceDimensions"]; } } private IConstrainedValueParameter InitializationParameter { get { return (IConstrainedValueParameter)Parameters["Initialization"]; } } #endregion #region Properties public IntValue K { get { return KParameter.Value; } } public IntValue ReduceDimensions { get { return ReduceDimensionsParameter.Value; } } #endregion [StorableConstructor] private NCA(bool deserializing) : base(deserializing) { } private NCA(NCA original, Cloner cloner) : base(original, cloner) { } public NCA() : base() { Parameters.Add(new ValueLookupParameter("k", "The k for the nearest neighbor.", new IntValue(1))); Parameters.Add(new ValueLookupParameter("ReduceDimensions", "The number of dimensions that NCA should reduce the data to.", new IntValue(2))); Parameters.Add(new ConstrainedValueParameter("Initialization", "Which method should be used to initialize the matrix. Typically LDA (linear discriminant analysis) should provide a good estimate.")); INCAInitializer defaultInitializer = null; foreach (var initializer in ApplicationManager.Manager.GetInstances().OrderBy(x => x.ItemName)) { if (initializer is LDAInitializer) defaultInitializer = initializer; InitializationParameter.ValidValues.Add(initializer); } if (defaultInitializer != null) InitializationParameter.Value = defaultInitializer; Problem = new ClassificationProblem(); } public override IDeepCloneable Clone(Cloner cloner) { return new NCA(this, cloner); } public override void Prepare() { if (Problem != null) base.Prepare(); } protected override void Run() { var classification = NeighborhoodComponentsAnalysis.CreateNCASolution(Problem.ProblemData, K.Value, ReduceDimensions.Value, InitializationParameter.Value); Results.Add(new Result("ClassificationSolution", "The classification solution.", classification)); } } }