2 #ifndef OPENGM_QPBO_HXX 3 #define OPENGM_QPBO_HXX 18 template<
class GM,
class MIN_ST_CUT>
34 template<
class _GM,
class _ACC>
47 std::string
name()
const;
50 template<
class VISITOR>
56 void addUnaryFactorType(
const FactorType& factor);
58 void addEdgeCapacity(
size_t v,
size_t w,
ValueType val);
59 void addPairwiseFactorType(
const FactorType& factor);
63 size_t neg(
size_t var)
const {
return (var+numVars_)%(2*numVars_); }
65 const GraphicalModelType& gm_;
67 std::vector<bool> stateBool_;
76 template<
class GM,
class MIN_ST_CUT>
83 numVars_(gm_.numberOfVariables()),
84 minStCut_(2*gm_.numberOfVariables()+2, 6*gm_.numberOfVariables())
88 sink_ = 2*numVars_ + 1;
91 for(
size_t j=0; j<gm_.numberOfFactors(); ++j) {
92 switch (gm_[j].numberOfVariables()) {
96 constTerm_ += gm_[j](c);
100 addUnaryFactorType(gm_[j]);
103 addPairwiseFactorType(gm_[j]);
105 default:
throw std::runtime_error(
"This implementation of the QPBO optimizer does not support factors of order >2.");
110 template<
class GM,
class MIN_ST_CUT>
117 template<
class GM,
class MIN_ST_CUT>
124 template<
class GM,
class MIN_ST_CUT>
131 template<
class GM,
class MIN_ST_CUT>
132 template<
class VISITOR>
136 visitor.begin(*
this);
137 minStCut_.calculateCut(stateBool_);
142 template<
class GM,
class MIN_ST_CUT>
146 std::vector<LabelType>&
arg,
154 arg.resize(numVars_);
155 for(
size_t j=0; j<arg.size(); ++j) {
156 if (stateBool_[j+2] ==
true && stateBool_[neg(j)+2] ==
false)
158 else if (stateBool_[j+2] ==
false && stateBool_[neg(j)+2] ==
true)
167 template<
class GM,
class MIN_ST_CUT>
171 std::vector<bool>& optVec
175 optVec.resize(numVars_);
176 for(
size_t j=0; j<optVec.size(); ++j)
177 if (stateBool_[j+2] != stateBool_[neg(j)+2]) {
183 return opt/gm_.numerOfVariables();
186 template<
class GM,
class MIN_ST_CUT>
190 minStCut_.addEdge((v+2)%(2*numVars_+2),(w+2)%(2*numVars_+2),val);
193 template<
class GM,
class MIN_ST_CUT>
198 size_t x_i = factor.variableIndex(0);
199 size_t nx_i = neg(x_i);
209 ValueType delta = std::min(c_nx_i, c_x_i);
214 addEdgeCapacity(x_i, sink_, 0.5*c_nx_i);
215 addEdgeCapacity(source_, nx_i, 0.5*c_nx_i);
217 addEdgeCapacity(nx_i, sink_, 0.5*c_x_i);
218 addEdgeCapacity(source_, x_i, 0.5*c_x_i);
221 template<
class GM,
class MIN_ST_CUT>
228 size_t x_i = factor.variableIndex(0);
229 size_t x_j = factor.variableIndex(1);
230 size_t nx_i = neg(x_i);
231 size_t nx_j = neg(x_j);
253 ValueType delta = std::min(c_nx_i_nx_j, c_x_i_nx_j);
256 c_nx_i_nx_j -= delta;
258 delta_c_nx_j += delta;
262 delta = std::min(c_nx_i_x_j, c_x_i_x_j);
267 delta_c_x_j += delta;
271 delta = std::min(c_nx_i_nx_j, c_nx_i_x_j);
274 c_nx_i_nx_j -= delta;
276 delta_c_nx_i += delta;
280 delta = std::min(c_x_i_nx_j, c_x_i_x_j);
285 delta_c_x_i += delta;
290 if (c_nx_i_nx_j != 0) {
291 addEdgeCapacity(x_i, nx_j, 0.5*c_nx_i_nx_j);
292 addEdgeCapacity(x_j, nx_i, 0.5*c_nx_i_nx_j);
294 if (c_nx_i_x_j != 0) {
295 addEdgeCapacity(x_i, x_j, 0.5*c_nx_i_x_j);
296 addEdgeCapacity(nx_j, nx_i, 0.5*c_nx_i_x_j);
298 if (c_x_i_nx_j != 0) {
299 addEdgeCapacity(nx_i, nx_j, 0.5*c_x_i_nx_j);
300 addEdgeCapacity(x_j, x_i, 0.5*c_x_i_nx_j);
302 if (c_x_i_x_j != 0) {
303 addEdgeCapacity(nx_i, x_j, 0.5*c_x_i_x_j);
304 addEdgeCapacity(nx_j, x_i, 0.5*c_x_i_x_j);
309 if (delta_c_nx_j != 0) {
310 addEdgeCapacity(x_j, sink_, 0.5*delta_c_nx_j);
311 addEdgeCapacity(source_, nx_j, 0.5*delta_c_nx_j);
313 if (delta_c_x_j != 0) {
314 addEdgeCapacity(nx_j, sink_, 0.5*delta_c_x_j);
315 addEdgeCapacity(source_, x_j, 0.5*delta_c_x_j);
317 if (delta_c_nx_i != 0) {
318 addEdgeCapacity(x_i, sink_, 0.5*delta_c_nx_i);
319 addEdgeCapacity(source_, nx_i, 0.5*delta_c_nx_i);
321 if (delta_c_x_i != 0) {
322 addEdgeCapacity(nx_i, sink_, 0.5*delta_c_x_i);
323 addEdgeCapacity(source_, x_i, 0.5*delta_c_x_i);
329 #endif // #ifndef OPENGM_EXTERNAL_QPBO_HXX
QPBO< _GM, MIN_ST_CUT > type
visitors::TimingVisitor< QPBO< GM, MIN_ST_CUT > > TimingVisitorType
QPBO(const GraphicalModelType &, Parameter=Parameter())
const GraphicalModelType & graphicalModel() const
GraphicalModelType::FactorType FactorType
double partialOptimality(std::vector< bool > &) const
visitors::EmptyVisitor< QPBO< GM, MIN_ST_CUT > > EmptyVisitorType
GraphicalModelType::ValueType ValueType
QPBO Algorithm C. Rother, V. Kolmogorov, V. Lempitsky, and M. Szummer, "Optimizing binary MRFs via e...
Inference algorithm interface.
opengm::Minimizer AccumulationType
InferenceTermination infer()
visitors::VerboseVisitor< QPBO< GM, MIN_ST_CUT > > VerboseVisitorType
QPBO< _GM,MIN_ST_CUT > type
Minimization as a unary accumulation.
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const