aGrUM  0.14.2
genericBNLearner_inl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}@lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it wil be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
29 // to help IDE parser
31 
32 namespace gum {
33 
34  namespace learning {
35 
36  // returns the row filter
38  return *__parser;
39  }
40 
41  // returns the modalities of the variables
42  INLINE const std::vector< std::size_t >&
44  return __domain_sizes;
45  }
46 
47  // returns the names of the variables in the database
48  INLINE const std::vector< std::string >&
50  return __database.variableNames();
51  }
52 
54  INLINE void
56  if (__database.nbRows() == std::size_t(0)) return;
57  const double weight = new_weight / double(__database.nbRows());
59  }
60 
61  // returns the node id corresponding to a variable name
62  INLINE NodeId
63  genericBNLearner::Database::idFromName(const std::string& var_name) const {
64  try {
65  const auto cols = __database.columnsFromVariableName(var_name);
66  return __nodeId2cols.first(cols[0]);
67  } catch (...) {
69  "Variable " << var_name
70  << " could not be found in the database");
71  }
72  }
73 
74 
75  // returns the variable name corresponding to a given node id
76  INLINE const std::string&
78  try {
80  } catch (...) {
82  "Variable of Id " << id
83  << " could not be found in the database");
84  }
85  }
86 
87 
89  INLINE const DatabaseTable<>&
91  return __database;
92  }
93 
94 
96  INLINE const std::vector< std::string >&
98  return __database.missingSymbols();
99  }
100 
104  return __nodeId2cols;
105  }
106 
107 
108  // ===========================================================================
109 
110  // returns the node id corresponding to a variable name
111  INLINE NodeId genericBNLearner::idFromName(const std::string& var_name) const {
112  return __score_database.idFromName(var_name);
113  }
114 
115  // returns the variable name corresponding to a given node id
116  INLINE const std::string& genericBNLearner::nameFromId(NodeId id) const {
117  return __score_database.nameFromId(id);
118  }
119 
121  INLINE void genericBNLearner::setDatabaseWeight(const double new_weight) {
123  }
124 
125  // sets an initial DAG structure
126  INLINE void genericBNLearner::setInitialDAG(const DAG& dag) {
127  __initial_dag = dag;
128  }
129 
130  // indicate that we wish to use an AIC score
134  }
135 
136  // indicate that we wish to use a BD score
140  }
141 
142  // indicate that we wish to use a BDeu score
146  }
147 
148  // indicate that we wish to use a BIC score
152  }
153 
154  // indicate that we wish to use a K2 score
158  }
159 
160  // indicate that we wish to use a Log2Likelihood score
164  }
165 
166  // sets the max indegree
167  INLINE void genericBNLearner::setMaxIndegree(Size max_indegree) {
169  }
170 
171  // indicate that we wish to use 3off2
175  }
176 
177  // indicate that we wish to use 3off2
181  }
182 
184  INLINE void genericBNLearner::useNML() {
187  "You must use the 3off2 algorithm before selecting "
188  << "the NML score");
189  }
191  }
192 
194  INLINE void genericBNLearner::useMDL() {
197  "You must use the 3off2 algorithm before selecting "
198  << "the MDL score");
199  }
201  }
202 
207  "You must use the 3off2 algorithm before selecting "
208  << "the NoCorr score");
209  }
211  }
212 
214  INLINE const std::vector< Arc > genericBNLearner::latentVariables() const {
217  "You must use the 3off2 algorithm before selecting "
218  << "the latentVariables method");
219  }
220  return __miic_3off2.latentVariables();
221  }
222 
223  // indicate that we wish to use a K2 algorithm
224  INLINE void genericBNLearner::useK2(const Sequence< NodeId >& order) {
226  __K2.setOrder(order);
227  }
228 
229  // indicate that we wish to use a K2 algorithm
230  INLINE void genericBNLearner::useK2(const std::vector< NodeId >& order) {
232  __K2.setOrder(order);
233  }
234 
235  // indicate that we wish to use a greedy hill climbing algorithm
238  }
239 
240  // indicate that we wish to use a local search with tabu list
242  Size nb_decrease) {
246  }
247 
249  INLINE void genericBNLearner::useEM(const double epsilon) {
251  }
252 
253 
256  }
257 
258  // assign a set of forbidden arcs
259  INLINE void genericBNLearner::setForbiddenArcs(const ArcSet& set) {
261  }
262 
263  // assign a new forbidden arc
264  INLINE void genericBNLearner::addForbiddenArc(const Arc& arc) {
266  }
267 
268  // remove a forbidden arc
269  INLINE void genericBNLearner::eraseForbiddenArc(const Arc& arc) {
271  }
272 
273  // assign a new forbidden arc
275  const NodeId head) {
276  addForbiddenArc(Arc(tail, head));
277  }
278 
279  // remove a forbidden arc
281  const NodeId head) {
282  eraseForbiddenArc(Arc(tail, head));
283  }
284 
285  // assign a new forbidden arc
286  INLINE void genericBNLearner::addForbiddenArc(const std::string& tail,
287  const std::string& head) {
288  addForbiddenArc(Arc(idFromName(tail), idFromName(head)));
289  }
290 
291  // remove a forbidden arc
292  INLINE void genericBNLearner::eraseForbiddenArc(const std::string& tail,
293  const std::string& head) {
295  }
296 
297  // assign a set of forbidden arcs
298  INLINE void genericBNLearner::setMandatoryArcs(const ArcSet& set) {
300  }
301 
302  // assign a new forbidden arc
303  INLINE void genericBNLearner::addMandatoryArc(const Arc& arc) {
305  }
306 
307  // remove a forbidden arc
308  INLINE void genericBNLearner::eraseMandatoryArc(const Arc& arc) {
310  }
311 
312  // assign a new forbidden arc
313  INLINE void genericBNLearner::addMandatoryArc(const std::string& tail,
314  const std::string& head) {
315  addMandatoryArc(Arc(idFromName(tail), idFromName(head)));
316  }
317 
318  // remove a forbidden arc
319  INLINE void genericBNLearner::eraseMandatoryArc(const std::string& tail,
320  const std::string& head) {
322  }
323 
324  // assign a new forbidden arc
326  const NodeId head) {
327  addMandatoryArc(Arc(tail, head));
328  }
329 
330  // remove a forbidden arc
332  const NodeId head) {
333  eraseMandatoryArc(Arc(tail, head));
334  }
335 
336  // sets a partial order on the nodes
337  INLINE void
340  }
341 
343  const std::vector< std::vector< std::string > >& slices) {
344  NodeProperty< NodeId > slice_order;
345  NodeId rank = 0;
346  for (const auto& slice : slices) {
347  for (const auto& name : slice) {
348  slice_order.insert(idFromName(name), rank);
349  }
350  rank++;
351  }
352  setSliceOrder(slice_order);
353  }
354 
355  // sets the apriori weight
356  INLINE void genericBNLearner::__setAprioriWeight(double weight) {
357  if (weight < 0) {
358  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
359  }
360 
361  __apriori_weight = weight;
363  }
364 
365  // use the apriori smoothing
369  }
370 
371  // use the apriori smoothing
372  INLINE void genericBNLearner::useAprioriSmoothing(double weight) {
373  if (weight < 0) {
374  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
375  }
376 
378  __setAprioriWeight(weight);
379 
381  }
382 
383  // use the Dirichlet apriori
384  INLINE void genericBNLearner::useAprioriDirichlet(const std::string& filename,
385  double weight) {
386  if (weight < 0) {
387  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
388  }
389 
390  __apriori_dbname = filename;
392  __setAprioriWeight(weight);
393 
395  }
396 
397 
398  // use the apriori BDeu
399  INLINE void genericBNLearner::useAprioriBDeu(double weight) {
400  if (weight < 0) {
401  GUM_ERROR(OutOfBounds, "the weight of the apriori must be positive");
402  }
403 
405  __setAprioriWeight(weight);
406 
408  }
409 
410 
411  // returns the type (as a string) of a given apriori
412  INLINE const std::string& genericBNLearner::__getAprioriType() const {
413  switch (__apriori_type) {
415 
417 
420 
422 
423  default:
425  "genericBNLearner getAprioriType does "
426  "not support yet this apriori");
427  }
428  }
429 
430  // returns the names of the variables in the database
431  INLINE const std::vector< std::string >& genericBNLearner::names() const {
432  return __score_database.names();
433  }
434 
435  // returns the modalities of the variables in the database
436  INLINE const std::vector< std::size_t >&
438  return __score_database.domainSizes();
439  }
440 
442  INLINE const std::vector< std::pair< std::size_t, std::size_t > >&
444  return __ranges;
445  }
446 
449 
453  }
454 
456  return __score_database.domainSizes().size();
457  }
458 
461  }
462  } /* namespace learning */
463 } /* 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
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
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
A class for generic framework of learning algorithms that can easily be used.
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 set3off2Behaviour()
Sets the orientation phase to follow the one of the 3off2 algorithm.
Definition: Miic.cpp:1062
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
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.
void setArcs(const ArcSet &set)
assign a set of forbidden arcs
void useScoreK2()
indicate that we wish to use a K2 score
const std::vector< Arc > latentVariables() const
get the list of arcs hiding latent variables
Definition: Miic.cpp:1046
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void setMiicBehaviour()
Sets the orientation phase to follow the one of the MIIC algorithm.
Definition: Miic.cpp:1061
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:51
The class for generic Hash Tables.
Definition: hashTable.h:676
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
LocalSearchWithTabuList __local_search_with_tabu_list
the local search with tabu list algorithm
bool hasMissingValues() const
indicates whether the database contains some missing values
void eraseArc(const Arc &arc)
remove a forbidden arc
The base class for all directed edgesThis class is used as a basis for manipulating all directed edge...
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
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
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
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
const std::string & variableName(const std::size_t k) const
returns the name of the kth column of the IDatabaseTable
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
const std::vector< std::size_t > & domainSizes() const
returns the domain sizes of the variables in the database
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:45
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:99
Size NodeId
Type for node ids.
Definition: graphElements.h:97
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:52
the structural constraint imposing a partial order over nodes