a bit modified sample code using QPBO

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

//Example usage: minimize energy E(x,y) = 2*x + 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;
}

CMakeLists.txt

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})
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s