-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupper_bound_matroid_intersection_algorithm.cc
More file actions
59 lines (50 loc) · 1.81 KB
/
upper_bound_matroid_intersection_algorithm.cc
File metadata and controls
59 lines (50 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2025 The Authors (see AUTHORS file)
// SPDX-License-Identifier: Apache-2.0
#include "upper_bound_matroid_intersection_algorithm.h"
#include <memory>
#include <string>
#include <vector>
#include "fairness_constraint.h"
#include "matroid.h"
#include "matroid_intersection.h"
#include "submodular_function.h"
void UpperBoundMatroidIntersectionAlgorithm::Init(const SubmodularFunction& sub_func_f,
const FairnessConstraint& fairness,
const Matroid& matroid) {
Algorithm::Init(sub_func_f, fairness, matroid);
universe_elements_.clear();
solution_vector_.clear();
}
void UpperBoundMatroidIntersectionAlgorithm::Insert(int element) {
universe_elements_.push_back(element);
}
void UpperBoundMatroidIntersectionAlgorithm::Solve() {
matroid_->Reset();
sub_func_f_->Reset();
auto fairness_matroid = fairness_->UpperBoundsToMatroid();
if (use_greedy_) {
Greedy(matroid_.get(), fairness_matroid.get(), sub_func_f_.get(),
universe_elements_);
} else {
SubMaxIntersectionSwapping(matroid_.get(), fairness_matroid.get(), sub_func_f_.get(),
universe_elements_);
}
solution_vector_ = matroid_->GetCurrent();
solution_value_ =
sub_func_f_->ObjectiveAndIncreaseOracleCall(solution_vector_);
}
double UpperBoundMatroidIntersectionAlgorithm::GetSolutionValue() {
if (solution_vector_.empty()) {
Solve();
}
return solution_value_;
}
std::vector<int> UpperBoundMatroidIntersectionAlgorithm::GetSolutionVector() {
if (solution_vector_.empty()) {
Solve();
}
return solution_vector_;
}
std::string UpperBoundMatroidIntersectionAlgorithm::GetAlgorithmName() const {
return std::string("Upper bound matroid intersection algorithm (") + (use_greedy_ ? "greedy" : "swapping") + ")";
}