From de6db2a971bc378c63cb334be8d9c1615d69e4cb Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Tue, 8 Feb 2011 17:00:01 +0000 Subject: [PATCH] Update mvu code. At this point it does not work but it does compile. --- .../fastlib-stl/mlpack/mvu/mvu_objectives.h | 142 +-- .../mlpack/mvu/mvu_objectives_impl.h | 812 +++++++++--------- 2 files changed, 489 insertions(+), 465 deletions(-) diff --git a/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives.h b/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives.h index e3d83960ce..83c6054a6c 100644 --- a/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives.h +++ b/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives.h @@ -18,10 +18,11 @@ #ifndef MVU_OBJECTIVES_H_ #define MVU_OBJECTIVES_H_ -#include "fastlib/fastlib.h" -#include "mlpack/allknn/allknn.h" -#include "mlpack/allkfn/allkfn.h" -#include "fastlib/optimization/lbfgs/optimization_utils.h" + +#include +#include +#include +#include const fx_entry_doc mvu_entries[] = { {"new_dimension", FX_REQUIRED, FX_INT, NULL, @@ -56,33 +57,41 @@ const fx_module_doc mvu_doc = { class MaxVariance { public: - static const index_t MAX_KNNS=30; - void Init(fx_module *module, Matrix &data); - void Init(fx_module *module); + static const index_t MAX_KNNS = 30; + + void Init(fx_module* module, arma::mat& data); + void Init(fx_module* module); + void Destruct(); - void ComputeGradient(Matrix &coordinates, Matrix *gradient); - void ComputeObjective(Matrix &coordinates, double *objective); - void ComputeFeasibilityError(Matrix &coordinates, double *error); - double ComputeLagrangian(Matrix &coordinates); - void UpdateLagrangeMult(Matrix &coordinates); - void Project(Matrix *coordinates); - void set_sigma(double sigma); - bool IsDiverging(double objective); - bool IsOptimizationOver(Matrix &coordinates, - Matrix &gradient, double step) { return false;} - bool IsIntermediateStepOver(Matrix &coordinates, - Matrix &gradient, double step) { return true; } - void GiveInitMatrix(Matrix *init_data); - index_t num_of_points(); + + void ComputeGradient(const arma::mat& coordinates, arma::mat& gradient); + void ComputeObjective(const arma::mat& coordinates, double& objective); + void ComputeFeasibilityError(const arma::mat& coordinates, double& error); + double ComputeLagrangian(const arma::mat& coordinates); + void UpdateLagrangeMult(const arma::mat& coordinates); + void Project(arma::mat& coordinates); + void set_sigma(double sigma); + bool IsDiverging(double objective); + + // what the hell is this? + bool IsOptimizationOver(const arma::mat& coordinates, arma::mat& gradient, double step) { return false; } + bool IsIntermediateStepOver(const arma::mat& coordinates, arma::mat& gradient, double step) { return true; } + + void GiveInitMatrix(arma::mat& init_data); + index_t num_of_points(); private: datanode *module_; - AllkNN allknn_; + + mlpack::allknn::AllkNN allknn_; index_t knns_; index_t leaf_size_; - ArrayList > nearest_neighbor_pairs_; - ArrayList nearest_distances_; - Vector eq_lagrange_mult_; + + std::vector > nearest_neighbor_pairs_; + std::vector nearest_distances_; + + arma::vec eq_lagrange_mult_; + index_t num_of_nearest_pairs_; double sigma_; double sum_of_furthest_distances_; @@ -91,40 +100,50 @@ class MaxVariance { }; class MaxFurthestNeighbors { -public: - static const index_t MAX_KNNS=30; - void Init(fx_module *module, Matrix &data); + public: + static const index_t MAX_KNNS = 30; + + void Init(fx_module *module, arma::mat& data); void Init(fx_module *module); + void Destruct(); - void ComputeGradient(Matrix &coordinates, Matrix *gradient); - void ComputeObjective(Matrix &coordinates, double *objective); - void ComputeFeasibilityError(Matrix &coordinates, double *error); - double ComputeLagrangian(Matrix &coordinates); - void UpdateLagrangeMult(Matrix &coordinates); - void Project(Matrix *coordinates); + + void ComputeGradient(const arma::mat& coordinates, arma::mat& gradient); + void ComputeObjective(const arma::mat& coordinates, double& objective); + void ComputeFeasibilityError(const arma::mat& coordinates, double& error); + double ComputeLagrangian(const arma::mat& coordinates); + void UpdateLagrangeMult(const arma::mat& coordinates); + void Project(arma::mat& coordinates); + void set_sigma(double sigma); void set_lagrange_mult(double val); - bool IsDiverging(double objective); - bool IsOptimizationOver(Matrix &coordinates, - Matrix &gradient, double step) ; - bool IsIntermediateStepOver(Matrix &coordinates, - Matrix &gradient, double step); - index_t num_of_points(); - void GiveInitMatrix(Matrix *init_data); -private: + bool IsDiverging(double objective); + bool IsOptimizationOver(arma::mat& coordinates, arma::mat& gradient, double step); + bool IsIntermediateStepOver(arma::mat& coordinates, arma::mat& gradient, double step); + + index_t num_of_points(); + void GiveInitMatrix(arma::mat& init_data); + + private: datanode *module_; - AllkNN allknn_; + + mlpack::allknn::AllkNN allknn_; AllkFN allkfn_; + index_t knns_; index_t leaf_size_; - ArrayList > nearest_neighbor_pairs_; - ArrayList nearest_distances_; - Vector eq_lagrange_mult_; + + std::vector > nearest_neighbor_pairs_; + std::vector nearest_distances_; + + arma::vec eq_lagrange_mult_; index_t num_of_nearest_pairs_; index_t num_of_furthest_pairs_; - ArrayList > furthest_neighbor_pairs_; - ArrayList furthest_distances_; + + std::vector > furthest_neighbor_pairs_; + std::vector furthest_distances_; + double sum_of_furthest_distances_; double sigma_; index_t num_of_points_; @@ -139,19 +158,20 @@ private: class MaxVarianceUtils { public: - static void ConsolidateNeighbors(ArrayList &from_tree_ind, - ArrayList &from_tree_dist, - index_t num_of_neighbors, - index_t chosen_neighbors, - ArrayList > *neighbor_pairs, - ArrayList *distances, - index_t *num_of_pairs); - static void EstimateKnns(ArrayList &neares_neighbors, - ArrayList &nearest_distances, - index_t maximum_knns, - index_t num_of_points, - index_t dimension, - index_t *optimum_knns); + static void ConsolidateNeighbors(const arma::Col& from_tree_ind, + const arma::vec& from_tree_dist, + index_t num_of_neighbors, + index_t chosen_neighbors, + std::vector >& neighbor_pairs, + std::vector& distances, + index_t& num_of_pairs); + + static void EstimateKnns(const arma::Col& nearest_neighbors, + const arma::vec& nearest_distances, + index_t maximum_knns, + index_t num_of_points, + index_t dimension, + index_t& optimum_knns); }; #include "mvu_objectives_impl.h" diff --git a/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives_impl.h b/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives_impl.h index 7567c1b6bb..2210c3ebcb 100644 --- a/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives_impl.h +++ b/fastlib/branches/fastlib-stl/mlpack/mvu/mvu_objectives_impl.h @@ -15,208 +15,215 @@ * * ===================================================================================== */ +#include + +void MaxVariance::Init(datanode* module, arma::mat& data) { + module_ = module; -void MaxVariance::Init(datanode *module, Matrix &data) { - module_=module; knns_ = fx_param_int(module_, "knns", 5); leaf_size_ = fx_param_int(module_, "leaf_size", 20); new_dimension_ = fx_param_int_req(module_, "new_dimension"); - num_of_points_ = data.n_cols(); - NOTIFY("Data loaded ...\n"); - NOTIFY("Nearest neighbor constraints ...\n"); - NOTIFY("Building tree with data ...\n"); - if (knns_==0) { - allknn_.Init(data, leaf_size_, MAX_KNNS); + num_of_points_ = data.n_cols; + + NOTIFY("Data loaded...\n"); + NOTIFY("Nearest neighbor constraints...\n"); + NOTIFY("Building tree with data...\n"); + + if (knns_ == 0) { + allknn_.Init(&data, leaf_size_, MAX_KNNS); } else { - allknn_.Init(data, leaf_size_, knns_); + allknn_.Init(&data, leaf_size_, knns_); } - NOTIFY("Tree built ...\n"); - NOTIFY("Computing neighborhoods ...\n"); - ArrayList from_tree_neighbors; - ArrayList from_tree_distances; - allknn_.ComputeNeighbors(&from_tree_neighbors, - &from_tree_distances); + + NOTIFY("Tree built...\n"); + + NOTIFY("Computing neighborhoods...\n"); + + arma::Col from_tree_neighbors; + arma::vec from_tree_distances; + allknn_.ComputeNeighbors(from_tree_neighbors, + from_tree_distances); + NOTIFY("Neighborhoods computed...\n"); - if (knns_==0) { + + if (knns_ == 0) { // automatically estimate the correct number for k NOTIFY("Auto-tuning the knn...\n" ); + MaxVarianceUtils::EstimateKnns(from_tree_neighbors, from_tree_distances, MAX_KNNS, - data.n_cols(), - data.n_rows(), - &knns_); + data.n_cols, + data.n_rows, + knns_); + NOTIFY("Optimum knns is %i", knns_); - fx_format_result(module_, "optimum_knns", "%i",knns_); + fx_format_result(module_, "optimum_knns", "%i", knns_); + NOTIFY("Consolidating neighbors...\n"); MaxVarianceUtils::ConsolidateNeighbors(from_tree_neighbors, from_tree_distances, MAX_KNNS, knns_, - &nearest_neighbor_pairs_, - &nearest_distances_, - &num_of_nearest_pairs_); + nearest_neighbor_pairs_, + nearest_distances_, + num_of_nearest_pairs_); } else { NOTIFY("Consolidating neighbors...\n"); MaxVarianceUtils::ConsolidateNeighbors(from_tree_neighbors, from_tree_distances, knns_, knns_, - &nearest_neighbor_pairs_, - &nearest_distances_, - &num_of_nearest_pairs_); + nearest_neighbor_pairs_, + nearest_distances_, + num_of_nearest_pairs_); } - eq_lagrange_mult_.Init(num_of_nearest_pairs_); - eq_lagrange_mult_.SetAll(1.0); - double max_nearest_distance=0; - for(index_t i=0; inum_of_points_) { - num_of_points_=n1; + fscanf(fp, "%i %i %lg", &n1, &n2, &distance); + nearest_neighbor_pairs_.push_back(std::make_pair(n1, n2)); + nearest_distances_.push_back(distance); + if (n1 > num_of_points_) { + num_of_points_ = n1; } - if (n2>num_of_points_) { - num_of_points_=n2; + if (n2 > num_of_points_) { + num_of_points_ = n2; } } num_of_points_++; fclose(fp); - num_of_nearest_pairs_=nearest_neighbor_pairs_.size(); - eq_lagrange_mult_.Init(num_of_nearest_pairs_); - eq_lagrange_mult_.SetAll(1.0); - double max_nearest_distance=0; - for(index_t i=0; iCopyValues(coordinates); - // we need to use -CRR^T because we want to maximize CRR^T - la::Scale(-1.0, gradient); - index_t dimension=coordinates.n_rows(); - for(index_t i=0; iGetColumnPtr(n1)); - la::AddExpert(dimension, - eq_lagrange_mult_[i]-dist_diff*sigma_, - a_i_r, - gradient->GetColumnPtr(n2)); + index_t dimension = coordinates.n_rows; + for(index_t i = 0; i < num_of_nearest_pairs_; i++) { + arma::vec a_i_r(dimension); + index_t n1 = nearest_neighbor_pairs_[i].first; + index_t n2 = nearest_neighbor_pairs_[i].second; + const arma::vec point1 = coordinates.unsafe_col(n1); + const arma::vec point2 = coordinates.unsafe_col(n2); + double dist_diff = la::DistanceSqEuclidean(point1, point2) - nearest_distances_[i]; + + a_i_r = point2 - point1; + + // equality constraints + double scale = eq_lagrange_mult_[i] - dist_diff * sigma_; + gradient.col(n1) -= scale * a_i_r; + gradient.col(n2) += scale * a_i_r; } } -void MaxVariance::ComputeObjective(Matrix &coordinates, double *objective) { - *objective=0; - index_t dimension = coordinates.n_rows(); - for(index_t i=0; i< coordinates.n_cols(); i++) { - *objective-=la::Dot(dimension, - coordinates.GetColumnPtr(i), - coordinates.GetColumnPtr(i)); +void MaxVariance::ComputeObjective(const arma::mat& coordinates, double& objective) { + objective = 0; + + index_t dimension = coordinates.n_rows; + for(index_t i = 0; i < coordinates.n_cols; i++) { + // could this be done more efficiently? + objective -= dot(coordinates.unsafe_col(i), coordinates.unsafe_col(i)); } } -void MaxVariance::ComputeFeasibilityError(Matrix &coordinates, double *error) { - index_t dimension=coordinates.n_rows(); - *error=0; - for(index_t i=0; iInit(new_dimension_, num_of_points_); - for(index_t i=0; iset(j, i,math::Random(0, 1) ); - } - } +void MaxVariance::GiveInitMatrix(arma::mat& init_data) { + init_data.randu(new_dimension_, num_of_points_); } /////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// -void MaxFurthestNeighbors::Init(datanode *module, Matrix &data) { - module_=module; +void MaxFurthestNeighbors::Init(datanode* module, arma::mat& data) { + module_ = module; new_dimension_ = fx_param_int_req(module_, "new_dimension"); - num_of_points_=data.n_cols(); - infeasibility1_=DBL_MAX; - previous_infeasibility1_=DBL_MAX; + num_of_points_ = data.n_cols; + + infeasibility1_ = DBL_MAX; + previous_infeasibility1_ = DBL_MAX; desired_feasibility_error_ = fx_param_double(module_, "desired_feasibility_error", 1); grad_tolerance_ = fx_param_double(module_, "grad_tolerance", 0.1); infeasibility_tolerance_= fx_param_double(module_, "infeasibility_tolerance", 0.01); + knns_ = fx_param_int(module_, "knns", 5); leaf_size_ = fx_param_int(module_, "leaf_size", 20); - NOTIFY("Data loaded ...\n"); - NOTIFY("Nearest neighbor constraints ...\n"); - NOTIFY("Building tree with data ...\n"); - if (knns_==0) { - allknn_.Init(data, leaf_size_, MAX_KNNS); + + NOTIFY("Data loaded...\n"); + NOTIFY("Nearest neighbor constraints...\n"); + NOTIFY("Building tree with data...\n"); + + if (knns_ == 0) { + allknn_.Init(&data, leaf_size_, MAX_KNNS); } else { - allknn_.Init(data, leaf_size_, knns_); + allknn_.Init(&data, leaf_size_, knns_); } + NOTIFY("Tree built ...\n"); NOTIFY("Computing neighborhoods ...\n"); - ArrayList from_tree_neighbors; - ArrayList from_tree_distances; - allknn_.ComputeNeighbors(&from_tree_neighbors, - &from_tree_distances); + + arma::Col from_tree_neighbors; + arma::vec from_tree_distances; + allknn_.ComputeNeighbors(from_tree_neighbors, + from_tree_distances); NOTIFY("Neighborhoods computed...\n"); - if (knns_==0) { + + if (knns_ == 0) { // automatically select k NOTIFY("Auto-tuning the knn...\n" ); MaxVarianceUtils::EstimateKnns(from_tree_neighbors, - from_tree_distances, - MAX_KNNS, - data.n_cols(), - data.n_rows(), - &knns_); + from_tree_distances, + MAX_KNNS, + data.n_cols, + data.n_rows, + knns_); + NOTIFY("Optimum knns is %i", knns_); - fx_format_result(module_, "optimum_knns", "%i",knns_); + fx_format_result(module_, "optimum_knns", "%i", knns_); MaxVarianceUtils::ConsolidateNeighbors(from_tree_neighbors, - from_tree_distances, - MAX_KNNS, - knns_, - &nearest_neighbor_pairs_, - &nearest_distances_, - &num_of_nearest_pairs_); + from_tree_distances, + MAX_KNNS, + knns_, + nearest_neighbor_pairs_, + nearest_distances_, + num_of_nearest_pairs_); } else { NOTIFY("Consolidating neighbors...\n"); MaxVarianceUtils::ConsolidateNeighbors(from_tree_neighbors, - from_tree_distances, - knns_, - knns_, - &nearest_neighbor_pairs_, - &nearest_distances_, - &num_of_nearest_pairs_); + from_tree_distances, + knns_, + knns_, + nearest_neighbor_pairs_, + nearest_distances_, + num_of_nearest_pairs_); } - sum_of_nearest_distances_= std::sqrt(la::Dot(nearest_distances_.size(), - &nearest_distances_[0], &nearest_distances_[0])); + + // is there a better way to do this? (take dot product of distances) + // this variable isn't even used before it's reset again! +// sum_of_nearest_distances_ = 0; +// for(int i = 0; i < nearest_distances_.size(); i++) +// sum_of_nearest_distances_ += std::pow(nearest_distances_[i], 2.0); +// sum_of_nearest_distances_ = std::pow(sum_of_nearest_distances_, 0.5); + fx_format_result(module_, "num_of_constraints", "%i", num_of_nearest_pairs_); - eq_lagrange_mult_.Init(num_of_nearest_pairs_); - eq_lagrange_mult_.SetAll(1.0); - NOTIFY("Furtherst neighbor constraints ...\n"); - NOTIFY("Building tree with data ...\n"); - allkfn_.Init(data, leaf_size_, 1); - NOTIFY("Tree built ...\n"); - NOTIFY("Computing furthest neighborhoods ...\n"); - from_tree_neighbors.Renew(); - from_tree_distances.Renew(); - allkfn_.ComputeNeighbors(&from_tree_neighbors, - &from_tree_distances); - NOTIFY("Furthest Neighbors computed...\n"); + + eq_lagrange_mult_.ones(num_of_nearest_pairs_); + + NOTIFY("Furthest neighbor constraints...\n"); + NOTIFY("Building tree with data...\n"); + + allkfn_.Init(&data, leaf_size_, 1); + + NOTIFY("Tree built...\n"); + NOTIFY("Computing furthest neighborhoods...\n"); + + // mem:: madness? +// from_tree_neighbors.Renew(); +// from_tree_distances.Renew(); + allkfn_.ComputeNeighbors(from_tree_neighbors, + from_tree_distances); + + NOTIFY("Furthest neighbors computed...\n"); NOTIFY("Consolidating neighbors...\n"); + MaxVarianceUtils::ConsolidateNeighbors(from_tree_neighbors, - from_tree_distances, - 1, - 1, - &furthest_neighbor_pairs_, - &furthest_distances_, - &num_of_furthest_pairs_); - double max_nearest_distance=0; - for(index_t i=0; inum_of_points_) { + fscanf(fp, "%i %i %lg", &n1, &n2, &distance); + nearest_neighbor_pairs_.push_back(std::make_pair(n1, n2)); + nearest_distances_.push_back(distance); + if (n1 > num_of_points_) { num_of_points_=n1; } - if (n2>num_of_points_) { + if (n2 > num_of_points_) { num_of_points_=n2; } } num_of_points_++; - num_of_nearest_pairs_=nearest_neighbor_pairs_.size(); - sum_of_nearest_distances_= std::sqrt(la::Dot(nearest_distances_.size(), - &nearest_distances_[0], &nearest_distances_[0])); + num_of_nearest_pairs_ = nearest_neighbor_pairs_.size(); + // this variable is not used before it is set again! +// sum_of_nearest_distances_ = std::pow(dot(nearest_distances_[0], +// nearest_distances_[0]), 0.5); // use norm()? fclose(fp); - fp=fopen(furthest_neighbor_file.c_str(), "r"); - if (fp==NULL) { - FATAL("Error while opening %s...%s", furthest_neighbor_file.c_str(), - strerror(errno)); + fp = fopen(furthest_neighbor_file.c_str(), "r"); + if (fp == NULL) { + FATAL("Error while opening %s...%s", furthest_neighbor_file.c_str(), strerror(errno)); } - furthest_neighbor_pairs_.Init(); - furthest_distances_.Init(); + while(!feof(fp)) { index_t n1, n2; double distance; - fscanf(fp,"%i %i %lg", &n1, &n2, &distance); - furthest_neighbor_pairs_.PushBackCopy(std::make_pair(n1, n2)); - furthest_distances_.PushBackCopy(distance); + fscanf(fp, "%i %i %lg", &n1, &n2, &distance); + furthest_neighbor_pairs_.push_back(std::make_pair(n1, n2)); + furthest_distances_.push_back(distance); } fclose(fp); - num_of_furthest_pairs_=furthest_neighbor_pairs_.size(); - eq_lagrange_mult_.Init(num_of_nearest_pairs_); - eq_lagrange_mult_.SetAll(1.0); - double max_nearest_distance=0; - for(index_t i=0; iSetAll(0.0); +void MaxFurthestNeighbors::ComputeGradient(const arma::mat& coordinates, arma::mat& gradient) { + gradient.zeros(); // objective - for(index_t i=0; iGetColumnPtr(n1)); - la::AddTo(dimension, a_i_r, - gradient->GetColumnPtr(n2)); + gradient.col(n1) -= a_i_r; + gradient.col(n2) += a_i_r; } // equality constraints - for(index_t i=0; iGetColumnPtr(n1)); - la::AddExpert(dimension, - eq_lagrange_mult_[i]-dist_diff*sigma_, - a_i_r, - gradient->GetColumnPtr(n2)); + index_t n1 = nearest_neighbor_pairs_[i].first; + index_t n2 = nearest_neighbor_pairs_[i].second; + arma::vec point1 = coordinates.unsafe_col(n1); + arma::vec point2 = coordinates.unsafe_col(n2); + double dist_diff = la::DistanceSqEuclidean(point1, point2) - + nearest_distances_[i]; + + a_i_r = point2 - point1; + + double scale = (dist_diff * sigma_ - eq_lagrange_mult_[i]); + gradient.col(n1) += scale * a_i_r; + gradient.col(n2) -= scale * a_i_r; } } -void MaxFurthestNeighbors::ComputeObjective(Matrix &coordinates, - double *objective) { - *objective=0; - index_t dimension = coordinates.n_rows(); - for(index_t i=0; iInit(new_dimension_, num_of_points_); - for(index_t i=0; iset(j, i,math::Random(0, 1) ); - } - } +void MaxFurthestNeighbors::GiveInitMatrix(arma::mat& init_data) { + init_data.randu(new_dimension_, num_of_points_); } -bool MaxFurthestNeighbors::IsOptimizationOver(Matrix &coordinates, - Matrix &gradient, double step) { - ComputeFeasibilityError(coordinates, &infeasibility1_); - if (infeasibility1_ &from_tree_ind, - ArrayList &from_tree_dist, - index_t num_of_neighbors, - index_t chosen_neighbors, - ArrayList > *neighbor_pairs, - ArrayList *distances, - index_t *num_of_pairs) { +void MaxVarianceUtils::ConsolidateNeighbors(const arma::Col& from_tree_ind, + const arma::vec& from_tree_dist, + index_t num_of_neighbors, + index_t chosen_neighbors, + std::vector >& neighbor_pairs, + std::vector& distances, + index_t& num_of_pairs) { - *num_of_pairs=0; - index_t num_of_points=from_tree_ind.size()/num_of_neighbors; - neighbor_pairs->Init(); - distances->Init(); - bool skip=false; - for(index_t i=0; i n2) { - for(index_t n=0; n n2) { + for(index_t n = 0; n < chosen_neighbors; n++) { + if(from_tree_ind[(n2 * num_of_neighbors) + n] == n1) { + skip = true; break; } } } - if (skip==false) { - *num_of_pairs+=1; - neighbor_pairs->PushBackCopy(std::make_pair(n1, n2)); - distances->PushBackCopy(from_tree_dist[i*num_of_neighbors+k]); + if(!skip) { + num_of_pairs += 1; + neighbor_pairs.push_back(std::make_pair(n1, n2)); + distances.push_back(from_tree_dist[(i * num_of_neighbors) + k]); } - skip=false; + skip = false; } } } -void MaxVarianceUtils::EstimateKnns(ArrayList &neares_neighbors, - ArrayList &nearest_distances, - index_t maximum_knns, +void MaxVarianceUtils::EstimateKnns(const arma::Col& nearest_neighbors, + const arma::vec& nearest_distances, + index_t maximum_knns, index_t num_of_points, index_t dimension, - index_t *optimum_knns) { - double max_loocv_score=-DBL_MAX; - double loocv_score=0; - //double unit_sphere_volume=math::SphereVolume(1.0, dimension); - *optimum_knns=0; - for(index_t k=2; k max_loocv_score) { - max_loocv_score=loocv_score; - *optimum_knns=k; + max_loocv_score = loocv_score; + optimum_knns = k; } } } -