Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/ModelCreation/NcaModelCreator.cs @ 11210

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

#2115 merged r11170 (copyright update) into trunk

File size: 4.0 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 System.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  [Item("NcaModelCreator", "Creates an NCA model with a given matrix.")]
34  [StorableClass]
35  public class NcaModelCreator : SingleSuccessorOperator, INcaModelCreator {
36
37    public ILookupParameter<IntValue> KParameter {
38      get { return (ILookupParameter<IntValue>)Parameters["K"]; }
39    }
40
41    public ILookupParameter<IntValue> DimensionsParameter {
42      get { return (ILookupParameter<IntValue>)Parameters["Dimensions"]; }
43    }
44
45    public ILookupParameter<RealVector> NcaMatrixParameter {
46      get { return (ILookupParameter<RealVector>)Parameters["NcaMatrix"]; }
47    }
48
49    public ILookupParameter<RealVector> NcaMatrixGradientsParameter {
50      get { return (ILookupParameter<RealVector>)Parameters["NcaMatrixGradients"]; }
51    }
52
53    public ILookupParameter<IClassificationProblemData> ProblemDataParameter {
54      get { return (ILookupParameter<IClassificationProblemData>)Parameters["ProblemData"]; }
55    }
56
57    public ILookupParameter<INcaModel> NcaModelParameter {
58      get { return (ILookupParameter<INcaModel>)Parameters["NcaModel"]; }
59    }
60
61    [StorableConstructor]
62    protected NcaModelCreator(bool deserializing) : base(deserializing) { }
63    protected NcaModelCreator(NcaModelCreator original, Cloner cloner) : base(original, cloner) { }
64    public NcaModelCreator() {
65      Parameters.Add(new LookupParameter<IntValue>("K", "How many neighbors should be considered for classification."));
66      Parameters.Add(new LookupParameter<IntValue>("Dimensions", "The dimensions to which the feature space should be reduced to."));
67      Parameters.Add(new LookupParameter<RealVector>("NcaMatrix", "The optimized matrix."));
68      Parameters.Add(new LookupParameter<RealVector>("NcaMatrixGradients", "The gradients from the matrix that is being optimized."));
69      Parameters.Add(new LookupParameter<IClassificationProblemData>("ProblemData", "The classification problem data."));
70      Parameters.Add(new LookupParameter<INcaModel>("NcaModel", "The NCA model that should be created."));
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new NcaModelCreator(this, cloner);
75    }
76
77    public override IOperation Apply() {
78      var k = KParameter.ActualValue.Value;
79      var dim = DimensionsParameter.ActualValue.Value;
80      var vector = NcaMatrixParameter.ActualValue;
81      var matrix = new double[vector.Length / dim, dim];
82
83      for (int i = 0; i < matrix.GetLength(0); i++)
84        for (int j = 0; j < dim; j++) {
85          matrix[i, j] = vector[i * dim + j];
86        }
87
88      var problemData = ProblemDataParameter.ActualValue;
89      NcaModelParameter.ActualValue = new NcaModel(k, matrix, problemData.Dataset, problemData.TrainingIndices, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.ClassValues.ToArray());
90      return base.Apply();
91    }
92  }
93}
Note: See TracBrowser for help on using the repository browser.