#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
template<class T>
size_t shape[]={nrOfVariables, nrOfVariables};
cout << "pariwise costs:" << endl;
srand(0);
for(
size_t v=0; v<data.
shape(0); ++v) {
for(
size_t s=0; s<data.
shape(0); ++s) {
data(v, s) = static_cast<float>(rand() % 100) * 0.01;
cout << left << setw(6) << setprecision(2) << data(v, s);
}
cout << endl;
}
}
void printSolution(const vector<size_t>& solution) {
set<size_t> unique;
cout << endl << "Solution Labels :" << endl;
for(size_t v=0;v<solution.size();++v) {
cout << left << setw(2) << v << " -> " << solution[v] << endl;
}
}
int main() {
const size_t nrOfVariables = 5;
const size_t nrOfLabels = nrOfVariables;
float high = 20;
cout << endl << "Matching with one to one correspondences:" << endl
<< nrOfVariables << " variables with "
<< nrOfLabels <<" labels" << endl << endl;
createAndPrintData(nrOfVariables, data);
OPENGM_TYPELIST_2(PottsFunction, ExplicitFunction),
> Model;
typedef Model::FunctionIdentifier FunctionIdentifier;
{
const size_t shape[] = {nrOfLabels};
ExplicitFunction f(shape, shape+1);
for(size_t v=0; v<nrOfVariables; ++v) {
for(size_t s=0; s<nrOfLabels; ++s) {
f(s) = 1.0f-data(v, s);
}
FunctionIdentifier id = gm.addFunction(f);
size_t vi[] = {v};
gm.addFactor(id, vi, vi+1);
}
}
{
PottsFunction f(nrOfLabels, nrOfLabels, high, 0);
FunctionIdentifier id = gm.addFunction(f);
for(size_t v1=0;v1<nrOfVariables;++v1)
for(size_t v2=v1+1;v2<nrOfVariables;++v2) {
size_t vi[] = {v1, v2};
gm.addFactor(id, vi, vi+2);
}
}
AstarType astar(gm);
AstarType::VerboseVisitorType verboseVisitor;
cout << "\nA-star search:\n";
astar.infer(verboseVisitor);
vector<size_t> argmin;
astar.arg(argmin);
printSolution(argmin);
}