1 #ifndef OPENGM_HMC_SUBMODEL2
2 #define OPENGM_HMC_SUBMODEL2
6 #include <boost/format.hpp>
7 #include <boost/unordered_map.hpp>
16 #if defined(WITH_BLOSSOM5) && defined(WITH_PLANARITY)
23 #undef OPENGM_CHECK_OP
24 #define OPENGM_CHECK_OP(A,OP,B,TXT)
28 #define OPENGM_CHECK(B,TXT)
48 typedef std::pair<size_t,size_t>
Edge;
50 typedef boost::unordered_map<Edge,size_t>
EdgeMap;
58 SubmodelCGC(
const GM & gm,
const IndexType maxBruteForceSize2,
const IndexType maxBruteForceSize4,
const bool useBfs);
81 const LabelType colorCC,
84 std::deque<IndexType> & deque,
98 const LabelType colorCC0,
99 const LabelType colorCC1,
100 const IndexType viCC0,
101 const IndexType viCC1,
103 const bool planar=
true
132 for(IndexType f=0;f<nInsideFactors_;++f){
133 if(dirtyFactors[insideFactors_[f]] == 2) {
136 dirtyFactors[insideFactors_[f]] = 0;
146 for(IndexType f=0;f<nInsideFactors_;++f){
147 if(dirtyFactors[insideFactors_[f]] == 2) {
150 dirtyFactors[insideFactors_[f]] = 0;
153 for(IndexType f=0;f<nBorderFactors_;++f){
154 if(dirtyFactors[borderFactor_[f]] == 2) {
157 dirtyFactors[borderFactor_[f]] = 1;
165 for(IndexType f=0;f<nInsideFactors_;++f){
166 if(dirtyFactors[insideFactors_[f]] == 2) {
169 dirtyFactors[insideFactors_[f]] = 1;
171 for(IndexType f=0;f<nBorderFactors_;++f){
172 if(dirtyFactors[borderFactor_[f]] == 2) {
175 dirtyFactors[borderFactor_[f]] = 1;
211 void setSubVarImplicit(
const ARG & arg,
const LabelType color,
const IndexType vi);
213 void setSubVarImplicitBfs(
const ARG & arg,
const LabelType color,
const IndexType vi);
220 void setSubVarImplicit(
const ARG & arg,
221 const LabelType color0,
const LabelType color1,
222 const IndexType vi0,
const IndexType vi1
225 void setSubVarImplicitBfs(
const ARG & arg,
226 const LabelType color0,
const LabelType color1,
227 const IndexType vi0,
const IndexType vi1
234 IndexType ccFromLocalArg(
const IndexType offset);
236 void getEmbeddingGraph();
237 void freeEmbeddingGraph();
240 void setUpSubFactors();
248 IndexVector subVarToGlobal_;
249 IndexVector globalVarToLocal_;
252 PseudoBoolVector incluedGlobalVar_;
253 BoolVector incluedGlobalFactors_;
254 BoolVector isABorderFactor_;
258 LabelVector localArg_;
259 LabelVector localArgTest_;
265 ValueVector globalLambdas_;
266 DoubleVector localLambdas_;
270 IndexVector insideFactors_;
271 IndexVector borderFactor_;
272 IndexType nInsideFactors_;
273 IndexType nBorderFactors_;
276 IndexType numSubVar_;
277 IndexType numSubFactors_;
280 EdgeMap localEdgemap_;
283 IndexType maxBruteForceSize2_;
284 IndexType maxBruteForceSize4_;
287 ValueType oldCutValue_;
292 std::vector<opengm::RandomAccessSet<IndexType> > visAdj_;
294 std::vector<IndexType> stack_;
320 this->setSubVarImplicitBfs(globalArg,colorCC0,colorCC1,viCC0,viCC1);
323 this->setSubVarImplicit(globalArg,colorCC0,colorCC1,viCC0,viCC1);
330 this->setUpSubFactors();
332 if(
false && isOptCut_==
true){
338 if(numSubVar_<= maxBruteForceSize2_ && planar==
true){
340 if(numSubVar_<maxBruteForceSize4_ && numSubVar_>=3)
341 valueOfArg=this->inferBruteForce4();
343 valueOfArg=this->inferBruteForce2();
345 else if(planar==
true){
350 valueOfArg = this->inferPlanarMaxCut();
355 valueOfArg = this->inferQPBOI(TwoSubsets);
359 if(oldCutValue_<valueOfArg){
366 else if(valueOfArg+0.0000001<oldCutValue_){
383 IndexType numCC = this->ccFromLocalArg(offset);
387 std::vector<IndexType> exampleForCC(numCC);
390 for(
IndexType localVi=0;localVi<numSubVar_;++localVi){
391 const LabelType ccLabel = localArg_[localVi];
392 const IndexType globalVi = subVarToGlobal_[localVi];
394 exampleForCC[ccLabel-offset]=globalVi;
395 globalArg[globalVi]=ccLabel;
400 return IVPairType(numCC,valueOfArg-oldCutValue_);
421 this->setSubVarImplicitBfs(globalArg,colorCC,viCC);
424 this->setSubVarImplicit(globalArg,colorCC,viCC);
428 std::cout << format(
" inferSubset with %d variables") % numSubVar_ << std::endl;
436 this->setUpSubFactors();
440 if(numSubVar_<= maxBruteForceSize2_){
441 if(numSubVar_<maxBruteForceSize4_ && numSubVar_>=3)
442 valueOfArg=this->inferBruteForce4();
444 valueOfArg=this->inferBruteForce2();
451 valueOfArg = this->inferPlanarMaxCut();
456 valueOfArg=this->inferQPBOI(SingleSubset);
461 IndexType numCC = this->ccFromLocalArg(offset);
465 std::vector<IndexType> exampleForCC(numCC);
469 for(
IndexType localVi=0;localVi<numSubVar_;++localVi){
470 const LabelType ccLabel = localArg_[localVi];
471 const IndexType globalVi = subVarToGlobal_[localVi];
473 exampleForCC[ccLabel-offset]=globalVi;
474 globalArg[globalVi]=ccLabel;
478 for(
size_t i=0;i<exampleForCC.size();++i){
479 deque.push_back(exampleForCC[i]);
499 for(
IndexType viGlobal=0;viGlobal<gm_.numberOfVariables();++viGlobal){
500 if(arg[viGlobal]==ccColor){
501 subVarToGlobal_[viLocal]=viGlobal;
502 globalVarToLocal_[viGlobal]=viLocal;
503 OPENGM_CHECK_OP(incluedGlobalVar_[viGlobal],==,0,
"internal inconsistency");
504 incluedGlobalVar_[viGlobal]=1;
520 for(
IndexType viGlobal=0;viGlobal<gm_.numberOfVariables();++viGlobal){
521 if(arg[viGlobal]==ccColor){
522 subVarToGlobal_[viLocal]=viGlobal;
523 globalVarToLocal_[viGlobal]=viLocal;
524 OPENGM_CHECK_OP(incluedGlobalVar_[viGlobal],==,0,
"internal inconsistency");
525 incluedGlobalVar_[viGlobal]=1;
545 for(
IndexType viGlobal=0;viGlobal<gm_.numberOfVariables();++viGlobal){
548 if(cVi==ccColor0 || cVi==ccColor1){
549 subVarToGlobal_[viLocal]=viGlobal;
550 globalVarToLocal_[viGlobal]=viLocal;
551 OPENGM_CHECK_OP(incluedGlobalVar_[viGlobal],==,0,
"internal inconsistency");
552 incluedGlobalVar_[viGlobal] = cVi==ccColor0 ? 1 : 2;
584 subVarToGlobal_[0]=vi0;
585 subVarToGlobal_[1]=vi1;
586 incluedGlobalVar_[vi0]=1;
587 incluedGlobalVar_[vi1]=2;
595 if(incluedGlobalVar_[vi]==0){
596 incluedGlobalVar_[vi]= (arg[vi]==ccColor0 ? 1 : 2);
598 subVarToGlobal_[numSubVar_]=vi;
602 for(
IndexType n=0;n<visAdj_[vi].size();++n){
606 if(incluedGlobalVar_[nvi]==0 && (cvi==ccColor0 || cvi==ccColor1)){
608 incluedGlobalVar_[nvi]= (arg[nvi]==ccColor0 ? 1 : 2);
610 subVarToGlobal_[numSubVar_]=nvi;
624 std::sort(subVarToGlobal_.begin(),subVarToGlobal_.begin()+numSubVar_);
628 for(
IndexType lvi=0;lvi<numSubVar_;++lvi){
629 const IndexType gvi=subVarToGlobal_[lvi];
630 globalVarToLocal_[gvi]=lvi;
645 subVarToGlobal_(gm.numberOfVariables()),
646 globalVarToLocal_(gm.numberOfVariables()),
647 incluedGlobalVar_(gm.numberOfVariables(),false),
648 incluedGlobalFactors_(gm.numberOfFactors(),false),
649 isABorderFactor_(gm.numberOfFactors(),false),
650 localArg_(gm.numberOfVariables()),
651 localArgTest_(gm.numberOfVariables()),
653 globalLambdas_(gm.numberOfFactors()),
654 localLambdas_(gm.numberOfFactors()),
655 insideFactors_(gm.numberOfFactors()),
656 borderFactor_(gm.numberOfFactors()),
662 maxBruteForceSize2_(0),
663 maxBruteForceSize4_(0)
665 gm_.variableAdjacencyList(visAdj_);
666 stack_.resize(gm_.numberOfVariables()*2);
668 std::fill(incluedGlobalVar_.begin(),incluedGlobalVar_.end(),
false);
670 IndexType shape[2]={gm.numberOfFactors(),3};
671 localFactorVis_.
resize(shape,shape+2);
675 for(
IndexType fi=0;fi<gm_.numberOfFactors();++fi){
676 globalLambdas_[fi]=gm[fi].operator()(lAB)-gm[fi].
operator()(lAA);
685 const IndexType offset
689 for(IndexType f=0;f<numSubFactors_;++f){
690 const IndexType sv0=localFactorVis_(f,0);
691 const IndexType sv1=localFactorVis_(f,1);
692 if( localArg_[sv0] == localArg_[sv1]){
699 const IndexType numberOfCCs=ufd.numberOfSets();
700 std::map<IndexType,IndexType> repLabeling;
701 ufd.representativeLabeling(repLabeling);
703 for(IndexType subVi=0;subVi<numSubVar_;++subVi){
704 const IndexType findSubVi = ufd.find(subVi);
705 const IndexType denseLabel = repLabeling[findSubVi];
706 localArg_[subVi]=denseLabel + offset;
720 typename Multicut::Parameter para;
721 para.workFlow_=
"(IC)(CC-I)";
722 Multicut mc(numSubVar_,numSubFactors_,localFactorVis_,localLambdas_,para);
724 if(mode == SingleSubset) {
725 std::cout <<
" [SS] MC with #var=" << numSubVar_ <<
", #factors=" << numSubFactors_ << std::flush;
728 std::cout <<
" [TS] MC with #var=" << numSubVar_ <<
", #factors=" << numSubFactors_ << std::flush;
732 std::vector<IndexType> mcarg;
739 std::cout <<
" ... " << std::fixed << 1000.0*e <<
" msec." << std::endl;
744 std::copy(mcarg.begin(),mcarg.end(),localArg_.begin());
750 for(
IndexType fiLocal=0;fiLocal<numSubFactors_;++fiLocal){
751 const IndexType localVi0 = localFactorVis_(fiLocal,0);
752 const IndexType localVi1 = localFactorVis_(fiLocal,1);
753 if(localArg_[localVi0]!=localArg_[localVi1])
754 value+=localLambdas_[fiLocal];
769 typedef typename kolmogorov::qpbo::QPBO<REAL>::NodeId NodeId;
770 typedef typename kolmogorov::qpbo::QPBO<REAL>::EdgeId EdgeId;
771 typedef typename kolmogorov::qpbo::QPBO<REAL>::ProbeOptions ProbeOptions;
773 kolmogorov::qpbo::QPBO<REAL>* qpbo =
new kolmogorov::qpbo::QPBO<REAL>(numSubVar_, numSubFactors_);
774 qpbo->AddNode(numSubVar_);
775 qpbo->AddUnaryTerm(0, 0.0, 10000000.0);
776 for(
size_t i=0; i<numSubFactors_; ++i){
777 qpbo->AddPairwiseTerm( (NodeId)localFactorVis_(i,0), (NodeId)localFactorVis_(i,1), (REAL)0.0, (REAL)localLambdas_[i],(REAL)localLambdas_[i],(REAL)0.0 );
779 qpbo->MergeParallelEdges();
780 for(
size_t i=0; i < numSubVar_ ; ++i)
781 qpbo->SetLabel(i, 0);
787 for (
size_t i=0; i < numSubVar_ ; ++i ) {
788 localArg_[i] = qpbo->GetLabel(i);
792 for(
IndexType fiLocal=0;fiLocal<numSubFactors_;++fiLocal){
793 const IndexType localVi0 = localFactorVis_(fiLocal,0);
794 const IndexType localVi1 = localFactorVis_(fiLocal,1);
795 if(localArg_[localVi0]!=localArg_[localVi1])
796 value+=localLambdas_[fiLocal];
813 #if defined(WITH_BLOSSOM5) && defined(WITH_PLANARITY)
814 opengm::external::PlanarMaxCut solver(numSubVar_, numSubFactors_);
815 for(
size_t i=0; i<numSubFactors_; ++i){
816 solver.addEdge(localFactorVis_(i,0),localFactorVis_(i,1), -1.0*localLambdas_[i]);
818 solver.calculateCut();
819 solver.getLabeling(localArg_);
824 const IndexType localVi0 = localFactorVis_(i,0);
825 const IndexType localVi1 = localFactorVis_(i,1);
826 if(localArg_[localVi0]!=localArg_[localVi1])
827 value+=localLambdas_[i];
840 OPENGM_CHECK_OP(numSubVar_,<=,64,
"too many variables for brute force");
846 std::fill(localArgTest_.begin(),localArgTest_.begin()+numSubVar_,0);
847 std::fill(localArg_.begin() ,localArg_.begin()+numSubVar_,0);
857 if(localArgTest_[localFactorVis_(f,0)]!=localArgTest_[localFactorVis_(f,1)])
858 newVal+=localLambdas_[f];
863 std::copy(localArgTest_.begin(),localArgTest_.begin()+numSubVar_,localArg_.begin());
871 OPENGM_CHECK_OP(numSubVar_,<=,15,
"to many variables for brute force 4");
877 std::fill(localArgTest_.begin(),localArgTest_.begin()+numSubVar_,0);
878 std::fill(localArg_.begin() ,localArg_.begin()+numSubVar_,0);
887 localArgTest_[j+1] = c;
893 if(localArgTest_[localFactorVis_(f,0)]!=localArgTest_[localFactorVis_(f,1)])
894 newVal+=localLambdas_[f];
899 std::copy(localArgTest_.begin(),localArgTest_.begin()+numSubVar_,localArg_.begin());
920 for(IndexType viLocal=0;viLocal<numSubVar_;++viLocal){
921 const IndexType viGlobal=subVarToGlobal_[viLocal];
922 const IndexType numFacVar = gm_.numberOfFactors(viGlobal);
923 for(IndexType f=0; f<numFacVar; ++f){
924 const IndexType fiGlobal=gm_.factorOfVariable(viGlobal,f);
926 if(incluedGlobalFactors_[fiGlobal]==
false){
927 const IndexType viA = gm_.variableOfFactor(fiGlobal,0);
928 const IndexType viB = gm_.variableOfFactor(fiGlobal,1);
929 const IndexType viGlobalOther = viA==viGlobal ? viB : viA;
931 const IndexType viGlobal0 = viGlobal<viGlobalOther ? viGlobal : viGlobalOther;
932 const IndexType viGlobal1 = viGlobal<viGlobalOther ? viGlobalOther : viGlobal;
936 if(incluedGlobalVar_[viGlobalOther]!=0){
939 insideFactors_[nInsideFactors_]=fiGlobal;
944 incluedGlobalFactors_[fiGlobal]=
true;
946 localFactorVis_(numSubFactors_,0)=globalVarToLocal_[viGlobal0];
947 localFactorVis_(numSubFactors_,1)=globalVarToLocal_[viGlobal1];
948 localFactorVis_(numSubFactors_,2)=fiGlobal;
950 const ValueType lamb=globalLambdas_[fiGlobal];
951 if(incluedGlobalVar_[viGlobalOther]!=incluedGlobalVar_[viGlobal]){
958 localLambdas_[numSubFactors_]=lamb;
964 borderFactor_[nBorderFactors_]=fiGlobal;
980 for(IndexType viLocal=0;viLocal<numSubVar_;++viLocal){
981 const IndexType viGlobal=subVarToGlobal_[viLocal];
982 OPENGM_CHECK(incluedGlobalVar_[viGlobal]>0,
"internal inconsistency");
983 incluedGlobalVar_[viGlobal]=0;
985 for(IndexType fiLocal=0;fiLocal<numSubFactors_;++fiLocal){
986 OPENGM_CHECK(incluedGlobalFactors_[localFactorVis_(fiLocal,2)],
"internal inconsistency");
987 incluedGlobalFactors_[localFactorVis_(fiLocal,2)]=
false;
995 localEdgemap_.clear();
std::pair< size_t, size_t > Edge
std::vector< double > DoubleVector
SubmodelCGC(const GM &gm, const IndexType maxBruteForceSize2, const IndexType maxBruteForceSize4, const bool useBfs)
ValueType inferBruteForce2()
Platform-independent runtime measurements.
void cleanInsideAndBorder()
std::vector< IndexType > IndexVector
detail_types::UInt64Type UInt64Type
uint64
ValueType inferPlanarMaxCut()
std::vector< bool > BoolVector
boost::unordered_map< Edge, size_t > EdgeMap
std::pair< Edge, size_t > EdgeMapEntry
double elapsedTime() const
ValueType inferQPBOI(Mode mode)
void updateDirtyness(std::vector< unsigned char > &dirtyFactors, const bool changes)
IVPairType inferSubset(ARG &globalArg, const LabelType colorCC, const IndexType viCC, IndexType offset, std::deque< IndexType > &deque, const bool planar, bool verbose=false)
#define OPENGM_CHECK_OP(A, OP, B, TXT)
ValueType inferMulticut(Mode mode)
Disjoint set data structure with path compression.
std::pair< int, ValueType > IVPairType
IVPairType infer2Subsets(ARG &globalArg, const LabelType colorCC0, const LabelType colorCC1, const IndexType viCC0, const IndexType viCC1, IndexType offset, const bool planar=true)
#define OPENGM_CHECK(B, TXT)
ValueType inferBruteForce4()
std::vector< LabelType > LabelVector
std::vector< ValueType > ValueVector
std::vector< unsigned char > PseudoBoolVector
void resize(ShapeIterator, ShapeIterator, const T &=T())
Resize (existing entries are preserved, new entries are initialized).