Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
kmeans_agglomeration.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2025 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
23#ifndef VULPES_GEOMETRY_AGGLOMERATION_KMEANS_AGGLOMERATION_HPP_
24#define VULPES_GEOMETRY_AGGLOMERATION_KMEANS_AGGLOMERATION_HPP_
25
26#include <iostream>
27#include <string>
28#include <variant>
29#include <vector>
30
31#include <Eigen/Dense>
32
36
37namespace vulpes {
38namespace geometry {
44enum class KMeansAlgType : u_int32_t { Base };
56template <typename MeshType>
58 public:
62
63 using MeshFloatType = typename MeshType::float_t;
64 using MeshIndexType = typename MeshType::index_t;
65
67
69 using MatType =
70 Eigen::Matrix<ClassFloatType, MeshType::self_dim, Eigen::Dynamic>;
73
75
76 static constexpr ClassIndexType dim = MeshType::self_dim;
77
87 size_t max_iter = 1e6,
88 ClassFloatType tol = 1e-6,
89 size_t seed = 0)
90 : m_alg_t(alg_t), m_max_iter(max_iter), m_tol(tol), m_seed(seed) {};
91
110 std::vector<MeshType> agglomerate(
111 const MeshType& mesh,
112 MethodType method_type,
113 ParamType params,
114 bool return_hierarchy = true,
115 const std::string& segregated_tag = "",
116 const IndVec& subset = {}) override;
117
131 void directKWay(
132 const MeshType& mesh,
133 std::size_t n_groups,
134 IndexTrackerType& indicator,
135 const std::vector<bool>& agglomerate) override;
136
137 private:
152 IndVec bisectSubGraph(const MeshType& mesh, const IndVec& subgraph) override;
153
155 const MeshType& mesh, std::size_t n_groups, const IndVec& subgraph);
156
166 size_t m_seed;
167};
168
171
172} // namespace geometry
173} // namespace vulpes
174
175#endif // VULPES_GEOMETRY_AGGLOMERATION_KMEANS_AGGLOMERATION_HPP_
Defines an abstract interface for mesh agglomeration strategies.
Definition abs_agg_model.hpp:71
double float_t
Definition abs_agg_model.hpp:73
datastructure::IndexTracker< index_t > IndexTrackerType
Definition abs_agg_model.hpp:78
datastructure::CrsMap< index_t, index_t > MapType
Definition abs_agg_model.hpp:76
std::variant< index_t, float_t > ParamType
Definition abs_agg_model.hpp:77
std::vector< index_t > IndVec
Definition abs_agg_model.hpp:75
int64_t index_t
Definition abs_agg_model.hpp:74
Agglomeration model that clusters cell barycenters with K-Means.
Definition kmeans_agglomeration.hpp:57
MatType m_barycenters_mat
Cached barycenter matrix used in the current call.
Definition kmeans_agglomeration.hpp:158
float_t m_tol
Convergence tolerance on centroids.
Definition kmeans_agglomeration.hpp:164
Cell< MeshType::self_dim, MeshFloatType, MeshIndexType > CellType
Definition kmeans_agglomeration.hpp:66
typename MeshType::float_t MeshFloatType
Definition kmeans_agglomeration.hpp:63
typename AbstractAgglomerationModel< MeshType >::IndVec IndVec
Definition kmeans_agglomeration.hpp:68
typename MeshType::index_t MeshIndexType
Definition kmeans_agglomeration.hpp:64
static constexpr ClassIndexType dim
Definition kmeans_agglomeration.hpp:76
IndVec splitSubsetKWay(const MeshType &mesh, std::size_t n_groups, const IndVec &subgraph)
Definition kmeans_agglomeration.cpp:89
typename AbstractAgglomerationModel< MeshType >::ParamType ParamType
Definition kmeans_agglomeration.hpp:74
size_t m_seed
Random seed for initialization.
Definition kmeans_agglomeration.hpp:166
IndVec bisectSubGraph(const MeshType &mesh, const IndVec &subgraph) override
Bisect a subgraph by clustering barycenters with 2-means.
Definition kmeans_agglomeration.cpp:82
std::vector< MeshType > agglomerate(const MeshType &mesh, MethodType method_type, ParamType params, bool return_hierarchy=true, const std::string &segregated_tag="", const IndVec &subset={}) override
Agglomerate a mesh using a selected high-level strategy.
Definition kmeans_agglomeration.cpp:38
Eigen::Matrix< ClassFloatType, MeshType::self_dim, Eigen::Dynamic > MatType
Definition kmeans_agglomeration.hpp:69
void directKWay(const MeshType &mesh, std::size_t n_groups, IndexTrackerType &indicator, const std::vector< bool > &agglomerate) override
Direct k-way partition via K-Means on barycenters.
Definition kmeans_agglomeration.cpp:104
typename AbstractAgglomerationModel< MeshType >::IndexTrackerType IndexTrackerType
Definition kmeans_agglomeration.hpp:71
typename AbstractAgglomerationModel< MeshType >::float_t ClassFloatType
Definition kmeans_agglomeration.hpp:59
typename AbstractAgglomerationModel< MeshType >::MapType MapType
Definition kmeans_agglomeration.hpp:61
KMeansAlgType m_alg_t
Selected K-Means algorithm family.
Definition kmeans_agglomeration.hpp:160
KMeansAgglomeration(KMeansAlgType alg_t=KMeansAlgType::Base, size_t max_iter=1e6, ClassFloatType tol=1e-6, size_t seed=0)
Construct a K-Means agglomerator.
Definition kmeans_agglomeration.hpp:85
size_t m_max_iter
Maximum number of iterations.
Definition kmeans_agglomeration.hpp:162
typename AbstractAgglomerationModel< MeshType >::index_t ClassIndexType
Definition kmeans_agglomeration.hpp:60
Definition abs_agg_model.cpp:40
@ Base
Definition gnn_agglomeration.hpp:63
KMeansAlgType
K-Means algorithm types.
Definition kmeans_agglomeration.hpp:44
@ Base
Definition kmeans_agglomeration.hpp:44
MethodType
Definition abs_agg_model.hpp:41
std::conditional_t< Dim==2, Cell2D< FloatType, IndexType >, Cell3D< FloatType, IndexType > > Cell
Definition cell.hpp:267
Definition cell_manager.hpp:38