aGrUM  0.13.2
gum::learning::DAG2BNLearner Class Reference

A class that, given a structure and a parameter estimator returns a full Bayes net. More...

#include <DAG2BNLearner.h>

Public Member Functions

Constructors / Destructors
 DAG2BNLearner ()
 default constructor More...
 
 DAG2BNLearner (const DAG2BNLearner &from)
 copy constructor More...
 
 DAG2BNLearner (DAG2BNLearner &&from)
 move constructor More...
 
 ~DAG2BNLearner ()
 destructor More...
 

Static Public Member Functions

Accessors / Modifiers
template<typename GUM_SCALAR = double, typename PARAM_ESTIMATOR , typename CELL_TRANSLATORS >
static BayesNet< GUM_SCALAR > createBN (PARAM_ESTIMATOR &estimator, const DAG &dag, const std::vector< std::string > &names, const std::vector< Size > &modal, const CELL_TRANSLATORS &translator)
 create a BN More...
 

Detailed Description

A class that, given a structure and a parameter estimator returns a full Bayes net.

Definition at line 49 of file DAG2BNLearner.h.

Constructor & Destructor Documentation

gum::learning::DAG2BNLearner::DAG2BNLearner ( )

default constructor

Definition at line 34 of file DAG2BNLearner.cpp.

34 { GUM_CONSTRUCTOR(learning::DAG2BNLearner); }
gum::learning::DAG2BNLearner::DAG2BNLearner ( const DAG2BNLearner from)

copy constructor

Definition at line 37 of file DAG2BNLearner.cpp.

37  {
38  GUM_CONS_CPY(learning::DAG2BNLearner);
39  }
gum::learning::DAG2BNLearner::DAG2BNLearner ( DAG2BNLearner &&  from)

move constructor

Definition at line 42 of file DAG2BNLearner.cpp.

42  {
43  GUM_CONS_MOV(learning::DAG2BNLearner);
44  }
gum::learning::DAG2BNLearner::~DAG2BNLearner ( )

destructor

Definition at line 47 of file DAG2BNLearner.cpp.

47 { GUM_DESTRUCTOR(learning::DAG2BNLearner); }

Member Function Documentation

template<typename GUM_SCALAR >
void gum::learning::DAG2BNLearner::__probaVarReordering ( gum::Potential< GUM_SCALAR > &  pot,
const gum::Potential< GUM_SCALAR > &  other_pot 
)
staticprivate

copy a potential into another whose variables' sequence differs

The variables of both potential should be the same, only their order differs

Definition at line 39 of file DAG2BNLearner_tpl.h.

References gum::Instantiation::end(), GUM_ERROR, gum::MultiDimDecorator< GUM_SCALAR >::set(), gum::Instantiation::setFirst(), gum::Instantiation::setVals(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

Referenced by createBN().

41  {
42  // check that the variables are identical
43  if (!pot.variablesSequence()
44  .diffSet(other_pot.variablesSequence())
45  .empty()) {
46  GUM_ERROR(gum::CPTError, "the potentials do not have the same variables");
47  }
48 
49  // perform the copy
50  Instantiation i(other_pot);
51  Instantiation j(pot);
52  for (i.setFirst(); !i.end(); ++i) {
53  j.setVals(i);
54  pot.set(j, other_pot[i]);
55  }
56  }
virtual const Sequence< const DiscreteVariable * > & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
#define GUM_ERROR(type, msg)
Definition: exceptions.h:66

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<typename GUM_SCALAR , typename PARAM_ESTIMATOR , typename CELL_TRANSLATORS >
BayesNet< GUM_SCALAR > gum::learning::DAG2BNLearner::createBN ( PARAM_ESTIMATOR &  estimator,
const DAG dag,
const std::vector< std::string > &  names,
const std::vector< Size > &  modal,
const CELL_TRANSLATORS &  translator 
)
static

create a BN

Definition at line 63 of file DAG2BNLearner_tpl.h.

References __probaVarReordering(), gum::MultiDimDecorator< GUM_SCALAR >::add(), gum::BayesNet< GUM_SCALAR >::add(), gum::BayesNet< GUM_SCALAR >::addArc(), gum::MultiDimDecorator< GUM_SCALAR >::beginMultipleChanges(), gum::BayesNet< GUM_SCALAR >::beginTopologyTransformation(), gum::BayesNet< GUM_SCALAR >::cpt(), gum::MultiDimDecorator< GUM_SCALAR >::endMultipleChanges(), gum::BayesNet< GUM_SCALAR >::endTopologyTransformation(), gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::erase(), gum::VariableNodeMap::get(), gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::insert(), gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::pos(), gum::SequenceImplementation< Key, Alloc, std::is_scalar< Key >::value >::size(), gum::BayesNet< GUM_SCALAR >::variableNodeMap(), and gum::MultiDimDecorator< GUM_SCALAR >::variablesSequence().

67  {
68  BayesNet< GUM_SCALAR > bn;
69 
70  // create a bn with dummy parameters corresponding to the dag
71  for (const auto id : dag) {
72  // create the labelized variable
73  /*
74  std::vector< std::string > labels;
75  auto& translator = translators.translator ( id );
76  for (Idx i = 0; i < modal[id]; ++i) {
77  labels.push_back(translator.translateBack(DBTranslatedValue{std::size_t(i)}));
78  }
79  sort(labels.begin(), labels.end());
80  LabelizedVariable variable(names[id], "", 0);
81  for (auto s : labels) {
82  variable.addLabel(s);
83  }
84  */
85 
86  bn.add(dynamic_cast< const DiscreteVariable& >(translators.variable(id)),
87  id);
88  }
89 
90  // add the arcs
91  bn.beginTopologyTransformation();
92  for (const auto& arc : dag.arcs()) {
93  bn.addArc(arc.tail(), arc.head());
94  }
95  bn.endTopologyTransformation();
96 
97  // estimate the parameters
98  const VariableNodeMap& varmap = bn.variableNodeMap();
99  for (const auto id : dag) {
100  estimator.clear();
101 
102  // get the sequence of variables and make the targets be the last
103  Potential< GUM_SCALAR >& pot =
104  const_cast< Potential< GUM_SCALAR >& >(bn.cpt(id));
105  const DiscreteVariable& var = varmap.get(id);
106 
107  // get the variables of the CPT of id in the correct order
108  Sequence< const DiscreteVariable* > vars = pot.variablesSequence();
109  if (vars.pos(&var) != vars.size() - 1) {
110  vars.erase(&var);
111  vars.insert(&var);
112  }
113 
114  // setup the estimation
115  if (vars.size() > 1) {
116  std::vector< Idx > cond_ids(vars.size() - 1);
117  for (Idx i = 0; i < cond_ids.size(); ++i) {
118  cond_ids[i] = varmap.get(*(vars[i]));
119  }
120  estimator.addNodeSet(id, cond_ids);
121  } else {
122  estimator.addNodeSet(id);
123  }
124 
125  // assign the parameters to the potentials
126  Idx index = 0;
127 
128  // create a potential with the appropriate size
129  Potential< GUM_SCALAR > ordered_pot;
130  ordered_pot.beginMultipleChanges();
131  for (const auto var : vars) {
132  ordered_pot.add(*var);
133  }
134  ordered_pot.endMultipleChanges();
135  estimator.setParameters(index, ordered_pot);
136 
137  // assign the potential to the BN
138  __probaVarReordering(pot, ordered_pot);
139  }
140 
141  return bn;
142  }
static void __probaVarReordering(gum::Potential< GUM_SCALAR > &pot, const gum::Potential< GUM_SCALAR > &other_pot)
copy a potential into another whose variables&#39; sequence differs
unsigned long Idx
Type for indexes.
Definition: types.h:43

+ Here is the call graph for this function:


The documentation for this class was generated from the following files: