18 namespace combilp_base{
20 template<
class FACTOR>
23 for (
typename FACTOR::VariablesIteratorType it=f.variableIndicesBegin();
24 it!=f.variableIndicesEnd();++it)
29 void DilateMask(
const GM& gm,
typename GM::IndexType varId,std::vector<bool>* pmask)
31 typename GM::IndexType numberOfFactors=gm.numberOfFactors(varId);
32 for (
typename GM::IndexType localFactorId=0;localFactorId<numberOfFactors;++localFactorId)
34 const typename GM::FactorType& f=gm[gm.factorOfVariable(varId,localFactorId)];
35 if (f.numberOfVariables()>1)
44 void DilateMask(
const GM& gm,
const std::vector<bool>& inmask, std::vector<bool>* poutmask)
47 for (
typename GM::IndexType varId=0;varId<inmask.size();++varId)
48 if (inmask[varId])
DilateMask(gm,varId,poutmask);
53 bool LabelingMatching(
const std::vector<typename GM::LabelType>& labeling1,
const std::vector<typename GM::LabelType>& labeling2,
54 const std::vector<bool>& mask,std::list<typename GM::IndexType>* presult)
59 for (
typename GM::IndexType varId=0;varId<mask.size();++varId)
60 if ((mask[varId]) && (labeling1[varId]!=labeling2[varId]))
61 presult->push_back(varId);
63 return presult->empty();
67 void GetMaskBoundary(
const GM& gm,
const std::vector<bool>& mask,std::vector<bool>* boundmask)
69 boundmask->assign(mask.size(),
false);
70 for (
typename GM::IndexType varId=0;varId<mask.size();++varId)
72 if (!mask[varId])
continue;
74 typename GM::IndexType numberOfFactors=gm.numberOfFactors(varId);
75 for (
typename GM::IndexType localFactorId=0;localFactorId<numberOfFactors;++localFactorId)
77 if ((*boundmask)[varId])
break;
79 const typename GM::FactorType& f=gm[gm.factorOfVariable(varId,localFactorId)];
80 if (f.numberOfVariables()>1)
82 for (
typename GM::FactorType::VariablesIteratorType it=f.variableIndicesBegin();
83 it!=f.variableIndicesEnd();++it)
86 (*boundmask)[varId]=
true;
99 const std::string& reparametrizedModelFileName=
"",
100 bool singleReparametrization=
true,
101 bool saveProblemMasks=
false,
102 std::string maskFileNamePre=
""):
120 #ifdef TRWS_DEBUG_OUTPUT
121 virtual void print(std::ostream& fout)
const
123 fout <<
"maxNumberOfILPCycles="<<maxNumberOfILPCycles_<<std::endl;
124 fout <<
"verbose"<<verbose_<<std::endl;
125 fout <<
"reparametrizedModelFileName="<<reparametrizedModelFileName_<<std::endl;
126 fout <<
"singleReparametrization="<<singleReparametrization_<<std::endl;
127 fout <<
"saveProblemMasks="<<saveProblemMasks_<<std::endl;
128 fout <<
"maskFileNamePre="<<maskFileNamePre_<<std::endl;
135 template<
class GM,
class ACC,
class LPREPARAMETRIZER>
146 typedef typename ReparametrizerType::MaskType
MaskType;
152 CombiLP_base(LPREPARAMETRIZER& reparametrizer,
const Parameter& param
153 #ifdef TRWS_DEBUG_OUTPUT
154 , std::ostream& fout=std::cout
159 const GraphicalModelType&
graphicalModel()
const {
return _lpparametrizer->graphicalModel(); }
161 template <
class VISITORWRAPPER>
InferenceTermination infer(MaskType& mask,
const std::vector<LabelType>& lp_labeling,VISITORWRAPPER& vis);
169 ValueType
value()
const{
return _value;};
170 ValueType
bound()
const{
return _bound;}
174 void _Reparametrize(
typename ReparametrizerType::ReparametrizedGMType* pgm,
const MaskType& mask);
175 InferenceTermination _PerformILPInference(GMManipulatorType& modelManipulator,std::vector<LabelType>* plabeling);
176 Parameter _parameter;
177 ReparametrizerType& _lpparametrizer;
178 std::vector<LabelType> _labeling;
181 #ifdef TRWS_DEBUG_OUTPUT
186 template<
class GM,
class ACC,
class LPREPARAMETRIZER>
188 #ifdef TRWS_DEBUG_OUTPUT
193 ,_lpparametrizer(reparametrizer)
194 ,_labeling(_lpparametrizer.graphicalModel().numberOfVariables(),
std::numeric_limits<
LabelType>::max())
195 ,_value(ACC::template neutral<ValueType>())
196 ,_bound(ACC::template ineutral<ValueType>())
197 #ifdef TRWS_DEBUG_OUTPUT
198 ,_fout(param.verbose_ ? fout : *OUT::nullstream::Instance())
203 template<
class GM,
class ACC,
class LPREPARAMETRIZER>
207 modelManipulator.buildModifiedSubModels();
209 std::vector<std::vector<LabelType> > submodelLabelings(modelManipulator.numberOfSubmodels());
210 for (
size_t modelIndex=0;modelIndex<modelManipulator.numberOfSubmodels();++modelIndex)
212 const typename GMManipulatorType::MGM& model=modelManipulator.getModifiedSubModel(modelIndex);
213 submodelLabelings[modelIndex].resize(model.numberOfVariables());
214 typename LPCPLEX::Parameter param;
215 param.integerConstraint_=
true;
216 param.numberOfThreads_= _parameter.threads_;
217 param.timeLimit_ = 3600;
218 param.workMem_= 1024*6;
219 LPCPLEX ilpSolver(model,param);
220 terminationILP=ilpSolver.infer();
223 return terminationILP;
228 ilpSolver.arg(submodelLabelings[modelIndex]);
231 modelManipulator.modifiedSubStates2OriginalState(submodelLabelings,*plabeling);
232 return terminationILP;
235 template<
class GM,
class ACC,
class LPREPARAMETRIZER>
236 template <
class VISITORWRAPPER>
239 #ifdef TRWS_DEBUG_OUTPUT
240 if (!_parameter.singleReparametrization_)
241 _fout <<
"Applying reparametrization for each ILP run ..."<<std::endl;
243 _fout <<
"Applying a single uniform reparametrization..."<<std::endl;
245 _fout <<
"Switching to ILP."<<std::endl;
249 typename ReparametrizerType::ReparametrizedGMType gm;
250 bool reparametrizedFlag=
false;
253 for (
size_t i=0;(startILP && (i<_parameter.maxNumberOfILPCycles_));++i)
260 #ifdef TRWS_DEBUG_OUTPUT
261 _fout <<
"Subproblem "<<i<<
" size="<<std::count(mask.begin(),mask.end(),
true)<<std::endl;
267 #ifdef TRWS_DEBUG_OUTPUT
268 if (_parameter.saveProblemMasks_)
270 OUT::saveContainer(std::string(_parameter.maskFileNamePre_+
"-mask-"+
trws_base::any2string(i)+
".txt"),mask.begin(),mask.end());
271 OUT::saveContainer(std::string(_parameter.maskFileNamePre_+
"-boundmask-"+
trws_base::any2string(i)+
".txt"),boundmask.begin(),boundmask.end());
275 if (_parameter.singleReparametrization_ && (!reparametrizedFlag) )
277 #ifdef TRWS_DEBUG_OUTPUT
278 _fout <<
"Reparametrizing..."<<std::endl;
280 _Reparametrize(&gm,
MaskType(mask.size(),
true));
281 reparametrizedFlag=
true;
283 else if (!_parameter.singleReparametrization_)
285 #ifdef TRWS_DEBUG_OUTPUT
286 _fout <<
"Reparametrizing..."<<std::endl;
288 _Reparametrize(&gm,mask);
294 modelManipulator.unlock();
295 modelManipulator.freeAllVariables();
296 for (IndexType varId=0;varId<mask.size();++varId)
297 if (mask[varId]==0) modelManipulator.fixVariable(varId,lp_labeling[varId]);
298 modelManipulator.lock();
301 std::vector<LabelType> labeling;
302 terminationILP=_PerformILPInference(modelManipulator,&labeling);
305 _labeling=lp_labeling;
306 #ifdef TRWS_DEBUG_OUTPUT
307 _fout <<
"ILP solver failed to solve the problem. LP solver results will be saved." <<std::endl;
311 return terminationILP;
314 #ifdef TRWS_DEBUG_OUTPUT
315 _fout <<
"Boundary size="<<std::count(boundmask.begin(),boundmask.end(),
true)<<std::endl;
318 std::list<IndexType> result;
319 if (LabelingMatching<GM>(lp_labeling,labeling,boundmask,&result))
323 _value=_bound=_lpparametrizer.graphicalModel().evaluate(_labeling);
325 #ifdef TRWS_DEBUG_OUTPUT
326 _fout <<
"Solved! Optimal energy="<<value()<<std::endl;
331 #ifdef TRWS_DEBUG_OUTPUT
332 _fout <<
"Adding "<<result.size()<<
" nodes."<<std::endl;
333 if (_parameter.saveProblemMasks_)
334 OUT::saveContainer(std::string(_parameter.maskFileNamePre_+
"-added-"+
trws_base::any2string(i)+
".txt"),result.begin(),result.end());
336 for (
typename std::list<IndexType>::const_iterator it=result.begin();it!=result.end();++it)
341 return terminationId;
345 template<
class GM,
class ACC,
class LPREPARAMETRIZER>
347 _Reparametrize(
typename ReparametrizerType::ReparametrizedGMType* pgm,
const MaskType& mask)
349 _lpparametrizer.reparametrize(&mask);
350 _lpparametrizer.getReparametrizedModel(*pgm);
353 template<
class GM,
class ACC,
class LPREPARAMETRIZER>
357 typename ReparametrizerType::ReparametrizedGMType gm;
358 _Reparametrize(&gm,
MaskType(_lpparametrizer.graphicalModel().numberOfVariables(),
true));
359 store_into_explicit(gm, _parameter.reparametrizedModelFileName_);
364 template<
class LPSOLVERPARAMETERS,
class REPARAMETRIZERPARAMETERS>
369 const REPARAMETRIZERPARAMETERS& repaParameter=REPARAMETRIZERPARAMETERS(),
370 size_t maxNumberOfILPCycles=100,
372 bool saveReparametrizedModel=
false,
373 const std::string& reparametrizedModelFileName=
"",
374 bool singleReparametrization=
true,
375 bool saveProblemMasks=
false,
376 std::string maskFileNamePre=
""):
377 parent(maxNumberOfILPCycles,
379 reparametrizedModelFileName,
380 singleReparametrization,
390 #ifdef TRWS_DEBUG_OUTPUT
391 void print(std::ostream& fout)
const
394 fout <<
"== lpsolverParameters: =="<<std::endl;
395 lpsolverParameter_.print(fout);
406 template<
class GM,
class ACC,
class LPSOLVER>
422 typedef typename ReparametrizerType::MaskType
MaskType;
427 CombiLP(
const GraphicalModelType& gm,
const Parameter& param
428 #ifdef TRWS_DEBUG_OUTPUT
429 , std::ostream& fout=std::cout
432 virtual ~CombiLP(){
if (_plpparametrizer!=0)
delete _plpparametrizer;};
433 std::string
name()
const{
return "CombiLP"; }
434 const GraphicalModelType&
graphicalModel()
const {
return _lpsolver.graphicalModel(); }
437 EmptyVisitorType vis;
451 Parameter _parameter;
453 ReparametrizerType* _plpparametrizer;
455 std::vector<LabelType> _labeling;
458 #ifdef TRWS_DEBUG_OUTPUT
463 template<
class GM,
class ACC,
class LPSOLVER>
465 #ifdef TRWS_DEBUG_OUTPUT
470 ,_lpsolver(gm,param.lpsolverParameter_
471 #ifdef TRWS_DEBUG_OUTPUT
472 ,(param.lpsolverParameter_.verbose_ ? fout : *OUT::nullstream::Instance())
475 ,_plpparametrizer(_lpsolver.getReparametrizer(_parameter.repaParameter_))
476 ,_base(*_plpparametrizer, param
477 #ifdef TRWS_DEBUG_OUTPUT
481 ,_labeling(gm.numberOfVariables(),
std::numeric_limits<
LabelType>::max())
482 ,_value(_lpsolver.value())
483 ,_bound(_lpsolver.bound())
484 #ifdef TRWS_DEBUG_OUTPUT
485 ,_fout(param.verbose_ ? fout : *OUT::nullstream::Instance())
488 #ifdef TRWS_DEBUG_OUTPUT
489 _fout <<
"Parameters of the "<<
name() <<
" algorithm:"<<std::endl;
494 template<
class GM,
class ACC,
class LPSOLVER>
495 template<
class VISITOR>
498 #ifdef TRWS_DEBUG_OUTPUT
499 _fout <<
"Running LP solver "<<_lpsolver.name()<<std::endl;
501 visitor.begin(*
this);
504 _value=_lpsolver.value();
505 _bound=_lpsolver.bound();
506 _lpsolver.arg(_labeling);
513 std::vector<LabelType> labeling_lp;
515 _plpparametrizer->reparametrize();
517 _lpsolver.getTreeAgreement(initialmask,&labeling_lp);
519 #ifdef TRWS_DEBUG_OUTPUT
520 _fout <<
"Energy of the labeling consistent with the arc consistency ="<<_lpsolver.graphicalModel().evaluate(labeling_lp)<<std::endl;
521 _fout <<
"Arc inconsistent set size ="<<std::count(initialmask.begin(),initialmask.end(),
false)<<std::endl;
524 #ifdef TRWS_DEBUG_OUTPUT
525 _fout <<
"Trivializing."<<std::endl;
529 if (_parameter.reparametrizedModelFileName_.compare(
"")!=0)
531 #ifdef TRWS_DEBUG_OUTPUT
532 _fout <<
"Saving reparametrized model..."<<std::endl;
538 _base.ReparametrizeAndSave();
546 if (std::count(initialmask.begin(),initialmask.end(),
false)==0)
559 _value=_base.value();
560 _bound=_base.bound();
561 _base.arg(_labeling);
LPCplex< typename GMManipulatorType::MGM, Minimizer > LPCPLEX
void DilateMask(const GM &gm, typename GM::IndexType varId, std::vector< bool > *pmask)
LPSOLVERPARAMETERS lpsolverParameter_
CombiLP Savchynskyy, B. and Kappes, J. H. and Swoboda, P. and Schnoerr, C.: "Global MAP-Optimality b...
GraphicalModelManipulator.
LPSOLVER::ReparametrizerType ReparametrizerType
bool singleReparametrization_
void GetMaskBoundary(const GM &gm, const std::vector< bool > &mask, std::vector< bool > *boundmask)
combilp_base::CombiLP_base< GM, ACC, ReparametrizerType > BaseType
CombiLP_Parameter< typename LPSOLVER::Parameter, typename ReparametrizerType::Parameter > Parameter
opengm::GraphicalModelManipulator< typename ReparametrizerType::ReparametrizedGMType > GMManipulatorType
InferenceTermination arg(std::vector< LabelType > &out, const size_t=1) const
combilp_base::CombiLP_base_Parameter parent
#define OPENGM_ASSERT(expression)
virtual ValueType bound() const
return a bound on the solution
LPCplex< typename GMManipulatorType::MGM, ACC > LPCPLEX
virtual ValueType value() const
return the solution (value)
BaseType::GMManipulatorType GMManipulatorType
InputIterator transform_inplace(InputIterator first, InputIterator last, UnaryOperator op)
bool LabelingMatching(const std::vector< typename GM::LabelType > &labeling1, const std::vector< typename GM::LabelType > &labeling2, const std::vector< bool > &mask, std::list< typename GM::IndexType > *presult)
InferenceTermination infer()
const GraphicalModelType & graphicalModel() const
CombiLP_base_Parameter Parameter
visitors::TimingVisitor< CombiLP< GM, ACC, LPSOLVER > > TimingVisitorType
size_t maxNumberOfILPCycles_
LPREPARAMETRIZER ReparametrizerType
const GraphicalModelType & graphicalModel() const
InferenceTermination infer(MaskType &mask, const std::vector< LabelType > &lp_labeling, VISITORWRAPPER &vis)
CombiLP_base_Parameter(size_t maxNumberOfILPCycles=100, bool verbose=false, const std::string &reparametrizedModelFileName="", bool singleReparametrization=true, bool saveProblemMasks=false, std::string maskFileNamePre="")
CombiLP_base(LPREPARAMETRIZER &reparametrizer, const Parameter ¶m)
Optimization by Linear Programming (LP) or Integer LP using IBM ILOG CPLEX http://www.ilog.com/products/cplex/.
GraphicalModelType::ValueType ValueType
Inference algorithm interface.
std::string any2string(const AnyType &any)
std::string reparametrizedModelFileName_
ReparametrizerType::MaskType MaskType
REPARAMETRIZERPARAMETERS repaParameter_
void ReparametrizeAndSave()
void MakeFactorVariablesTrue(const FACTOR &f, std::vector< bool > *pmask)
CombiLP_Parameter(const LPSOLVERPARAMETERS &lpsolverParameter=LPSOLVERPARAMETERS(), const REPARAMETRIZERPARAMETERS &repaParameter=REPARAMETRIZERPARAMETERS(), size_t maxNumberOfILPCycles=100, bool verbose=false, bool saveReparametrizedModel=false, const std::string &reparametrizedModelFileName="", bool singleReparametrization=true, bool saveProblemMasks=false, std::string maskFileNamePre="")
virtual ~CombiLP_base_Parameter()
GraphicalModelType::LabelType LabelType
std::string maskFileNamePre_
static const size_t ContinueInf
ReparametrizerType::MaskType MaskType
visitors::EmptyVisitor< CombiLP< GM, ACC, LPSOLVER > > EmptyVisitorType
CombiLP(const GraphicalModelType &gm, const Parameter ¶m)
visitors::VerboseVisitor< CombiLP< GM, ACC, LPSOLVER > > VerboseVisitorType
InferenceTermination arg(std::vector< LabelType > &out, const size_t=1) const
output a solution