Alpha Expansion Library
C++ library for the Alpha-Expansion graph-cut algorithm with Python bindings
Loading...
Searching...
No Matches
RandomizedStrategy.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <vector>
6#include <numeric>
7#include <random>
8#include <algorithm>
9
17template <typename T>
19public:
23 RandomizedStrategy(int max_cycles = 100, unsigned int seed = 42)
24 : max_cycles_(max_cycles), seed_(seed) {}
25
28 int execute(AlphaExpansion<T> &optimizer, EnergyModel<T> &model) const override {
29 int num_labels = model.num_labels();
30 int cycle = 0;
31 bool converged = false;
32
33 std::vector<int> label_order(num_labels);
34 std::iota(label_order.begin(), label_order.end(), 0);
35 std::mt19937 g(seed_);
36
37 while (!converged && cycle < max_cycles_) {
38 bool any_changed = false;
39 std::shuffle(label_order.begin(), label_order.end(), g);
40 for (int alpha: label_order) {
41 if (optimizer.perform_expansion_move(alpha)) any_changed = true;
42 }
43 if (!any_changed) converged = true;
44 cycle++;
45 }
46 return cycle;
47 }
48
49private:
50 int max_cycles_;
51 unsigned int seed_;
52};
Performs alpha-expansion moves on an EnergyModel using a pluggable max-flow solver.
Definition AlphaExpansion.hpp:20
bool perform_expansion_move(const int alpha_label) const
Attempts a single alpha-expansion move for alpha_label.
Definition AlphaExpansion.hpp:41
Stores the graph and energy costs for the Alpha-Expansion algorithm.
Definition EnergyModel.hpp:17
int num_labels() const
Returns the total number of labels.
Definition EnergyModel.hpp:35
Abstract base class for alpha-expansion iteration strategies.
Definition ExpansionStrategy.hpp:32
Expansion strategy that shuffles the label order randomly each cycle.
Definition RandomizedStrategy.hpp:18
int execute(AlphaExpansion< T > &optimizer, EnergyModel< T > &model) const override
Runs randomized alpha-expansion until convergence or max_cycles.
Definition RandomizedStrategy.hpp:28
RandomizedStrategy(int max_cycles=100, unsigned int seed=42)
Constructs the strategy.
Definition RandomizedStrategy.hpp:23