2 #ifndef OPENGM_REDUCEDINFERENCE_HXX 3 #define OPENGM_REDUCEDINFERENCE_HXX 19 #include "opengm/inference/fix-fusion/fusion-move.hpp" 42 typedef typename meta::TypeListGenerator< ViewFixVariablesFunction<GM>,
73 template<
class GM,
class ACC,
class INF>
94 template<
class _GM,
class _ACC>
113 : subParameter_(p.subParameter_),
114 Persistency_(p.Persistency_),
115 Tentacle_(p.Tentacle_),
116 ConnectedComponents_(p.ConnectedComponents_)
121 const bool Persistency=
false,
122 const bool Tentacle=
false,
123 const bool ConnectedComponents=
false,
124 typename INF::Parameter subParameter =
typename INF::Parameter()
127 Persistency_(Persistency),
129 ConnectedComponents_(ConnectedComponents),
130 subParameter_(subParameter)
137 std::string name()
const;
138 const GmType& graphicalModel()
const;
140 typename GM::ValueType bound()
const;
141 template<
class VisitorType>
144 typename GM::ValueType value()
const;
162 std::vector<LabelType> state_;
164 void getPartialOptimalityByQPBO(std::vector<LabelType>&, std::vector<bool>&);
165 void getPartialOptimalityByFixsHOQPBO(std::vector<LabelType>&, std::vector<bool>&);
166 void getPartialOptimalityByKovtunsMethod(std::vector<LabelType>&, std::vector<bool>&);
167 void getPartialOptimalityByMQPBO(std::vector<LabelType>&, std::vector<bool>&);
168 void getPartialOptimalityByAutoSelection(std::vector<LabelType>&, std::vector<bool>&);
169 void setPartialOptimality(std::vector<LabelType>&, std::vector<bool>&);
188 template<
class GM,
class ACC,
class INF>
198 ACC::ineutral(bound_);
199 OperatorType::neutral(value_);
200 state_.resize(gm.numberOfVariables(),0);
207 template<
class GM,
class ACC,
class INF>
214 for(
IndexType j = 0; j < gm_.numberOfVariables(); ++j) {
215 if(gm_.numberOfLabels(j) != 2) {
220 for(
IndexType j = 0; j < gm_.numberOfFactors(); ++j) {
221 if(potts && gm_[j].numberOfVariables() >1 && (gm_[j].numberOfVariables() > 3 || !gm_[j].isPotts() ) )
223 if(gm_[j].numberOfVariables() > order) {
224 order = gm_[j].numberOfVariables();
230 getPartialOptimalityByQPBO(arg,opt);
232 getPartialOptimalityByFixsHOQPBO(arg,opt);
236 getPartialOptimalityByKovtunsMethod(arg,opt);
238 getPartialOptimalityByMQPBO(arg,opt);
240 throw RuntimeError(
"This implementation of Reduced Inference supports no higher order multi-label problems.");
244 template<
class GM,
class ACC,
class INF>
249 paraQPBO.strongPersistency_=
false;
250 QPBO qpbo(gm_,paraQPBO);
256 template<
class GM,
class ACC,
class INF>
259 const size_t maxOrder = 10;
261 HigherOrderEnergy<ValueType, maxOrder> hoe;
262 hoe.AddVars(gm_.numberOfVariables());
263 for(
IndexType f=0; f<gm_.numberOfFactors(); ++f){
264 IndexType size = gm_[f].numberOfVariables();
268 constV += gm_[f](&l0);
270 }
else if (size == 1) {
274 hoe.AddUnaryTerm(var, e1 - e0);
276 unsigned int numAssignments = 1 << size;
279 for (
unsigned int subset = 1; subset < numAssignments; ++subset) {
286 for(
unsigned int assignment = 0; assignment < numAssignments; ++assignment){
287 for (
unsigned int i = 0; i < size; ++i) {
288 if (assignment & (1 << i)) {
289 cliqueLabels[i] = l1;
291 cliqueLabels[i] = l0;
295 for (
unsigned int subset = 1; subset < numAssignments; ++subset){
296 if (assignment & ~subset) {
300 for (
unsigned int b = 0; b < size; ++b) {
301 parity ^= (((assignment ^ subset) & (1 << b)) != 0);
303 coeffs[subset] += parity ? -energy : energy;
307 typename HigherOrderEnergy<ValueType, maxOrder> ::VarId vars[maxOrder];
308 for (
unsigned int subset = 1; subset < numAssignments; ++subset) {
310 for (
unsigned int b = 0; b < size; ++b) {
311 if (subset & (1 << b)) {
312 vars[degree++] = gm_[f].variableIndex(b);
315 std::sort(vars, vars+degree);
316 hoe.AddTerm(coeffs[subset], degree, vars);
320 kolmogorov::qpbo::QPBO<ValueType> qr(gm_.numberOfVariables(), 0);
324 for (
IndexType i = 0; i < gm_.numberOfVariables(); ++i) {
325 int label = qr.GetLabel(i);
339 bound_ = constV + 0.5 * qr.ComputeTwiceLowerBound();
342 template<
class GM,
class ACC,
class INF>
346 typename MQPBOType::Parameter mqpboPara;
347 mqpboPara.useKovtunsMethod_ =
false;
348 mqpboPara.strongPersistency_ =
true;
349 mqpboPara.rounds_ = 10;
351 MQPBOType mqpbo(gm_,mqpboPara);
353 arg.resize(gm_.numberOfVariables(),0);
354 opt.resize(gm_.numberOfVariables(),
false);
355 for(
IndexType var=0; var<gm_.numberOfVariables(); ++var){
356 opt[var] = mqpbo.partialOptimality(var,arg[var]);
360 template<
class GM,
class ACC,
class INF>
364 typename MQPBOType::Parameter mqpboPara;
365 mqpboPara.strongPersistency_ =
true;
366 MQPBOType mqpbo(gm_,mqpboPara);
368 arg.resize(gm_.numberOfVariables(),0);
369 opt.resize(gm_.numberOfVariables(),
false);
370 for(
IndexType var=0; var<gm_.numberOfVariables(); ++var){
371 opt[var] = mqpbo.partialOptimality(var,arg[var]);
376 template<
class GM,
class ACC,
class INF>
380 return "ReducedInference";
383 template<
class GM,
class ACC,
class INF>
390 template<
class GM,
class ACC,
class INF>
399 template<
class GM,
class ACC,
class INF>
400 template<
class VisitorType>
406 visitor.begin(*
this);
411 size_t numFixedVars = 0;
412 if(param_.Persistency_ ==
true){
413 std::vector<bool> opt(gm_.numberOfVariables(),
false);
414 std::vector<LabelType> arg(gm_.numberOfVariables(),0);
415 getPartialOptimalityByAutoSelection(arg,opt);
416 for(
IndexType i=0; i<gm_.numberOfVariables(); ++i){
426 if(numFixedVars == gm_.numberOfVariables()){
428 std::vector<LabelType> arg(0);
436 if(param_.Tentacle_ ==
true){
438 gmm.template lockAndTentacelElimination<ACC>();
452 OperatorType::neutral(sb);
456 if(param_.ConnectedComponents_ ==
true){
458 std::vector<std::vector<LabelType> > args(gmm.
numberOfSubmodels(),std::vector<LabelType>() );
464 subinf(agm, param_.Tentacle_, args[i],v,b);
466 OperatorType::op(b,sb);
485 std::vector<LabelType> arg;
488 subinf(agm, param_.Tentacle_, arg,v,b);
500 template<
class GM,
class ACC,
class INF>
504 const bool tentacleElimination,
505 std::vector<LabelType>& arg,
506 typename GM::ValueType& value,
507 typename GM::ValueType& bound
511 InfType inf(agm, param_.subParameter_);
520 template<
class GM,
class ACC,
class INF>
525 template<
class GM,
class ACC,
class INF>
527 return gm_.evaluate(state_);
530 template<
class GM,
class ACC,
class INF>
534 std::vector<LabelType>& x,
539 x.resize(gm_.numberOfVariables());
540 for(
size_t i=0; i<x.size(); ++i){
551 #endif // #ifndef OPENGM_ReducedInference_HXX
GraphicalModelManipulator.
Discrete space in which variables can have differently many labels.
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
Parameter(const bool Persistency=false, const bool Tentacle=false, const bool ConnectedComponents=false, typename INF::Parameter subParameter=typename INF::Parameter())
void buildModifiedModel()
build modified model
ReducedInference(const GmType &, const Parameter &=Parameter())
[class reducedinference]
void infer(const typename INF::GraphicalModelType &gm, const typename INF::Parameter ¶m, std::vector< typename INF::LabelType > &conf)
InferenceTermination infer()
visitors::VerboseVisitor< ReducedInference< GM, ACC, INF > > VerboseVisitorType
void fixVariable(const typename GM::IndexType, const typename GM::LabelType)
fix label for variable
GM::ValueType bound() const
return a bound on the solution
GM::OperatorType OperatorType
virtual ValueType bound() const
return a bound on the solution
[class mqpbo] Multilabel QPBO (MQPBO) Implements the algorithms described in i) Ivan Kovtun: Partial ...
void modifiedState2OriginalState(const std::vector< LabelType > &, std::vector< LabelType > &) const
transforming label of the modified to the labeling of the original problem
ReducedInference< _GM, _ACC, RebindedInf > type
GraphicalModel< ValueType, OperatorType, FunctionTypeList, SpaceType > InfGmType
void buildModifiedSubModels()
build modified sub-models
GraphicalModelType::IndexType IndexType
reference to a Factor of a GraphicalModel
visitors::EmptyVisitor< ReducedInference< GM, ACC, INF > > EmptyVisitorType
const MGM & getModifiedModel() const
return the modified graphical model
double partialOptimality(std::vector< bool > &) const
ReducedInferenceHelper< _GM >::InfGmType RebindedInfGmType
ReducedInference< _GM, ACC, RebindedInf > type
INF::template RebindGmAndAcc< RebindedInfGmType, _ACC >::type RebindedInf
GraphicalModelType::ValueType ValueType
QPBO Algorithm C. Rother, V. Kolmogorov, V. Lempitsky, and M. Szummer, "Optimizing binary MRFs via e...
Inference algorithm interface.
visitors::TimingVisitor< ReducedInference< GM, ACC, INF > > TimingVisitorType
void modifiedSubStates2OriginalState(const std::vector< std::vector< LabelType > > &, std::vector< LabelType > &) const
transforming label of the modified subproblems to the labeling of the original problem ...
IndexType numberOfVariables() const
DiscreteSpace< IndexType, LabelType > SpaceType
GM::ValueType value() const
return the solution (value)
InferenceTermination infer()
bool ConnectedComponents_
ReducedInferenceHelper< _GM >::InfGmType RebindedInfGmType
INF::template RebindGm< RebindedInfGmType >::type RebindedInf
const GmType & graphicalModel() const
const MGM & getModifiedSubModel(size_t) const
return the i-th modified sub graphical model
GraphicalModelType::LabelType LabelType
meta::TypeListGenerator< ViewFixVariablesFunction< GM >, ViewFunction< GM >, ConstantFunction< ValueType, IndexType, LabelType >, ExplicitFunction< ValueType, IndexType, LabelType > >::type FunctionTypeList
static const size_t ContinueInf
INF::Parameter subParameter_
size_t numberOfSubmodels() const
return the number of submodels
[class reducedinference] Reduced Inference Implementation of the reduction techniques proposed in J...
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const