29 #define VERBOSITY(x) \ 31 if (isVerbose()) std::cerr << "[BN factory] " << x << std::endl; \ 40 template <
typename GUM_SCALAR >
48 for (
auto node : bn->
nodes()) {
51 "Name already used: " << bn->
variable(node).name());
61 template <
typename GUM_SCALAR >
77 template <
typename GUM_SCALAR >
87 <<
"[BN factory] Implementation defined for a variable but not used. " 88 "You should call endVariableDeclaration() before " 96 template <
typename GUM_SCALAR >
101 template <
typename GUM_SCALAR >
104 return __bn->variable(
id);
108 template <
typename GUM_SCALAR >
117 template <
typename GUM_SCALAR >
127 template <
typename GUM_SCALAR >
138 template <
typename GUM_SCALAR >
143 return __bn->cpt(n).domainSize();
147 template <
typename GUM_SCALAR >
159 template <
typename GUM_SCALAR >
161 const std::string& propName,
const std::string& propValue) {
162 __bn->setProperty(propName, propValue);
166 template <
typename GUM_SCALAR >
178 template <
typename GUM_SCALAR >
192 template <
typename GUM_SCALAR >
210 template <
typename GUM_SCALAR >
224 template <
typename GUM_SCALAR >
244 template <
typename GUM_SCALAR >
255 "An implementation for this variable is already " 257 }
else if (impl->
nbrDim() > 0) {
266 template <
typename GUM_SCALAR >
274 for (
size_t i = 2; i < __stringBag.size(); ++i) {
295 std::stringstream msg;
296 msg <<
"Not enough modalities (";
304 msg <<
") declared for variable ";
324 template <
typename GUM_SCALAR >
326 const std::string& var) {
331 std::vector< std::string >::iterator iter =
__stringBag.begin();
336 VERBOSITY(
"starting parents for " << var);
342 template <
typename GUM_SCALAR >
356 template <
typename GUM_SCALAR >
365 for (
size_t i = __stringBag.size() - 1; i > 0; --i) {
367 VERBOSITY(
" adding parent " << __stringBag[i] <<
" for " 382 template <
typename GUM_SCALAR >
384 const std::string& var) {
407 template <
typename GUM_SCALAR >
409 const std::vector< std::string >& variables,
410 const std::vector< float >& rawTable) {
418 template <
typename GUM_SCALAR >
420 const std::vector< std::string >& variables,
421 const std::vector< float >& rawTable) {
427 for (
size_t i = 0; i < variables.size(); ++i) {
435 std::vector< Idx > modCounter;
438 for (
NodeId i = 0; i < nbrVar; i++) {
439 modCounter.push_back(
Idx(0));
445 for (
NodeId i = 0; i < nbrVar; i++) {
446 cptInst.
chgVal(*(varList[i]), modCounter[i]);
449 if (j < rawTable.size()) {
450 table.
set(cptInst, (GUM_SCALAR)rawTable[j]);
452 table.
set(cptInst, (GUM_SCALAR)0);
459 template <
typename GUM_SCALAR >
461 const std::vector< float >& rawTable) {
469 template <
typename GUM_SCALAR >
471 const std::vector< float >& rawTable) {
484 (j < rawTable.size()) ? (GUM_SCALAR)rawTable[j++]
491 template <
typename GUM_SCALAR >
493 std::vector< gum::Idx >& modCounter,
497 for (
NodeId j = 0; j < modCounter.size(); j++) {
498 last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
503 if (last) {
return false; }
510 if (modCounter[i] == (varList[i]->domainSize() - 1)) {
526 template <
typename GUM_SCALAR >
539 template <
typename GUM_SCALAR >
541 const std::string& var) {
546 std::vector< std::string >::iterator iter =
__stringBag.begin();
554 template <
typename GUM_SCALAR >
566 template <
typename GUM_SCALAR >
579 template <
typename GUM_SCALAR >
581 const std::string& parent,
const std::string& modality) {
619 template <
typename GUM_SCALAR >
621 const std::vector< float >& values) {
636 for (
auto node :
__bn->parents(varId)) {
638 inst_default <<
__bn->variable(node);
644 inst.
incIn(inst_default)) {
647 inst.
val(var) < values.size() ? (GUM_SCALAR)values[inst.
val(var)]
655 for (var_inst.
setFirst(); !var_inst.
end(); ++var_inst) {
661 inst.
val(var) < values.size()
662 ? (GUM_SCALAR)values[inst.
val(var)]
670 template <
typename GUM_SCALAR >
672 const std::vector< float >& values) {
682 <<
" : invalid number of modalities: found " << values.size()
692 template <
typename GUM_SCALAR >
714 template <
typename GUM_SCALAR >
747 template <
typename GUM_SCALAR >
761 if (redefineParents) {
763 }
else if (pot->contains(var)) {
764 for (
auto node :
__bn->parents(varId)) {
765 if (!pot->contains(
__bn->variable(node))) {
767 "The CPT is not valid in the current BayesNet.");
772 __bn->_unsafeChangePotential(varId, pot);
778 template <
typename GUM_SCALAR >
787 template <
typename GUM_SCALAR >
790 std::string msg =
"Illegal state call (";
792 msg +=
") in state ";
830 default: { msg +=
"Unknown state"; }
838 template <
typename GUM_SCALAR >
846 template <
typename GUM_SCALAR >
848 const std::string& name,
const std::string& mod) {
853 if (mod == var.
label(i)) {
return i; }
860 template <
typename GUM_SCALAR >
872 template <
typename GUM_SCALAR >
876 __bn->_dag.eraseParents(varId);
886 __bn->_unsafeChangePotential(varId, table);
890 template <
typename GUM_SCALAR >
aGrUM's Potential is a multi-dimensional array with tensor operators.
Class representing a Bayesian Network.
void endRawProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
void variableName(const std::string &name) final
Tells the factory the current variable's name.
const DiscreteVariable & variable(NodeId id) const final
Returns a gum::DiscreteVariable given its gum::NodeId in the gum::BayesNet.
Size domainSize() const final
Returns the product of the variable's domain size in the Instantiation.
void endFactorizedEntry() final
Tells the factory that we end an entry of a factorized conditional probability table.
void setVariableCPTImplementation(MultiDimAdressable *adressable) final
Defines the implementation to use for var's Potential.
void startFactorizedEntry() final
Tells the factory that we start an entry of a factorized conditional probability table.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
void addParent(const std::string &var) final
Tells the factory for which variable we're declaring parents.
void setVariable(const DiscreteVariable &var) final
Define a variable.
BayesNet< GUM_SCALAR > * bayesNet()
Returns the BayesNet created by this factory.
factory_state state() const final
Returns the current state of the factory.
void addNetworkProperty(const std::string &propName, const std::string &propValue) final
Tells the factory to add a property to the current network.
void startParentsDeclaration(const std::string &var) final
Tells the factory that we're declaring parents for some variable.
bool exists(const Key &key) const
Checks whether there exists an element with a given key in the hashtable.
void endParentsDeclaration() final
Tells the factory that we've finished declaring parents for some variable.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
Base class for discrete random variable.
Generic doubly linked lists.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
gum is the global namespace for all aGrUM entities
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
BayesNetFactory< GUM_SCALAR > & operator=(const BayesNetFactory< GUM_SCALAR > &source)
Copy operator is illegal, use only copy constructor.
void setFirstNotVar(const DiscreteVariable &v)
Assign the first values to variables different of v.
void unsetEnd()
Alias for unsetOverflow().
void __fillProbaWithValuesTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a potential from a raw CPT.
Idx val(Idx i) const
Returns the current value of the variable at position i.
virtual Size domainSize() const =0
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
void __illegalStateError(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
void setFirstIn(const Instantiation &i)
Assign the first values in the Instantiation for the variables in i.
void startFactorizedProbabilityDeclaration(const std::string &var) final
Tells the factory that we're starting a factorized declaration.
NodeId variableId(const std::string &name) const final
Returns the NodeId of a variable given it's name.
Headers of the BayesNetFactory class.
void startNetworkDeclaration() final
Tells the factory that we're in a network declaration.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
const NodeGraphPart & nodes() const
Returns a constant reference to the dag of this Bayes Net.
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
HashTable< std::string, NodeId > __varNameMap
Mapping between a declared variable's name and it's node id.
Size size() const noexcept
Returns the number of elements in the list.
void startVariableDeclaration() final
Tells the factory that we're in a variable declaration.
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
factory_state
The enumeration of states in which the factory can be in.
BayesNet< GUM_SCALAR > * __bn
The constructed BayesNet.
virtual const DiscreteVariable & variable(Idx) const final
Returns a const ref to the ith var.
Idx __checkVariableModality(const std::string &name, const std::string &mod)
Check if var exists and if mod is one of it's modality, if not raise an NotFound exception.
NodeId endVariableDeclaration() final
Tells the factory that we're out of a variable declaration.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
std::vector< factory_state > __states
State stack.
Size cptDomainSize(NodeId n) const final
Returns the domainSize of the cpt for the node n.
void startRawProbabilityDeclaration(const std::string &var) final
Tells the factory that we're declaring a conditional probability table for some variable.
std::vector< std::string > __stringBag
Just to keep track of strings between two start/end calls.
const DiscreteVariable & varInBN(NodeId id) final
short-cut accessor for a DiscreveVariable in the BN
Abstract base class for all multi dimensionnal addressable.
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.
virtual Idx nbrDim() const override
Returns the number of vars in the multidimensional container.
Class for assigning/browsing values to tuples of discrete variables.
void setVariableCPT(const std::string &varName, MultiDimAdressable *table, bool redefineParents) final
Define a variable's CPT.
void endFactorizedProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
bool __foo_flag
Depending on the context this flag is used for some VERY important reasons.
Instantiation * __parents
Used when a factorized CPT is built.
virtual void set(const Instantiation &i, const GUM_SCALAR &value) const final
Default implementation of MultiDimContainer::set().
void setFirst()
Assign the first values to the tuple of the Instantiation.
void variableDescription(const std::string &desc) final
Tells the factory the current variable's description.
void __checkVariableName(const std::string &name)
Check if a variable with the given name exists, if not raise an NotFound exception.
void rawConditionalTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable) final
Fills the variable's table with the values in rawTable.
<agrum/multidim/multiDimImplementation.h>
Size Idx
Type for indexes.
void endNetworkDeclaration() final
Tells the factory that we're out of a network declaration.
const DiscreteVariable & variable(const std::string &name) const
Returns a constant reference on a variable given it's name.
void __checkModalityInBag(const std::string &mod)
Check if in __stringBag there is no other modality with the same name.
void addModality(const std::string &name) final
Adds a modality to the current variable.
bool __increment(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList)
Increment a modality counter for the __fillProbaWithValuesTable method.
bool __bar_flag
Depending on the context this flag is used for some VERY important reasons.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
MultiDimImplementation< GUM_SCALAR > * __impl
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls...
void __setCPTAndParents(const DiscreteVariable &var, Potential< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.
virtual const Sequence< const DiscreteVariable *> & variablesSequence() const final
Returns a const ref to the sequence of DiscreteVariable*.
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::string & name() const
returns the name of the variable
LabelizedVariable & addLabel(const std::string &aLabel)
add a label with a new index (we assume that we will NEVER remove a label)
Size NodeId
Type for node ids.
void setVariableValues(const std::vector< float > &values) final
same than below with gum::OperationNotAllowed exception if value's size not OK.
#define GUM_ERROR(type, msg)
void __resetParts()
Reset the different parts used to constructed the BayesNet.
A factory class to ease BayesNet construction.
bool end() const
Returns true if the Instantiation reached the end.
virtual ~BayesNetFactory()
Destructor.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.
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.