TTK
Loading...
Searching...
No Matches
PDClustering.h
Go to the documentation of this file.
1
14
15#pragma once
16
17#include <KDTree.h>
18#include <PDBarycenter.h>
20
21#include <array>
22#include <limits>
23
24namespace ttk {
25
26 class PDClustering : virtual public Debug {
27
28 public:
30 threadNumber_ = 1;
31 this->setDebugMsgPrefix("PersistenceDiagramClustering");
32 }
33
34 ~PDClustering() override = default;
35
36 std::vector<int>
37 execute(std::vector<DiagramType> &final_centroids,
38 std::vector<std::vector<std::vector<std::vector<MatchingType>>>>
39 &all_matchings);
40
41 std::array<double, 3> getDistances() const {
42 return {this->cost_min_, this->cost_sad_, this->cost_max_};
43 }
44
45 double getMostPersistent(int type = -1);
46 std::vector<std::vector<int>> get_centroids_sizes();
47 double getLessPersistent(int type = -1);
48 std::vector<std::vector<double>> getMinDiagonalPrices();
49 std::vector<std::vector<double>> getMinPrices();
50
52 std::vector<std::vector<std::vector<std::vector<MatchingType>>>>
53 &previous_matchings);
54
55 double computeDistance(const BidderDiagram &D1,
56 const BidderDiagram &D2,
57 const double delta_lim);
58 double computeDistance(const BidderDiagram &D1,
59 const GoodDiagram &D2,
60 const double delta_lim);
61 double computeDistance(BidderDiagram *const D1,
62 const GoodDiagram *const D2,
63 const double delta_lim);
64 double computeDistance(const GoodDiagram &D1,
65 const GoodDiagram &D2,
66 const double delta_lim);
67
72
73 void setBidderDiagrams();
78 void initializeBarycenterComputers(std::vector<double> &min_persistence);
80 void printMatchings(std::vector<std::vector<std::vector<MatchingType>>>);
82 void printPricesToFile(int);
83 double computeRealCost();
84
85 std::vector<double> enrichCurrentBidderDiagrams(
86 std::vector<double> &previous_min_persistence,
87 std::vector<double> &min_persistence,
88 std::vector<std::vector<double>> &initial_diagonal_prices,
89 std::vector<std::vector<double>> &initial_off_diagonal_prices,
90 std::vector<int> &min_points_to_add,
91 bool add_points_to_barycenter,
92 bool first_enrichment);
93
94 std::vector<std::vector<double>> getDistanceMatrix();
97
98 void updateClusters();
99 void invertClusters();
102 std::vector<std::vector<std::vector<std::vector<MatchingType>>>> &);
103
105 std::vector<std::vector<MatchingType>> &matchings,
106 std::vector<std::vector<int>> &bidders_ids,
107 std::vector<BidderDiagram> &current_bidder_diagrams,
108 std::vector<BidderDiagram> &bidder_diagrams,
109 GoodDiagram &barycenter);
110
112 std::vector<double> updateCentroidsPosition(
113 std::vector<std::vector<double>> *min_price,
114 std::vector<std::vector<double>> *min_diag_price,
115 std::vector<std::vector<std::vector<std::vector<MatchingType>>>>
116 &all_matchings,
117 int only_matchings);
118
123 }
124 inline int setDiagrams(std::vector<DiagramType> *data_min,
125 std::vector<DiagramType> *data_saddle,
126 std::vector<DiagramType> *data_max) {
127 inputDiagramsMin_ = data_min;
128 inputDiagramsSaddle_ = data_saddle;
129 inputDiagramsMax_ = data_max;
130 return 0;
131 }
132
133 inline int setDos(bool doMin, bool doSad, bool doMax) {
134 do_min_ = doMin;
135 do_sad_ = doSad;
136 do_max_ = doMax;
137
141 return 0;
142 }
143
144 inline int setNumberOfInputs(int numberOfInputs) {
145 numberOfInputs_ = numberOfInputs;
146 return 0;
147 }
148
149 inline int setK(const int k) {
150 k_ = k;
151 return 0;
152 }
153
154 inline void setWasserstein(const int &wasserstein) {
155 wasserstein_ = wasserstein;
156 }
157
158 inline void setUseProgressive(const bool use_progressive) {
159 use_progressive_ = use_progressive;
160 }
161
162 inline void setKMeanspp(const bool use_kmeanspp) {
163 use_kmeanspp_ = use_kmeanspp;
164 }
165
166 inline void setUseKDTree(const bool use_kdtree) {
167 use_kdtree_ = use_kdtree;
168 }
169
170 inline void setAccelerated(const bool use_accelerated) {
171 use_accelerated_ = use_accelerated;
172 }
173
174 inline void setTimeLimit(const double time_limit) {
175 time_limit_ = time_limit;
176 }
177
178 inline void setGeometricalFactor(const double geometrical_factor) {
179 geometrical_factor_ = geometrical_factor;
180 }
181 inline void setLambda(const double lambda) {
182 lambda_ = lambda;
183 }
184 inline void setForceUseOfAlgorithm(const bool forceUseOfAlgorithm) {
185 forceUseOfAlgorithm_ = forceUseOfAlgorithm;
186 }
187 inline void setDeterministic(const bool deterministic) {
188 deterministic_ = deterministic;
189 }
190
191 inline void setUseDeltaLim(const bool UseDeltaLim) {
192 UseDeltaLim_ = UseDeltaLim;
193 if(UseDeltaLim_) {
194 epsilon_min_ = 1e-8;
195 } else {
196 epsilon_min_ = 5e-5;
197 }
198 }
199
200 inline void setDistanceWritingOptions(const int distanceWritingOptions) {
201 distanceWritingOptions_ = distanceWritingOptions;
202 }
203 inline void setDeltaLim(const double deltaLim) {
204 deltaLim_ = deltaLim;
205 }
206
207 inline void setNonMatchingWeight(double nonMatchingWeight) {
208 nonMatchingWeight_ = nonMatchingWeight;
209 }
210
211 inline void printClustering() {
212 std::string msg{};
213 for(int c = 0; c < k_; ++c) {
214 msg.append(" Cluster " + std::to_string(c) + " = {");
215 for(size_t idx = 0; idx < clustering_[c].size(); ++idx) {
216 if(idx == clustering_[c].size() - 1) {
217 msg.append(std::to_string(clustering_[c][idx]) + "}");
218 this->printMsg(msg);
219 msg = "";
220 // msg << clustering_[c][idx] << "}" << std::endl;
221 } else {
222 msg.append(std::to_string(clustering_[c][idx]) + ", ");
223 // msg << clustering_[c][idx] << ", ";
224 }
225 }
226 }
227 // cout<<msg.str()<<endl;
228 }
229
230 inline void printOldClustering() {
231 std::stringstream msg;
232 for(int c = 0; c < k_; ++c) {
233 msg << "Cluster " << c << " = {";
234 for(size_t idx = 0; idx < old_clustering_[c].size(); ++idx) {
235 if(idx == old_clustering_[c].size() - 1) {
236 msg << old_clustering_[c][idx] << "}" << std::endl;
237 } else {
238 msg << old_clustering_[c][idx] << ", ";
239 }
240 }
241 }
242 this->printMsg(msg.str());
243 }
244
245 protected:
246 std::vector<PDBarycenter> barycenter_computer_min_{};
247 std::vector<PDBarycenter> barycenter_computer_sad_{};
248 std::vector<PDBarycenter> barycenter_computer_max_{};
249
252 bool precision_max_{false};
253 bool precision_min_{false};
254 bool precision_sad_{false};
256 bool deterministic_{true};
259 double deltaLim_;
260 bool UseDeltaLim_{false};
262 // lambda : 0<=lambda<=1
263 // parametrizes the point used for the physical (critical) coordinates of
264 // the persistence paired lambda = 1 : extremum (min if pair min-sad, max if
265 // pair sad-max) lambda = 0 : saddle (bad stability) lambda = 1/2 : middle
266 // of the 2 critical points of the pair
267 double lambda_;
268 double nonMatchingWeight_ = 1.0;
269
270 int k_;
276 double time_limit_{std::numeric_limits<double>::max()};
277
278 double epsilon_min_{1e-8};
279 std::array<double, 3> epsilon_;
280 double cost_;
281 double cost_min_{0.0};
282 double cost_sad_{0.0};
283 double cost_max_{0.0};
284
285 std::vector<std::vector<int>> current_bidder_ids_min_;
286 std::vector<std::vector<int>> current_bidder_ids_sad_;
287 std::vector<std::vector<int>> current_bidder_ids_max_;
288 std::vector<DiagramType> *inputDiagramsMin_;
289 std::vector<DiagramType> *inputDiagramsSaddle_;
290 std::vector<DiagramType> *inputDiagramsMax_;
291
292 std::array<bool, 3> original_dos;
293
295 std::vector<BidderDiagram> bidder_diagrams_min_;
296 std::vector<BidderDiagram> current_bidder_diagrams_min_;
297 std::vector<GoodDiagram> centroids_min_;
298 std::vector<GoodDiagram> centroids_with_price_min_;
299
301 std::vector<BidderDiagram> bidder_diagrams_saddle_;
302 std::vector<BidderDiagram> current_bidder_diagrams_saddle_;
303 std::vector<GoodDiagram> centroids_saddle_;
304 std::vector<GoodDiagram> centroids_with_price_saddle_;
305
307 std::vector<BidderDiagram> bidder_diagrams_max_;
308 std::vector<BidderDiagram> current_bidder_diagrams_max_;
309 std::vector<GoodDiagram> centroids_max_;
310 std::vector<GoodDiagram> centroids_with_price_max_;
311
312 std::vector<std::vector<int>> clustering_;
313 std::vector<std::vector<int>> old_clustering_;
314 std::vector<int> inv_clustering_;
315
316 std::vector<std::vector<int>> centroids_sizes_;
317
318 std::vector<bool> r_;
319 std::vector<double> u_;
320 std::vector<std::vector<double>> l_;
321 std::vector<std::vector<double>> centroidsDistanceMatrix_{};
322 std::vector<double> distanceToCentroid_{};
323
325 };
326} // namespace ttk
Minimalist debugging class.
Definition Debug.h:88
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
void setKMeanspp(const bool use_kmeanspp)
std::vector< std::vector< int > > old_clustering_
void initializeAcceleratedKMeans()
std::vector< double > enrichCurrentBidderDiagrams(std::vector< double > &previous_min_persistence, std::vector< double > &min_persistence, std::vector< std::vector< double > > &initial_diagonal_prices, std::vector< std::vector< double > > &initial_off_diagonal_prices, std::vector< int > &min_points_to_add, bool add_points_to_barycenter, bool first_enrichment)
void setGeometricalFactor(const double geometrical_factor)
std::vector< int > inv_clustering_
std::vector< double > u_
std::vector< bool > r_
double getLessPersistent(int type=-1)
std::vector< DiagramType > * inputDiagramsMax_
void setUseDeltaLim(const bool UseDeltaLim)
void computeBarycenterForTwo(std::vector< std::vector< MatchingType > > &matchings, std::vector< std::vector< int > > &bidders_ids, std::vector< BidderDiagram > &current_bidder_diagrams, std::vector< BidderDiagram > &bidder_diagrams, GoodDiagram &barycenter)
int setDos(bool doMin, bool doSad, bool doMax)
std::vector< std::vector< double > > centroidsDistanceMatrix_
void setDeltaLim(const double deltaLim)
std::vector< std::vector< double > > l_
std::vector< BidderDiagram > current_bidder_diagrams_saddle_
std::vector< PDBarycenter > barycenter_computer_min_
int setDiagrams(std::vector< DiagramType > *data_min, std::vector< DiagramType > *data_saddle, std::vector< DiagramType > *data_max)
BidderDiagram centroidToDiagram(const GoodDiagram &centroid)
void initializeCentroidsKMeanspp()
double computeDistance(const BidderDiagram &D1, const BidderDiagram &D2, const double delta_lim)
std::vector< BidderDiagram > current_bidder_diagrams_min_
void setLambda(const double lambda)
std::vector< BidderDiagram > bidder_diagrams_saddle_
std::vector< BidderDiagram > current_bidder_diagrams_max_
int setK(const int k)
std::array< double, 3 > getDistances() const
void setDistanceWritingOptions(const int distanceWritingOptions)
std::vector< PDBarycenter > barycenter_computer_max_
void setTimeLimit(const double time_limit)
void setUseProgressive(const bool use_progressive)
bool barycenter_inputs_reset_flag
std::vector< double > updateCentroidsPosition(std::vector< std::vector< double > > *min_price, std::vector< std::vector< double > > *min_diag_price, std::vector< std::vector< std::vector< std::vector< MatchingType > > > > &all_matchings, int only_matchings)
std::vector< GoodDiagram > centroids_with_price_min_
void printPricesToFile(int)
BidderDiagram diagramWithZeroPrices(const BidderDiagram &diagram)
double getMostPersistent(int type=-1)
void setUseKDTree(const bool use_kdtree)
std::vector< std::vector< int > > clustering_
std::vector< GoodDiagram > centroids_min_
GoodDiagram diagramToCentroid(const BidderDiagram &diagram)
std::vector< double > distanceToCentroid_
std::vector< PDBarycenter > barycenter_computer_sad_
std::vector< std::vector< int > > current_bidder_ids_sad_
std::array< double, 3 > epsilon_
std::vector< BidderDiagram > bidder_diagrams_min_
std::vector< BidderDiagram > bidder_diagrams_max_
std::vector< GoodDiagram > centroids_saddle_
void setForceUseOfAlgorithm(const bool forceUseOfAlgorithm)
std::vector< std::vector< double > > getMinDiagonalPrices()
void setWasserstein(const int &wasserstein)
void computeBarycenterForTwoGlobal(std::vector< std::vector< std::vector< std::vector< MatchingType > > > > &)
~PDClustering() override=default
std::vector< std::vector< int > > centroids_sizes_
std::vector< std::vector< double > > getMinPrices()
void printMatchings(std::vector< std::vector< std::vector< MatchingType > > >)
std::vector< std::vector< double > > getDistanceMatrix()
std::vector< DiagramType > * inputDiagramsMin_
void initializeBarycenterComputers(std::vector< double > &min_persistence)
std::vector< std::vector< int > > current_bidder_ids_min_
void setNonMatchingWeight(double nonMatchingWeight)
int setNumberOfInputs(int numberOfInputs)
GoodDiagram centroidWithZeroPrices(const GoodDiagram &centroid)
std::vector< GoodDiagram > centroids_with_price_max_
std::vector< std::vector< int > > current_bidder_ids_max_
void resetDosToOriginalValues()
void setDeterministic(const bool deterministic)
std::vector< GoodDiagram > centroids_max_
std::vector< int > execute(std::vector< DiagramType > &final_centroids, std::vector< std::vector< std::vector< std::vector< MatchingType > > > > &all_matchings)
std::array< bool, 3 > original_dos
std::vector< DiagramType > * inputDiagramsSaddle_
std::vector< std::vector< int > > get_centroids_sizes()
void correctMatchings(std::vector< std::vector< std::vector< std::vector< MatchingType > > > > &previous_matchings)
void setAccelerated(const bool use_accelerated)
std::vector< GoodDiagram > centroids_with_price_saddle_
The Topology ToolKit.
std::vector< Bidder > BidderDiagram
std::vector< Good > GoodDiagram
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/|__ _|"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)