/usr/include/opengm/inference/auxiliary/lp_reparametrization.hxx is in libopengm-dev 2.3.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | /*
* lp_reparametrization_storage.hxx
*
* Created on: Sep 16, 2013
* Author: bsavchyn
*/
#ifndef LP_REPARAMETRIZATION_STORAGE_HXX_
#define LP_REPARAMETRIZATION_STORAGE_HXX_
#include <opengm/inference/trws/utilities2.hxx>
#include <opengm/graphicalmodel/graphicalmodel_factor_accumulator.hxx>
//#ifdef WITH_HDF5
//#include <opengm/inference/auxiliary/lp_reparametrization_hdf5.hxx>
//#endif
namespace opengm{
#ifdef TRWS_DEBUG_OUTPUT
using OUT::operator <<;
#endif
template<class GM>
class LPReparametrisationStorage{
public:
typedef GM GraphicalModelType;
typedef typename GM::ValueType ValueType;
typedef typename GM::FactorType FactorType;
typedef typename GM::IndexType IndexType;
typedef typename GM::LabelType LabelType;
//typedef std::valarray<ValueType> UnaryFactor;
typedef std::vector<ValueType> UnaryFactor;
typedef ValueType* uIterator;
typedef std::vector<UnaryFactor> VecUnaryFactors;
typedef std::map<IndexType,IndexType> VarIdMapType;
LPReparametrisationStorage(const GM& gm);
const UnaryFactor& get(IndexType factorIndex,IndexType relativeVarIndex)const//const access
{
OPENGM_ASSERT(factorIndex < _gm.numberOfFactors());
OPENGM_ASSERT(relativeVarIndex < _dualVariables[factorIndex].size());
return _dualVariables[factorIndex][relativeVarIndex];
}
std::pair<uIterator,uIterator> getIterators(IndexType factorIndex,IndexType relativeVarIndex)
{
OPENGM_ASSERT(factorIndex < _gm.numberOfFactors());
OPENGM_ASSERT(relativeVarIndex < _dualVariables[factorIndex].size());
UnaryFactor& uf=_dualVariables[factorIndex][relativeVarIndex];
uIterator begin=&uf[0];
return std::make_pair(begin,begin+uf.size());
}
template<class ITERATOR>
ValueType getFactorValue(IndexType findex,ITERATOR it)const
{
OPENGM_ASSERT(findex < _gm.numberOfFactors());
const typename GM::FactorType& factor=_gm[findex];
ValueType res=0;//factor(it);
if (factor.numberOfVariables()>1)
{
res=factor(it);
for (IndexType varId=0;varId<factor.numberOfVariables();++varId)
{
OPENGM_ASSERT(varId < _dualVariables[findex].size());
OPENGM_ASSERT(*(it+varId) < _dualVariables[findex][varId].size());
res+=_dualVariables[findex][varId][*(it+varId)];
}
}else
{
res=getVariableValue(factor.variableIndex(0),*it);
}
return res;
}
ValueType getVariableValue(IndexType varIndex,LabelType label)const
{
OPENGM_ASSERT(varIndex < _gm.numberOfVariables());
ValueType res=0.0;
for (IndexType i=0;i<_gm.numberOfFactors(varIndex);++i)
{
IndexType factorId=_gm.factorOfVariable(varIndex,i);
OPENGM_ASSERT(factorId < _gm.numberOfFactors());
if (_gm[factorId].numberOfVariables()==1)
{
res+=_gm[factorId](&label);
continue;
}
OPENGM_ASSERT( factorId < _dualVariables.size() );
OPENGM_ASSERT(label < _dualVariables[factorId][localId(factorId,varIndex)].size());
res-=_dualVariables[factorId][localId(factorId,varIndex)][label];
}
return res;
}
#ifdef TRWS_DEBUG_OUTPUT
void PrintTestData(std::ostream& fout)const;
#endif
IndexType localId(IndexType factorId,IndexType varIndex)const{
typename VarIdMapType::const_iterator it = _localIdMap[factorId].find(varIndex);
trws_base::exception_check(it!=_localIdMap[factorId].end(),"LPReparametrisationStorage:localId() - factor and variable are not connected!");
return it->second;};
const GM& graphicalModel()const{return _gm;}
template<class VECTOR>
void serialize(VECTOR* pserialization)const;
template<class VECTOR>
void deserialize(const VECTOR& serialization);
private:
LPReparametrisationStorage(const LPReparametrisationStorage&);//TODO: carefully implement, when needed
LPReparametrisationStorage& operator=(const LPReparametrisationStorage&);//TODO: carefully implement, when needed
const GM& _gm;
std::vector<VecUnaryFactors> _dualVariables;
std::vector<VarIdMapType> _localIdMap;
};
template<class GM>
LPReparametrisationStorage<GM>::LPReparametrisationStorage(const GM& gm)
:_gm(gm),_localIdMap(gm.numberOfFactors())
{
_dualVariables.resize(_gm.numberOfFactors());
//for all factors with order > 1
for (IndexType findex=0;findex<_gm.numberOfFactors();++findex)
{
IndexType numVars=_gm[findex].numberOfVariables();
VarIdMapType& mapFindex=_localIdMap[findex];
if (numVars>=2)
{
_dualVariables[findex].resize(numVars);
//std::valarray<IndexType> v(numVars);
std::vector<IndexType> v(numVars);
_gm[findex].variableIndices(&v[0]);
for (IndexType n=0;n<numVars;++n)
{
//_dualVariables[findex][n].assign(_gm.numberOfLabels(v[n]),0.0);//TODO. Do it like this
_dualVariables[findex][n].resize(_gm.numberOfLabels(v[n]));
mapFindex[v[n]]=n;
}
}
}
}
#ifdef TRWS_DEBUG_OUTPUT
template<class GM>
void LPReparametrisationStorage<GM>::PrintTestData(std::ostream& fout)const
{
fout << "_dualVariables.size()=" << _dualVariables.size()<<std::endl;
for (IndexType factorIndex=0;factorIndex<_dualVariables.size();++factorIndex )
{
fout <<"factorIndex="<<factorIndex<<": ---------------------------------"<<std::endl;
for (IndexType varId=0;varId<_dualVariables[factorIndex].size();++varId)
fout <<"varId="<<varId<<": "<< _dualVariables[factorIndex][varId]<<std::endl;
}
}
#endif
template<class GM>
template<class VECTOR>
void LPReparametrisationStorage<GM>::serialize(VECTOR* pserialization)const
{
//computing total space needed:
size_t i=0;
for (IndexType factorId=0;factorId<_dualVariables.size();++factorId)
for (IndexType localId=0;localId<_dualVariables[factorId].size();++localId)
for (LabelType label=0;label<_dualVariables[factorId][localId].size();++label)
++i;
pserialization->resize(i);
//serializing....
i=0;
for (IndexType factorId=0;factorId<_dualVariables.size();++factorId)
for (IndexType localId=0;localId<_dualVariables[factorId].size();++localId)
for (LabelType label=0;label<_dualVariables[factorId][localId].size();++label)
(*pserialization)[i++]=_dualVariables[factorId][localId][label];
}
template<class GM>
template<class VECTOR>
void LPReparametrisationStorage<GM>::deserialize(const VECTOR& serialization)
{
size_t i=0;
for (IndexType factorId=0;factorId<_gm.numberOfFactors();++factorId)
{
OPENGM_ASSERT(factorId<_dualVariables.size());
if (_gm[factorId].numberOfVariables()==1) continue;
for (IndexType localId=0;localId<_gm[factorId].numberOfVariables();++localId)
{
OPENGM_ASSERT(localId<_dualVariables[factorId].size());
for (LabelType label=0;label<_dualVariables[factorId][localId].size();++label)
{
OPENGM_ASSERT(label<_dualVariables[factorId][localId].size());
if (i>=serialization.size())
throw std::runtime_error("LPReparametrisationStorage<GM>::deserialize(): Size of serialization is less than required for the graphical model! Deserialization failed.");
_dualVariables[factorId][localId][label]=serialization[i++];
}
}
}
if (i!=serialization.size())
throw std::runtime_error("LPReparametrisationStorage<GM>::deserialize(): Size of serialization is greater than required for the graphical model! Deserialization failed.");
}
/*
#ifdef WITH_HDF5
template<class GM>
void save(const LPReparametrisationStorage<GM>& repa,const std::string& filename,const std::string& modelname)
{
hid_t file = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
OPENGM_ASSERT(file >= 0);
marray::Vector<typename GM::ValueType> marr;
repa.serialize(&marr);
marray::hdf5::save(file,modelname.c_str(),marr);
H5Fclose(file);
}
template<class GM>
void load(LPReparametrisationStorage<GM>* prepa, const std::string& filename, const std::string& modelname)
{
hid_t file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
OPENGM_ASSERT(file>=0);
marray::Vector<typename GM::ValueType> marr;
marray::hdf5::load(file,modelname.c_str(),marr);
prepa->deserialize(marr);
H5Fclose(file);
};
#endif
*/
template<class GM, class REPASTORAGE>
class ReparametrizationView : public opengm::FunctionBase<ReparametrizationView<GM,REPASTORAGE>,
typename GM::ValueType,typename GM::IndexType, typename GM::LabelType>
{
public:
typedef typename GM::ValueType ValueType;
typedef ValueType value_type;
typedef typename GM::FactorType FactorType;
typedef typename GM::OperatorType OperatorType;
typedef typename GM::IndexType IndexType;
typedef typename GM::LabelType LabelType;
typedef GM GraphicalModelType;
typedef REPASTORAGE ReparametrizationStorageType;
ReparametrizationView(const FactorType & factor,const REPASTORAGE& repaStorage,IndexType factorId)
:_pfactor(&factor),_prepaStorage(&repaStorage),_factorId(factorId)
{};
template<class Iterator>
ValueType operator()(Iterator begin)const
{
switch (_pfactor->numberOfVariables())
{
case 1: return _prepaStorage->getVariableValue(_pfactor->variableIndex(0),*begin);
default: return _prepaStorage->getFactorValue(_factorId,begin);
};
}
LabelType shape(const IndexType& index)const{return _pfactor->numberOfLabels(index);};
IndexType dimension()const{return _pfactor->numberOfVariables();};
IndexType size()const{return _pfactor->size();};
private:
const FactorType* _pfactor;
const REPASTORAGE* _prepaStorage;
IndexType _factorId;
};
struct LPReparametrizer_Parameter
{
LPReparametrizer_Parameter(){};
};
template<class GM, class ACC>
class LPReparametrizer
{
public:
typedef GM GraphicalModelType;
typedef typename GraphicalModelType::ValueType ValueType;
typedef typename GraphicalModelType::IndexType IndexType;
typedef typename GraphicalModelType::LabelType LabelType;
typedef typename std::vector<bool> MaskType;
typedef typename std::vector<MaskType> ImmovableLabelingType;
typedef LPReparametrisationStorage<GM> RepaStorageType;
typedef opengm::GraphicalModel<ValueType,opengm::Adder,opengm::ReparametrizationView<GM,RepaStorageType>,
opengm::DiscreteSpace<IndexType,LabelType> > ReparametrizedGMType;
typedef LPReparametrizer_Parameter Parameter;
LPReparametrizer(const GM& gm):_gm(gm),_repastorage(_gm){};
virtual ~LPReparametrizer(){};
RepaStorageType& Reparametrization(){return _repastorage;};
//TODO: To implement
virtual void getArcConsistency(std::vector<bool>* pmask,std::vector<LabelType>* plabeling,IndexType modelorder=2);
virtual void reparametrize(const MaskType* pmask=0){};
void reparametrize(const ImmovableLabelingType& immovableLabeling){};
virtual void getReparametrizedModel(ReparametrizedGMType& gm)const;
const GM& graphicalModel()const{return _gm;}
private:
const GM& _gm;
RepaStorageType _repastorage;
};
template<class GM, class ACC>
void LPReparametrizer<GM,ACC>::getReparametrizedModel(ReparametrizedGMType& gm)const
{
gm=ReparametrizedGMType(_gm.space());
//copying factors
for (typename GM::IndexType factorID=0;factorID<_gm.numberOfFactors();++factorID)
{
const typename GM::FactorType& f=_gm[factorID];
opengm::ReparametrizationView<GM,RepaStorageType> repaView(f,_repastorage,factorID);
typename ReparametrizedGMType::FunctionIdentifier fId=gm.addFunction(repaView);
gm.addFactor(fId,f.variableIndicesBegin(), f.variableIndicesEnd());
}
}
template<class GM, class ACC>
void LPReparametrizer<GM,ACC>::getArcConsistency(std::vector<bool>* pmask,std::vector<LabelType>* plabeling,IndexType modelorder)
{
pmask->assign(_gm.numberOfVariables(),true);
ReparametrizedGMType repagm;
getReparametrizedModel(repagm);
/** for (all factors)
compute optimal values and labels (label sequences)
create the list of unary factors;
find optimal label for each variable
**/
std::vector<ValueType> optimalValues(repagm.numberOfFactors());
std::vector< std::vector<LabelType> > optimalLabelings(repagm.numberOfFactors(),std::vector<LabelType>(modelorder));
//std::vector<LabelType> locallyOptimalLabels(repagm.numberOfVariables(),0);//in case there is no corresponding unary factor 0 is always one of optimal labels (all labels are optimal)
std::vector<LabelType>& locallyOptimalLabels=*plabeling;
locallyOptimalLabels.assign(repagm.numberOfVariables(),0);//in case there is no corresponding unary factor 0 is always one of optimal labels (all labels are optimal)
std::vector<IndexType> unaryFactors; unaryFactors.reserve(repagm.numberOfFactors());
std::vector<ValueType> worstValue(repagm.numberOfFactors(),0);
// std::cout << "First cycle:" <<std::endl;
for (IndexType factorId=0;factorId<repagm.numberOfFactors();++factorId)
{
const typename ReparametrizedGMType::FactorType& factor=repagm[factorId];
optimalLabelings[factorId].resize(factor.numberOfVariables());
//accumulate.template<ACC>(factor,optimalValues[factorId],optimalLabelings[factorId]);
accumulate<ACC,typename ReparametrizedGMType::FactorType,ValueType,LabelType>(factor,optimalValues[factorId],optimalLabelings[factorId]);
// std::cout << "factorId=" << factorId<< ", optimalValues=" << optimalValues[factorId] << ", optimalLabelings=" << optimalLabelings[factorId] <<std::endl;
if (factor.numberOfVariables() == 1)
{
unaryFactors.push_back(factorId);
locallyOptimalLabels[factor.variableIndex(0)]=optimalLabelings[factorId][0];
// std::cout << "locallyOptimalLabels[" << factor.variableIndex(0)<<"]=" << locallyOptimalLabels[factor.variableIndex(0)] << std::endl;
}else
{
if (ACC::bop(0,1))
worstValue[factorId]=factor.max();
else
worstValue[factorId]=factor.min();
}
}
/** for (unary factors and the optimal label)
{
for (each NON-nary factor)
if NOT (locally optimal labels form an eps-optimal factor value
or the optimal label produces THE (very) optimal factor value)
mark the node as NON-consistent
}
**/
for (IndexType i=0;i<unaryFactors.size();++i)
{
IndexType var= repagm[unaryFactors[i]].variableIndex(0);
IndexType numOfFactors=repagm.numberOfFactors(var);
for (IndexType f=0;f<numOfFactors;++f)
{
IndexType factorId=repagm.factorOfVariable(var,f);
const typename ReparametrizedGMType::FactorType& factor=repagm[factorId];
// std::cout << "factorId=" <<factorId <<", optimalValues="<< optimalValues[factorId]<< std::endl;
if (factor.numberOfVariables() <= 1) continue;//!> only higher order factors are considered
IndexType localVarIndex= std::find(factor.variableIndicesBegin(),factor.variableIndicesEnd(),var) -factor.variableIndicesBegin();//!>find the place of the variable
OPENGM_ASSERT((IndexType)localVarIndex != (IndexType)(factor.variableIndicesEnd()-factor.variableIndicesBegin()));
if (optimalLabelings[factorId][localVarIndex]==locallyOptimalLabels[var]) continue; //!>if the label belongs to the optimal configuration of the factor
std::vector<LabelType> labeling(optimalLabelings[factorId].size());
//labeling[localVarIndex]=locallyOptimalLabels[var];
for (IndexType v=0;v<factor.numberOfVariables();++v)
labeling[v]=locallyOptimalLabels[factor.variableIndex(v)];
// std::cout <<"worstValue="<<worstValue[factorId] << ", localVarIndex=" <<localVarIndex <<", labeling="<< labeling<<std::endl;
if (fabs(factor(labeling.begin())-optimalValues[factorId])
<factor.numberOfVariables()*fabs(worstValue[factorId])*std::numeric_limits<ValueType>::epsilon()) continue;//!> if it is connected to other optimal labels with eps-optimal hyperedge
/** else **/
// std::cout << "False:("<<std::endl;
(*pmask)[var]=false; break;
}
}
}
}
#endif /* LP_REPARAMETRIZATION_STORAGE_HXX_ */
|