/usr/include/opengm/unittests/blackboxtester.hxx is in libopengm-dev 2.3.6+20160905-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | #pragma once
#ifndef OPENGM_TEST_INFERENCE_BLACKBOXTESTER_HXX
#define OPENGM_TEST_INFERENCE_BLACKBOXTESTER_HXX
#ifdef OPENGM_TESTFILE
#define OPENGM_TESTFILE_FILENAME "/tmp/model.h5"
#include <opengm/graphicalmodel/graphicalmodel_hdf5.hxx>
#endif
#include <vector>
#include <typeinfo>
#include <opengm/opengm.hxx>
#include <opengm/unittests/test.hxx>
#include <opengm/inference/bruteforce.hxx>
#include <opengm/unittests/blackboxtests/blackboxtestbase.hxx>
#include <opengm/operations/minimizer.hxx>
#include <opengm/operations/maximizer.hxx>
#include <opengm/operations/integrator.hxx>
/// \cond HIDDEN_SYMBOLS
namespace opengm {
template<class GM>
class InferenceBlackBoxTester
{
public:
typedef GM GraphicalModelType;
template<class INF> void test(const typename INF::Parameter&, bool tValue=true, bool tArg=false, bool tMarg=false, bool tFacMarg=false);
void addTest(BlackBoxTestBase<GraphicalModelType>*);
~InferenceBlackBoxTester();
private:
std::vector<BlackBoxTestBase<GraphicalModelType>*> testList;
};
//***************
//IMPLEMENTATION
//***************
template<class GM>
InferenceBlackBoxTester<GM>::~InferenceBlackBoxTester()
{
for(size_t testId = 0; testId < testList.size(); ++testId) {
delete testList[testId];
}
}
template<class GM>
template<class INF>
void InferenceBlackBoxTester<GM>::test(const typename INF::Parameter& infPara, bool tValue, bool tArg, bool tMarg, bool tFacMarg)
{
typedef typename GraphicalModelType::ValueType ValueType;
typedef typename GraphicalModelType::OperatorType OperatorType;
typedef typename INF::AccumulationType AccType;
for(size_t testId = 0; testId < testList.size(); ++testId) {
size_t numTests = testList[testId]->numberOfTests();
BlackBoxBehaviour behaviour = testList[testId]->behaviour();
std::cout << testList[testId]->infoText();
std::cout << " " << std::flush;
for(size_t n = 0; n < numTests; ++n) {
std::cout << "*" << std::flush;
GraphicalModelType gm = testList[testId]->getModel(n);
#ifdef OPENGM_TESTFILE
std::cout<< "save test-file" << std::endl;
opengm::hdf5::save(gm,OPENGM_TESTFILE_FILENAME,"gm");
#endif
//Run Algorithm
bool exceptionFlag = false;
std::vector<typename GM::LabelType> state;
try{
INF inf(gm, infPara);
InferenceTermination returnValue=inf.infer();
OPENGM_TEST((returnValue==opengm::NORMAL) || (returnValue==opengm::CONVERGENCE));
if(typeid(AccType) == typeid(opengm::Minimizer) || typeid(AccType) == typeid(opengm::Maximizer)) {
OPENGM_TEST(inf.arg(state)==opengm::NORMAL);
OPENGM_TEST(state.size()==gm.numberOfVariables());
for(size_t varId = 0; varId < gm.numberOfVariables(); ++varId) {
OPENGM_TEST(state[varId]<gm.numberOfLabels(varId));
}
{
ValueType bound = inf.bound();
ValueType value = inf.value();
ValueType value2 = 0;
if(typeid(AccType) == typeid(opengm::Minimizer))
value2 = value + std::min<ValueType>(1e20,std::max<ValueType>(1e-4,fabs(value)))*1e-6;
if(typeid(AccType) == typeid(opengm::Maximizer))
value2 = value - std::min<ValueType>(1e20,std::max<ValueType>(1e-4,fabs(value)))*1e-6;
std::cout << "value = " << value << " , bound = " << bound << std::endl;
OPENGM_TEST(AccType::bop(bound,value2)|| bound==value2);
}
if(behaviour == opengm::OPTIMAL) {
std::vector<typename GM::LabelType> optimalState;
opengm::Bruteforce<GraphicalModelType, AccType> bf(gm);
OPENGM_TEST(bf.infer()==opengm::NORMAL);
OPENGM_TEST(bf.arg(optimalState)==opengm::NORMAL);
OPENGM_TEST(optimalState.size()==gm.numberOfVariables());
for(size_t i = 0; i < gm.numberOfVariables(); ++i) {
OPENGM_TEST(optimalState[i]<gm.numberOfLabels(i));
}
OPENGM_TEST_EQUAL_TOLERANCE(gm.evaluate(state), gm.evaluate(optimalState), 0.00001);
//testEqualSequence(states1.begin(), states1.end(), states2.begin());
}
}
if(tMarg){
typename GM::IndependentFactorType out;
for(size_t varId = 0; varId < gm.numberOfVariables(); ++varId) {
OPENGM_TEST(inf.marginal(varId,out)==opengm::NORMAL);
}
}
if(tFacMarg){
typename GM::IndependentFactorType out;
for(size_t factorId = 0; factorId < gm.numberOfFactors(); ++factorId) {
OPENGM_TEST(inf.factorMarginal(factorId,out)==opengm::NORMAL);
}
}
} catch(std::exception& e) {
exceptionFlag = true;
std::cout << e.what() <<std::endl;
}
if(behaviour == opengm::FAIL) {
OPENGM_TEST(exceptionFlag);
}else{
OPENGM_TEST(!exceptionFlag);
}
}
if(behaviour == opengm::OPTIMAL) {
std::cout << " OPTIMAL!" << std::endl;
}else if(behaviour == opengm::PASS) {
std::cout << " PASS!" << std::endl;
}else{
std::cout << " OK!" << std::endl;
}
}
}
template<class GM>
void InferenceBlackBoxTester<GM>::addTest(BlackBoxTestBase<GraphicalModelType>* test)
{
testList.push_back(test);
}
/*
template<class GM, class INF>
void InferenceBlackBoxTest::test
(
const INF::Parameter& infPara,
const GM& gm,
const BlackBoxBehaviour behaviour) const
{
typedef GM GraphicalModelType;
typedef INF InfType;
typedef typename GraphicalModelType::ExplicitFunctionType ExplicitFunctionType;
typedef typename GraphicalModelType::SparseFunctionType SparseFunctionType;
typedef typename GraphicalModelType::ImplicitFunctionType ImplicitFunctionType;
typedef typename GraphicalModelType::FunctionIdentifier FunctionIdentifier;
typedef typename GraphicalModelType::ValueType ValueType;
typedef typename GraphicalModelType::OperatorType OperatorType;
typedef typename InfType::AccumulationType AccType;
typedef typename InfType::Parameter InfParaType;
//Run Algorithm
bool exceptionFlag = false;
std::vector<size_t> state;
try{
InfType inf(gm, infPara);
OPENGM_TEST(inf.infer()==opengm::NORMAL);
if(typeid(AccType) == typeid(opengm::Minimizer) || typeid(AccType) == typeid(opengm::Maximizer)) {
OPENGM_TEST(inf.arg(state)==opengm::NORMAL);
OPENGM_TEST(state.size()==gm.numberOfVariabl {
std::string str;
str = " - 2nd order grid model (" + height_ + "x" + width_;
str += ", " + varStates_ ? "~" : "" + numStates_ + ") ";
str += withUnary_ ? "with unary" : "without unary and " + functionString(function_);
return str;
}es());
for(size_t i = 0; i < gm.numberOfVariables(); ++i) {
OPENGM_TEST(state[i]<gm.numberOfLabels(i));
}
{
const ValueType value = gm.evaluate(state);
const ValueType bound = inf.bound();
OPENGM_TEST(AccType::bop(value,bound));
}
}
if(typeid(AccType) == typeid(opengm::Integrator)) {
for(size_t varId = 0; varId < gm.numberOfVariables; ++varId) {
OPENGM_TEST(inf.marginal(varId)==opengm::NORMAL);
}
for(size_t factorId = 0; factorId < gm.numberOfFactors; ++factorId) {
OPENGM_TEST(inf.factorMarginal(factorId)==opengm::NORMAL);
}
}
} catch(std::exception& e) {
exceptionFlag = true;
}
if(behaviour == FAIL) {
OPENGM_ASSERT(exceptionFlag);
}else{
OPENGM_ASSERT(!exceptionFlag);
}
if(behaviour == OPTIMAL) {
std::vector<size_t> optimalState;
opengm::Bruteforce<GraphicalModelType, AccType> bf(gm);
OPENGM_TEST(bf.infer()==opengm::NORMAL);
OPENGM_TEST(bf.arg(optimalState)==opengm::NORMAL);
OPENGM_TEST(optimalState.size()==gm.numberOfVariables());
for(size_t i = 0; i < gm.numberOfVariables(); ++i)
OPENGM_TEST(optimalState[i]<gm.numberOfLabels(i));
OPENGM_TEST_EQUAL_TOLERANCE(gm.evaluate(state), gm.evaluate(optimalState), 0.00001);
//testEqualSequence(states1.begin(), states1.end(), states2.begin());
}
}
template<class GM, class INF>
void InferenceBlackBoxTest::
test(
const INF::Parameter& para,
const BlackBoxTest test,
const BlackBoxFunction function,
const BlackBoxVar var,
const BlackBoxBehaviour behaviour,
const size_t orders) const
{
opengm::SyntheticModelGenerator2 modelGenerator;
opengm::SyntheticModelGenerator2::Parameter modelGeneratorPara;
size_t order = (size_t) log2((double) (orders)) + 1;
modelGeneratorPara.functionParameters_.resize(order);
modelGeneratorPara.functionTypes_.resize(order, opengm::SyntheticModelGenerator2::URANDOM);
modelGeneratorPara.sharedFunctions_.resize(order, false);
size_t tester = 1;
for(size_t i = 0; i < order; ++i) {
if(orders & tester == 0) {
modelGeneratorPara.functionTypes_[i] = opengm::SyntheticModelGenerator2::EMPTY;
}else{
if(i > 0) {
modelGeneratorPara.functionTypes_[i] = opengm::SyntheticModelGenerator2::URANDOM;
}
}
tester *= 2;
}
if(test == GRID)
numberOfVariables_ = height_ * width_;
std::vector<size_t> numberOfLabels;
switch (var) {
case RANDOM:
numberOfLabels.resize(numberOfVariables_);
for(size_t i = 0; i < numberOfLabels.size(); ++i) {
numberOfLabels[i] = rand() % numberOfStates_ + 1;
}
break;
case FIX:
numberOfLabels.resize(numberOfVariables_, numberOfStates_);
break;
case BINARY:
numberOfLabels.resize(numberOfVariables_, 2);
break;
}
switch (test) {
case STAR:
GraphicalModelType gm = modelGenerator.buildStar(numberOfVariables_, numberOfLabels, modelGeneratorPara);
break;
case TREE:
break;
case GRID:
GraphicalModelType gm = modelGenerator.buildGrid(height_, width_, numberOfLabels, modelGeneratorPara);
break;
case FULL:
GraphicalModelType gm = modelGenerator.buildFull(numberOfVariables_, numberOfLabels, modelGeneratorPara);
break;
}
}
*/
}
/// \endcond
#endif // #ifndef OPENGM_TEST_INFERENCE_BLACKBOXTESTER_HXX
|