29 std::vector<int> &sizes,
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,
36 bool actual_distance) {
37 Timer const time_matchings;
39 double local_cost = *total_cost;
40#ifdef TTK_ENABLE_OPENMP
41#pragma omp parallel for num_threads(threadNumber_) schedule(dynamic, 1) reduction(+:local_cost)
43 for(
int i = 0; i < numberOfInputs_; i++) {
45 current_bidder_diagrams_[i], barycenter_goods_[i], wasserstein_,
46 geometrical_factor_, lambda_, delta_lim_, kdt, correspondence_kdt_map,
47 epsilon, min_diag_price->at(i), use_kdt, nonMatchingWeight_);
56 min_price->at(i) = getMinimalPrice(i);
57 std::vector<MatchingType> matchings;
59 all_matchings->at(i) = matchings;
61 local_cost += sqrt(cost);
68 precision_[i] = quotient < 1 ? 1. / sqrt(1 - quotient) - 1 : 10;
74 current_bidder_diagrams_[i].resize(sizes[i]);
76 *total_cost = local_cost;
81 std::vector<int> &sizes,
83 std::vector<KDT *> &correspondence_kdt_map,
84 std::vector<double> *min_diag_price,
85 std::vector<std::vector<MatchingType>> *all_matchings,
87 bool actual_distance) {
88 double local_cost = *total_cost;
89#ifdef TTK_ENABLE_OPENMP
90#pragma omp parallel for num_threads(threadNumber_) schedule(dynamic, 1) reduction(+:local_cost)
92 for(
int i = 0; i < numberOfInputs_; i++) {
94 current_bidder_diagrams_[i], barycenter_goods_[i], wasserstein_,
95 geometrical_factor_, lambda_, delta_lim_, kdt, correspondence_kdt_map, 0,
96 (*min_diag_price)[i], use_kdt, nonMatchingWeight_);
97 std::vector<MatchingType> matchings;
98 double const cost = auction.
run(matchings, i);
99 all_matchings->at(i) = matchings;
100 if(actual_distance) {
101 local_cost += sqrt(cost);
109 current_bidder_diagrams_[i].resize(sizes[i]);
111 *total_cost = local_cost;
164 std::vector<std::vector<MatchingType>> &matchings) {
166 Timer const t_update;
167 size_t const n_goods = barycenter_goods_[0].size();
169 size_t const n_diagrams = current_bidder_diagrams_.size();
172 double max_shift = 0;
174 std::vector<size_t> count_diag_matchings(
177 std::vector<double> x(n_goods, 0);
178 std::vector<double> y(n_goods, 0);
179 std::vector<double> crit_coords_x(n_goods, 0);
180 std::vector<double> crit_coords_y(n_goods, 0);
181 std::vector<double> crit_coords_z(n_goods, 0);
183 std::vector<double> min_prices(
184 n_diagrams, std::numeric_limits<double>::max());
186 std::vector<Bidder *>
188 std::vector<int> diagonalToNewGood;
189 if(numberOfInputs_ == 2)
190 diagonalToNewGood.resize(current_bidder_diagrams_[0].size()
191 + current_bidder_diagrams_[1].size());
193 for(
size_t j = 0; j < matchings.size(); j++) {
194 for(
size_t i = 0; i < matchings[j].size(); i++) {
195 int const bidder_id = std::get<0>(matchings[j][i]);
196 int const good_id = std::get<1>(matchings[j][i]);
197 if(good_id < 0 && bidder_id >= 0) {
199 points_to_append.push_back(¤t_bidder_diagrams_[j].at(bidder_id));
200 if(numberOfInputs_ == 2)
201 diagonalToNewGood[-good_id - 1]
202 = n_goods + points_to_append.size() - 1;
205 else if(good_id >= 0 && bidder_id >= 0) {
207 x[good_id] += current_bidder_diagrams_[j].at(bidder_id).x_;
208 y[good_id] += current_bidder_diagrams_[j].at(bidder_id).y_;
209 if(geometrical_factor_ < 1) {
210 const auto &critical_coordinates = current_bidder_diagrams_[j]
212 .GetCriticalCoordinates();
213 crit_coords_x[good_id] += critical_coordinates[0];
214 crit_coords_y[good_id] += critical_coordinates[1];
215 crit_coords_z[good_id] += critical_coordinates[2];
217 }
else if(good_id >= 0 && bidder_id < 0) {
220 count_diag_matchings[good_id] = count_diag_matchings[good_id] + 1;
226 for(
size_t i = 0; i < n_goods; i++) {
227 if(count_diag_matchings[i] < n_diagrams) {
231 = x[i] / (double)(n_diagrams - count_diag_matchings[i]);
233 = y[i] / (double)(n_diagrams - count_diag_matchings[i]);
237 = ((double)(n_diagrams - count_diag_matchings[i]) * x_bar
238 + (double)count_diag_matchings[i] * (x_bar + y_bar) / 2.)
239 / (
double)n_diagrams;
241 = ((double)(n_diagrams - count_diag_matchings[i]) * y_bar
242 + (double)count_diag_matchings[i] * (x_bar + y_bar) / 2.)
243 / (
double)n_diagrams;
245 double const new_crit_coord_x
246 = crit_coords_x[i] / (double)(n_diagrams - count_diag_matchings[i]);
247 double const new_crit_coord_y
248 = crit_coords_y[i] / (double)(n_diagrams - count_diag_matchings[i]);
249 double const new_crit_coord_z
250 = crit_coords_z[i] / (double)(n_diagrams - count_diag_matchings[i]);
254 double const dx = barycenter_goods_[0].at(i).x_ - new_x;
255 double const dy = barycenter_goods_[0].at(i).y_ - new_y;
256 double const shift =
Geometry::pow(std::abs(dx), wasserstein_)
258 if(shift > max_shift) {
262 for(
size_t j = 0; j < n_diagrams; j++) {
263 barycenter_goods_[j].at(i).SetCoordinates(new_x, new_y);
264 if(geometrical_factor_ < 1) {
265 barycenter_goods_[j].at(i).SetCriticalCoordinates(
266 new_crit_coord_x, new_crit_coord_y, new_crit_coord_z);
268 if(barycenter_goods_[j].at(i).getPrice() < min_prices[j]) {
269 min_prices[j] = barycenter_goods_[j].at(i).getPrice();
275 for(
size_t j = 0; j < n_diagrams; j++) {
276 if(min_prices[j] >= std::numeric_limits<double>::max() / 2.) {
283 for(
size_t i = 0; i < n_goods; i++) {
284 if(count_diag_matchings[i] == n_diagrams) {
285 points_deleted_ += 1;
289 barycenter_goods_[0].at(i).getPersistence() / 2., wasserstein_);
290 if(shift > max_shift) {
293 for(
size_t j = 0; j < n_diagrams; j++) {
294 barycenter_goods_[j].at(i).id_ = -1;
300 for(
size_t k = 0; k < points_to_append.size(); k++) {
302 Bidder *b = points_to_append[k];
304 = (b->
x_ + (n_diagrams - 1) * (b->
x_ + b->
y_) / 2.) / (n_diagrams);
306 = (b->
y_ + (n_diagrams - 1) * (b->
x_ + b->
y_) / 2.) / (n_diagrams);
308 for(
size_t j = 0; j < n_diagrams; j++) {
309 Good g =
Good(gx, gy,
false, barycenter_goods_[j].size());
311 if(geometrical_factor_ < 1) {
313 std::get<1>(critical_coordinates),
314 std::get<2>(critical_coordinates));
316 barycenter_goods_[j].emplace_back(g);
320 barycenter_goods_[j].at(g.
id_).getPersistence() / 2., wasserstein_);
321 if(shift > max_shift) {
328 for(
size_t j = 0; j < n_diagrams; j++) {
331 for(
size_t i = 0; i < barycenter_goods_[j].size(); i++) {
332 Good g = barycenter_goods_[j].at(i);
335 new_barycenter.emplace_back(g);
339 barycenter_goods_[j] = new_barycenter;
343 if(numberOfInputs_ == 2) {
344 std::vector<double> costs(n_goods + points_to_append.size(), 0.);
345 for(
int i = 0; i < 2; ++i) {
346 for(
auto &[b_id, g_id, c] : matchings[i]) {
347 if(g_id < 0 && diagonalToNewGood[-g_id - 1] > 0)
348 g_id = diagonalToNewGood[-g_id - 1];
353 for(
int i = 0; i < 2; ++i) {
354 for(
auto &[b_id, g_id, c] : matchings[i]) {
396 double previous_min_persistence,
397 double min_persistence,
398 std::vector<double> &initial_diagonal_prices,
399 std::vector<double> &initial_off_diagonal_prices,
400 int min_points_to_add,
401 bool add_points_to_barycenter) {
403 double new_min_persistence = min_persistence;
407 size_t max_diagram_size{};
408 for(
int i = 0; i < numberOfInputs_; i++) {
409 if(current_bidder_diagrams_[i].size() > max_diagram_size) {
410 max_diagram_size = current_bidder_diagrams_[i].size();
413 int const max_points_to_add = std::max(
414 min_points_to_add, min_points_to_add + (
int)(max_diagram_size / 10));
417 std::vector<std::vector<int>> candidates_to_be_added(numberOfInputs_);
418 std::vector<std::vector<int>> idx(numberOfInputs_);
419 for(
int i = 0; i < numberOfInputs_; i++) {
421 std::vector<double> persistences;
422 for(
size_t j = 0; j < bidder_diagrams_[i].size(); j++) {
423 Bidder const b = bidder_diagrams_[i].at(j);
425 if(persistence >= min_persistence
426 && persistence < previous_min_persistence) {
427 candidates_to_be_added[i].push_back(j);
428 idx[i].push_back(idx[i].size());
429 persistences.push_back(persistence);
432 sort(idx[i].
begin(), idx[i].
end(), [&persistences](
int &a,
int &b) {
433 return ((persistences[a] > persistences[b])
434 || ((persistences[a] == persistences[b]) && (a > b)));
436 int const size = candidates_to_be_added[i].size();
437 if(size >= max_points_to_add) {
438 double const last_persistence_added
439 = persistences[idx[i][max_points_to_add - 1]];
440 if(last_persistence_added > new_min_persistence) {
441 new_min_persistence = last_persistence_added;
449 int counter_for_adding_points = 0;
451 for(
int i = 0; i < numberOfInputs_; i++) {
452 int const size = candidates_to_be_added[i].size();
453 for(
int j = 0; j < std::min(max_points_to_add, size); j++) {
454 Bidder b = bidder_diagrams_[i].at(candidates_to_be_added[i][idx[i][j]]);
456 b.
id_ = current_bidder_diagrams_[i].size();
459 current_bidder_diagrams_[i].emplace_back(b);
461 current_bidder_ids_[i][candidates_to_be_added[i][idx[i][j]]]
462 = current_bidder_diagrams_[i].size() - 1;
464 int const to_be_added_to_barycenter
465 = deterministic_ ? counter_for_adding_points % numberOfInputs_
466 : rand() % numberOfInputs_;
468 if(to_be_added_to_barycenter == 0 && add_points_to_barycenter) {
469 for(
int k = 0; k < numberOfInputs_; k++) {
470 Good g =
Good(b.
x_, b.
y_,
false, barycenter_goods_[k].size());
471 g.
setPrice(initial_off_diagonal_prices[k]);
473 barycenter_goods_[k].emplace_back(g);
477 counter_for_adding_points++;
480 return new_min_persistence;
571 auto kdt = std::make_unique<KDT>(
true, wasserstein_);
573 const int dimension = geometrical_factor_ >= 1 ? 2 : 5;
575 std::vector<double> coordinates;
576 std::vector<std::vector<double>> weights;
578 for(
size_t i = 0; i < barycenter_goods_[0].size(); i++) {
579 const Good &g = barycenter_goods_[0].at(i);
580 coordinates.push_back(geometrical_factor_ * g.
x_);
581 coordinates.push_back(geometrical_factor_ * g.
y_);
582 if(geometrical_factor_ < 1) {
583 coordinates.push_back((1 - geometrical_factor_) * g.
coords_[0]);
584 coordinates.push_back((1 - geometrical_factor_) * g.
coords_[1]);
585 coordinates.push_back((1 - geometrical_factor_) * g.
coords_[2]);
589 for(
size_t idx = 0; idx < barycenter_goods_.size(); idx++) {
590 std::vector<double>
const empty_weights;
591 weights.push_back(empty_weights);
592 for(
size_t i = 0; i < barycenter_goods_[idx].size(); i++) {
593 const Good &g = barycenter_goods_[idx].at(i);
594 weights[idx].push_back(g.
getPrice());
599 auto correspondence_kdt_map
600 = kdt->build(coordinates.data(), barycenter_goods_[0].size(), dimension,
601 weights, barycenter_goods_.size());
604 return std::make_pair(std::move(kdt), correspondence_kdt_map);
610 std::vector<std::vector<MatchingType>> previous_matchings;
611 double const min_persistence = 0;
612 double min_cost = std::numeric_limits<double>::max();
613 int last_min_cost_obtained = 0;
615 this->setBidderDiagrams();
616 this->setInitialBarycenter(
619 double const max_persistence = getMaxPersistence();
621 std::vector<double> min_diag_price(numberOfInputs_, 0);
622 std::vector<double> min_price(numberOfInputs_, 0);
624 int const min_points_to_add = std::numeric_limits<int>::max();
625 this->enrichCurrentBidderDiagrams(2 * max_persistence, min_persistence,
626 min_diag_price, min_price,
627 min_points_to_add,
false);
629 bool converged =
false;
630 bool finished =
false;
636 std::pair<std::unique_ptr<KDT>, std::vector<KDT *>> pair;
637 bool use_kdt =
false;
640 if(!barycenter_goods_[0].empty()) {
641 pair = this->getKDTree();
645 std::vector<std::vector<MatchingType>> all_matchings(numberOfInputs_);
646 std::vector<int> sizes(numberOfInputs_);
647 for(
int i = 0; i < numberOfInputs_; i++) {
648 sizes[i] = current_bidder_diagrams_[i].size();
654 for(
size_t j = 0; j < barycenter_goods_[0].size(); j++) {
655 Good const &g = barycenter_goods_[0].at(j);
658 diagramType_,
true});
661 bool const actual_distance = (numberOfInputs_ == 2);
662 runMatchingAuction(&total_cost, sizes, *pair.first, pair.second,
663 &min_diag_price, &all_matchings, use_kdt,
666 this->
printMsg(
"Barycenter cost : " + std::to_string(total_cost),
674 updateBarycenter(all_matchings);
676 if(min_cost > total_cost) {
677 min_cost = total_cost;
678 last_min_cost_obtained = 0;
680 last_min_cost_obtained += 1;
683 converged = last_min_cost_obtained > 1;
684 if(numberOfInputs_ == 2)
688 previous_matchings = std::move(all_matchings);
691 for(
size_t i = 0; i < barycenter_goods_.size(); ++i) {
692 for(
size_t j = 0; j < barycenter_goods_[i].size(); ++j) {
693 barycenter_goods_[i].at(j).setPrice(0);
696 for(
size_t i = 0; i < current_bidder_diagrams_.size(); ++i) {
697 for(
size_t j = 0; j < current_bidder_diagrams_[i].size(); ++j) {
698 current_bidder_diagrams_[i].at(j).setDiagonalPrice(0);
701 min_diag_price.assign(numberOfInputs_, 0);
702 min_price.assign(numberOfInputs_, 0);
704 barycenter.resize(0);
705 for(
size_t j = 0; j < barycenter_goods_[0].size(); j++) {
706 Good const &g = barycenter_goods_[0].at(j);
709 diagramType_,
true});
713 std::vector<std::vector<MatchingType>> corrected_matchings
714 = correctMatchings(previous_matchings);
715 return corrected_matchings;