1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System.Linq;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HEAL.Attic;
|
---|
30 | using HeuristicLab.Problems.DataAnalysis;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
33 | [Item("NcaModelCreator", "Creates an NCA model with a given matrix.")]
|
---|
34 | [StorableType("30D2840C-1FE3-4A45-97FF-294C93D33D8C")]
|
---|
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(StorableConstructorFlag _) : base(_) { }
|
---|
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 | }
|
---|