Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/ModelCreation/NcaModelCreator.cs @ 14237

Last change on this file since 14237 was 14237, checked in by gkronber, 8 years ago

#2650: work in progress..

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