TTK
Loading...
Searching...
No Matches
PDBarycenter.cpp
Go to the documentation of this file.
1
14
16
17#include <cmath>
18#include <cstdlib> /* srand, rand */
19#include <numeric>
20
21std::vector<std::vector<ttk::MatchingType>>
23 return executeAuctionBarycenter(barycenter);
24}
25
27 double *total_cost,
28 double epsilon,
29 std::vector<int> &sizes,
30 KDT &kdt,
31 std::vector<KDT *> &correspondence_kdt_map,
32 std::vector<double> *min_diag_price,
33 std::vector<double> *min_price,
34 std::vector<std::vector<MatchingType>> *all_matchings,
35 bool use_kdt,
36 bool actual_distance) {
37 Timer const time_matchings;
38
39 double local_cost = *total_cost;
40#ifdef TTK_ENABLE_OPENMP
41#pragma omp parallel for num_threads(threadNumber_) schedule(dynamic, 1) \
42 reduction(+ : local_cost)
43#endif
44 for(int i = 0; i < numberOfInputs_; i++) {
47 geometrical_factor_, lambda_, delta_lim_, kdt, correspondence_kdt_map,
48 epsilon, min_diag_price->at(i), use_kdt, nonMatchingWeight_);
49 int n_biddings = 0;
51 auction.initLowerBoundCost(i);
52 auction.buildUnassignedBidders();
53 auction.reinitializeGoods();
54 auction.runAuctionRound(n_biddings, i);
55 auction.updateDiagonalPrices();
56 min_diag_price->at(i) = auction.getMinimalDiagonalPrice();
57 min_price->at(i) = getMinimalPrice(i);
58 std::vector<MatchingType> matchings;
59 double const cost = auction.getMatchingsAndDistance(matchings, true);
60 all_matchings->at(i) = matchings;
61 if(actual_distance) {
62 local_cost += sqrt(cost);
63 } else {
64 local_cost += cost;
65 }
66
67 double const quotient
68 = epsilon * auction.getAugmentedNumberOfBidders() / cost;
69 precision_[i] = quotient < 1 ? 1. / sqrt(1 - quotient) - 1 : 10;
70 if(auction.getRelativePrecision() == 0)
71 precision_[i] = 0;
72 // Resizes the diagram which was enrich with diagonal bidders
73 // during the auction
74 // TODO do this inside the auction !
75 current_bidder_diagrams_[i].resize(sizes[i]);
76 }
77 *total_cost = local_cost;
78}
79
81 double *total_cost,
82 std::vector<int> &sizes,
83 KDT &kdt,
84 std::vector<KDT *> &correspondence_kdt_map,
85 std::vector<double> *min_diag_price,
86 std::vector<std::vector<MatchingType>> *all_matchings,
87 bool use_kdt,
88 bool actual_distance) {
89 double local_cost = *total_cost;
90#ifdef TTK_ENABLE_OPENMP
91#pragma omp parallel for num_threads(threadNumber_) schedule(dynamic, 1) \
92 reduction(+ : local_cost)
93#endif
94 for(int i = 0; i < numberOfInputs_; i++) {
97 geometrical_factor_, lambda_, delta_lim_, kdt, correspondence_kdt_map, 0,
98 (*min_diag_price)[i], use_kdt, nonMatchingWeight_);
99 std::vector<MatchingType> matchings;
100 double const cost = auction.run(matchings, i);
101 all_matchings->at(i) = matchings;
102 if(actual_distance) {
103 local_cost += sqrt(cost);
104 } else {
105 local_cost += cost;
106 }
107
108 // Resizes the diagram which was enrich with diagonal bidders
109 // during the auction
110 // TODO do this inside the auction !
111 current_bidder_diagrams_[i].resize(sizes[i]);
112 }
113 *total_cost = local_cost;
114}
115
117 std::vector<std::vector<MatchingType>> &matchings,
118 std::vector<std::vector<MatchingType>> &previous_matchings) {
119 if(points_added_ > 0 || points_deleted_ > 0 || previous_matchings.empty()) {
120 return false;
121 }
122
123 for(size_t j = 0; j < matchings.size(); j++) {
124 for(size_t i = 0; i < matchings[j].size(); i++) {
125 MatchingType t = matchings[j][i];
126 MatchingType previous_t = previous_matchings[j][i];
127
128 if(std::get<1>(t) != std::get<1>(previous_t)
129 && (std::get<0>(t) >= 0 && std::get<0>(previous_t) >= 0)) {
130 return false;
131 }
132 }
133 }
134 return true;
135}
136
137std::vector<std::vector<ttk::MatchingType>> ttk::PDBarycenter::correctMatchings(
138 std::vector<std::vector<MatchingType>> &previous_matchings) {
139
140 std::vector<std::vector<MatchingType>> corrected_matchings(numberOfInputs_);
141 for(int i = 0; i < numberOfInputs_; i++) {
142 // 1. Invert the current_bidder_ids_ vector
143 std::vector<int> new_to_old_id(current_bidder_diagrams_[i].size());
144 for(size_t j = 0; j < current_bidder_ids_[i].size(); j++) {
145 int const new_id = current_bidder_ids_[i][j];
146 if(new_id >= 0) {
147 new_to_old_id[new_id] = j;
148 }
149 }
150 // 2. Reconstruct the matchings
151 std::vector<MatchingType> matchings_diagram_i;
152 for(size_t j = 0; j < previous_matchings[i].size(); j++) {
153 MatchingType m = previous_matchings[i][j];
154 int const new_id = std::get<0>(m);
155 if(new_id >= 0 && std::get<1>(m) >= 0) {
156 std::get<0>(m) = new_to_old_id[new_id];
157 matchings_diagram_i.push_back(m);
158 }
159 }
160 corrected_matchings[i] = matchings_diagram_i;
161 }
162 return corrected_matchings;
163}
164
166 std::vector<std::vector<MatchingType>> &matchings) {
167 // 1. Initialize variables used in the sequel
168 Timer const t_update;
169 size_t const n_goods = barycenter_goods_[0].size();
170
171 size_t const n_diagrams = current_bidder_diagrams_.size();
172 points_added_ = 0;
173 points_deleted_ = 0;
174 double max_shift = 0;
175
176 std::vector<size_t> count_diag_matchings(
177 n_goods,
178 0); // Number of diagonal matchings for each point of the barycenter
179 std::vector<double> x(n_goods, 0);
180 std::vector<double> y(n_goods, 0);
181 std::vector<double> crit_coords_x(n_goods, 0);
182 std::vector<double> crit_coords_y(n_goods, 0);
183 std::vector<double> crit_coords_z(n_goods, 0);
184
185 std::vector<double> min_prices(
186 n_diagrams, std::numeric_limits<double>::max());
187
188 std::vector<Bidder *>
189 points_to_append; // Will collect bidders linked to diagonal
190 std::vector<int> diagonalToNewGood;
191 if(numberOfInputs_ == 2)
192 diagonalToNewGood.resize(current_bidder_diagrams_[0].size()
193 + current_bidder_diagrams_[1].size());
194 // 2. Preprocess the matchings
195 for(size_t j = 0; j < matchings.size(); j++) {
196 for(size_t i = 0; i < matchings[j].size(); i++) {
197 int const bidder_id = std::get<0>(matchings[j][i]);
198 int const good_id = std::get<1>(matchings[j][i]);
199 if(good_id < 0 && bidder_id >= 0) {
200 // Future new barycenter point
201 points_to_append.push_back(&current_bidder_diagrams_[j].at(bidder_id));
202 if(numberOfInputs_ == 2)
203 diagonalToNewGood[-good_id - 1]
204 = n_goods + points_to_append.size() - 1;
205 }
206
207 else if(good_id >= 0 && bidder_id >= 0) {
208 // Update coordinates (to be divided by the number of diagrams later on)
209 x[good_id] += current_bidder_diagrams_[j].at(bidder_id).x_;
210 y[good_id] += current_bidder_diagrams_[j].at(bidder_id).y_;
211 if(geometrical_factor_ < 1) {
212 const auto &critical_coordinates = current_bidder_diagrams_[j]
213 .at(bidder_id)
214 .GetCriticalCoordinates();
215 crit_coords_x[good_id] += critical_coordinates[0];
216 crit_coords_y[good_id] += critical_coordinates[1];
217 crit_coords_z[good_id] += critical_coordinates[2];
218 }
219 } else if(good_id >= 0 && bidder_id < 0) {
220 // Counting the number of times this barycenter point is linked to the
221 // diagonal
222 count_diag_matchings[good_id] = count_diag_matchings[good_id] + 1;
223 }
224 }
225 }
226
227 // 3. Update the previous points of the barycenter
228 for(size_t i = 0; i < n_goods; i++) {
229 if(count_diag_matchings[i] < n_diagrams) {
230 // Barycenter point i is matched at least to one off-diagonal bidder
231 // 3.1 Compute the arithmetic mean of off-diagonal bidders linked to it
232 double const x_bar
233 = x[i] / (double)(n_diagrams - count_diag_matchings[i]);
234 double const y_bar
235 = y[i] / (double)(n_diagrams - count_diag_matchings[i]);
236 // 3.2 Compute the new coordinates of the point (the more linked to the
237 // diagonal it was, the closer to the diagonal it'll be)
238 double const new_x
239 = ((double)(n_diagrams - count_diag_matchings[i]) * x_bar
240 + (double)count_diag_matchings[i] * (x_bar + y_bar) / 2.)
241 / (double)n_diagrams;
242 double const new_y
243 = ((double)(n_diagrams - count_diag_matchings[i]) * y_bar
244 + (double)count_diag_matchings[i] * (x_bar + y_bar) / 2.)
245 / (double)n_diagrams;
246 // TODO Weight by persistence
247 double const new_crit_coord_x
248 = crit_coords_x[i] / (double)(n_diagrams - count_diag_matchings[i]);
249 double const new_crit_coord_y
250 = crit_coords_y[i] / (double)(n_diagrams - count_diag_matchings[i]);
251 double const new_crit_coord_z
252 = crit_coords_z[i] / (double)(n_diagrams - count_diag_matchings[i]);
253
254 // 3.3 Compute and store how much the point has shifted
255 // TODO adjust shift with geometrical_factor_
256 double const dx = barycenter_goods_[0].at(i).x_ - new_x;
257 double const dy = barycenter_goods_[0].at(i).y_ - new_y;
258 double const shift = Geometry::pow(std::abs(dx), wasserstein_)
259 + Geometry::pow(std::abs(dy), wasserstein_);
260 if(shift > max_shift) {
261 max_shift = shift;
262 }
263 // 3.4 Update the position of the point
264 for(size_t j = 0; j < n_diagrams; j++) {
265 barycenter_goods_[j].at(i).SetCoordinates(new_x, new_y);
266 if(geometrical_factor_ < 1) {
267 barycenter_goods_[j].at(i).SetCriticalCoordinates(
268 new_crit_coord_x, new_crit_coord_y, new_crit_coord_z);
269 }
270 if(barycenter_goods_[j].at(i).getPrice() < min_prices[j]) {
271 min_prices[j] = barycenter_goods_[j].at(i).getPrice();
272 }
273 }
274 // TODO Reinitialize/play with prices here if you wish
275 }
276 }
277 for(size_t j = 0; j < n_diagrams; j++) {
278 if(min_prices[j] >= std::numeric_limits<double>::max() / 2.) {
279 min_prices[j] = 0;
280 }
281 }
282
283 // 4. Delete off-diagonal barycenter points not linked to any
284 // off-diagonal bidder
285 for(size_t i = 0; i < n_goods; i++) {
286 if(count_diag_matchings[i] == n_diagrams) {
287 points_deleted_ += 1;
288 double const shift
289 = 2
291 barycenter_goods_[0].at(i).getPersistence() / 2., wasserstein_);
292 if(shift > max_shift) {
293 max_shift = shift;
294 }
295 for(size_t j = 0; j < n_diagrams; j++) {
296 barycenter_goods_[j].at(i).id_ = -1;
297 }
298 }
299 }
300
301 // 5. Append the new points to the barycenter
302 for(size_t k = 0; k < points_to_append.size(); k++) {
303 points_added_ += 1;
304 Bidder *b = points_to_append[k];
305 double const gx
306 = (b->x_ + (n_diagrams - 1) * (b->x_ + b->y_) / 2.) / (n_diagrams);
307 double const gy
308 = (b->y_ + (n_diagrams - 1) * (b->x_ + b->y_) / 2.) / (n_diagrams);
309 const auto &critical_coordinates = b->GetCriticalCoordinates();
310 for(size_t j = 0; j < n_diagrams; j++) {
311 Good g = Good(gx, gy, false, barycenter_goods_[j].size());
312 g.setPrice(min_prices[j]);
313 if(geometrical_factor_ < 1) {
314 g.SetCriticalCoordinates(std::get<0>(critical_coordinates),
315 std::get<1>(critical_coordinates),
316 std::get<2>(critical_coordinates));
317 }
318 barycenter_goods_[j].emplace_back(g);
319 double const shift
320 = 2
322 barycenter_goods_[j].at(g.id_).getPersistence() / 2., wasserstein_);
323 if(shift > max_shift) {
324 max_shift = shift;
325 }
326 }
327 }
328
329 // 6. Finally, recreate barycenter_goods
330 for(size_t j = 0; j < n_diagrams; j++) {
331 int count = 0;
332 GoodDiagram new_barycenter;
333 for(size_t i = 0; i < barycenter_goods_[j].size(); i++) {
334 Good g = barycenter_goods_[j].at(i);
335 if(g.id_ != -1) {
336 g.id_ = count;
337 new_barycenter.emplace_back(g);
338 count++;
339 }
340 }
341 barycenter_goods_[j] = new_barycenter;
342 }
343
344 // final update of matchings here if there are only 2 input diagrams
345 if(numberOfInputs_ == 2) {
346 std::vector<double> costs(n_goods + points_to_append.size(), 0.);
347 for(int i = 0; i < 2; ++i) {
348 for(auto &[b_id, g_id, c] : matchings[i]) {
349 if(g_id < 0 && diagonalToNewGood[-g_id - 1] > 0)
350 g_id = diagonalToNewGood[-g_id - 1];
351 if(g_id >= 0)
352 costs[g_id] += c;
353 }
354 }
355 for(int i = 0; i < 2; ++i) {
356 for(auto &[b_id, g_id, c] : matchings[i]) {
357 if(g_id >= 0)
358 c = costs[g_id] / Geometry::pow(2, wasserstein_);
359 }
360 }
361 }
362
363 return max_shift;
364}
365
367 return rho * rho / 8.0;
368}
369
370double ttk::PDBarycenter::getRho(double epsilon) {
371 return std::sqrt(8.0 * epsilon);
372}
373
375
376 for(int i = 0; i < numberOfInputs_; i++) {
377 DiagramType *CTDiagram = &((*inputDiagrams_)[i]);
378
379 BidderDiagram bidders;
380 for(size_t j = 0; j < CTDiagram->size(); j++) {
381 // Add bidder to bidders
382 Bidder b((*CTDiagram)[j], j, lambda_);
383
384 b.setPositionInAuction(bidders.size());
385 bidders.emplace_back(b);
386 if(b.isDiagonal() || b.x_ == b.y_) {
387 this->printWrn("Diagonal point in diagram !!!");
388 }
389 }
390 bidder_diagrams_.push_back(bidders);
391 current_bidder_diagrams_.emplace_back();
392 std::vector<int> ids(bidders.size(), -1);
393 current_bidder_ids_.push_back(ids);
394 }
395}
396
398 double previous_min_persistence,
399 double min_persistence,
400 std::vector<double> &initial_diagonal_prices,
401 std::vector<double> &initial_off_diagonal_prices,
402 int min_points_to_add,
403 bool add_points_to_barycenter) {
404
405 double new_min_persistence = min_persistence;
406
407 // 1. Get size of the largest current diagram, deduce the maximal number of
408 // points to append
409 size_t max_diagram_size{};
410 for(int i = 0; i < numberOfInputs_; i++) {
411 if(current_bidder_diagrams_[i].size() > max_diagram_size) {
412 max_diagram_size = current_bidder_diagrams_[i].size();
413 }
414 }
415 int const max_points_to_add = std::max(
416 min_points_to_add, min_points_to_add + (int)(max_diagram_size / 10));
417
418 // 2. Get which points can be added, deduce the new minimal persistence
419 std::vector<std::vector<int>> candidates_to_be_added(numberOfInputs_);
420 std::vector<std::vector<int>> idx(numberOfInputs_);
421 for(int i = 0; i < numberOfInputs_; i++) {
422
423 std::vector<double> persistences;
424 for(size_t j = 0; j < bidder_diagrams_[i].size(); j++) {
425 Bidder const b = bidder_diagrams_[i].at(j);
426 double const persistence = b.getPersistence();
427 if(persistence >= min_persistence
428 && persistence < previous_min_persistence) {
429 candidates_to_be_added[i].push_back(j);
430 idx[i].push_back(idx[i].size());
431 persistences.push_back(persistence);
432 }
433 }
434 sort(idx[i].begin(), idx[i].end(), [&persistences](int &a, int &b) {
435 return ((persistences[a] > persistences[b])
436 || ((persistences[a] == persistences[b]) && (a > b)));
437 });
438 int const size = candidates_to_be_added[i].size();
439 if(size >= max_points_to_add) {
440 double const last_persistence_added
441 = persistences[idx[i][max_points_to_add - 1]];
442 if(last_persistence_added > new_min_persistence) {
443 new_min_persistence = last_persistence_added;
444 }
445 }
446 }
447
448 // 3. Add the points to the current diagrams
449
450 // only to give determinism
451 int counter_for_adding_points = 0;
452
453 for(int i = 0; i < numberOfInputs_; i++) {
454 int const size = candidates_to_be_added[i].size();
455 for(int j = 0; j < std::min(max_points_to_add, size); j++) {
456 Bidder b = bidder_diagrams_[i].at(candidates_to_be_added[i][idx[i][j]]);
457 if(b.getPersistence() >= new_min_persistence) {
458 b.id_ = current_bidder_diagrams_[i].size();
460 b.setDiagonalPrice(initial_diagonal_prices[i]);
461 current_bidder_diagrams_[i].emplace_back(b);
462 // b.id_ --> position of b in current_bidder_diagrams_[i]
463 current_bidder_ids_[i][candidates_to_be_added[i][idx[i][j]]]
464 = current_bidder_diagrams_[i].size() - 1;
465
466 int const to_be_added_to_barycenter
467 = deterministic_ ? counter_for_adding_points % numberOfInputs_
468 : rand() % numberOfInputs_;
469 // We add the bidder as a good with probability 1/n_diagrams
470 if(to_be_added_to_barycenter == 0 && add_points_to_barycenter) {
471 for(int k = 0; k < numberOfInputs_; k++) {
472 Good g = Good(b.x_, b.y_, false, barycenter_goods_[k].size());
473 g.setPrice(initial_off_diagonal_prices[k]);
474 g.SetCriticalCoordinates(b.coords_[0], b.coords_[1], b.coords_[2]);
475 barycenter_goods_[k].emplace_back(g);
476 }
477 }
478 }
479 counter_for_adding_points++;
480 }
481 }
482 return new_min_persistence;
483}
484
486 double max_persistence = 0;
487 for(int i = 0; i < numberOfInputs_; i++) {
489 for(size_t j = 0; j < D.size(); j++) {
490 // Add bidder to bidders
491 Bidder const &b = D.at(j);
492 double const persistence = b.getPersistence();
493 if(persistence > max_persistence) {
494 max_persistence = persistence;
495 }
496 }
497 }
498 return max_persistence;
499}
500
502 double min_price = std::numeric_limits<double>::max();
503
505 if(D.empty()) {
506 return 0;
507 }
508 for(size_t j = 0; j < D.size(); j++) {
509 Good const &b = D.at(j);
510 double const price = b.getPrice();
511 if(price < min_price) {
512 min_price = price;
513 }
514 }
515 if(min_price >= std::numeric_limits<double>::max() / 2.) {
516 return 0;
517 }
518 return min_price;
519}
520
522 double lowest_persistence = std::numeric_limits<double>::max();
523 for(int i = 0; i < numberOfInputs_; i++) {
525 for(size_t j = 0; j < D.size(); j++) {
526 // Add bidder to bidders
527 Bidder const &b = D.at(j);
528 double const persistence = b.getPersistence();
529 if(persistence < lowest_persistence && persistence > 0) {
530 lowest_persistence = persistence;
531 }
532 }
533 }
534 if(lowest_persistence >= std::numeric_limits<double>::max() / 2.) {
535 return 0;
536 }
537 return lowest_persistence;
538}
539
540void ttk::PDBarycenter::setInitialBarycenter(double min_persistence) {
541 int size = 0;
542 int random_idx;
543 DiagramType *CTDiagram;
544 int iter = 0;
545 while(size == 0) {
546 random_idx
547 = deterministic_ ? iter % numberOfInputs_ : rand() % numberOfInputs_;
548 CTDiagram = &((*inputDiagrams_)[random_idx]);
549 for(int i = 0; i < numberOfInputs_; i++) {
550 GoodDiagram goods;
551 int count = 0;
552 for(size_t j = 0; j < CTDiagram->size(); j++) {
553 // Add good to goods
554 Good const g = Good((*CTDiagram)[j], count, lambda_);
555 if(g.getPersistence() >= min_persistence) {
556 goods.emplace_back(g);
557 count++;
558 }
559 }
560 if(static_cast<int>(barycenter_goods_.size()) < (i + 1)) {
561 barycenter_goods_.push_back(goods);
562 } else {
563 barycenter_goods_[i] = goods;
564 }
565 }
566 size = barycenter_goods_[0].size();
567 iter++;
568 }
569}
570
572 Timer tm;
573 auto kdt = std::make_unique<KDT>(true, wasserstein_);
574
575 const int dimension = geometrical_factor_ >= 1 ? 2 : 5;
576
577 std::vector<double> coordinates;
578 std::vector<std::vector<double>> weights;
579
580 for(size_t i = 0; i < barycenter_goods_[0].size(); i++) {
581 const Good &g = barycenter_goods_[0].at(i);
582 coordinates.push_back(geometrical_factor_ * g.x_);
583 coordinates.push_back(geometrical_factor_ * g.y_);
584 if(geometrical_factor_ < 1) {
585 coordinates.push_back((1 - geometrical_factor_) * g.coords_[0]);
586 coordinates.push_back((1 - geometrical_factor_) * g.coords_[1]);
587 coordinates.push_back((1 - geometrical_factor_) * g.coords_[2]);
588 }
589 }
590
591 for(size_t idx = 0; idx < barycenter_goods_.size(); idx++) {
592 std::vector<double> const empty_weights;
593 weights.push_back(empty_weights);
594 for(size_t i = 0; i < barycenter_goods_[idx].size(); i++) {
595 const Good &g = barycenter_goods_[idx].at(i);
596 weights[idx].push_back(g.getPrice());
597 }
598 }
599 // Correspondence map : position in barycenter_goods_ --> KDT node
600
601 auto correspondence_kdt_map
602 = kdt->build(coordinates.data(), barycenter_goods_[0].size(), dimension,
603 weights, barycenter_goods_.size());
604 this->printMsg(" Building KDTree", 1, tm.getElapsedTime(),
606 return std::make_pair(std::move(kdt), correspondence_kdt_map);
607}
608
609std::vector<std::vector<ttk::MatchingType>>
611
612 std::vector<std::vector<MatchingType>> previous_matchings;
613 double const min_persistence = 0;
614 double min_cost = std::numeric_limits<double>::max();
615 int last_min_cost_obtained = 0;
616
617 this->setBidderDiagrams();
619 min_persistence); // false for a determinist initialization
620
621 double const max_persistence = getMaxPersistence();
622
623 std::vector<double> min_diag_price(numberOfInputs_, 0);
624 std::vector<double> min_price(numberOfInputs_, 0);
625
626 int const min_points_to_add = std::numeric_limits<int>::max();
627 this->enrichCurrentBidderDiagrams(2 * max_persistence, min_persistence,
628 min_diag_price, min_price,
629 min_points_to_add, false);
630
631 bool converged = false;
632 bool finished = false;
633 double total_cost;
634
635 while(!finished) {
636 Timer const tm;
637
638 std::pair<std::unique_ptr<KDT>, std::vector<KDT *>> pair;
639 bool use_kdt = false;
640 // If the barycenter is empty, do not compute the kdt (or it will crash :/)
641 // TODO Fix KDTree to handle empty inputs...
642 if(!barycenter_goods_[0].empty()) {
643 pair = this->getKDTree();
644 use_kdt = true;
645 }
646
647 std::vector<std::vector<MatchingType>> all_matchings(numberOfInputs_);
648 std::vector<int> sizes(numberOfInputs_);
649 for(int i = 0; i < numberOfInputs_; i++) {
650 sizes[i] = current_bidder_diagrams_[i].size();
651 }
652
653 total_cost = 0;
654
655 barycenter.clear();
656 for(size_t j = 0; j < barycenter_goods_[0].size(); j++) {
657 Good const &g = barycenter_goods_[0].at(j);
658 barycenter.emplace_back(PersistencePair{
659 CriticalVertex{0, g.x_, {}, {}, nt1_},
660 CriticalVertex{0, g.y_, {}, {}, nt2_}, diagramType_, true});
661 }
662
663 bool const actual_distance = (numberOfInputs_ == 2);
664 runMatchingAuction(&total_cost, sizes, *pair.first, pair.second,
665 &min_diag_price, &all_matchings, use_kdt,
666 actual_distance);
667
668 this->printMsg("Barycenter cost : " + std::to_string(total_cost),
670
671 if(converged) {
672 finished = true;
673 }
674
675 if(!finished) {
676 updateBarycenter(all_matchings);
677
678 if(min_cost > total_cost) {
679 min_cost = total_cost;
680 last_min_cost_obtained = 0;
681 } else {
682 last_min_cost_obtained += 1;
683 }
684
685 converged = last_min_cost_obtained > 1;
686 if(numberOfInputs_ == 2)
687 finished = true;
688 }
689
690 previous_matchings = std::move(all_matchings);
691 // END OF TIMER
692
693 for(size_t i = 0; i < barycenter_goods_.size(); ++i) {
694 for(size_t j = 0; j < barycenter_goods_[i].size(); ++j) {
695 barycenter_goods_[i].at(j).setPrice(0);
696 }
697 }
698 for(size_t i = 0; i < current_bidder_diagrams_.size(); ++i) {
699 for(size_t j = 0; j < current_bidder_diagrams_[i].size(); ++j) {
700 current_bidder_diagrams_[i].at(j).setDiagonalPrice(0);
701 }
702 }
703 min_diag_price.assign(numberOfInputs_, 0);
704 min_price.assign(numberOfInputs_, 0);
705 }
706 barycenter.resize(0);
707 for(size_t j = 0; j < barycenter_goods_[0].size(); j++) {
708 Good const &g = barycenter_goods_[0].at(j);
709 barycenter.emplace_back(PersistencePair{
710 CriticalVertex{0, g.x_, {}, {}, nt1_},
711 CriticalVertex{0, g.y_, {}, {}, nt2_}, diagramType_, true});
712 }
713
714 cost_ = total_cost;
715 std::vector<std::vector<MatchingType>> corrected_matchings
716 = correctMatchings(previous_matchings);
717 return corrected_matchings;
718}
719
721 double total_real_cost = 0;
722 std::vector<MatchingType> fake_matchings;
723 for(int i = 0; i < numberOfInputs_; i++) {
725 lambda_, 0.01, true, nonMatchingWeight_);
726 GoodDiagram const current_barycenter = barycenter_goods_[0];
727 BidderDiagram const current_bidder_diagram = bidder_diagrams_[i];
728 auction.BuildAuctionDiagrams(current_bidder_diagram, current_barycenter);
729 double const cost = auction.run(fake_matchings);
730 total_real_cost += cost * cost;
731 }
732 return sqrt(total_real_cost);
733}
734
735bool ttk::PDBarycenter::isPrecisionObjectiveMet(double precision_objective,
736 int mode) {
737 if(mode == 0) { // ABSOLUTE PRECISION
738 for(int i_input = 0; i_input < numberOfInputs_; i_input++) {
739 if(precision_[i_input] > precision_objective) {
740 return false;
741 }
742 }
743 } else if(mode == 1) { // AVERAGE PRECISION
744 double const average_precision
745 = std::accumulate(precision_.begin(), precision_.end(), 0.0)
747 if(average_precision > precision_objective) {
748 return false;
749 }
750 }
751 return true;
752}
void setDiagonalPrice(const double price)
void setPositionInAuction(const int pos)
int printWrn(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:159
void setPrice(const double price)
std::vector< std::vector< MatchingType > > executeAuctionBarycenter(DiagramType &barycenter)
std::vector< double > precision_
ttk::CriticalType nt1_
double getEpsilon(double rho)
void runMatching(double *total_cost, double epsilon, std::vector< int > &sizes, KDT &kdt, std::vector< KDT * > &correspondence_kdt_map, std::vector< double > *min_diag_price, std::vector< double > *min_price, std::vector< std::vector< MatchingType > > *all_matchings, bool use_kdt, bool actual_distance)
std::pair< typename KDT::KDTreeRoot, typename KDT::KDTreeMap > KDTreePair
ttk::CriticalType nt2_
std::vector< GoodDiagram > barycenter_goods_
double getRho(double epsilon)
std::vector< BidderDiagram > current_bidder_diagrams_
double updateBarycenter(std::vector< std::vector< MatchingType > > &matchings)
void setInitialBarycenter(double min_persistence)
bool isPrecisionObjectiveMet(double, int)
std::vector< BidderDiagram > bidder_diagrams_
KDTreePair getKDTree() const
std::vector< std::vector< MatchingType > > correctMatchings(std::vector< std::vector< MatchingType > > &previous_matchings)
void runMatchingAuction(double *total_cost, std::vector< int > &sizes, KDT &kdt, std::vector< KDT * > &correspondence_kdt_map, std::vector< double > *min_diag_price, std::vector< std::vector< MatchingType > > *all_matchings, bool use_kdt, bool actual_distance)
PersistenceDiagramAuction::KDT KDT
double getMinimalPrice(int i)
double getLowestPersistence()
std::vector< std::vector< MatchingType > > execute(DiagramType &barycenter)
bool hasBarycenterConverged(std::vector< std::vector< MatchingType > > &matchings, std::vector< std::vector< MatchingType > > &previous_matchings)
std::vector< std::vector< int > > current_bidder_ids_
double enrichCurrentBidderDiagrams(double previous_min_persistence, double min_persistence, std::vector< double > &initial_diagonal_prices, std::vector< double > &initial_off_diagonal_prices, int min_points_to_add, bool add_points_to_barycenter=true)
void SetCriticalCoordinates(const float coords_x, const float coords_y, const float coords_z)
std::array< float, 3 > GetCriticalCoordinates() const
void runAuctionRound(int &n_biddings, const int kdt_index=0)
double run(std::vector< MatchingType > &matchings, const int kdt_index=0)
double initLowerBoundCost(const int kdt_index=0)
double getMatchingsAndDistance(std::vector< MatchingType > &matchings, bool get_diagonal_matches=false)
void BuildAuctionDiagrams(const BidderDiagram &BD, const GoodDiagram &GD)
void initLowerBoundCostWeight(double delta_lim)
double getElapsedTime()
Definition Timer.h:15
T1 pow(const T1 val, const T2 n)
Definition Geometry.h:456
std::tuple< int, int, double > MatchingType
Matching between two Persistence Diagram pairs.
std::vector< Good > GoodDiagram
std::vector< Bidder > BidderDiagram
std::vector< PersistencePair > DiagramType
Persistence Diagram type as a vector of Persistence pairs.
T end(std::pair< T, T > &p)
Definition ripser.cpp:503
T begin(std::pair< T, T > &p)
Definition ripser.cpp:499
printMsg(debug::output::BOLD+" | | | | | . \\ | | (__| | / __/| |_| / __/| (_) |"+debug::output::ENDCOLOR, debug::Priority::PERFORMANCE, debug::LineMode::NEW, stream)