aGrUM  0.16.0
genericBNLearner_inl.h
Go to the documentation of this file.
1 
32 // to help IDE parser
34 #include <agrum/graphs/undiGraph.h>
35 
36 namespace gum {
37 
38  namespace learning {
39 
40  // returns the row filter
42  return *__parser;
43  }
44 
45  // returns the modalities of the variables
46  INLINE const std::vector< std::size_t >&
48  return __domain_sizes;
49  }
50 
51  // returns the names of the variables in the database
52  INLINE const std::vector< std::string >&
54  return __database.variableNames();
55  }
56 
58  INLINE void
60  if (__database.nbRows() == std::size_t(0)) return;
61  const double weight = new_weight / double(__database.nbRows());
63  }
64 
65  // returns the node id corresponding to a variable name
66  INLINE NodeId
67  genericBNLearner::Database::idFromName(const std::string& var_name) const {
68  try {
69  const auto cols = __database.columnsFromVariableName(var_name);
70  return __nodeId2cols.first(cols[0]);
71  } catch (...) {
73  "Variable " << var_name
74  << " could not be found in the database");
75  }
76  }
77 
78 
79  // returns the variable name corresponding to a given node id
80  INLINE const std::string&
82  try {
84  } catch (...) {
86  "Variable of Id " << id
87  << " could not be found in the database");
88  }
89  }
90 
91 
93  INLINE const DatabaseTable<>&
95  return __database;
96  }
97 
98 
100  INLINE const std::vector< std::string >&
102  return __database.missingSymbols();
103  }
104 
105 
109  return __nodeId2cols;
110  }
111 
112 
114  INLINE std::size_t genericBNLearner::Database::nbRows() const {
115  return __database.nbRows();
116  }
117 
118 
120  INLINE std::size_t genericBNLearner::Database::size() const {
121  return __database.size();
122  }
123 
124 
126  INLINE void genericBNLearner::Database::setWeight(const std::size_t i,
127  const double weight) {
128  __database.setWeight(i, weight);
129  }
130 
131 
133  INLINE double genericBNLearner::Database::weight(const std::size_t i) const {
134  return __database.weight(i);
135  }
136 
137 
139  INLINE double genericBNLearner::Database::weight() const {
140  return __database.weight();
141  }
142 
143 
144  // ===========================================================================
145 
146  // returns the node id corresponding to a variable name
147  INLINE NodeId genericBNLearner::idFromName(const std::string& var_name) const {
148  return __score_database.idFromName(var_name);
149  }
150 
151  // returns the variable name corresponding to a given node id
152  INLINE const std::string& genericBNLearner::nameFromId(NodeId id) const {
153  return __score_database.nameFromId(id);
154  }
155 
157  INLINE void genericBNLearner::setDatabaseWeight(const double new_weight) {
159  }
160 
162  INLINE void genericBNLearner::setRecordWeight(const std::size_t i,
163  const double new_weight) {
164  __score_database.setWeight(i, new_weight);
165  }
166 
168  INLINE double genericBNLearner::recordWeight(const std::size_t i) const {
169  return __score_database.weight(i);
170  }
171 
173  INLINE double genericBNLearner::databaseWeight() const {
174  return __score_database.weight();
175  }
176 
177  // sets an initial DAG structure
178  INLINE void genericBNLearner::setInitialDAG(const DAG& dag) {
179  __initial_dag = dag;
180  }
181 
182  // indicate that we wish to use an AIC score
186  }
187 
188  // indicate that we wish to use a BD score
192  }
193 
194  // indicate that we wish to use a BDeu score
198  }
199 
200  // indicate that we wish to use a BIC score
204  }
205 
206  // indicate that we wish to use a K2 score
210  }
211 
212  // indicate that we wish to use a Log2Likelihood score
216  }
217 
218  // sets the max indegree
219  INLINE void genericBNLearner::setMaxIndegree(Size max_indegree) {
221  }
222 
223  // indicate that we wish to use 3off2
227  }
228 
229  // indicate that we wish to use 3off2
233  }
234 
236  INLINE void genericBNLearner::useNML() {
239  "You must use the 3off2 algorithm before selecting "
240  << "the NML score");
241  }
243  }
244 
246  INLINE void genericBNLearner::useMDL() {
249  "You must use the 3off2 algorithm before selecting "
250  << "the MDL score");
251  }
253  }
254 
259  "You must use the 3off2 algorithm before selecting "
260  << "the NoCorr score");
261  }
263  }
264 
266  INLINE const std::vector< Arc > genericBNLearner::latentVariables() const {
269  "You must use the 3off2 algorithm before selecting "
270  << "the latentVariables method");
271  }
272  return __miic_3off2.latentVariables();
273  }
274 
275  // indicate that we wish to use a K2 algorithm
276  INLINE void genericBNLearner::useK2(const Sequence< NodeId >& order) {
278  __K2.setOrder(order);
279  }
280 
281  // indicate that we wish to use a K2 algorithm
282  INLINE void genericBNLearner::useK2(const std::vector< NodeId >& order) {
284  __K2.setOrder(order);
285  }
286 
287  // indicate that we wish to use a greedy hill climbing algorithm
290  }
291 
292  // indicate that we wish to use a local search with tabu list
294  Size nb_decrease) {
298  }
299 
301  INLINE void genericBNLearner::useEM(const double epsilon) {
303  }
304 
305 
308  }
309 
310  // assign a set of forbidden edges
311  INLINE void genericBNLearner::setPossibleEdges(const EdgeSet& set) {
313  }
314  // assign a set of forbidden edges from an UndiGraph
316  setPossibleEdges(g.edges());
317  }
318 
319  // assign a new possible edge
320  INLINE void genericBNLearner::addPossibleEdge(const Edge& edge) {
322  }
323 
324  // remove a forbidden edge
325  INLINE void genericBNLearner::erasePossibleEdge(const Edge& edge) {
327  }
328 
329  // assign a new forbidden edge
331  const NodeId head) {
332  addPossibleEdge(Edge(tail, head));
333  }
334 
335  // remove a forbidden edge
337  const NodeId head) {
338  erasePossibleEdge(Edge(tail, head));
339  }
340 
341  // assign a new forbidden edge
342  INLINE void genericBNLearner::addPossibleEdge(const std::string& tail,
343  const std::string& head) {
344  addPossibleEdge(Edge(idFromName(tail), idFromName(head)));
345  }
346 
347  // remove a forbidden edge
348  INLINE void genericBNLearner::erasePossibleEdge(const std::string& tail,
349  const std::string& head) {
351  }
352 
353  // assign a set of forbidden arcs
354  INLINE void genericBNLearner::setForbiddenArcs(const ArcSet& set) {
356  }
357 
358  // assign a new forbidden arc
359  INLINE void genericBNLearner::addForbiddenArc(const Arc& arc) {
361  }
362 
363  // remove a forbidden arc
364  INLINE void genericBNLearner::eraseForbiddenArc(const Arc& arc) {
366  }
367 
368  // assign a new forbidden arc
370  const NodeId head) {
371  addForbiddenArc(Arc(tail, head));
372  }
373 
374  // remove a forbidden arc
376  const NodeId head) {
377  eraseForbiddenArc(Arc(tail, head));
378  }
379 
380  // assign a new forbidden arc
381  INLINE void genericBNLearner::addForbiddenArc(const std::string& tail,
382  const std::string& head) {
383  addForbiddenArc(Arc(idFromName(tail), idFromName(head)));
384  }
385 
386  // remove a forbidden arc
387  INLINE void genericBNLearner::eraseForbiddenArc(const std::string& tail,
388  const std::string& head) {
390  }
391 
392  // assign a set of forbidden arcs
393  INLINE void genericBNLearner::setMandatoryArcs(const ArcSet& set) {
395  }
396 
397  // assign a new forbidden arc
398  INLINE void genericBNLearner::addMandatoryArc(const Arc& arc) {
400  }
401 
402  // remove a forbidden arc
403  INLINE void genericBNLearner::eraseMandatoryArc(const Arc& arc) {
405  }
406 
407  // assign a new forbidden arc
408  INLINE void genericBNLearner::addMandatoryArc(const std::string& tail,
409  const std::string& head) {
410  addMandatoryArc(Arc(idFromName(tail), idFromName(head)));
411  }
412 
413  // remove a forbidden arc
414  INLINE void genericBNLearner::eraseMandatoryArc(const std::string& tail,
415  const std::string& head) {
417  }
418 
419  // assign a new forbidden arc
421  const NodeId head) {
422  addMandatoryArc(Arc(tail, head));
423  }
424 
425  // remove a forbidden arc
427  const NodeId head) {
428  eraseMandatoryArc(Arc(tail, head));
429  }
430 
431  // sets a partial order on the nodes
432  INLINE void
435  }
436 
438  const std::vector< std::vector< std::string > >& slices) {
439  NodeProperty< NodeId > slice_order;
440  NodeId rank = 0;
441  for (const auto& slice : slices) {
442  for (const auto& name : slice) {
443  slice_order.insert(idFromName(name), rank);
444  }
445  rank++;
446  }
447  setSliceOrder(slice_order);
448  }
449 
450  // sets the apriori weight
452  if (weight < 0) {
453  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
454  }
455 
458  }
459 
460  // use the apriori smoothing
464  }
465 
466  // use the apriori smoothing
468  if (weight < 0) {
469  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
470  }
471 
473  __setAprioriWeight(weight);
474 
476  }
477 
478  // use the Dirichlet apriori
479  INLINE void genericBNLearner::useAprioriDirichlet(const std::string& filename,
480  double weight) {
481  if (weight < 0) {
482  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
483  }
484 
485  __apriori_dbname = filename;
487  __setAprioriWeight(weight);
488 
490  }
491 
492 
493  // use the apriori BDeu
495  if (weight < 0) {
496  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
497  }
498 
500  __setAprioriWeight(weight);
501 
503  }
504 
505 
506  // returns the type (as a string) of a given apriori
507  INLINE const std::string& genericBNLearner::__getAprioriType() const {
508  switch (__apriori_type) {
510 
512 
515 
517 
518  default:
520  "genericBNLearner getAprioriType does "
521  "not support yet this apriori");
522  }
523  }
524 
525  // returns the names of the variables in the database
526  INLINE const std::vector< std::string >& genericBNLearner::names() const {
527  return __score_database.names();
528  }
529 
530  // returns the modalities of the variables in the database
531  INLINE const std::vector< std::size_t >&
533  return __score_database.domainSizes();
534  }
535 
537  INLINE const std::vector< std::pair< std::size_t, std::size_t > >&
539  return __ranges;
540  }
541 
544 
548  }
549 
551  return __score_database.domainSizes().size();
552  }
553 
556  }
557  } /* namespace learning */
558 } /* namespace gum */
AlgoType __selected_algo
the selected learning algorithm
void setMaxIndegree(Size max_indegree)
sets the max indegree
void useScoreBIC()
indicate that we wish to use a BIC score
const T2 & second(const T1 &first) const
Returns the second value of a pair given its first value.
bool hasMissingValues() const
returns true if the learner&#39;s database has missing values
Database __score_database
the database to be used by the scores and parameter estimators
std::size_t size() const
returns the number of records in the database
void addEdge(const Edge &edge)
assign a new forbidden arc
CorrectedMutualInformation ::KModeTypes __3off2_kmode
the penalty used in 3off2
const std::string & __getAprioriType() const
returns the type (as a string) of a given apriori
double __EMepsilon
epsilon for EM. if espilon=0.0 : no EM
void setEdges(const EdgeSet &set)
assign a set of forbidden arcs
The class computing n times the corrected mutual information, as used in the 3off2 algorithm...
void addArc(const Arc &arc)
assign a new forbidden arc
void setDatabaseWeight(const double new_weight)
assign a weight to all the rows of the learning database so that the sum of their weights is equal to...
void clearDatabaseRanges()
reset the ranges to the one range corresponding to the whole database
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
const T1 & first(const T2 &second) const
Returns the first value of a pair given its second value.
const std::vector< std::string > & missingSymbols() const
returns the set of missing symbols taken into account
void setRecordWeight(const std::size_t i, const double weight)
sets the weight of the ith record of the database
void set3off2Behaviour()
Sets the orientation phase to follow the one of the 3off2 algorithm.
Definition: Miic.cpp:1065
DBVector< std::size_t > columnsFromVariableName(const std::string &name) const
returns the indices of all the columns whose name is passed in argument
void setTabuListSize(Size new_size)
sets the size of the tabu list
void setArcs(const ArcSet &set)
assign a set of forbidden arcs
StructuralConstraintSliceOrder __constraint_SliceOrder
the constraint for 2TBNs
void setMaxIndegree(Size max_indegree, bool update_all_node=false)
resets the default max indegree and possibly updates the indegree of all nodes
void addArc(const Arc &arc)
assign a new forbidden arc
Miic __miic_3off2
the 3off2 algorithm
void setOrder(const Sequence< NodeId > &order)
sets the order on the variables
void setInitialDAG(const DAG &)
sets an initial DAG structure
std::size_t size() const noexcept
returns the number of records (rows) in the database
const std::string & nameFromId(NodeId id) const
returns the variable name corresponding to a given node id
void setPossibleSkeleton(const UndiGraph &skeleton)
assign a set of forbidden edges
const DBVector< std::string > & missingSymbols() const
returns the set of missing symbols
DatabaseTable __database
the database itself
const std::vector< std::size_t > & domainSizes() const
returns the domain sizes of the variables
void use3off2()
indicate that we wish to use 3off2
double epsilon() const
Get the value of epsilon.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
void setArcs(const ArcSet &set)
assign a set of forbidden arcs
void useScoreK2()
indicate that we wish to use a K2 score
void setWeight(const std::size_t i, const double weight)
sets the weight of the ith record
const std::vector< Arc > latentVariables() const
get the list of arcs hiding latent variables
Definition: Miic.cpp:1049
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
Definition: agrum.h:25
void setMiicBehaviour()
Sets the orientation phase to follow the one of the MIIC algorithm.
Definition: Miic.cpp:1064
std::size_t nbRows() const
returns the number of records in the database
const std::vector< std::string > & names() const
returns the names of the variables in the database
AprioriType __apriori_type
the a priori selected for the score and parameters
NodeId idFromName(const std::string &var_name) const
returns the node id corresponding to a variable name
the internal apriori for the BDeu score (N&#39; / (r_i * q_i)BDeu is a BD score with a N&#39;/(r_i * q_i) apr...
Definition: aprioriBDeu.h:54
The class for generic Hash Tables.
Definition: hashTable.h:679
void useScoreLog2Likelihood()
indicate that we wish to use a Log2Likelihood score
A dirichlet priori: computes its N&#39;_ijk from a database.
const std::vector< Arc > latentVariables() const
get the list of arcs hiding latent variables
DAG __initial_dag
an initial DAG given to learners
void useNML()
indicate that we wish to use the NML correction for 3off2
StructuralConstraintMandatoryArcs __constraint_MandatoryArcs
the constraint on forbidden arcs
void setMandatoryArcs(const ArcSet &set)
assign a set of forbidden arcs
const EdgeSet & edges() const
returns the set of edges stored within the EdgeGraphPart
LocalSearchWithTabuList __local_search_with_tabu_list
the local search with tabu list algorithm
void erasePossibleEdge(const Edge &edge)
bool hasMissingValues() const
indicates whether the database contains some missing values
void eraseArc(const Arc &arc)
remove a forbidden arc
double databaseWeight() const
returns the weight of the whole database
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
double recordWeight(const std::size_t i) const
returns the weight of the ith record
void useNoCorr()
indicate that we wish to use the NoCorr correction for 3off2
StructuralConstraintTabuList __constraint_TabuList
the constraint for tabu lists
std::string __apriori_dbname
the filename for the Dirichlet a priori, if any
void addPossibleEdge(const Edge &edge)
StructuralConstraintPossibleEdges __constraint_PossibleEdges
the constraint on possible Edges
std::size_t nbRows() const noexcept
returns the number of records (rows) in the database
void useLocalSearchWithTabuList(Size tabu_size=100, Size nb_decrease=2)
indicate that we wish to use a local search with tabu list
const std::string & nameFromId(NodeId id) const
returns the variable name corresponding to a given node id
const DatabaseTable & databaseTable() const
returns the internal database table
void __setAprioriWeight(double weight)
sets the apriori weight
std::string checkScoreAprioriCompatibility()
checks whether the current score and apriori are compatible
double weight(const std::size_t i) const
returns the weight of the ith record
void useGreedyHillClimbing()
indicate that we wish to use a greedy hill climbing algorithm
void eraseArc(const Arc &arc)
remove a forbidden arc
std::vector< std::pair< std::size_t, std::size_t > > __ranges
the set of rows&#39; ranges within the database in which learning is done
void useAprioriSmoothing(double weight=1)
use the apriori smoothing
double weight() const
returns the weight of the whole database
const Bijection< NodeId, std::size_t > & nodeId2Columns() const
returns the mapping between node ids and their columns in the database
The class representing a tabular database as used by learning tasks.
void useScoreAIC()
indicate that we wish to use an AIC score
void useK2(const Sequence< NodeId > &order)
indicate that we wish to use K2
StructuralConstraintIndegree __constraint_Indegree
the constraint for indegrees
ScoreType __score_type
the score selected for learning
const std::vector< std::pair< std::size_t, std::size_t > > & databaseRanges() const
returns the current database rows&#39; ranges used for learning
DBRowGeneratorParser * __parser
the parser used for reading the database
double weight(const std::size_t i) const
returns the weight of the ith record
void setPossibleEdges(const EdgeSet &set)
assign a set of forbidden edges
const std::string & variableName(const std::size_t k) const
returns the name of the kth column of the IDatabaseTable
The base class for all undirected edges.
void useEM(const double epsilon)
use The EM algorithm to learn paramters
DBRowGeneratorParser & parser()
returns the parser for the database
void useAprioriDirichlet(const std::string &filename, double weight=1)
use the Dirichlet apriori
double __apriori_weight
the weight of the apriori
void eraseEdge(const Edge &edge)
remove a forbidden arc
const std::vector< std::size_t > & domainSizes() const
returns the domain sizes of the variables in the database
Base class for undirected graphs.
Definition: undiGraph.h:109
void setDatabaseWeight(const double new_weight)
assign a weight to all the rows of the database so that the sum of their weights is equal to new_weig...
the smooth a priori: adds a weight w to all the countings
void setAllRowsWeight(const double new_weight)
assign a given weight to all the rows of the database
std::vector< std::size_t > __domain_sizes
the domain sizes of the variables (useful to speed-up computations)
void setForbiddenArcs(const ArcSet &set)
assign a set of forbidden arcs
Bijection< NodeId, std::size_t > __nodeId2cols
a bijection assigning to each variable name its NodeId
void useAprioriBDeu(double weight=1)
use the BDeu apriori
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
const DatabaseTable & database() const
returns the database used by the BNLearner
StructuralConstraintForbiddenArcs __constraint_ForbiddenArcs
the constraint on forbidden arcs
void setSliceOrder(const NodeProperty< NodeId > &slice_order)
sets a partial order on the nodes
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
const std::vector< std::string > & names() const
returns the names of the variables in the database
const DBVector< std::string > & variableNames() const noexcept
returns the variable names for all the columns of the database
void useMIIC()
indicate that we wish to use MIIC
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
void useScoreBD()
indicate that we wish to use a BD score
void useMDL()
indicate that we wish to use the MDL correction for 3off2
void setMaxNbDecreasingChanges(Size nb)
set the max number of changes decreasing the score that we allow to apply
void useScoreBDeu()
indicate that we wish to use a BDeu score
Base class for dag.
Definition: DAG.h:102
Size NodeId
Type for node ids.
Definition: graphElements.h:98
the no a priori class: corresponds to 0 weight-sample
NodeId idFromName(const std::string &var_name) const
returns the node id corresponding to a variable name
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
void setWeight(const std::size_t i, const double weight)
assigns a given weight to the ith row of the database
the structural constraint imposing a partial order over nodes