1 #ifndef REPARAMETRIZATION_HXX 2 #define REPARAMETRIZATION_HXX 21 template<
class GM,
class ACC,
class InputIterator>
42 DualStorage& dualstorage,
43 const FactorProperties& fp,
44 bool fastComputations=
true)
45 :parent(primalstorage,fp,fastComputations),
46 _dualstorage(dualstorage){};
55 IndexType _distanceFromStart();
56 void _InitBackwardMoveBuffer(IndexType index);
58 DualStorage& _dualstorage;
60 IndexType _numberOfBoundaryTerms;
68 template<
class GM,
class ACC,
class InputIterator>
78 template<
class GM,
class ACC,
class InputIterator>
87 _numberOfBoundaryTerms=std::count(_mask.begin(),_mask.end(),
true);
92 template<
class GM,
class ACC,
class InputIterator>
99 multiplier=((
ValueType)_numberOfBoundaryTerms-1.0)/_numberOfBoundaryTerms;
100 --_numberOfBoundaryTerms;
109 std::bind2nd(std::multiplies<ValueType>(),multiplier));
115 std::minus<ValueType>());
123 parent::_currentUnaryIndex=
parent::_next(parent::_currentUnaryIndex);
129 std::bind2nd(std::multiplies<ValueType>(),-1.0));
136 std::minus<ValueType>());
200 template<
class GM,
class ACC,
class InputIterator>
210 _InitBackwardMoveBuffer(0);
244 template<
class GM,
class ACC,
class InputIterator>
260 std::pair<typename DualStorage::uIterator,typename DualStorage::uIterator> dualIt
262 std::copy(it,it+(dualIt.second-dualIt.first),dualIt.first);
267 template<
class ValueType>
273 fastComputations_(fastComputations)
277 template<
class Storage,
class ACC>
283 typedef typename GraphicalModelType::ValueType
ValueType;
284 typedef typename GraphicalModelType::IndexType
IndexType;
285 typedef typename GraphicalModelType::LabelType
LabelType;
299 void reparametrize(
const MaskType* pmask=0);
300 void reparametrize(
const ImmovableLabelingType& immovableLabeling);
304 std::vector<SubSolverType*> _subSolvers;
307 template<
class Storage,
class ACC>
310 std::for_each(_subSolvers.begin(),_subSolvers.end(),trws_base::DeallocatePointer<SubSolverType>);
313 template<
class Storage,
class ACC>
315 const FunctionParametersType& fparams,
316 const Parameter& params):
317 parent(storage.masterModel()),
320 _subSolvers.resize(_storage.numberOfModels());
322 for (
size_t modelId=0;modelId<_subSolvers.size();++modelId)
329 template<
class Storage,
class ACC>
333 MaskType mask(pmask!=0 ? *pmask :
MaskType(_storage.masterModel().numberOfVariables(),
true));
334 OPENGM_ASSERT(mask.size()==_storage.masterModel().numberOfVariables());
336 MaskType sequenceMask;
337 for (
size_t i=0;i<_subSolvers.size();++i)
339 typename Storage::SubModel& model=_storage.subModel(i);
340 sequenceMask.resize(model.size());
341 for (IndexType localInd=0; localInd<sequenceMask.size();++localInd)
344 sequenceMask[localInd]=mask[model.varIndex(localInd)];
347 _subSolvers[i]->ForwardMove();
348 _subSolvers[i]->BackwardMove(&sequenceMask);
349 bound+=_subSolvers[i]->GetObjectiveValue();
381 template<
class Storage,
class ACC>
384 OPENGM_ASSERT(immovableLabeling.size()==_storage.masterModel().numberOfVariables());
387 typedef typename parent::RepaStorageType::uIterator uIterator;
389 const typename Storage::GraphicalModelType& gm=_storage.masterModel();
390 for (IndexType factorID=0;factorID < gm.numberOfFactors();++factorID)
392 if (gm[factorID].numberOfVariables()<2)
continue;
397 for (IndexType localVarID=0;localVarID<gm[factorID].numberOfVariables();++localVarID)
400 IndexType globalVarID=gm[factorID].variableIndex(localVarID);
401 typename MaskType::const_iterator labIt=immovableLabeling[globalVarID].begin();
402 for (;it.first!=it.second;++it.first)
403 if (*labIt++) *it.first=0;
410 if (gm[factorID].numberOfVariables()!=2)
throw std::runtime_error(
"TRWS_Reparametrizer<Storage,ACC>::reparametrize(): factors of order higher than 2 are not supported!");
412 std::vector<IndexType> labeling(2);
413 for (IndexType localVarID=0;localVarID<gm[factorID].numberOfVariables();++localVarID)
416 uIterator it_begin=it.first;
417 IndexType globalVarID=gm[factorID].variableIndex(localVarID);
418 typename MaskType::const_iterator labIt=immovableLabeling[globalVarID].begin();
419 ValueType res=ACC::template neutral<ValueType>();
421 for (;it.first!=it.second;++it.first)
424 IndexType otherVarID=(localVarID==0 ? 1 : 0);
425 labeling[localVarID]=it.first-it_begin;
426 for (LabelType label=0;label<gm.numberOfLabels(otherVarID);++label)
428 labeling[otherVarID]=label;
431 ACC::op(res,res1,res);
449 typedef typename GM::ValueType
ValueType;
450 typedef typename GM::IndexType
IndexType;
451 typedef typename GM::LabelType
LabelType;
454 std::vector<ValueType> repaUnary;
456 for (IndexType varId=0;varId<lpRepa.
graphicalModel().numberOfVariables();++varId)
457 {
const typename DecompositionStorage::SubVariableListType& varList=ptrwsRepa->
getSubVariableList(varId);
459 if (varList.size()==1)
continue;
463 for (LabelType label=0;label<repaUnary.size();++label)
471 for(
typename DecompositionStorage::SubVariableListType::const_iterator modelIt=varList.begin();
472 modelIt!=varList.end();++modelIt)
474 typename DecompositionStorage::SubModel& subModel=ptrwsRepa->
subModel(modelIt->subModelId_);
475 typename DecompositionStorage::SubModel::UnaryFactor::iterator uit_begin=subModel.ufBegin(modelIt->subVariableId_);
476 typename DecompositionStorage::SubModel::UnaryFactor::iterator uit_end =subModel.ufEnd(modelIt->subVariableId_);
478 std::copy(repaUnary.begin(),repaUnary.end(),uit_begin);
482 if (modelIt->subVariableId_ < subModel.size()-1)
484 IndexType pwId=subModel.pwForwardFactor(modelIt->subVariableId_);
486 prepaUF=&lpRepa.
get(pwId,0);
487 else prepaUF=&lpRepa.
get(pwId,1);
489 std::transform(uit_begin,uit_end,prepaUF->begin(),uit_begin,std::plus<ValueType>());
491 if (modelIt->subVariableId_ >0)
493 IndexType pwId=subModel.pwForwardFactor(modelIt->subVariableId_-1);
495 prepaUF=&lpRepa.
get(pwId,0);
496 else prepaUF=&lpRepa.
get(pwId,1);
498 std::transform(uit_begin,uit_end,prepaUF->begin(),uit_begin,std::plus<ValueType>());
parent::LabelType LabelType
parent::IndexType IndexType
std::pair< uIterator, uIterator > getIterators(IndexType factorIndex, IndexType relativeVarIndex)
void _PushMessagesToFactor()
TRWS_Reparametrizer_Parameters(bool fastComputations=true)
virtual void Move()
>initializes move, which is reverse to the current one//TODO: remove virtual ?
trws_base::FunctionParameters< GraphicalModelType > FunctionParametersType
std::vector< bool > MaskType
const UnaryFactor & get(IndexType factorIndex, IndexType relativeVarIndex) const
parent::ValueType ValueType
parent::MaskType MaskType
std::vector< MaskType > ImmovableLabelingType
const GM & masterModel() const
UnaryFactor _currentUnaryFactor
TrivializationSolver(Storage &primalstorage, DualStorage &dualstorage, const FactorProperties &fp, bool fastComputations=true)
IndexType pwForwardFactor(IndexType var) const
std::vector< bool > MaskType
trws_base::TrivializationSolver< GraphicalModelType, ACC, typename std::vector< typename GraphicalModelType::ValueType >::const_iterator > SubSolverType
#define OPENGM_ASSERT(expression)
std::vector< MaskType > ImmovableLabelingType
IndexType _currentUnaryIndex
TRWS_Reparametrizer_Parameters< ValueType > Parameter
parent::InputIteratorType InputIteratorType
std::vector< ValueType > UnaryFactor
static MoveDirection ReverseDirection(MoveDirection dir)
InputIterator transform_inplace(InputIterator first, InputIterator last, UnaryOperator op)
GraphicalModelType::IndexType IndexType
void LPtoDecompositionStorage(const LPReparametrisationStorage< GM > &lpRepa, trws_base::DecompositionStorage< GM > *ptrwsRepa)
TRWS_Reparametrizer(Storage &storage, const FunctionParametersType &fparams, const Parameter ¶ms=Parameter())
LPReparametrisationStorage< GM > RepaStorageType
MaxSumSolver< GM, ACC, InputIterator > parent
parent::ValueType ValueType
opengm::LPReparametrisationStorage< GM > DualStorage
void _ClearMessages(UnaryFactor *pbuffer=0)
virtual ~TRWS_Reparametrizer()
const SubVariableListType & getSubVariableList(IndexType varId) const
parent::GraphicalModelType GraphicalModelType
parent::UnaryFactor UnaryFactor
parent::ReparametrizedGMType ReparametrizedGMType
GraphicalModelType::ValueType ValueType
opengm::LPReparametrizer< typename Storage::GraphicalModelType, ACC > parent
bool _bInitializationNeeded
parent::IndexType IndexType
parent::InputIteratorType InputIteratorType
GraphicalModelType::LabelType LabelType
const GM & graphicalModel() const
parent::MoveDirection MoveDirection
parent::LabelType LabelType
MoveDirection pwDirection(IndexType pwInd) const
parent::ImmovableLabelingType ImmovableLabelingType
parent::FactorProperties FactorProperties
std::vector< UnaryFactor > _marginals
ValueType GetObjectiveValue() const
parent::RepaStorageType RepaStorageType
ValueType getVariableValue(IndexType varIndex, LabelType label) const
void BackwardMove(const MaskType *pmask=0)
SubModel & subModel(IndexType modelId)
RepaStorageType & Reparametrization()
MoveDirection _moveDirection
ValueType GetObjectiveValue() const
parent::FactorProperties FactorProperties
void reparametrize(const MaskType *pmask=0)
IndexType _next(IndexType begin) const
void ForwardMove(MoveDirection direction=Storage::Direct)
parent::UnaryFactor UnaryFactor
UnaryFactor::const_iterator const_uIterator