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>
69 typename TrivializationSolver<GM,ACC,InputIterator>::IndexType
70 TrivializationSolver<GM,ACC,InputIterator>::_distanceFromStart()
72 if (parent::_moveDirection==Storage::Direct)
73 return parent::_currentUnaryIndex;
75 return parent::size()-1-parent::_currentUnaryIndex;
78 template<
class GM,
class ACC,
class InputIterator>
79 void TrivializationSolver<GM,ACC,InputIterator>::_InitBackwardMoveBuffer(IndexType index)
81 assert(index < parent::_storage.size());
82 parent::_currentUnaryIndex=index;
83 parent::_currentUnaryFactor.resize(parent::_storage.unaryFactors(parent::_currentUnaryIndex).size());
84 std::copy(parent::_marginals[parent::_currentUnaryIndex].
85 begin(),parent::_marginals[parent::_currentUnaryIndex].end(),parent::_currentUnaryFactor.begin());
87 _numberOfBoundaryTerms=std::count(_mask.begin(),_mask.end(),
true);
92 template<
class GM,
class ACC,
class InputIterator>
93 void TrivializationSolver<GM,ACC,InputIterator>::_PushBack()
97 if (_mask[parent::_currentUnaryIndex])
99 multiplier=((ValueType)_numberOfBoundaryTerms-1.0)/_numberOfBoundaryTerms;
100 --_numberOfBoundaryTerms;
108 parent::_currentUnaryFactor.end(),
109 std::bind2nd(std::multiplies<ValueType>(),multiplier));
111 std::transform(parent::_currentUnaryFactor.begin(),
112 parent::_currentUnaryFactor.end(),
113 parent::_marginals[parent::_currentUnaryIndex].begin(),
114 parent::_currentUnaryFactor.begin(),
115 std::minus<ValueType>());
116 std::transform(parent::_currentUnaryFactor.begin(),parent::_currentUnaryFactor.end(),
117 parent::_storage.unaryFactors(parent::_currentUnaryIndex).begin(),
118 parent::_currentUnaryFactor.begin(),std::plus<ValueType>());
120 _setDuals(parent::_currentUnaryIndex,parent::_moveDirection,parent::_currentUnaryFactor.begin());
122 parent::_PushMessagesToFactor();
123 parent::_currentUnaryIndex=parent::_next(parent::_currentUnaryIndex);
124 parent::_currentUnaryFactor.assign(parent::_storage.unaryFactors(parent::_currentUnaryIndex).size(),0.0);
125 parent::_ClearMessages();
128 parent::_currentUnaryFactor.end(),
129 std::bind2nd(std::multiplies<ValueType>(),-1.0));
130 _setDuals(parent::_currentUnaryIndex,Storage::ReverseDirection(parent::_moveDirection),
131 parent::_currentUnaryFactor.begin());
133 std::transform(parent::_marginals[parent::_currentUnaryIndex].begin(),
134 parent::_marginals[parent::_currentUnaryIndex].end(),
135 parent::_currentUnaryFactor.begin(),parent::_currentUnaryFactor.begin(),
136 std::minus<ValueType>());
200 template<
class GM,
class ACC,
class InputIterator>
204 _mask.assign(parent::size(),
true);
208 parent::_moveDirection=Storage::ReverseDirection(parent::_moveDirection);
209 if (parent::_moveDirection==Storage::Direct)
210 _InitBackwardMoveBuffer(0);
212 _InitBackwardMoveBuffer(parent::size()-1);
214 for (
IndexType i=0;i<parent::size()-1;++i)
217 parent::_bInitializationNeeded=
true;
244 template<
class GM,
class ACC,
class InputIterator>
248 IndexType pwId, varId;
250 if (movedir==Storage::Direct)
252 pwId=parent::_storage.pwForwardFactor(index);
253 varId=(parent::_storage.pwDirection(index)==Storage::Direct ? 0 : 1);
257 pwId=parent::_storage.pwForwardFactor(index-1);
258 varId=(parent::_storage.pwDirection(index-1)==Storage::Direct ? 1 : 0);
260 std::pair<typename DualStorage::uIterator,typename DualStorage::uIterator> dualIt
261 =_dualstorage.getIterators(pwId,varId);
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;
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)
399 std::pair<uIterator,uIterator> it=parent::Reparametrization().getIterators(factorID,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)
415 std::pair<uIterator,uIterator> it=parent::Reparametrization().getIterators(factorID,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;
430 ValueType res1=parent::Reparametrization().getFactorValue(factorID,labeling.begin());
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
const SubVariableListType & getSubVariableList(IndexType varId) const
TRWS_Reparametrizer_Parameters(bool fastComputations=true)
ValueType getVariableValue(IndexType varIndex, LabelType label) const
virtual void Move()
>initializes move, which is reverse to the current one//TODO: remove virtual ?
trws_base::FunctionParameters< GraphicalModelType > FunctionParametersType
std::vector< bool > MaskType
parent::ValueType ValueType
parent::MaskType MaskType
std::vector< MaskType > ImmovableLabelingType
const GM & masterModel() const
TrivializationSolver(Storage &primalstorage, DualStorage &dualstorage, const FactorProperties &fp, bool fastComputations=true)
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
const GM & graphicalModel() const
TRWS_Reparametrizer_Parameters< ValueType > Parameter
parent::InputIteratorType InputIteratorType
std::vector< ValueType > UnaryFactor
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
ValueType GetObjectiveValue() const
parent::ValueType ValueType
opengm::LPReparametrisationStorage< GM > DualStorage
virtual ~TRWS_Reparametrizer()
parent::GraphicalModelType GraphicalModelType
parent::UnaryFactor UnaryFactor
parent::ReparametrizedGMType ReparametrizedGMType
const UnaryFactor & get(IndexType factorIndex, IndexType relativeVarIndex) const
GraphicalModelType::ValueType ValueType
opengm::LPReparametrizer< typename Storage::GraphicalModelType, ACC > parent
parent::IndexType IndexType
parent::InputIteratorType InputIteratorType
GraphicalModelType::LabelType LabelType
parent::MoveDirection MoveDirection
parent::LabelType LabelType
parent::ImmovableLabelingType ImmovableLabelingType
parent::FactorProperties FactorProperties
parent::RepaStorageType RepaStorageType
void BackwardMove(const MaskType *pmask=0)
SubModel & subModel(IndexType modelId)
RepaStorageType & Reparametrization()
ValueType GetObjectiveValue() const
parent::FactorProperties FactorProperties
void reparametrize(const MaskType *pmask=0)
void ForwardMove(MoveDirection direction=Storage::Direct)
parent::UnaryFactor UnaryFactor
UnaryFactor::const_iterator const_uIterator