Class Random¶
Defined in File Random.h
Inheritance Relationships¶
Base Type¶
public SubmodularOptimizer(Class SubmodularOptimizer)
Class Documentation¶
-
class Random : public SubmodularOptimizer¶
The Random Optimizers for submodular functions. It randomly picks \( K \) elements as a solution. For streaming, Reservoir Sampling is used. Feige et al. showed that a uniform random sample for unconstrained is a 1/4 approximation in expectation, but for constraint maximization problems no such result is known.
Stream: Yes
Solution: no guarantee
Runtime: \( O(N) \)
Memory: \( O(K) \)
Function Queries per Element: \( O(1) \) (In case of Reservoir Sampling to maintain a consistent function value)
Function Types: nonnegative submodular functions
Example usage in C++:
//read some data std::vector<std::vector<data_t>> = read_some_data(); auto K = 50; // Define the function to be maximized and select the summary FastIVM fastIVM(K, RBFKernel( std::sqrt(data[0].size()), 1.0) , 1.0); Random opt(K, fastIVM); opt.fit(data); std::cout << "fval:" << opt.get_fval() << "num_elements: " << opt.get_num_elements_stored() << "num_candidates: " << opt.get_num_candidate_solutions() << std::endl; // Process summary auto summary = opt.get_solution();
Example usage in Python:
X = read_some_data(); K = 50 # Create function to be maximized kernel = RBFKernel(sigma=sigma,scale=scale) fastLogDet = FastIVM(K, kernel, 1.0) opt = Random(K, fastLogDet) opt.fit(X, K) print("fval: {} num_elements: {} num_candidates: {}".format(opt.get_fval(), opt.get_num_elements_stored(), opt.get_num_candidate_solutions())) # process summary summary = opt.get_solution()
References:
Feige, U., Mirrokni, V. S., & Vondrák, J. (2011). Maximizing non-monotone submodular functions. SIAM Journal on Computing. https://doi.org/10.1137/090779346
Vitter, J. S. (1985). Random Sampling with a Reservoir. ACM Transactions on Mathematical Software (TOMS). https://doi.org/10.1145/3147.3165
Public Functions
-
inline Random(unsigned int K, SubmodularFunction &f, unsigned long seed = 0)¶
Construct a new Random object.
- Parameters
K – The cardinality constraint you of the optimization problem, that is the number of items selected.
f – The function which should be maximized. Note, that the clone function is used to construct a new SubmodularFunction which is owned by this object. If you implement a custom SubmodularFunction make sure that everything you need is actually cloned / copied.
seed – The random seed used for randomization.
-
inline Random(unsigned int K, std::function<data_t(std::vector<std::vector<data_t>> const&)> f, unsigned long seed = 0)¶
Construct a new Random object.
- Parameters
K – The cardinality constraint you of the optimization problem, that is the number of items selected.
f – The function which should be maximized. Note, that this parameter is likely moved and not copied. Thus, if you construct multiple optimizers with the same function they all reference the same function. This can be very efficient for state-less functions, but may lead to weird side effects if f keeps track of a state.
seed – The random seed used for randomization.
-
inline virtual void fit(std::vector<std::vector<data_t>> const &X, std::vector<idx_t> const &ids, unsigned int iterations = 1)¶
Randomly pick K elements as a solution. You can access the solution via
get_solutionand the ids can be accessed viaget_ids.Note
- Parameters
X – A constant reference to the entire data set
ids – A list of identifier for each object. This can be used to uniquely identify the objects in the summary. If ids.size() < X.size(), then only partial ids are stored. No ids are stored if ids is empty. Make sure, that either all or no object receives an id to keep track which id belongs to which object. This algorithm simply stores the objects and the ids in two separate lists and performs no safety checks.
iterations – Has no effect. Random samples K elements, no iterations required.
-
inline virtual void fit(std::vector<std::vector<data_t>> const &X, unsigned int iterations = 1)¶
Randomly pick K elements as a solution. You can access the solution via
get_solution.- Parameters
X – A constant reference to the entire data set
iterations – Has no effect. Random samples K elements, no iterations required.
-
inline virtual void next(std::vector<data_t> const &x, std::optional<idx_t> const id = std::nullopt)¶
Consume the next object in the data stream. This call uses Reservoir Sampling to sample the current solution which can access via
get_solution.- Parameters
x – A constant reference to the next object on the stream.
id – The id of the given object. If this is a
std::nulloptthis parameter is ignored. Otherwise the id is inserted into the solution. Make sure, that either all or no object receives an id to keep track which id belongs to which object. This algorithm simply stores the objects and the ids in two separate lists and performs no safety checks.
Protected Static Functions
-
static inline std::vector<idx_t> sample_without_replacement(int k, int N, std::default_random_engine &gen)¶
Sample k elements from the range \( [1, N] \) without replacement. Caller needs to make sure that \( k \le N \). The runtime is \( O(k) \) and memory is \( O(k) \).
Note
Basically copied from https://www.gormanalysis.com/blog/random-numbers-in-cpp/#sampling-without-replacement
- Parameters
k – The number of samples to be selected
N – The upper bound of the interval to sample from
gen – A random generator used for sampling
- Returns The
sampled vector of indices