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>
95 const bool Persistency=
false,
96 const bool Tentacle=
false,
97 const bool ConnectedComponents=
false,
98 typename INF::Parameter subParameter =
typename INF::Parameter()
101 Persistency_(Persistency),
103 ConnectedComponents_(ConnectedComponents),
104 subParameter_(subParameter)
111 std::string
name()
const;
114 typename GM::ValueType
bound()
const;
115 template<
class VisitorType>
118 typename GM::ValueType
value()
const;
136 std::vector<LabelType> state_;
138 void getPartialOptimalityByQPBO(std::vector<LabelType>&, std::vector<bool>&);
139 void getPartialOptimalityByFixsHOQPBO(std::vector<LabelType>&, std::vector<bool>&);
140 void getPartialOptimalityByKovtunsMethod(std::vector<LabelType>&, std::vector<bool>&);
141 void getPartialOptimalityByMQPBO(std::vector<LabelType>&, std::vector<bool>&);
142 void getPartialOptimalityByAutoSelection(std::vector<LabelType>&, std::vector<bool>&);
143 void setPartialOptimality(std::vector<LabelType>&, std::vector<bool>&);
162 template<
class GM,
class ACC,
class INF>
172 ACC::ineutral(bound_);
173 OperatorType::neutral(value_);
174 state_.resize(gm.numberOfVariables(),0);
181 template<
class GM,
class ACC,
class INF>
188 for(IndexType j = 0; j < gm_.numberOfVariables(); ++j) {
189 if(gm_.numberOfLabels(j) != 2) {
194 for(IndexType j = 0; j < gm_.numberOfFactors(); ++j) {
195 if(potts && gm_[j].numberOfVariables() >1 && (gm_[j].numberOfVariables() > 3 || !gm_[j].isPotts() ) )
197 if(gm_[j].numberOfVariables() > order) {
198 order = gm_[j].numberOfVariables();
204 getPartialOptimalityByQPBO(arg,opt);
206 getPartialOptimalityByFixsHOQPBO(arg,opt);
210 getPartialOptimalityByKovtunsMethod(arg,opt);
212 getPartialOptimalityByMQPBO(arg,opt);
214 throw RuntimeError(
"This implementation of Reduced Inference supports no higher order multi-label problems.");
218 template<
class GM,
class ACC,
class INF>
219 void ReducedInference<GM,ACC,INF>::getPartialOptimalityByQPBO(std::vector<LabelType>& arg, std::vector<bool>& opt)
221 typedef external::QPBO<GM> QPBO;
222 typename QPBO::Parameter paraQPBO;
223 paraQPBO.strongPersistency_=
false;
224 QPBO qpbo(gm_,paraQPBO);
227 qpbo.partialOptimality(opt);
230 template<
class GM,
class ACC,
class INF>
231 void ReducedInference<GM,ACC,INF>::getPartialOptimalityByFixsHOQPBO(std::vector<LabelType>& arg, std::vector<bool>& opt)
233 const size_t maxOrder = 10;
234 ValueType constV = 0;
235 HigherOrderEnergy<ValueType, maxOrder> hoe;
236 hoe.AddVars(gm_.numberOfVariables());
237 for(IndexType f=0; f<gm_.numberOfFactors(); ++f){
238 IndexType size = gm_[f].numberOfVariables();
242 constV += gm_[f](&l0);
244 }
else if (size == 1) {
245 IndexType var = gm_[f].variableIndex(0);
246 const ValueType e0 = gm_[f](&l0);
247 const ValueType e1 = gm_[f](&l1);
248 hoe.AddUnaryTerm(var, e1 - e0);
250 unsigned int numAssignments = 1 << size;
251 ValueType coeffs[numAssignments];
252 for (
unsigned int subset = 1; subset < numAssignments; ++subset) {
258 for(
unsigned int assignment = 0; assignment < numAssignments; ++assignment){
259 for (
unsigned int i = 0; i < size; ++i) {
260 if (assignment & (1 << i)) {
261 cliqueLabels[i] = l1;
263 cliqueLabels[i] = l0;
266 ValueType energy = gm_[f](cliqueLabels);
267 for (
unsigned int subset = 1; subset < numAssignments; ++subset){
268 if (assignment & ~subset) {
272 for (
unsigned int b = 0; b < size; ++b) {
273 parity ^= (((assignment ^ subset) & (1 << b)) != 0);
275 coeffs[subset] += parity ? -energy : energy;
279 typename HigherOrderEnergy<ValueType, maxOrder> ::VarId vars[maxOrder];
280 for (
unsigned int subset = 1; subset < numAssignments; ++subset) {
282 for (
unsigned int b = 0; b < size; ++b) {
283 if (subset & (1 << b)) {
284 vars[degree++] = gm_[f].variableIndex(b);
287 std::sort(vars, vars+degree);
288 hoe.AddTerm(coeffs[subset], degree, vars);
292 kolmogorov::qpbo::QPBO<ValueType> qr(gm_.numberOfVariables(), 0);
296 for (IndexType i = 0; i < gm_.numberOfVariables(); ++i) {
297 int label = qr.GetLabel(i);
311 bound_ = constV + 0.5 * qr.ComputeTwiceLowerBound();
314 template<
class GM,
class ACC,
class INF>
315 void ReducedInference<GM,ACC,INF>::getPartialOptimalityByMQPBO(std::vector<LabelType>& arg, std::vector<bool>& opt)
318 typename MQPBOType::Parameter mqpboPara;
319 mqpboPara.useKovtunsMethod_ =
false;
320 mqpboPara.strongPersistency_ =
true;
321 mqpboPara.rounds_ = 10;
323 MQPBOType mqpbo(gm_,mqpboPara);
325 arg.resize(gm_.numberOfVariables(),0);
326 opt.resize(gm_.numberOfVariables(),
false);
327 for(IndexType var=0; var<gm_.numberOfVariables(); ++var){
328 opt[var] = mqpbo.partialOptimality(var,arg[var]);
332 template<
class GM,
class ACC,
class INF>
333 void ReducedInference<GM,ACC,INF>::getPartialOptimalityByKovtunsMethod(std::vector<LabelType>& arg, std::vector<bool>& opt)
336 typename MQPBOType::Parameter mqpboPara;
337 mqpboPara.strongPersistency_ =
true;
338 MQPBOType mqpbo(gm_,mqpboPara);
340 arg.resize(gm_.numberOfVariables(),0);
341 opt.resize(gm_.numberOfVariables(),
false);
342 for(IndexType var=0; var<gm_.numberOfVariables(); ++var){
343 opt[var] = mqpbo.partialOptimality(var,arg[var]);
348 template<
class GM,
class ACC,
class INF>
352 return "ReducedInference";
355 template<
class GM,
class ACC,
class INF>
362 template<
class GM,
class ACC,
class INF>
371 template<
class GM,
class ACC,
class INF>
372 template<
class VisitorType>
378 visitor.begin(*
this);
383 size_t numFixedVars = 0;
384 if(param_.Persistency_ ==
true){
385 std::vector<bool> opt(gm_.numberOfVariables(),
false);
386 std::vector<LabelType> arg(gm_.numberOfVariables(),0);
387 getPartialOptimalityByAutoSelection(arg,opt);
388 for(
IndexType i=0; i<gm_.numberOfVariables(); ++i){
398 if(numFixedVars == gm_.numberOfVariables()){
400 std::vector<LabelType> arg(0);
408 if(param_.Tentacle_ ==
true){
410 gmm.template lockAndTentacelElimination<ACC>();
424 OperatorType::neutral(sb);
428 if(param_.ConnectedComponents_ ==
true){
430 std::vector<std::vector<LabelType> > args(gmm.
numberOfSubmodels(),std::vector<LabelType>() );
436 subinf(agm, param_.Tentacle_, args[i],v,b);
438 OperatorType::op(b,sb);
457 std::vector<LabelType> arg;
460 subinf(agm, param_.Tentacle_, arg,v,b);
472 template<
class GM,
class ACC,
class INF>
476 const bool tentacleElimination,
477 std::vector<LabelType>& arg,
478 typename GM::ValueType& value,
479 typename GM::ValueType& bound
483 InfType inf(agm, param_.subParameter_);
492 template<
class GM,
class ACC,
class INF>
497 template<
class GM,
class ACC,
class INF>
499 return gm_.evaluate(state_);
502 template<
class GM,
class ACC,
class INF>
506 std::vector<LabelType>& x,
511 x.resize(gm_.numberOfVariables());
512 for(
size_t i=0; i<x.size(); ++i){
523 #endif // #ifndef OPENGM_ReducedInference_HXX
void modifiedState2OriginalState(const std::vector< LabelType > &, std::vector< LabelType > &) const
transforming label of the modified to the labeling of the original problem
GraphicalModelManipulator.
Discrete space in which variables can have differently many labels.
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
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 ...
ReducedInference(const GmType &, const Parameter &=Parameter())
[class reducedinference]
InferenceTermination infer()
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
visitors::VerboseVisitor< ReducedInference< GM, ACC, INF > > VerboseVisitorType
void fixVariable(const typename GM::IndexType, const typename GM::LabelType)
fix label for variable
GM::OperatorType OperatorType
const GmType & graphicalModel() const
[class mqpbo] Multilabel QPBO (MQPBO) Implements the algorithms described in i) Ivan Kovtun: Partial ...
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
GM::ValueType bound() const
return a bound on the solution
GraphicalModelType::ValueType ValueType
const MGM & getModifiedSubModel(size_t) const
return the i-th modified sub graphical model
Inference algorithm interface.
visitors::TimingVisitor< ReducedInference< GM, ACC, INF > > TimingVisitorType
DiscreteSpace< IndexType, LabelType > SpaceType
bool ConnectedComponents_
size_t numberOfSubmodels() const
return the number of submodels
meta::TypeListGenerator< ViewFixVariablesFunction< GM >, ViewFunction< GM >, ConstantFunction< ValueType, IndexType, LabelType >, ExplicitFunction< ValueType, IndexType, LabelType > >::type FunctionTypeList
IndexType numberOfVariables() const
static const size_t ContinueInf
GM::ValueType value() const
return the solution (value)
INF::Parameter subParameter_
[class reducedinference] Reduced Inference Implementation of the reduction techniques proposed in J...