![]() |
aGrUM
0.20.3
a C++ library for (probabilistic) graphical models
|
A factory class to ease BayesNet construction. More...
#include <agrum/BN/BayesNetFactory.h>
Public Member Functions | |
Constructor & destructor. | |
BayesNetFactory (BayesNet< GUM_SCALAR > *bn) | |
Use this constructor if you want to use an already created BayesNet. More... | |
BayesNetFactory (const BayesNetFactory< GUM_SCALAR > &source) | |
Copy constructor. More... | |
virtual | ~BayesNetFactory () |
Destructor. More... | |
Getter and setters. | |
BayesNet< GUM_SCALAR > * | bayesNet () |
Returns the BayesNet created by this factory. More... | |
const DiscreteVariable & | varInBN (NodeId id) final |
short-cut accessor for a DiscreveVariable in the BN More... | |
factory_state | state () const final |
Returns the current state of the factory. More... | |
NodeId | variableId (const std::string &name) const final |
Returns the NodeId of a variable given it's name. More... | |
const DiscreteVariable & | variable (const std::string &name) const |
Returns a constant reference on a variable given it's name. More... | |
Size | cptDomainSize (NodeId n) const final |
Returns the domainSize of the cpt for the node n. More... | |
Network declaration methods (NONE -> NETWORK) | |
void | startNetworkDeclaration () final |
Tells the factory that we're in a network declaration. More... | |
void | variableType (const VarType &type) |
Tells the factory the current variable's type. More... | |
void | addNetworkProperty (const std::string &propName, const std::string &propValue) final |
Tells the factory to add a property to the current network. More... | |
void | endNetworkDeclaration () final |
Tells the factory that we're out of a network declaration. More... | |
Variable declaration methods (NONE -> VARIABLE) | |
void | startVariableDeclaration () final |
Tells the factory that we're in a variable declaration. More... | |
void | variableName (const std::string &name) final |
Tells the factory the current variable's name. More... | |
void | variableDescription (const std::string &desc) final |
Tells the factory the current variable's description. More... | |
void | addModality (const std::string &name) final |
Adds a modality to the current labelized variable. More... | |
void | addMin (const long &min) |
Adds the min value of the current range variable. More... | |
void | addMax (const long &max) |
Adds the max value of the current range variable. More... | |
void | addTick (const GUM_SCALAR &tick) |
Adds a tick to the current Discretized variable. More... | |
void | setVariableCPTImplementation (MultiDimAdressable *adressable) final |
Defines the implementation to use for var's Potential. More... | |
NodeId | endVariableDeclaration () final |
Tells the factory that we're out of a variable declaration. More... | |
Parents declaration methods (NONE -> PARENTS) | |
void | startParentsDeclaration (const std::string &var) final |
Tells the factory that we're declaring parents for some variable. More... | |
void | addParent (const std::string &var) final |
Tells the factory for which variable we're declaring parents. More... | |
void | endParentsDeclaration () final |
Tells the factory that we've finished declaring parents for some variable. More... | |
Raw Probability table declaration methods (NONE -> RAW_CPT) | |
void | startRawProbabilityDeclaration (const std::string &var) final |
Tells the factory that we're declaring a conditional probability table for some variable. More... | |
void | rawConditionalTable (const std::vector< std::string > &variables, const std::vector< float > &rawTable) final |
Fills the variable's table with the values in rawTable. More... | |
void | rawConditionalTable (const std::vector< float > &rawTable) final |
Fills the variable's table with the values in rawTable. More... | |
void | endRawProbabilityDeclaration () final |
Tells the factory that we finished declaring a conditional probability table. More... | |
Factorized probability table declaration methods | |
void | startFactorizedProbabilityDeclaration (const std::string &var) final |
Tells the factory that we're starting a factorized declaration. More... | |
void | startFactorizedEntry () final |
Tells the factory that we start an entry of a factorized conditional probability table. More... | |
void | endFactorizedEntry () final |
Tells the factory that we end an entry of a factorized conditional probability table. More... | |
void | setParentModality (const std::string &parent, const std::string &modality) final |
Tells the factory on which modality we want to instantiate one of variable's parent. More... | |
void | setVariableValuesUnchecked (const std::vector< float > &values) final |
Gives the values of the variable with respect to precedent parents modality. More... | |
void | setVariableValues (const std::vector< float > &values) final |
same than below with gum::OperationNotAllowed exception if value's size not OK. More... | |
void | endFactorizedProbabilityDeclaration () final |
Tells the factory that we finished declaring a conditional probability table. More... | |
Delegated CPT definitions methods | |
(NONE, NETWORK) | |
void | setVariable (const DiscreteVariable &var) final |
Define a variable. More... | |
void | setVariableCPT (const std::string &varName, MultiDimAdressable *table, bool redefineParents) final |
Define a variable's CPT. More... | |
verbosity control | |
void | setVerbose () |
void | resetVerbose () |
bool | isVerbose () |
Public Types | |
enum | factory_state : char { factory_state::NONE, factory_state::NETWORK, factory_state::VARIABLE, factory_state::PARENTS, factory_state::RAW_CPT, factory_state::FACT_CPT, factory_state::FACT_ENTRY } |
The enumeration of states in which the factory can be in. More... | |
A factory class to ease BayesNet construction.
A BayesNetFactory will never create a BayesNet and works on only one BayesNet.
The only exception of this behaviour is when you create a copy of the factory, it will create a copy of it's BayesNet. This is useful if you want to create two BayesNet sharing a common base.
However be very careful because the copy will not delete it's BayesNet.
Each method will raise an OperationNotAllowed if you call it when the factory is not in a valid state for that call. The error message is "Illegal state.".
Definition at line 44 of file BayesNet.h.
|
stronginherited |
The enumeration of states in which the factory can be in.
Every documentation section's name indicates from which state you can call it's methods, and in which state it places the factory.
There is an exception for the delegated CPT definition methods which do not change the state of the factory.
Enumerator | |
---|---|
NONE | |
NETWORK | |
VARIABLE | |
PARENTS | |
RAW_CPT | |
FACT_CPT | |
FACT_ENTRY |
Definition at line 59 of file IBayesNetFactory.h.
|
explicit |
Use this constructor if you want to use an already created BayesNet.
bn | A pointer over the BayesNet filled by this factory. |
DuplicateElement | Raised if two variables in bn share the same name. |
Definition at line 39 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE gum::BayesNetFactory< GUM_SCALAR >::BayesNetFactory | ( | const BayesNetFactory< GUM_SCALAR > & | source | ) |
Copy constructor.
The copy will have an exact copy of the constructed BayesNet in source.
OperationNotAllowed | Raised if the state of source is not NONE or NETWORK. |
Definition at line 58 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
virtual |
Destructor.
To prevent strange behaviour you should always destroy a BayesNetFactory when it's state equals NONE.
FatalError | Raised if the state of the factory prevents it to die peacefully. |
Definition at line 73 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Check if in stringBag there is no other modality with the same name.
Definition at line 897 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Check if var exists and if mod is one of it's modality, if not raise an NotFound exception.
Definition at line 883 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Check if a variable with the given name exists, if not raise an NotFound exception.
Definition at line 876 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Fill a potential from a raw CPT.
Definition at line 477 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Fill a potential from a raw CPT.(using the canonical order of vars)
Definition at line 528 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Raise an OperationNotAllowed with the message "Illegal state.".
Definition at line 824 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Increment a modality counter for the fillProbaWithValuesTable method.
Definition at line 548 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Reset the different parts used to constructed the BayesNet.
Definition at line 924 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.
Definition at line 906 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
virtual |
Adds the max value of the current range variable.
Implements gum::IBayesNetFactory.
Definition at line 256 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
virtual |
Adds the min value of the current range variable.
Implements gum::IBayesNetFactory.
Definition at line 244 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Adds a modality to the current labelized variable.
Implements gum::IBayesNetFactory.
Definition at line 231 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory to add a property to the current network.
Implements gum::IBayesNetFactory.
Definition at line 146 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory for which variable we're declaring parents.
var | The parent's name. |
NotFound | Raised if var does not exists. |
Implements gum::IBayesNetFactory.
Definition at line 407 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE void gum::BayesNetFactory< GUM_SCALAR >::addTick | ( | const GUM_SCALAR & | tick | ) |
Adds a tick to the current Discretized variable.
Definition at line 268 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE BayesNet< GUM_SCALAR > * gum::BayesNetFactory< GUM_SCALAR >::bayesNet | ( | ) |
Returns the BayesNet created by this factory.
OperationNotAllowed | Raise if the state of the factory is different than NONE. |
Definition at line 91 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Returns the domainSize of the cpt for the node n.
NotFound | raised if no such NodeId exists. |
Implements gum::IBayesNetFactory.
Definition at line 130 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we end an entry of a factorized conditional probability table.
Implements gum::IBayesNetFactory.
Definition at line 620 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we finished declaring a conditional probability table.
Implements gum::IBayesNetFactory.
Definition at line 739 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're out of a network declaration.
Implements gum::IBayesNetFactory.
Definition at line 153 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we've finished declaring parents for some variable.
Implements gum::IBayesNetFactory.
Definition at line 421 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we finished declaring a conditional probability table.
Implements gum::IBayesNetFactory.
Definition at line 582 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're out of a variable declaration.
gum::OperationNotAllowed | Raised if the variable isn't defined (or / not enough defined). |
Implements gum::IBayesNetFactory.
Definition at line 309 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
inlineinherited |
|
privatedelete |
Copy operator is illegal, use only copy constructor.
|
finalvirtual |
Fills the variable's table with the values in rawTable.
Parse the parents in the same order in variables
Given a sequence [var, p_1, p_2, ...,p_n-1, p_n] of parents, modalities are parsed in the given order (if all p_i are binary):
* [0, 0, ..., 0, 0], * [0, 0, ..., 0, 1], * [0, 0, ..., 1, 0], * [0, 0, ..., 1, 1], * ..., * [1, 1, ..., 1, 0], * [1, 1, ..., 1, 1]. *
variables | the vector giving the order of parents |
rawTable | The raw table. |
Implements gum::IBayesNetFactory.
Definition at line 467 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Fills the variable's table with the values in rawTable.
use the canonical ordering for the variables (e.g. see BIF format) ...
rawTable | The raw table. |
Implements gum::IBayesNetFactory.
Definition at line 519 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
inlineinherited |
|
finalvirtual |
Tells the factory on which modality we want to instantiate one of variable's parent.
Implements gum::IBayesNetFactory.
Definition at line 633 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Define a variable.
You can only call this method is the factory is in the NONE or NETWORK state.
The variable is added by copy.
var | The pointer over a DiscreteVariable used to define a new variable in the built BayesNet. |
DuplicateElement | Raised if a variable with the same name already exists. |
OperationNotAllowed | Raised if redefineParents == false and if table is not a valid CPT for var in the current state of the BayesNet. |
Implements gum::IBayesNetFactory.
Definition at line 760 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Define a variable's CPT.
You can only call this method if the factory is in the NONE or NETWORK state.
Be careful that table is given to the built BayesNet, so it will be deleted with it, and you should not directly access it after you call this method.
When the redefineParents flag is set to true the constructed BayesNet's DAG is changed to fit with table's definition.
varName | The name of the concerned variable. |
table | A pointer over the CPT used for var. |
redefineParents | If true redefine var's parents to match table's variables set. |
NotFound | Raised if no variable matches var. |
OperationNotAllowed | Raised if redefineParents == false and if table is not a valid CPT for var in the current state of the BayesNet. |
Implements gum::IBayesNetFactory.
Definition at line 793 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Defines the implementation to use for var's Potential.
NotFound | Raised if no variable matches var. |
OperationNotAllowed | Raised if impl is not empty. |
OperationNotAllowed | If an implementation is already defined for the current variable. |
Implements gum::IBayesNetFactory.
Definition at line 288 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
same than below with gum::OperationNotAllowed exception if value's size not OK.
Implements gum::IBayesNetFactory.
Definition at line 719 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Gives the values of the variable with respect to precedent parents modality.
If some parents have no modality set, then we apply values for all instantiations of that parent.
This means you can declare a default value for the table by doing
as for rawProba, if value's size is different than the number of modalities of the current variable, we don't use the supplementary values and we fill by 0 the missing values.
Implements gum::IBayesNetFactory.
Definition at line 673 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
inlineinherited |
|
finalvirtual |
Tells the factory that we start an entry of a factorized conditional probability table.
Implements gum::IBayesNetFactory.
Definition at line 608 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're starting a factorized declaration.
Implements gum::IBayesNetFactory.
Definition at line 594 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're in a network declaration.
Implements gum::IBayesNetFactory.
Definition at line 136 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're declaring parents for some variable.
var | The concerned variable's name. |
NotFound | Raised if var does not exists. |
Implements gum::IBayesNetFactory.
Definition at line 392 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're declaring a conditional probability table for some variable.
var | The concerned variable's name. |
Implements gum::IBayesNetFactory.
Definition at line 444 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory that we're in a variable declaration.
Implements gum::IBayesNetFactory.
Definition at line 164 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Returns the current state of the factory.
Implements gum::IBayesNetFactory.
Definition at line 102 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
INLINE const DiscreteVariable & gum::BayesNetFactory< GUM_SCALAR >::variable | ( | const std::string & | name | ) | const |
Returns a constant reference on a variable given it's name.
NotFound | Raised if no variable matches the name. |
Definition at line 120 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory the current variable's description.
Implements gum::IBayesNetFactory.
Definition at line 190 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Returns the NodeId of a variable given it's name.
NotFound | Raised if no variable matches the name. |
Implements gum::IBayesNetFactory.
Definition at line 110 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
Tells the factory the current variable's name.
DuplicateElement | Raised if a variable with the same name already exist. |
Implements gum::IBayesNetFactory.
Definition at line 177 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
virtual |
Tells the factory the current variable's type.
Implements gum::IBayesNetFactory.
Definition at line 205 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
finalvirtual |
short-cut accessor for a DiscreveVariable in the BN
Implements gum::IBayesNetFactory.
Definition at line 96 of file BayesNetFactory_tpl.h.
References gum::Set< Key, Alloc >::emplace().
|
private |
Depending on the context this flag is used for some VERY important reasons.
Definition at line 404 of file BayesNetFactory.h.
|
private |
The constructed BayesNet.
Definition at line 422 of file BayesNetFactory.h.
|
private |
Depending on the context this flag is used for some VERY important reasons.
Definition at line 400 of file BayesNetFactory.h.
|
private |
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.
Definition at line 414 of file BayesNetFactory.h.
|
private |
Used when a factorized CPT is built.
Definition at line 410 of file BayesNetFactory.h.
|
private |
State stack.
Definition at line 419 of file BayesNetFactory.h.
|
private |
Just to keep track of strings between two start/end calls.
Definition at line 407 of file BayesNetFactory.h.
|
private |
Mapping between a declared variable's name and it's node id.
Definition at line 425 of file BayesNetFactory.h.