Alpha Expansion Library
C++ library for the Alpha-Expansion graph-cut algorithm with Python bindings
Loading...
Searching...
No Matches
Implementing a Custom Expansion Strategy

ExpansionStrategy<T> controls the iteration loop: which labels to expand, in what order and when to stop. Subclass it to implement a custom policy.

Interface

// src/strategies/ExpansionStrategy.hpp
template <typename T>
public:
virtual int execute(AlphaExpansion<T>& optimizer, EnergyModel<T>& model) const = 0;
virtual ~ExpansionStrategy() = default;
};
Performs alpha-expansion moves on an EnergyModel using a pluggable max-flow solver.
Definition AlphaExpansion.hpp:20
Stores the graph and energy costs for the Alpha-Expansion algorithm.
Definition EnergyModel.hpp:17
Abstract base class for alpha-expansion iteration strategies.
Definition ExpansionStrategy.hpp:32
virtual ~ExpansionStrategy()=default
virtual int execute(AlphaExpansion< T > &optimizer, EnergyModel< T > &model) const =0
Runs the expansion strategy to convergence (or up to a cycle limit).

execute() calls optimizer.perform_expansion_move(alpha) for whatever labels and in whatever order the strategy decides. It returns the number of full cycles completed.

Example: Single-Pass Strategy

Tries each label exactly once and stops:

// src/strategies/SinglePassStrategy.hpp
#pragma once
template <typename T>
class SinglePassStrategy : public ExpansionStrategy<T> {
public:
int execute(AlphaExpansion<T>& optimizer, EnergyModel<T>& model) const override {
for (int alpha = 0; alpha < model.num_labels(); ++alpha)
optimizer.perform_expansion_move(alpha);
return 1;
}
};
bool perform_expansion_move(const int alpha_label) const
Attempts a single alpha-expansion move for alpha_label.
Definition AlphaExpansion.hpp:41
int num_labels() const
Returns the total number of labels.
Definition EnergyModel.hpp:35

Using the Custom Strategy

#include "strategies/SinglePassStrategy.hpp"
EnergyModel<int> model(100, 3);
// ... set up costs and graph ...
auto factory = [](int v, int e) {
return std::make_unique<BKSolver<int>>(v, e);
};
AlphaExpansion<int> optimizer(model, factory);
SinglePassStrategy<int> strategy;
int cycles = strategy.execute(optimizer, model);

Tips

  • Call model.evaluate_total_energy() to check progress between moves.
  • Call model.get_labels() and model.set_labels() to save and restore state. GreedyStrategy uses this to roll back non-improving moves.
  • Return the number of full cycles from execute(). The demo app shows this value in the convergence dialog.