#region License Information
/* HeuristicLab
* Copyright (C) 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.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HEAL.Attic;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.DataAnalysis {
[Item("NcaModelCreator", "Creates an NCA model with a given matrix.")]
[StorableType("30D2840C-1FE3-4A45-97FF-294C93D33D8C")]
public class NcaModelCreator : SingleSuccessorOperator, INcaModelCreator {
public ILookupParameter KParameter {
get { return (ILookupParameter)Parameters["K"]; }
}
public ILookupParameter DimensionsParameter {
get { return (ILookupParameter)Parameters["Dimensions"]; }
}
public ILookupParameter NcaMatrixParameter {
get { return (ILookupParameter)Parameters["NcaMatrix"]; }
}
public ILookupParameter NcaMatrixGradientsParameter {
get { return (ILookupParameter)Parameters["NcaMatrixGradients"]; }
}
public ILookupParameter ProblemDataParameter {
get { return (ILookupParameter)Parameters["ProblemData"]; }
}
public ILookupParameter NcaModelParameter {
get { return (ILookupParameter)Parameters["NcaModel"]; }
}
[StorableConstructor]
protected NcaModelCreator(StorableConstructorFlag _) : base(_) { }
protected NcaModelCreator(NcaModelCreator original, Cloner cloner) : base(original, cloner) { }
public NcaModelCreator() {
Parameters.Add(new LookupParameter("K", "How many neighbors should be considered for classification."));
Parameters.Add(new LookupParameter("Dimensions", "The dimensions to which the feature space should be reduced to."));
Parameters.Add(new LookupParameter("NcaMatrix", "The optimized matrix."));
Parameters.Add(new LookupParameter("NcaMatrixGradients", "The gradients from the matrix that is being optimized."));
Parameters.Add(new LookupParameter("ProblemData", "The classification problem data."));
Parameters.Add(new LookupParameter("NcaModel", "The NCA model that should be created."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new NcaModelCreator(this, cloner);
}
public override IOperation Apply() {
var k = KParameter.ActualValue.Value;
var dim = DimensionsParameter.ActualValue.Value;
var vector = NcaMatrixParameter.ActualValue;
var matrix = new double[vector.Length / dim, dim];
for (int i = 0; i < matrix.GetLength(0); i++)
for (int j = 0; j < dim; j++) {
matrix[i, j] = vector[i * dim + j];
}
var problemData = ProblemDataParameter.ActualValue;
NcaModelParameter.ActualValue = new NcaModel(k, matrix, problemData.Dataset, problemData.TrainingIndices, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.ClassValues.ToArray());
return base.Apply();
}
}
}