Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
kmeans.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22// Author: Pedro Peixoto <ppeixoto@usp.br>
23
24#ifndef VULPES_GEOMETRY_KMEANS_HPP_
25#define VULPES_GEOMETRY_KMEANS_HPP_
26
27#include <array>
28#include <cstdint>
29#include <limits>
30#include <queue>
31#include <random>
32#include <utility>
33#include <vector>
34
35#include <Eigen/Dense>
36
39
40namespace vulpes {
41namespace geometry {
42
55
56template <typename T, size_t Dim>
58 public:
59 using LabelType = std::vector<size_t>;
60 using CentersType = Eigen::Matrix<T, Dim, Eigen::Dynamic>;
61 using DataType = Eigen::Map<const CentersType>;
62
63 explicit KMeansBase(
64 size_t k, size_t max_iter = 1e6, T tol = 1e-6, size_t seed = 0)
65 : m_fitted(false),
66 m_n_clusters(k),
67 m_max_iter(max_iter),
68 m_tol(tol),
69 m_seed(seed),
70 m_n_iter(0) {
71 m_n_samples = 0;
72 };
73
75
82
83 virtual void fit() = 0;
84
85 auto getIterNum() const { return m_n_iter; };
86 const LabelType& getLabels() const { return m_labels; };
87 const CentersType& getCentroids() const { return m_centroids; };
88
89 protected:
92 size_t m_max_iter;
94 size_t m_seed;
96 size_t m_n_iter;
99};
100
114
115template <typename T, size_t Dim>
116class KMeansNaive : public KMeansBase<T, Dim> {
117 public:
121
123 DataType x,
124 size_t k,
125 size_t max_iter = 1e6,
126 T tol = 1e-6,
127 std::size_t seed = 0)
128 : KMeansBase<T, Dim>(k, max_iter, tol, seed), m_data_points(x) {
129 assert(x.cols() >= static_cast<Eigen::Index>(k));
130 this->m_n_samples = x.cols();
131 this->m_centroids.resize(Dim, k);
132 this->m_labels.resize(this->m_n_samples);
133 };
134
135 void fit() override;
136
137 private:
139};
140
141// extern template class KMeansNaive<float, 1>;
142extern template class KMeansNaive<float, 2>;
143// extern template class KMeansNaive<float, 3>;
144// extern template class KMeansNaive<double, 1>;
145extern template class KMeansNaive<double, 2>;
146extern template class KMeansNaive<double, 3>;
147
148} // namespace geometry
149} // namespace vulpes
150
151#endif // VULPES_GEOMETRY_KMEANS_HPP_
T m_tol
Definition kmeans.hpp:93
const CentersType & getCentroids() const
Definition kmeans.hpp:87
size_t m_n_samples
Definition kmeans.hpp:95
LabelType m_labels
Definition kmeans.hpp:98
size_t m_n_clusters
Definition kmeans.hpp:91
std::vector< size_t > LabelType
Definition kmeans.hpp:59
Eigen::Map< const CentersType > DataType
Definition kmeans.hpp:61
KMeansBase(size_t k, size_t max_iter=1e6, T tol=1e-6, size_t seed=0)
Definition kmeans.hpp:63
size_t m_max_iter
Definition kmeans.hpp:92
bool m_fitted
Definition kmeans.hpp:90
CentersType m_centroids
Definition kmeans.hpp:97
auto getIterNum() const
Definition kmeans.hpp:85
Eigen::Matrix< T, Dim, Eigen::Dynamic > CentersType
Definition kmeans.hpp:60
size_t m_seed
Definition kmeans.hpp:94
KMeansBase()
Definition kmeans.hpp:74
virtual void fit()=0
Runs the KMeans clustering algorithm on the input data.
const LabelType & getLabels() const
Definition kmeans.hpp:86
size_t m_n_iter
Definition kmeans.hpp:96
Implementation of the Lloyd algorithm (also known as the naive KMeans) for clustering.
Definition kmeans.hpp:116
KMeansBase< T, Dim >::CentersType CentersType
Definition kmeans.hpp:120
DataType m_data_points
Definition kmeans.hpp:138
void fit() override
Runs the KMeans clustering algorithm on the input data.
Definition kmeans.cpp:192
KMeansBase< T, Dim >::DataType DataType
Definition kmeans.hpp:119
KMeansNaive(DataType x, size_t k, size_t max_iter=1e6, T tol=1e-6, std::size_t seed=0)
Definition kmeans.hpp:122
KMeansBase< T, Dim >::LabelType LabelType
Definition kmeans.hpp:118
Definition abs_agg_model.cpp:40
Definition cell_manager.hpp:38