-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_algorithm.cc
More file actions
43 lines (34 loc) · 1.17 KB
/
random_algorithm.cc
File metadata and controls
43 lines (34 loc) · 1.17 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
// Copyright 2025 The Authors (see AUTHORS file)
// SPDX-License-Identifier: Apache-2.0
#include "random_algorithm.h"
#include "utilities.h"
void RandomAlgorithm::Init(const SubmodularFunction& sub_func_f,
const FairnessConstraint& fairness,
const Matroid& matroid) {
Algorithm::Init(sub_func_f, fairness, matroid);
universe_elements_.clear();
solution_.clear();
}
void RandomAlgorithm::Insert(int element) {
universe_elements_.push_back(element);
}
double RandomAlgorithm::GetSolutionValue() {
matroid_->Reset();
solution_.clear();
std::unique_ptr<Matroid> upper_bound_matroid = fairness_->UpperBoundsToMatroid();
RandomHandler::Shuffle(universe_elements_);
for (int element : universe_elements_) {
if (matroid_->CanAdd(element) && upper_bound_matroid->CanAdd(element)) {
matroid_->Add(element);
upper_bound_matroid->Add(element);
solution_.push_back(element);
}
}
return sub_func_f_->ObjectiveAndIncreaseOracleCall(solution_);
}
std::vector<int> RandomAlgorithm::GetSolutionVector() {
return solution_;
}
std::string RandomAlgorithm::GetAlgorithmName() const {
return "Random algorithm";
}