8 #ifndef LP_REPARAMETRIZATION_STORAGE_HXX_
9 #define LP_REPARAMETRIZATION_STORAGE_HXX_
21 #ifdef TRWS_DEBUG_OUTPUT
22 using OUT::operator <<;
42 const UnaryFactor&
get(IndexType factorIndex,IndexType relativeVarIndex)
const
45 OPENGM_ASSERT(relativeVarIndex < _dualVariables[factorIndex].size());
46 return _dualVariables[factorIndex][relativeVarIndex];
48 std::pair<uIterator,uIterator>
getIterators(IndexType factorIndex,IndexType relativeVarIndex)
51 OPENGM_ASSERT(relativeVarIndex < _dualVariables[factorIndex].size());
52 UnaryFactor& uf=_dualVariables[factorIndex][relativeVarIndex];
53 uIterator begin=&uf[0];
54 return std::make_pair(begin,begin+uf.size());
57 template<
class ITERATOR>
61 const typename GM::FactorType& factor=_gm[findex];
64 if (factor.numberOfVariables()>1)
67 for (IndexType varId=0;varId<factor.numberOfVariables();++varId)
70 OPENGM_ASSERT(*(it+varId) < _dualVariables[findex][varId].size());
71 res+=_dualVariables[findex][varId][*(it+varId)];
84 for (IndexType i=0;i<_gm.numberOfFactors(varIndex);++i)
86 IndexType factorId=_gm.factorOfVariable(varIndex,i);
88 if (_gm[factorId].numberOfVariables()==1)
90 res+=_gm[factorId](&label);
96 res-=_dualVariables[factorId][
localId(factorId,varIndex)][label];
101 #ifdef TRWS_DEBUG_OUTPUT
102 void PrintTestData(std::ostream& fout)
const;
104 IndexType
localId(IndexType factorId,IndexType varIndex)
const{
105 typename VarIdMapType::const_iterator it = _localIdMap[factorId].find(varIndex);
106 trws_base::exception_check(it!=_localIdMap[factorId].end(),
"LPReparametrisationStorage:localId() - factor and variable are not connected!");
111 template<
class VECTOR>
112 void serialize(VECTOR* pserialization)
const;
113 template<
class VECTOR>
119 std::vector<VecUnaryFactors> _dualVariables;
120 std::vector<VarIdMapType> _localIdMap;
125 :_gm(gm),_localIdMap(gm.numberOfFactors())
127 _dualVariables.resize(_gm.numberOfFactors());
129 for (
IndexType findex=0;findex<_gm.numberOfFactors();++findex)
131 IndexType numVars=_gm[findex].numberOfVariables();
136 _dualVariables[findex].resize(numVars);
138 std::vector<IndexType> v(numVars);
139 _gm[findex].variableIndices(&v[0]);
143 _dualVariables[findex][n].resize(_gm.numberOfLabels(v[n]));
151 #ifdef TRWS_DEBUG_OUTPUT
155 fout <<
"_dualVariables.size()=" << _dualVariables.size()<<std::endl;
156 for (IndexType factorIndex=0;factorIndex<_dualVariables.size();++factorIndex )
158 fout <<
"factorIndex="<<factorIndex<<
": ---------------------------------"<<std::endl;
159 for (IndexType varId=0;varId<_dualVariables[factorIndex].size();++varId)
160 fout <<
"varId="<<varId<<
": "<< _dualVariables[factorIndex][varId]<<std::endl;
166 template<
class VECTOR>
171 for (
IndexType factorId=0;factorId<_dualVariables.size();++factorId)
172 for (
IndexType localId=0;localId<_dualVariables[factorId].size();++localId)
173 for (
LabelType label=0;label<_dualVariables[factorId][localId].size();++label)
176 pserialization->resize(i);
179 for (
IndexType factorId=0;factorId<_dualVariables.size();++factorId)
180 for (
IndexType localId=0;localId<_dualVariables[factorId].size();++localId)
181 for (
LabelType label=0;label<_dualVariables[factorId][localId].size();++label)
182 (*pserialization)[i++]=_dualVariables[factorId][localId][label];
186 template<
class VECTOR>
190 for (
IndexType factorId=0;factorId<_gm.numberOfFactors();++factorId)
193 if (_gm[factorId].numberOfVariables()==1)
continue;
194 for (
IndexType localId=0;localId<_gm[factorId].numberOfVariables();++localId)
197 for (
LabelType label=0;label<_dualVariables[factorId][localId].size();++label)
199 OPENGM_ASSERT(label<_dualVariables[factorId][localId].size());
200 if (i>=serialization.size())
201 throw std::runtime_error(
"LPReparametrisationStorage<GM>::deserialize(): Size of serialization is less than required for the graphical model! Deserialization failed.");
202 _dualVariables[factorId][localId][label]=serialization[i++];
206 if (i!=serialization.size())
207 throw std::runtime_error(
"LPReparametrisationStorage<GM>::deserialize(): Size of serialization is greater than required for the graphical model! Deserialization failed.");
237 template<
class GM,
class REPASTORAGE>
239 typename GM::ValueType,typename GM::IndexType, typename GM::LabelType>
253 :_pfactor(&factor),_prepaStorage(&repaStorage),_factorId(factorId)
256 template<
class Iterator>
259 switch (_pfactor->numberOfVariables())
261 case 1:
return _prepaStorage->getVariableValue(_pfactor->variableIndex(0),*begin);
262 default:
return _prepaStorage->getFactorValue(_factorId,begin);
266 LabelType
shape(
const IndexType& index)
const{
return _pfactor->numberOfLabels(index);};
267 IndexType
dimension()
const{
return _pfactor->numberOfVariables();};
268 IndexType
size()
const{
return _pfactor->size();};
271 const FactorType* _pfactor;
272 const REPASTORAGE* _prepaStorage;
281 template<
class GM,
class ACC>
286 typedef typename GraphicalModelType::ValueType
ValueType;
287 typedef typename GraphicalModelType::IndexType
IndexType;
288 typedef typename GraphicalModelType::LabelType
LabelType;
300 virtual void getArcConsistency(std::vector<bool>* pmask,std::vector<LabelType>* plabeling,IndexType modelorder=2);
307 RepaStorageType _repastorage;
310 template<
class GM,
class ACC>
315 for (
typename GM::IndexType factorID=0;factorID<_gm.numberOfFactors();++factorID)
317 const typename GM::FactorType& f=_gm[factorID];
320 gm.
addFactor(fId,f.variableIndicesBegin(), f.variableIndicesEnd());
324 template<
class GM,
class ACC>
327 pmask->assign(_gm.numberOfVariables(),
true);
329 getReparametrizedModel(repagm);
338 std::vector< std::vector<LabelType> > optimalLabelings(repagm.
numberOfFactors(),std::vector<LabelType>(modelorder));
340 std::vector<LabelType>& locallyOptimalLabels=*plabeling;
343 std::vector<IndexType> unaryFactors; unaryFactors.reserve(repagm.
numberOfFactors());
353 accumulate<ACC,typename ReparametrizedGMType::FactorType,ValueType,LabelType>(factor,optimalValues[factorId],optimalLabelings[factorId]);
357 if (factor.numberOfVariables() == 1)
359 unaryFactors.push_back(factorId);
360 locallyOptimalLabels[factor.variableIndex(0)]=optimalLabelings[factorId][0];
365 worstValue[factorId]=factor.max();
367 worstValue[factorId]=factor.min();
381 for (
IndexType i=0;i<unaryFactors.size();++i)
383 IndexType var= repagm[unaryFactors[i]].variableIndex(0);
399 if (optimalLabelings[factorId][localVarIndex]==locallyOptimalLabels[var])
continue;
401 std::vector<LabelType> labeling(optimalLabelings[factorId].size());
409 if (fabs(factor(labeling.begin())-optimalValues[factorId])
410 <factor.
numberOfVariables()*fabs(worstValue[factorId])*std::numeric_limits<ValueType>::epsilon())
continue;
415 (*pmask)[var]=
false;
break;
IndexType addFactor(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
void exception_check(bool condition, const std::string &str)
REPASTORAGE ReparametrizationStorageType
Fallback implementation of member functions of OpenGM functions.
ReparametrizationView(const FactorType &factor, const REPASTORAGE &repaStorage, IndexType factorId)
std::map< IndexType, IndexType > VarIdMapType
ValueType operator()(Iterator begin) const
std::pair< uIterator, uIterator > getIterators(IndexType factorIndex, IndexType relativeVarIndex)
Discrete space in which variables can have differently many labels.
const GM & graphicalModel() const
virtual void getReparametrizedModel(ReparametrizedGMType &gm) const
FunctionIdentifier addFunction(const FUNCTION_TYPE &)
add a function to the graphical model
ValueType getVariableValue(IndexType varIndex, LabelType label) const
std::vector< bool > MaskType
IndexType dimension() const
IndexType factorOfVariable(const IndexType, const IndexType) const
return the k-th factor connected to the j-th variable
VariablesIteratorType variableIndicesBegin() const
#define OPENGM_ASSERT(expression)
std::vector< MaskType > ImmovableLabelingType
const GM & graphicalModel() const
LPReparametrisationStorage(const GM &gm)
void deserialize(const VECTOR &serialization)
IndexType localId(IndexType factorId, IndexType varIndex) const
std::vector< ValueType > UnaryFactor
VariablesIteratorType variableIndicesEnd() const
LPReparametrizer_Parameter Parameter
LabelType shape(const IndexType &index) const
LPReparametrisationStorage< GM > RepaStorageType
opengm::GraphicalModel< ValueType, opengm::Adder, opengm::ReparametrizationView< GM, RepaStorageType >, opengm::DiscreteSpace< IndexType, LabelType > > ReparametrizedGMType
Abstraction (wrapper class) of factors, independent of the function used to implement the factor...
ValueType getFactorValue(IndexType findex, ITERATOR it) const
IndexType numberOfVariables() const
GM::FactorType FactorType
GM::FactorType FactorType
std::vector< UnaryFactor > VecUnaryFactors
LPReparametrizer(const GM &gm)
GraphicalModelType::ValueType ValueType
GraphicalModelType::LabelType LabelType
virtual void getArcConsistency(std::vector< bool > *pmask, std::vector< LabelType > *plabeling, IndexType modelorder=2)
IndexType variableIndex(const IndexType) const
return the index of the j-th variable
void reparametrize(const ImmovableLabelingType &immovableLabeling)
GM::OperatorType OperatorType
virtual ~LPReparametrizer()
RepaStorageType & Reparametrization()
GraphicalModelType::IndexType IndexType
IndexType numberOfVariables() const
void serialize(VECTOR *pserialization) const
LPReparametrizer_Parameter()
IndexType numberOfFactors() const
virtual void reparametrize(const MaskType *pmask=0)