Advertisement
Guest User

Untitled

a guest
May 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. #include <opengm/opengm.hxx>
  5. #include <opengm/graphicalmodel/graphicalmodel.hxx>
  6. #include <opengm/operations/adder.hxx>
  7. #include <opengm/functions/potts.hxx>
  8.  
  9. #include <opengm/inference/icm.hxx>
  10. #include <opengm/inference/messagepassing/messagepassing.hxx>
  11. #include <opengm/inference/trws/trws_trws.hxx>
  12. #include <opengm/inference/trws/smooth_nesterov.hxx>
  13.  
  14. //*******************
  15. //** Typedefs
  16. //*******************
  17. typedef double ValueType; // type used for values
  18. typedef size_t IndexType; // type used for indexing nodes and factors (default : size_t)
  19. typedef size_t LabelType; // type used for labels (default : size_t)
  20. typedef opengm::Multiplier OpType; // operation used to combine terms
  21. typedef opengm::ExplicitFunction<ValueType,IndexType,LabelType> ExplicitFunction; // shortcut for explicite function
  22. typedef opengm::PottsFunction<ValueType,IndexType,LabelType> PottsFunction; // shortcut for Potts function
  23. typedef opengm::meta::TypeListGenerator<ExplicitFunction,PottsFunction>::type FunctionTypeList; // list of all function the model cal use (this trick avoids virtual methods) - here only one
  24. typedef opengm::DiscreteSpace<IndexType, LabelType> SpaceType; // type used to define the feasible statespace
  25. typedef opengm::GraphicalModel<ValueType,OpType,FunctionTypeList,SpaceType> Model; // type of the model
  26. typedef Model::FunctionIdentifier FunctionIdentifier; // type of the function identifier
  27.  
  28. //Build a model that corresponds to x_1 ... x_N being drawn IID from the uniform distribution on M labels
  29. Model buildIID(size_t N, size_t M)
  30. {
  31. LabelType numLabel = M;
  32. std::vector<LabelType> numbersOfLabels(N,numLabel);
  33. Model gm(SpaceType(numbersOfLabels.begin(), numbersOfLabels.end()));
  34. // Add 1st order functions and factors to the model
  35. // Univariate factors are constant
  36. for(IndexType variable = 0; variable < gm.numberOfVariables(); ++variable) {
  37. // construct 1st order function
  38. const LabelType shape[] = {gm.numberOfLabels(variable)};
  39. ExplicitFunction f(shape, shape + 1);
  40. for (size_t i=0; i<numLabel; ++i)
  41. f(i) = 2;
  42. // f(i) = static_cast<ValueType>(rand()) / (RAND_MAX) * 1000.0;
  43. FunctionIdentifier id = gm.addFunction(f);
  44. IndexType variableIndex[] = {variable};
  45. gm.addFactor(id, variableIndex, variableIndex + 1);
  46. }
  47. // two-variable factors are constant
  48. for(IndexType variable = 0; variable < gm.numberOfVariables()-1; ++variable) {
  49. // construct 1st order function
  50. const IndexType vars[] = {variable,variable+1};
  51. const LabelType shape[] = {gm.numberOfLabels(variable),gm.numberOfLabels(variable+1)};
  52. ExplicitFunction f(shape, shape + 2);
  53. for (size_t i=0; i<numLabel; ++i)
  54. for (size_t j=0; j<numLabel; ++j)
  55. f(i,j) = 1;
  56. FunctionIdentifier id = gm.addFunction(f);
  57. gm.addFactor(id, vars, vars + 2);
  58. }
  59.  
  60. return gm;
  61. }
  62.  
  63.  
  64. void inferBP(const Model& gm, bool normalization = true){
  65. typedef opengm::BeliefPropagationUpdateRules<Model, opengm::Integrator> UpdateRules;
  66. typedef opengm::MessagePassing<Model, opengm::Integrator, UpdateRules, opengm::MaxDistance> LBP;
  67.  
  68. LBP::Parameter parameter(size_t(100)); //maximal number of iterations=0
  69. parameter.useNormalization_ = normalization;
  70. LBP lbp(gm, parameter);
  71.  
  72. lbp.infer();
  73.  
  74.  
  75. Model::IndependentFactorType marg;
  76. for(IndexType var=0; var<gm.numberOfVariables(); ++var)
  77. {
  78. std::cout<< "Sum of marginal distribution P(x_"<<var<<") :";
  79. lbp.marginal(var,marg);
  80. double s = 0;
  81. for(LabelType i=0; i<gm.numberOfLabels(var); ++i)
  82. s+=marg(&i);
  83. std::cout<< s << std::endl;
  84. }
  85. }
  86.  
  87.  
  88. int main(int argc, char** argv) {
  89. // Infer with LBP
  90. Model gm_small = buildIID(3,15);
  91. Model gm_large = buildIID(3,350);
  92. std::cout << "Sum of marginals with 15 labels" <<std::endl;
  93. inferBP(gm_small);
  94. std::cout << "Sum of marginals with 350 labels" <<std::endl;
  95. inferBP(gm_large);;
  96. return 0;
  97. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement