18 std::cout<<*begin<<
" ";
25 template<
class GM,
class GM_OUT>
30 typedef typename GM::IndexType IndexType;
32 typedef typename GM::ValueType ValueType;
33 typedef typename GM::FactorType FactorType;
34 typedef typename GM::FunctionIdentifier Fid;
37 typedef std::map< UInt64Type , Fid > FidMap;
39 FidMap secondOrderMap;
43 for(IndexType fi=0;fi<gm.numberOfFactors();++fi){
44 const FactorType & fac = gm[fi];
45 const IndexType order = fac.numberOfVariables();
54 if(order==1 || order==2){
59 key+=(fac.variableIndex(1)*gm.numberOfVariables());
62 FidMap & fidMap = ( order == 1 ? firstOrderF : secondOrderMap );
65 if(fidMap.find(key)==fidMap.end()){
66 ArrayFunction
function(fac.shapeBegin(),fac.shapeEnd());
67 fac.copyValues(&
function(0));
68 const Fid fid = gmOut.addFunction(
function);
69 gmOut.addFactor(fid,fac.variableIndicesBegin(),fac.variableIndicesEnd());
74 const Fid fid = fidMap[key];
75 ArrayFunction &
function = gmOut.
template getFunction<ArrayFunction>(fid);
76 ArrayFunction buffer(fac.shapeBegin(),fac.shapeEnd());
77 fac.copyValues(&buffer(0));
107 template<
class GM,
class ACC>
135 localVariables_(gm.numberOfVariables()),
136 globalToLocalVariables_(gm.numberOfVariables()),
137 submodelSpace_(gm.numberOfVariables()),
138 inSubmodel_(gm.numberOfVariables(),false),
139 localFactorViBuffer_(),
140 fixedVarPosBuffer_(),
141 notFixedVarPosBuffer_(),
143 handledFactor_(gm.numberOfFactors(),false),
144 labels_(gm.numberOfVariables())
147 const IndexType maxOrder = gm_.factorOrder();
148 localFactorViBuffer_.resize(maxOrder);
149 fixedVarPosBuffer_.resize(maxOrder);
150 notFixedVarPosBuffer_.resize(maxOrder);
174 template<
class VI_ITER>
182 nLocalVar_=std::distance(begin,end);
183 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
184 const IndexType globalVi = begin[localVi];
186 localVariables_[localVi]=globalVi;
187 submodelSpace_[localVi]=gm_.numberOfLabels(globalVi);
188 globalToLocalVariables_[globalVi]=localVi;
189 OPENGM_CHECK(inSubmodel_[globalVi]==
false,
"internal error");
190 inSubmodel_[globalVi]=
true;
198 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
199 const IndexType globalVi = localVariables_[localVi];
200 OPENGM_CHECK(inSubmodel_[globalVi]==
true,
"internal error");
201 inSubmodel_[globalVi]=
false;
218 template<
class SOLVER>
220 const typename SOLVER::Parameter & para ,
221 std::vector<LabelType> & resultArg,
222 const bool improving=
true,
223 const bool warmStart=
false
227 if(resultArg.size()!=nLocalVar_){
228 resultArg.resize(nLocalVar_);
231 SOLVER solver(submodelSpace_.begin(),submodelSpace_.begin()+nLocalVar_,para);
232 buildModelInplace(solver);
234 for(IndexType viLocal=0;viLocal<nLocalVar_;++viLocal){
235 resultArg[viLocal]=labels_[localVariables_[viLocal]];
237 solver.setStartingPoint(resultArg.begin());
240 solver.arg(resultArg);
242 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
243 const IndexType globalVi=localVariables_[localVi];
244 if(resultArg[localVi]!=labels_[globalVi]){
251 template<
class SOLVER>
253 const typename SOLVER::Parameter & para ,
254 std::vector<LabelType> & resultArg,
255 const bool improving=
true,
256 const bool warmStart=
false
259 if(resultArg.size()!=nLocalVar_){
260 resultArg.resize(nLocalVar_);
262 SubGmType subGm( SubSpaceType(submodelSpace_.begin(),submodelSpace_.begin()+nLocalVar_) );
263 reserveGraphicalModel(subGm);
264 buildModelOpenGm(subGm);
265 SOLVER solver(subGm,para);
267 for(IndexType viLocal=0;viLocal<nLocalVar_;++viLocal){
268 resultArg[viLocal]=labels_[localVariables_[viLocal]];
270 solver.setStartingPoint(resultArg.begin());
273 solver.arg(resultArg);
275 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
276 const IndexType globalVi=localVariables_[localVi];
277 if(resultArg[localVi]!=labels_[globalVi]){
289 std::vector<LabelType> & resultArg
292 if(resultArg.size()!=nLocalVar_){
293 resultArg.resize(nLocalVar_);
295 SubGmType subGm( SubSpaceType(submodelSpace_.begin(),submodelSpace_.begin()+nLocalVar_) );
296 reserveGraphicalModel(subGm);
297 buildModelOpenGm(subGm);
300 MergedSubGmType mergeGm( SubSpaceType(submodelSpace_.begin(),submodelSpace_.begin()+nLocalVar_) );
306 DpSubInf solver(mergeGm);
308 solver.arg(resultArg);
310 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
311 const IndexType globalVi=localVariables_[localVi];
312 if(resultArg[localVi]!=labels_[globalVi]){
325 IndexType nFullUnaries = 0;
326 IndexType nFullHighOrder = 0;
327 IndexType nFixeHighOrder = 0;
328 IndexType nFixedUnary = 0;
329 IndexType facVisSpace = 0;
346 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
347 const IndexType globalVi = localVariables_[localVi];
349 OPENGM_CHECK_OP(globalToLocalVariables_[globalVi],==,localVi,
"internal error...mapping invalid");
352 const IndexType nFacVar = gm_.numberOfFactors(globalVi);
353 for(IndexType f=0;f<nFacVar;++f){
354 const IndexType fi = gm_.factorOfVariable(globalVi,f);
355 if(handledFactor_[fi]==
false){
356 handledFactor_[fi]=
true;
358 const FactorType & factor = gm_[fi];
359 const IndexType order = factor.numberOfVariables();
367 const IndexType viGlobal = factor.variableIndex(0);
368 const IndexType viLocal = globalToLocalVariables_[viGlobal];
394 IndexType fixedVars = 0;
395 IndexType notFixedVars = 0;
396 for(IndexType v=0;v<order;++v){
397 const IndexType facVi=factor.variableIndex(v);
398 if(inSubmodel_[facVi]==
false){
399 fixedVarPosBuffer_[fixedVars]=v;
403 notFixedVarPosBuffer_[notFixedVars]=v;
407 const IndexType partialOrder = order - fixedVars;
420 facVisSpace+=partialOrder;
436 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
437 const IndexType globalVi = localVariables_[localVi];
440 const IndexType nFac = gm_.numberOfFactors(globalVi);
441 for(IndexType f=0;f<nFac;++f){
442 const IndexType fi = gm_.factorOfVariable(globalVi,f);
443 handledFactor_[fi]=
false;
447 OPENGM_CHECK_OP(nFac,==,nFullUnaries+nFullHighOrder+nFixeHighOrder+nFixedUnary,
"");
450 subGm.
template reserveFunctions<ViewingFunction> (nFullUnaries+nFullHighOrder);
451 subGm.
template reserveFunctions<FixFunction> (nFixeHighOrder+nFixedUnary);
463 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
464 const IndexType globalVi = localVariables_[localVi];
467 const IndexType nFac = gm_.numberOfFactors(globalVi);
468 for(IndexType f=0;f<nFac;++f){
469 const IndexType fi = gm_.factorOfVariable(globalVi,f);
470 if(handledFactor_[fi]==
false){
471 handledFactor_[fi]=
true;
473 const FactorType & factor = gm_[fi];
474 const IndexType order = factor.numberOfVariables();
480 const IndexType viGlobal = factor.variableIndex(0);
481 const IndexType viLocal = globalToLocalVariables_[viGlobal];
491 IndexType fixedVars = 0;
492 IndexType notFixedVars = 0;
493 for(IndexType v=0;v<order;++v){
494 const IndexType facVi=factor.variableIndex(v);
495 if(!inSubmodel_[facVi]){
496 fixedVarPosBuffer_[fixedVars]=v;
500 notFixedVarPosBuffer_[notFixedVars]=v;
504 const IndexType partialOrder = order - fixedVars;
512 for(IndexType v=0;v<order;++v){
513 const IndexType facVi=factor.variableIndex(v);
514 localFactorViBuffer_[v]=globalToLocalVariables_[facVi];
519 localFactorViBuffer_.begin(),localFactorViBuffer_.begin()+order);
522 PosAndLabelVector positionsAndLabelsOfFixedVars(fixedVars);
525 for(IndexType v=0;v<partialOrder;++v){
526 const IndexType facVi=factor.variableIndex(notFixedVarPosBuffer_[v]);
527 localFactorViBuffer_[v]=globalToLocalVariables_[facVi];
531 for(IndexType fv=0;fv<fixedVars;++fv){
532 const IndexType facVi=factor.variableIndex(fixedVarPosBuffer_[fv]);
533 positionsAndLabelsOfFixedVars[fv].position_ = fixedVarPosBuffer_[fv];
534 positionsAndLabelsOfFixedVars[fv].label_ = labels_[facVi];
538 localFactorViBuffer_.begin(),localFactorViBuffer_.begin()+order-fixedVars);
547 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
548 const IndexType globalVi = localVariables_[localVi];
551 const IndexType nFac = gm_.numberOfFactors(globalVi);
552 for(IndexType f=0;f<nFac;++f){
553 const IndexType fi = gm_.factorOfVariable(globalVi,f);
554 handledFactor_[fi]=
false;
560 template<
class INF_TYPE>
563 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
564 const IndexType globalVi = localVariables_[localVi];
567 const IndexType nFac = gm_.numberOfFactors(globalVi);
568 for(IndexType f=0;f<nFac;++f){
569 const IndexType fi = gm_.factorOfVariable(globalVi,f);
570 if(handledFactor_[fi]==
false){
571 handledFactor_[fi]=
true;
573 const FactorType & factor = gm_[fi];
574 const IndexType order = factor.numberOfVariables();
580 const IndexType viGlobal = factor.variableIndex(0);
581 const IndexType viLocal = globalToLocalVariables_[viGlobal];
583 infType.addFactor(&viLocal,&viLocal+1,factor);
590 IndexType fixedVars = 0;
591 IndexType notFixedVars = 0;
592 for(IndexType v=0;v<order;++v){
593 const IndexType facVi=factor.variableIndex(v);
594 if(!inSubmodel_[facVi]){
595 fixedVarPosBuffer_[fixedVars]=v;
599 notFixedVarPosBuffer_[notFixedVars]=v;
603 const IndexType partialOrder = order - fixedVars;
610 for(IndexType v=0;v<order;++v){
611 const IndexType facVi=factor.variableIndex(v);
612 localFactorViBuffer_[v]=globalToLocalVariables_[facVi];
616 infType.addFactor(localFactorViBuffer_.begin(),localFactorViBuffer_.begin()+order,factor);
619 PosAndLabelVector positionsAndLabelsOfFixedVars(fixedVars);
622 for(IndexType v=0;v<partialOrder;++v){
623 const IndexType facVi=factor.variableIndex(notFixedVarPosBuffer_[v]);
624 localFactorViBuffer_[v]=globalToLocalVariables_[facVi];
628 for(IndexType fv=0;fv<fixedVars;++fv){
629 const IndexType facVi=factor.variableIndex(fixedVarPosBuffer_[fv]);
630 positionsAndLabelsOfFixedVars[fv].position_ = fixedVarPosBuffer_[fv];
631 positionsAndLabelsOfFixedVars[fv].label_ = labels_[facVi];
634 infType.addFactor(localFactorViBuffer_.begin(),localFactorViBuffer_.begin()+order-fixedVars,
635 FixFunction(factor,positionsAndLabelsOfFixedVars));
644 for(IndexType localVi=0;localVi<nLocalVar_;++localVi){
645 const IndexType globalVi = localVariables_[localVi];
648 const IndexType nFac = gm_.numberOfFactors(globalVi);
649 for(IndexType f=0;f<nFac;++f){
650 const IndexType fi = gm_.factorOfVariable(globalVi,f);
651 handledFactor_[fi]=
false;
658 return inSubmodel_[vi];
671 std::vector<IndexType> localVariables_;
672 std::vector<IndexType> globalToLocalVariables_;
673 std::vector<LabelType> submodelSpace_;
674 std::vector<bool> inSubmodel_;
675 std::vector<IndexType> localFactorViBuffer_;
676 std::vector<IndexType> fixedVarPosBuffer_;
677 std::vector<IndexType> notFixedVarPosBuffer_;
678 IndexType nLocalVar_;
682 std::vector<bool> handledFactor_;
685 std::vector<LabelType> labels_;
IndexType addFactor(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
Discrete space in which variables can have differently many labels.
meta::TypeListGenerator< ViewingFunction, FixFunction >::type SubFunctionTypeList
FunctionIdentifier addFunction(const FUNCTION_TYPE &)
add a function to the graphical model
bool inferSubmodel(const typename SOLVER::Parameter ¶, std::vector< LabelType > &resultArg, const bool improving=true, const bool warmStart=false)
PositionAndLabel< IndexType, LabelType > PosAndLabel
void printIter(ITER begin, ITER end)
bool mergeFactorsAndInferDp(std::vector< LabelType > &resultArg)
void buildModelOpenGm(SubGmType &subGm)
bool inferSubmodelInplace(const typename SOLVER::Parameter ¶, std::vector< LabelType > &resultArg, const bool improving=true, const bool warmStart=false)
void buildModelInplace(INF_TYPE &infType)
void reserveFactors(const size_t numF)
detail_types::UInt64Type UInt64Type
uint64
IndexType submodelSize() const
void mergeFactors(const GM &gm, GM_OUT &gmOut)
reference to a Factor of a GraphicalModel
void setVariableIndices(VI_ITER begin, VI_ITER end)
void reserveFactorsVarialbeIndices(const size_t size)
ViewFixVariablesFunction< GM > FixFunction
meta::TypeListGenerator< ArrayFunction >::type MergeSubFunctionTypeList
std::vector< PosAndLabel > PosAndLabelVector
opengm::DiscreteSpace< IndexType, LabelType > SubSpaceType
bool inSubmodel(const IndexType vi) const
GraphicalModel< ValueType, typename GM::OperatorType, SubFunctionTypeList, SubSpaceType > SubGmType
GraphicalModel< ValueType, typename GM::OperatorType, MergeSubFunctionTypeList, SubSpaceType > MergedSubGmType
#define OPENGM_CHECK_OP(A, OP, B, TXT)
ExplicitFunction< ValueType, IndexType, LabelType > ArrayFunction
Funcion that refers to a factor of another GraphicalModel in which some variables are fixed...
#define OPENGM_CHECK(B, TXT)
ViewFunction< GM > ViewingFunction
void setLabel(const IndexType vi, const LabelType label)
void reserveGraphicalModel(SubGmType &subGm)
SubmodelOptimizer(const GM &gm)
void unsetVariableIndices()