Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/NcaInitializer.cs @ 9270

Last change on this file since 9270 was 9270, checked in by abeham, 11 years ago

#1913: Changed NCA to use LM-BFGS optimization algorithm, added model/solution creators, added operator for gradient calculation

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Encodings.RealVectorEncoding;
26using HeuristicLab.Operators;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [Item("NcaInitializer", "Base class for initializers for NCA.")]
33  [StorableClass]
34  public abstract class NcaInitializer : SingleSuccessorOperator, INcaInitializer {
35
36    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
37      get { return (ILookupParameter<IClassificationProblemData>)Parameters["ProblemData"]; }
38    }
39    public ILookupParameter<IntValue> DimensionsParameter {
40      get { return (ILookupParameter<IntValue>)Parameters["Dimensions"]; }
41    }
42    public ILookupParameter<RealVector> NcaMatrixParameter {
43      get { return (ILookupParameter<RealVector>)Parameters["NcaMatrix"]; }
44    }
45    public ILookupParameter<Scaling> ScalingParameter {
46      get { return (ILookupParameter<Scaling>)Parameters["Scaling"]; }
47    }
48
49    [StorableConstructor]
50    protected NcaInitializer(bool deserializing) : base(deserializing) { }
51    protected NcaInitializer(NcaInitializer original, Cloner cloner) : base(original, cloner) { }
52    public NcaInitializer() {
53      Parameters.Add(new LookupParameter<IClassificationProblemData>("ProblemData", "The classification problem data."));
54      Parameters.Add(new LookupParameter<IntValue>("Dimensions", "The number of dimensions to which the features should be pruned."));
55      Parameters.Add(new LookupParameter<RealVector>("NcaMatrix", "The coefficients of the matrix that need to be optimized. Note that the matrix is flattened."));
56      Parameters.Add(new LookupParameter<Scaling>("Scaling", "Each dataset is scaled and the information is stored in a scaling object."));
57    }
58
59    public override IOperation Apply() {
60      var problemData = ProblemDataParameter.ActualValue;
61      var scaling = new Scaling(problemData.Dataset, problemData.AllowedInputVariables, problemData.TrainingIndices);
62
63      var dimensions = DimensionsParameter.ActualValue.Value;
64      var matrix = Initialize(problemData, scaling, dimensions);
65      var attributes = matrix.GetLength(0);
66
67      var result = new double[attributes * dimensions];
68      for (int i = 0; i < attributes; i++)
69        for (int j = 0; j < dimensions; j++)
70          result[i * dimensions + j] = matrix[i, j];
71
72      ScalingParameter.ActualValue = scaling;
73      NcaMatrixParameter.ActualValue = new RealVector(result);
74      return base.Apply();
75    }
76
77    public abstract double[,] Initialize(IClassificationProblemData data, Scaling scaling, int dimensions);
78  }
79}
Note: See TracBrowser for help on using the repository browser.