Class Greedy

Inheritance Relationships

Base Type

Class Documentation

class Greedy : public SubmodularOptimizer

The Greedy optimizer for submodular functions. It rates the marginal gain of each element and picks that element with the largest gain. This process is repeated until it K elements have been selected:

  • Stream: No

  • Solution: \( 1 - 1/\exp(1) \)

  • Runtime: \( O(N \cdot K) \)

  • Memory: \( O(K) \)

  • Function Queries per Element: \( O(1) \)

  • 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);
Greedy 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 = Greedy(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

  • Nemhauser, G. L., Wolsey, L. A., & Fisher, M. L. (1978). An analysis of approximations for maximizing submodular set functions-I. Mathematical Programming, 14(1), 265–294. https://doi.org/10.1007/BF01588971

Public Functions

inline Greedy(unsigned int K, SubmodularFunction &f)

Construct a new Greedy 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.

inline Greedy(unsigned int K, std::function<data_t(std::vector<std::vector<data_t>> const&)> f)

Construct a new Greedy 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.

inline virtual void fit(std::vector<std::vector<data_t>> const &X, std::vector<idx_t> const &ids, unsigned int iterations = 1)

Pick that element with the largest marginal gain in the entire dataset. Repeat this until K element have been selected. You can access the solution via get_solution and the corresponding ids (if passed) with get_ids()

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. Greedy iterates K times over the entire dataset in any case.

inline virtual void fit(std::vector<std::vector<data_t>> const &X, unsigned int iterations = 1)

Pick that element with the largest marginal gain in the entire dataset. Repeat this until K element have been selected. You can access the solution via get_solution

Note

This internally calls fit with an empty id set.

Parameters
  • X – A constant reference to the entire data set

  • iterations – Has no effect. Greedy iterates K times over the entire dataset in any case.

inline virtual void next(std::vector<data_t> const &x, std::optional<idx_t> id = std::nullopt)

Throws an exception when called. Greedy does not support streaming!

Parameters

x – A constant reference to the next object on the stream.