-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmodular_function.h
More file actions
90 lines (67 loc) · 3.17 KB
/
submodular_function.h
File metadata and controls
90 lines (67 loc) · 3.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2025 The Authors (see AUTHORS file)
// SPDX-License-Identifier: Apache-2.0
#ifndef FAIR_SUBMODULAR_MATROID_SUBMODULAR_FUNCTION_H_
#define FAIR_SUBMODULAR_MATROID_SUBMODULAR_FUNCTION_H_
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
// A submodular function object maintains a current solution set S,
// but does *not* maintain its value (that should be maintained by the user,
// i.e., the algorithm, or alternatively one can e.g. call
// `ObjectiveAndIncreaseOracleCall()` at the end for the final solution).
class SubmodularFunction {
public:
static int64_t oracle_calls_;
virtual ~SubmodularFunction() = default;
// Sets S = empty set.
virtual void Reset() = 0;
// there used to be a function Init() here too, but removed it
// Return the objective value of a set 'S' and also increases oracle_calls.
// Does not depend on the current state of the object.
double ObjectiveAndIncreaseOracleCall(const std::vector<int>& elements) const;
// Adds a new element to set S. Does not return any value and does not cost an
// oracle call.
virtual void Add(int element) = 0;
// Removes an element from S. Does not return any value and does not cost an
// oracle call. Assumes (without checking) that e is in S.
virtual void Remove(int element) = 0;
// Swap an element for one already in the solution.
virtual void Swap(int element, int swap);
// Returns the delta if this element were added (but does not add it),
// and also increases oracle_calls.
double DeltaAndIncreaseOracleCall(int element);
// Adds element if and only if its contribution is >= thre and also increases
// oracle_calls. Returns the contribution increase (if added, otherwise 0).
virtual double AddAndIncreaseOracleCall(int element, double thre);
// Computes f(S) - f(S - e). Does not remove e. Costs one oracle call.
// Assumes (without checking) that e is in S.
double RemovalDeltaAndIncreaseOracleCall(int element);
// Remove an element e from S.
// Return f(S) - f(S - e).
// Costs one oracle call.
// Assumes (without checking) that e is in S.
virtual double RemoveAndIncreaseOracleCall(int element);
// Returns the universe of the utility function, as pairs.
// The first element of the pair is the name of the element and the second is
// its color.
virtual const std::vector<int>& GetUniverse() const = 0;
// Get name of utility function.
virtual std::string GetName() const = 0;
// Clone the object (see e.g. GraphUtility for an example).
virtual std::unique_ptr<SubmodularFunction> Clone() const = 0;
// Gets geometrically increasing sequence of estimates for OPT.
// Should be always run on an empty function.
std::vector<double> GetOptEstimates(
int upper_bound_on_size_of_any_feasible_set);
protected:
// Computes f(S u {e}) - f(S).
virtual double Delta(int element) = 0;
// Computes f(S) - f(S - e).
// Assumes (without checking) that e is in S.
virtual double RemovalDelta(int element) = 0;
// Computes f(S).
// Does not depend on the current state of the object.
virtual double Objective(const std::vector<int>& elements) const = 0;
};
#endif // FAIR_SUBMODULAR_MATROID_SUBMODULAR_FUNCTION_H_