32 #define VERBOSITY(x) \ 34 if (isVerbose()) std::cerr << "[BN factory] " << x << std::endl; \ 43 template <
typename GUM_SCALAR >
51 for (
auto node : bn->
nodes()) {
54 "Name already used: " << bn->
variable(node).name());
64 template <
typename GUM_SCALAR >
80 template <
typename GUM_SCALAR >
90 <<
"[BN factory] Implementation defined for a variable but not used. " 91 "You should call endVariableDeclaration() before " 99 template <
typename GUM_SCALAR >
104 template <
typename GUM_SCALAR >
107 return __bn->variable(
id);
111 template <
typename GUM_SCALAR >
120 template <
typename GUM_SCALAR >
130 template <
typename GUM_SCALAR >
141 template <
typename GUM_SCALAR >
146 return __bn->cpt(n).domainSize();
150 template <
typename GUM_SCALAR >
162 template <
typename GUM_SCALAR >
164 const std::string& propName,
const std::string& propValue) {
165 __bn->setProperty(propName, propValue);
169 template <
typename GUM_SCALAR >
181 template <
typename GUM_SCALAR >
195 template <
typename GUM_SCALAR >
213 template <
typename GUM_SCALAR >
227 template <
typename GUM_SCALAR >
247 template <
typename GUM_SCALAR >
258 "An implementation for this variable is already " 260 }
else if (impl->
nbrDim() > 0) {
269 template <
typename GUM_SCALAR >
277 for (
size_t i = 2; i < __stringBag.size(); ++i) {
298 std::stringstream msg;
299 msg <<
"Not enough modalities (";
307 msg <<
") declared for variable ";
327 template <
typename GUM_SCALAR >
329 const std::string& var) {
334 std::vector< std::string >::iterator iter =
__stringBag.begin();
339 VERBOSITY(
"starting parents for " << var);
345 template <
typename GUM_SCALAR >
359 template <
typename GUM_SCALAR >
368 for (
size_t i = __stringBag.size() - 1; i > 0; --i) {
370 VERBOSITY(
" adding parent " << __stringBag[i] <<
" for " 385 template <
typename GUM_SCALAR >
387 const std::string& var) {
410 template <
typename GUM_SCALAR >
412 const std::vector< std::string >& variables,
413 const std::vector< float >& rawTable) {
421 template <
typename GUM_SCALAR >
423 const std::vector< std::string >& variables,
424 const std::vector< float >& rawTable) {
430 for (
size_t i = 0; i < variables.size(); ++i) {
438 std::vector< Idx > modCounter;
441 for (
NodeId i = 0; i < nbrVar; i++) {
442 modCounter.push_back(
Idx(0));
448 for (
NodeId i = 0; i < nbrVar; i++) {
449 cptInst.
chgVal(*(varList[i]), modCounter[i]);
452 if (j < rawTable.size()) {
453 table.
set(cptInst, (GUM_SCALAR)rawTable[j]);
455 table.
set(cptInst, (GUM_SCALAR)0);
462 template <
typename GUM_SCALAR >
464 const std::vector< float >& rawTable) {
472 template <
typename GUM_SCALAR >
474 const std::vector< float >& rawTable) {
487 (j < rawTable.size()) ? (GUM_SCALAR)rawTable[j++]
494 template <
typename GUM_SCALAR >
496 std::vector< gum::Idx >& modCounter,
500 for (
NodeId j = 0; j < modCounter.size(); j++) {
501 last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
506 if (last) {
return false; }
513 if (modCounter[i] == (varList[i]->domainSize() - 1)) {
529 template <
typename GUM_SCALAR >
542 template <
typename GUM_SCALAR >
544 const std::string& var) {
549 std::vector< std::string >::iterator iter =
__stringBag.begin();
557 template <
typename GUM_SCALAR >
569 template <
typename GUM_SCALAR >
582 template <
typename GUM_SCALAR >
584 const std::string& parent,
const std::string& modality) {
622 template <
typename GUM_SCALAR >
624 const std::vector< float >& values) {
639 for (
auto node :
__bn->parents(varId)) {
641 inst_default <<
__bn->variable(node);
647 inst.
incIn(inst_default)) {
650 inst.
val(var) < values.size() ? (GUM_SCALAR)values[inst.
val(var)]
658 for (var_inst.
setFirst(); !var_inst.
end(); ++var_inst) {
664 inst.
val(var) < values.size()
665 ? (GUM_SCALAR)values[inst.
val(var)]
673 template <
typename GUM_SCALAR >
675 const std::vector< float >& values) {
685 <<
" : invalid number of modalities: found " << values.size()
695 template <
typename GUM_SCALAR >
717 template <
typename GUM_SCALAR >
750 template <
typename GUM_SCALAR >
764 if (redefineParents) {
766 }
else if (pot->contains(var)) {
767 for (
auto node :
__bn->parents(varId)) {
768 if (!pot->contains(
__bn->variable(node))) {
770 "The CPT is not valid in the current BayesNet.");
775 __bn->_unsafeChangePotential(varId, pot);
781 template <
typename GUM_SCALAR >
790 template <
typename GUM_SCALAR >
793 std::string msg =
"Illegal state call (";
795 msg +=
") in state ";
834 msg +=
"Unknown state";
843 template <
typename GUM_SCALAR >
851 template <
typename GUM_SCALAR >
853 const std::string& name,
const std::string& mod) {
858 if (mod == var.
label(i)) {
return i; }
865 template <
typename GUM_SCALAR >
877 template <
typename GUM_SCALAR >
881 __bn->_dag.eraseParents(varId);
891 __bn->_unsafeChangePotential(varId, table);
895 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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.
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.