Class SubmodularOptimizer

Inheritance Relationships

Derived Types

Class Documentation

class SubmodularOptimizer

Interface class which every optimizer should implement. Each optimizer must offer a next() and fit() function. However, if a certain optimizer does not support streaming (next) or batch (fit) processing it is okay to throw an exeception with an appropriate message. This class already offers a member to store the best solution (solution) and its function value (fval) including getter functions. You can access the function to be maximized via f which is a shared pointer (and thus there is no need for explicit delete in the destructor). Please make sure to set is_fitted after the fit / next has been called. Please make sure that you use the peek and update function of the SubmodularFunction correctly. Always call peek if you want to know the function value if you would add a new element to the current solution and call update if you know which element to add to the current solution. See SubmodularFunction.h for more details.

Subclassed by Greedy, IndependentSetImprovement, Random, Salsa, Salsa::Dense, Salsa::FixedThreshold, Salsa::HighLowThreshold, SieveStreaming, SieveStreamingPP, ThreeSieves

Public Functions

inline SubmodularOptimizer(unsigned int K, SubmodularFunction &f)

Creates a submodular optimizer object.

Parameters
  • K – The cardinality constraint K of the optimization problem or differently put the number of items to be 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.

Returns A

new SubmodularOptimizer object.

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

Creates a submodular optimizer object.

Parameters
  • K – The cardinality constraint K of the optimization problem or differently put the number of items to be 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.

Returns A

new SubmodularOptimizer object.

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

Find a solution given the entire data set.

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. The exact behavior depends on the optimizer, but generally no safety checks or anything are performed. If you don’t want to use ids, leaf it empty. Otherwise, ids.size() == X.size() unless you know what you are doing.

  • iterations – Maximum number of iterations over the entire data-set (default = 1). Tries to select exactly K elements by iterating multiple times over the entire dataset, but at most iterations times and at-least once. Early exits once K elements are found and at-least one iteration is completed.

Returns None

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

Find a solution given the entire data set.

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

  • iterations – Maximum number of iterations over the entire data-set (default = 1). Tries to select exactly K elements by iterating multiple times over the entire dataset, but at most iterations times and at-least once. Early exits once K elements are found and at-least one iteration is completed.

Returns None

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

Consume the next object in the data stream. This may throw an exception if the optimizer does not support streaming.

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

  • id – The id of the given object. If this is a std::nullopt this 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.

Returns None

inline std::vector<std::vector<data_t>> const &get_solution() const

Return the current solution.

Returns A

const reference to the current solution.

inline std::vector<idx_t> const &get_ids() const

Return the corresponding ids for the current solution. This might be empty if no ids have been passed to fit / next.

Returns A

const reference to the corresponding ids.

inline virtual unsigned int get_num_candidate_solutions() const

The number of candidate solutions stored.

inline virtual unsigned long get_num_elements_stored() const

The number of items stored in the current solution.

inline data_t get_fval() const

Returns the current function value.

inline virtual ~SubmodularOptimizer()

Destroys this object.

Public Members

std::vector<std::vector<data_t>> solution
std::vector<idx_t> ids
data_t fval

Protected Attributes

unsigned int K
std::shared_ptr<SubmodularFunction> f

Okay lets explain the reasoning behind this a bit more. 1): Use SubmodularFunction as an object. This does not work because we are dealing with inheritance / virtual functions 2): Use a reference to SubmodularFunction. This works well with PyBind and has an easy interface, but breaks does not work well together with the SubmodularFunctionWrapper if we want to allow users to pass a std::function directly. 3): Use SubmodularFunction* which would be a very “pure” and old-school approach. Should be do-able, but is not the modern c++ style 4): Use std::unique_ptr<SubmodularFunction>. This would IMHO be the best approach as it reflects our intend, that the SubmodularOptimizer owns the SubmodularFunction which it clones beforehand. This does not work well with PyBind since PyBind wants to own the memory. 5): Use std::shared_ptr<SubmodularFunction> which is basically a modern version of 3) and works better with PyBind.

bool is_fitted