quick_start.cxx
#include <iostream>
#include <opengm/opengm.hxx>
#include <opengm/graphicalmodel/graphicalmodel.hxx>
#include <opengm/operations/minimizer.hxx>
#include <opengm/operations/adder.hxx>
#include <opengm/inference/icm.hxx>
using namespace std;
int main(int argc, char** argv) {
typedef opengm::GraphicalModel<float, opengm::Adder> Model;
size_t numbersOfLabels[] = {5, 5, 2, 2, 10};
Model gm(opengm::DiscreteSpace<>(numbersOfLabels, numbersOfLabels + 5));
typedef opengm::ExplicitFunction<float> ExplicitFunction;
typedef Model::FunctionIdentifier FunctionIdentifier;
for(size_t variable = 0; variable < gm.numberOfVariables(); ++variable) {
const size_t shape[] = {gm.numberOfLabels(variable)};
ExplicitFunction f(shape, shape + 1);
for(size_t state = 0; state < gm.numberOfLabels(variable); ++state) {
f(state) = float(rand()) / RAND_MAX;
}
FunctionIdentifier id = gm.addFunction(f);
size_t variableIndex[] = {variable};
gm.addFactor(id, variableIndex, variableIndex + 1);
}
for(size_t variable1 = 0; variable1 < gm.numberOfVariables(); ++variable1)
for(size_t variable2 = variable1 + 1; variable2 < gm.numberOfVariables(); ++variable2)
for(size_t variable3 = variable2 + 1; variable3 < gm.numberOfVariables(); ++variable3) {
const size_t shape[] = {
gm.numberOfLabels(variable1),
gm.numberOfLabels(variable2),
gm.numberOfLabels(variable3)
};
ExplicitFunction f(shape, shape + 3);
for(size_t state1 = 0; state1 < gm.numberOfLabels(variable1); ++state1)
for(size_t state2 = 0; state2 < gm.numberOfLabels(variable2); ++state2)
for(size_t state3 = 0; state3 < gm.numberOfLabels(variable3); ++state3) {
f(state1, state2, state3) = float(rand()) / RAND_MAX;
}
FunctionIdentifier id = gm.addFunction(f);
size_t variableIndexSequence[] = {variable1, variable2, variable3};
gm.addFactor(id, variableIndexSequence, variableIndexSequence + 3);
}
typedef opengm::ICM<Model, opengm::Minimizer> IcmType;
typedef IcmType::VerboseVisitorType VerboseVisitorType;
IcmType icm(gm);
VerboseVisitorType verboseVisitor;
icm.infer(verboseVisitor);
vector<size_t> argmin;
icm.arg(argmin);
for(size_t variable = 0; variable < gm.numberOfVariables(); ++variable) {
cout << "x" << variable << "=" << argmin[variable] << "\n";
}
}