I started using QPBO, published by Vladimir Kolmogorov, for solving discrete optimization problem. QPBO.h contains a sample code, which I couldn't understand at first moment. Since my colleague told me what the code means, I wrote a bit modified sample code as well as CMakeLists.txt.
main.cpp [code lang="c"] //Example usage: minimize energy E(x,y) = 2x + 3(y+1) + (x+1)*(y+2), where x,y \in {0,1}.
include
include "QPBO.h"
int UnaryX(const int x) { return 2*x; }
int UnaryY(const int y) { return 3*(y+1); }
int Pairwise( const int x, const int y ) { return (x+1)*(y+2); }
int main() { typedef int REAL; QPBO* q;
q = new QPBO(2, 1); // max number of nodes & edges q->AddNode(2); // add two nodes
// add term 2*x q->AddUnaryTerm( 0, UnaryX(0), UnaryX(1) );
// add term 3*(y+1) q->AddUnaryTerm( 1, UnaryY(0), UnaryY(1) );
// add term (x+1)*(y+2) q->AddPairwiseTerm( 0, 1, Pairwise(0, 0), Pairwise(0, 1), Pairwise(1, 0), Pairwise(1, 1) );
q->Solve(); q->ComputeWeakPersistencies();
int x = q->GetLabel(0); int y = q->GetLabel(1); std::cout << "Solution: "; std::cout << " x = " << x; std::cout << " y = " << y << std::endl;
return 0; } [/code]
CMakeLists.txt [code lang="c"] set(PROJ_NAME testQPBO) project(PROJ_NAME)
cmake_minimum_required(VERSION 2.8)
set(QPBO_DIR "../QPBO-v1.3.src/" CACHE STRING "directory storing QPBO sources/headers") include_directories(${QPBO_DIR}) set(QPBO_SRC ${QPBO_DIR}/QPBO.cpp ${QPBO_DIR}/QPBO_extra.cpp ${QPBO_DIR}/QPBO_maxflow.cpp ${QPBO_DIR}/QPBO_postprocessing.cpp ) set(PROJ_SRC main.cpp ) add_executable(${PROJ_NAME} ${QPBO_SRC} ${PROJ_SRC}) [/code]