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 printClustering() {
208 std::string msg{};
209 for(int c = 0; c < k_; ++c) {
210 msg.append(" Cluster " + std::to_string(c) + " = {");
211 for(size_t idx = 0; idx < clustering_[c].size(); ++idx) {
212 if(idx == clustering_[c].size() - 1) {
213 msg.append(std::to_string(clustering_[c][idx]) + "}");
214 this->printMsg(msg);
215 msg = "";
216 // msg << clustering_[c][idx] << "}" << std::endl;
217 } else {
218 msg.append(std::to_string(clustering_[c][idx]) + ", ");
219 // msg << clustering_[c][idx] << ", ";
220 }
221 }
222 }
223 // cout<<msg.str()<<endl;
224 }
225
226 inline void printOldClustering() {
227 std::stringstream msg;
228 for(int c = 0; c < k_; ++c) {
229 msg << "Cluster " << c << " = {";
230 for(size_t idx = 0; idx < old_clustering_[c].size(); ++idx) {
231 if(idx == old_clustering_[c].size() - 1) {
232 msg << old_clustering_[c][idx] << "}" << std::endl;
233 } else {
234 msg << old_clustering_[c][idx] << ", ";
235 }
236 }
237 }
238 this->printMsg(msg.str());
239 }
240
241 protected:
242 std::vector<PDBarycenter> barycenter_computer_min_{};
243 std::vector<PDBarycenter> barycenter_computer_sad_{};
244 std::vector<PDBarycenter> barycenter_computer_max_{};
245
248 bool precision_max_{false};
249 bool precision_min_{false};
250 bool precision_sad_{false};
252 bool deterministic_{true};
255 double deltaLim_;
256 bool UseDeltaLim_{false};
258 // lambda : 0<=lambda<=1
259 // parametrizes the point used for the physical (critical) coordinates of
260 // the persistence paired lambda = 1 : extremum (min if pair min-sad, max if
261 // pair sad-max) lambda = 0 : saddle (bad stability) lambda = 1/2 : middle
262 // of the 2 critical points of the pair
263 double lambda_;
264
265 int k_;
271 double time_limit_{std::numeric_limits<double>::max()};
272
273 double epsilon_min_{1e-8};
274 std::array<double, 3> epsilon_;
275 double cost_;
276 double cost_min_{0.0};
277 double cost_sad_{0.0};
278 double cost_max_{0.0};
279
280 std::vector<std::vector<int>> current_bidder_ids_min_;
281 std::vector<std::vector<int>> current_bidder_ids_sad_;
282 std::vector<std::vector<int>> current_bidder_ids_max_;
283 std::vector<DiagramType> *inputDiagramsMin_;
284 std::vector<DiagramType> *inputDiagramsSaddle_;
285 std::vector<DiagramType> *inputDiagramsMax_;
286
287 std::array<bool, 3> original_dos;
288
290 std::vector<BidderDiagram> bidder_diagrams_min_;
291 std::vector<BidderDiagram> current_bidder_diagrams_min_;
292 std::vector<GoodDiagram> centroids_min_;
293 std::vector<GoodDiagram> centroids_with_price_min_;
294
296 std::vector<BidderDiagram> bidder_diagrams_saddle_;
297 std::vector<BidderDiagram> current_bidder_diagrams_saddle_;
298 std::vector<GoodDiagram> centroids_saddle_;
299 std::vector<GoodDiagram> centroids_with_price_saddle_;
300
302 std::vector<BidderDiagram> bidder_diagrams_max_;
303 std::vector<BidderDiagram> current_bidder_diagrams_max_;
304 std::vector<GoodDiagram> centroids_max_;
305 std::vector<GoodDiagram> centroids_with_price_max_;
306
307 std::vector<std::vector<int>> clustering_;
308 std::vector<std::vector<int>> old_clustering_;
309 std::vector<int> inv_clustering_;
310
311 std::vector<std::vector<int>> centroids_sizes_;
312
313 std::vector<bool> r_;
314 std::vector<double> u_;
315 std::vector<std::vector<double>> l_;
316 std::vector<std::vector<double>> centroidsDistanceMatrix_{};
317 std::vector<double> distanceToCentroid_{};
318
320 };
321} // namespace ttk
int threadNumber_
Definition: BaseClass.h:95
Minimalist debugging class.
Definition: Debug.h:88
void setDebugMsgPrefix(const std::string &prefix)
Definition: Debug.h:364
int printMsg(const std::string &msg, const debug::Priority &priority=debug::Priority::INFO, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cout) const
Definition: Debug.h:118
void setKMeanspp(const bool use_kmeanspp)
Definition: PDClustering.h:162
std::vector< std::vector< int > > old_clustering_
Definition: PDClustering.h:308
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)
Definition: PDClustering.h:178
std::vector< int > inv_clustering_
Definition: PDClustering.h:309
std::vector< double > u_
Definition: PDClustering.h:314
std::vector< bool > r_
Definition: PDClustering.h:313
void computeDistanceToCentroid()
double getLessPersistent(int type=-1)
std::vector< DiagramType > * inputDiagramsMax_
Definition: PDClustering.h:285
void setUseDeltaLim(const bool UseDeltaLim)
Definition: PDClustering.h:191
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)
Definition: PDClustering.h:133
std::vector< std::vector< double > > centroidsDistanceMatrix_
Definition: PDClustering.h:316
void setDeltaLim(const double deltaLim)
Definition: PDClustering.h:203
std::vector< std::vector< double > > l_
Definition: PDClustering.h:315
std::vector< BidderDiagram > current_bidder_diagrams_saddle_
Definition: PDClustering.h:297
std::vector< PDBarycenter > barycenter_computer_min_
Definition: PDClustering.h:242
int setDiagrams(std::vector< DiagramType > *data_min, std::vector< DiagramType > *data_saddle, std::vector< DiagramType > *data_max)
Definition: PDClustering.h:124
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_
Definition: PDClustering.h:291
void setLambda(const double lambda)
Definition: PDClustering.h:181
void printRealDistancesToFile()
std::vector< BidderDiagram > bidder_diagrams_saddle_
Definition: PDClustering.h:296
std::vector< BidderDiagram > current_bidder_diagrams_max_
Definition: PDClustering.h:303
int setK(const int k)
Definition: PDClustering.h:149
std::array< double, 3 > getDistances() const
Definition: PDClustering.h:41
void setDistanceWritingOptions(const int distanceWritingOptions)
Definition: PDClustering.h:200
std::vector< PDBarycenter > barycenter_computer_max_
Definition: PDClustering.h:244
void setTimeLimit(const double time_limit)
Definition: PDClustering.h:174
void setUseProgressive(const bool use_progressive)
Definition: PDClustering.h:158
bool barycenter_inputs_reset_flag
Definition: PDClustering.h:246
void getCentroidDistanceMatrix()
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_
Definition: PDClustering.h:293
void printPricesToFile(int)
BidderDiagram diagramWithZeroPrices(const BidderDiagram &diagram)
double getMostPersistent(int type=-1)
void setUseKDTree(const bool use_kdtree)
Definition: PDClustering.h:166
std::vector< std::vector< int > > clustering_
Definition: PDClustering.h:307
std::vector< GoodDiagram > centroids_min_
Definition: PDClustering.h:292
GoodDiagram diagramToCentroid(const BidderDiagram &diagram)
void acceleratedUpdateClusters()
std::vector< double > distanceToCentroid_
Definition: PDClustering.h:317
std::vector< PDBarycenter > barycenter_computer_sad_
Definition: PDClustering.h:243
std::vector< std::vector< int > > current_bidder_ids_sad_
Definition: PDClustering.h:281
std::array< double, 3 > epsilon_
Definition: PDClustering.h:274
std::vector< BidderDiagram > bidder_diagrams_min_
Definition: PDClustering.h:290
std::vector< BidderDiagram > bidder_diagrams_max_
Definition: PDClustering.h:302
std::vector< GoodDiagram > centroids_saddle_
Definition: PDClustering.h:298
void setForceUseOfAlgorithm(const bool forceUseOfAlgorithm)
Definition: PDClustering.h:184
std::vector< std::vector< double > > getMinDiagonalPrices()
void setWasserstein(const int &wasserstein)
Definition: PDClustering.h:154
void computeBarycenterForTwoGlobal(std::vector< std::vector< std::vector< std::vector< MatchingType > > > > &)
~PDClustering() override=default
std::vector< std::vector< int > > centroids_sizes_
Definition: PDClustering.h:311
void initializeEmptyClusters()
std::vector< std::vector< double > > getMinPrices()
void printMatchings(std::vector< std::vector< std::vector< MatchingType > > >)
double geometrical_factor_
Definition: PDClustering.h:254
std::vector< std::vector< double > > getDistanceMatrix()
std::vector< DiagramType > * inputDiagramsMin_
Definition: PDClustering.h:283
void initializeBarycenterComputers(std::vector< double > &min_persistence)
std::vector< std::vector< int > > current_bidder_ids_min_
Definition: PDClustering.h:280
int setNumberOfInputs(int numberOfInputs)
Definition: PDClustering.h:144
GoodDiagram centroidWithZeroPrices(const GoodDiagram &centroid)
std::vector< GoodDiagram > centroids_with_price_max_
Definition: PDClustering.h:305
std::vector< std::vector< int > > current_bidder_ids_max_
Definition: PDClustering.h:282
void resetDosToOriginalValues()
Definition: PDClustering.h:119
void setDeterministic(const bool deterministic)
Definition: PDClustering.h:187
std::vector< GoodDiagram > centroids_max_
Definition: PDClustering.h:304
void printOldClustering()
Definition: PDClustering.h:226
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
Definition: PDClustering.h:287
std::vector< DiagramType > * inputDiagramsSaddle_
Definition: PDClustering.h:284
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)
Definition: PDClustering.h:170
std::vector< GoodDiagram > centroids_with_price_saddle_
Definition: PDClustering.h:299
The Topology ToolKit.
std::vector< Bidder > BidderDiagram
std::vector< Good > GoodDiagram