59 template <
typename GUM_SCALAR >
65 for (
auto node: bn->nodes()) {
77 template <
typename GUM_SCALAR >
87 _bn_ =
new BayesNet< GUM_SCALAR >(*(source.
_bn_));
92 template <
typename GUM_SCALAR >
101 std::cerr <<
"[BN factory] Implementation defined for a variable but not used. "
102 "You should call endVariableDeclaration() before "
110 template <
typename GUM_SCALAR >
115 template <
typename GUM_SCALAR >
117 return _bn_->variable(
id);
121 template <
typename GUM_SCALAR >
129 template <
typename GUM_SCALAR >
138 template <
typename GUM_SCALAR >
149 template <
typename GUM_SCALAR >
151 return _bn_->cpt(n).domainSize();
155 template <
typename GUM_SCALAR >
165 template <
typename GUM_SCALAR >
167 const std::string& propValue) {
168 _bn_->setProperty(propName, propValue);
172 template <
typename GUM_SCALAR >
183 template <
typename GUM_SCALAR >
196 template <
typename GUM_SCALAR >
209 template <
typename GUM_SCALAR >
224 template <
typename GUM_SCALAR >
237 +
") are not supported in Bayesian networks.")
246 template <
typename GUM_SCALAR >
259 template <
typename GUM_SCALAR >
271 template <
typename GUM_SCALAR >
283 template <
typename GUM_SCALAR >
302 template <
typename GUM_SCALAR >
310 if (impl ==
nullptr) {
312 "An implementation for this variable is already "
314 }
else if (impl->nbrDim() > 0) {
323 template <
typename GUM_SCALAR >
342 std::vector< int > domain;
369 if (var ==
nullptr) {
389 std::stringstream msg;
390 msg <<
"Not enough modalities (";
398 msg <<
") declared for variable ";
418 template <
typename GUM_SCALAR >
432 template <
typename GUM_SCALAR >
446 template <
typename GUM_SCALAR >
455 for (
size_t i =
_stringBag_.size() - 1; i > 0; --i) {
468 template <
typename GUM_SCALAR >
491 template <
typename GUM_SCALAR >
493 const std::vector< std::string >& variables,
494 const std::vector< float >& rawTable) {
502 template <
typename GUM_SCALAR >
504 const std::vector< std::string >& variables,
505 const std::vector< float >& rawTable) {
510 table.fillWith(GUM_SCALAR(0.0));
512 for (
size_t i = 0; i < variables.size(); ++i) {
518 std::vector< Idx > modCounter;
521 for (
NodeId i = 0; i < nbrVar; i++) {
522 modCounter.push_back(
Idx(0));
525 for (
Idx j = 0; j < rawTable.size(); j++) {
526 for (
NodeId i = 0; i < nbrVar; i++) {
527 cptInst.
chgVal(*(varList[i]), modCounter[i]);
530 table.set(cptInst,
static_cast< GUM_SCALAR
>(rawTable[j]));
535 template <
typename GUM_SCALAR >
545 template <
typename GUM_SCALAR >
547 const std::vector< float >& rawTable) {
560 (j < rawTable.size()) ?
static_cast< GUM_SCALAR
>(rawTable[j++])
561 :
static_cast< GUM_SCALAR
>(0));
567 template <
typename GUM_SCALAR >
573 for (
NodeId j = 0; j < modCounter.size(); j++) {
574 last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
579 if (last) {
return false; }
586 if (modCounter[i] == (varList[i]->domainSize() - 1)) {
602 template <
typename GUM_SCALAR >
613 template <
typename GUM_SCALAR >
627 template <
typename GUM_SCALAR >
639 template <
typename GUM_SCALAR >
652 template <
typename GUM_SCALAR >
654 const std::string& modality) {
691 template <
typename GUM_SCALAR >
693 const std::vector< float >& values) {
708 for (
auto node:
_bn_->parents(varId)) {
709 if (!
_parents_->contains(
_bn_->variable(node))) { inst_default <<
_bn_->variable(node); }
716 inst.
val(var) < values.size() ?
static_cast< GUM_SCALAR
>(values[inst.
val(var)])
717 :
static_cast< GUM_SCALAR
>(0));
724 for (var_inst.
setFirst(); !var_inst.
end(); ++var_inst) {
730 inst.
val(var) < values.size()
731 ?
static_cast< GUM_SCALAR
>(values[inst.
val(var)])
732 :
static_cast< GUM_SCALAR
>(0));
739 template <
typename GUM_SCALAR >
746 values.size() != var.domainSize()) {
748 var.name() <<
" : invalid number of modalities: found " << values.size()
749 <<
" while needed " << var.domainSize())
758 template <
typename GUM_SCALAR >
779 template <
typename GUM_SCALAR >
812 template <
typename GUM_SCALAR >
815 bool redefineParents) {
816 auto pot =
dynamic_cast< Tensor< GUM_SCALAR >*
>(table);
827 if (redefineParents) {
829 }
else if (pot->contains(var)) {
830 for (
auto node:
_bn_->parents(varId)) {
831 if (!pot->contains(
_bn_->variable(node))) {
837 _bn_->_unsafeChangeTensor_(varId, pot);
843 template <
typename GUM_SCALAR >
845 std::string msg =
"Illegal state call (";
847 msg +=
") in state ";
886 msg +=
"Unknown state";
895 template <
typename GUM_SCALAR >
902 template <
typename GUM_SCALAR >
904 const std::string& mod) {
909 if (mod == var.
label(i)) {
return i; }
916 template <
typename GUM_SCALAR >
925 template <
typename GUM_SCALAR >
927 Tensor< GUM_SCALAR >* table) {
929 _bn_->dag_.eraseParents(varId);
931 for (
auto v: table->variablesSequence()) {
939 _bn_->_unsafeChangeTensor_(varId, table);
943 template <
typename GUM_SCALAR >
Headers of the BayesNetFactory class.
factory_state state() const final
Returns the current state of the factory.
void variableDescription(const std::string &desc) final
Tells the factory the current variable's description.
void endRawProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
void addTick(const GUM_SCALAR &tick)
Adds a tick to the current Discretized variable.
void startFactorizedProbabilityDeclaration(const std::string &var) final
Tells the factory that we're starting a factorized declaration.
std::vector< factory_state > _states_
State stack.
BayesNet< GUM_SCALAR > * bayesNet()
Returns the BayesNet created by this factory.
const DiscreteVariable & variable(const std::string &name) const
Returns a constant reference on a variable given it's name.
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
void _setCPTAndParents_(const DiscreteVariable &var, Tensor< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.
void rawConditionalTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable) final
Fills the variable's table with the values in rawTable.
void startParentsDeclaration(const std::string &var) final
Tells the factory that we're declaring parents for some variable.
void _illegalStateError_(const std::string &s)
Raise an OperationNotAllowed with the message "Illegal state.".
Size cptDomainSize(NodeId n) const final
Returns the domainSize of the cpt for the node n.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable's name and it's node id.
void startNetworkDeclaration() final
Tells the factory that we're in a network declaration.
void variableType(const VarType &type)
Tells the factory the current variable's type.
void endFactorizedEntry() final
Tells the factory that we end an entry of a factorized conditional probability table.
void addModality(const std::string &name) final
Adds a modality to the current labelized variable.
void startFactorizedEntry() final
Tells the factory that we start an entry of a factorized conditional probability table.
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
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.
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.
void startRawProbabilityDeclaration(const std::string &var) final
Tells the factory that we're declaring a conditional probability table for some variable.
void _checkVariableName_(const std::string &name) const
Check if a variable with the given name exists, if not raise an NotFound exception.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
void addMin(const long &min)
Adds the min value of the current range variable.
void setVariableCPTImplementation(MultiDimAdressable *adressable) final
Defines the implementation to use for var's Tensor.
void endNetworkDeclaration() final
Tells the factory that we're out of a network declaration.
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.
const DiscreteVariable & varInBN(NodeId id) final
short-cut accessor for a DiscreveVariable in the BN
NodeId endVariableDeclaration() final
Tells the factory that we're out of a variable declaration.
void setVariable(const DiscreteVariable &var) final
Define a variable.
virtual ~BayesNetFactory()
Destructor.
Instantiation * _parents_
Used when a factorized CPT is built.
void startVariableDeclaration() final
Tells the factory that we're in a variable declaration.
void _checkModalityInBag_(const std::string &mod)
Check if in stringBag there is no other modality with the same name.
NodeId variableId(const std::string &name) const final
Returns the NodeId of a variable given it's name.
void addNetworkProperty(const std::string &propName, const std::string &propValue) final
Tells the factory to add a property to the current network.
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.
void variableName(const std::string &name) final
Tells the factory the current variable's name.
void endParentsDeclaration() final
Tells the factory that we've finished declaring parents for some variable.
bool _increment_(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList) const
Increment a modality counter for the fillProbaWithValuesTable method.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.
void addParent(const std::string &var) final
Tells the factory for which variable we're declaring parents.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.
void setVariableValues(const std::vector< float > &values) final
same than below with gum::OperationNotAllowed exception if value's size not OK.
void _resetParts_()
Reset the different parts used to constructed the BayesNet.
void _fillProbaWithValuesTable_(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a tensor from a raw CPT.
void addMax(const long &max)
Adds the max value of the current range variable.
Base class for discrete random variable.
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
virtual Size domainSize() const =0
Class for discretized random variable.
Exception : a similar element already exists.
factory_state
The enumeration of states in which the factory can be in.
Class for assigning/browsing values to tuples of discrete variables.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
Instantiation & chgVal(const DiscreteVariable &v, Idx newval)
Assign newval to variable v in the Instantiation.
bool end() const
Returns true if the Instantiation reached the end.
void incOut(const Instantiation &i)
Operator increment for the variables not in i.
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
void setFirstNotVar(const DiscreteVariable &v)
Assign the first values to variables different of v.
void setFirstIn(const Instantiation &i)
Assign the first values in the Instantiation for the variables in i.
Instantiation & setVals(const Instantiation &i)
Assign the values from i in the Instantiation.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setFirst()
Assign the first values to the tuple of the Instantiation.
void setFirstOut(const Instantiation &i)
Assign the first values in the Instantiation for the variables not in i.
void unsetEnd()
Alias for unsetOverflow().
Generic doubly linked lists.
Size size() const noexcept
Returns the number of elements in the list.
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Abstract base class for all multi dimensionnal addressable.
<agrum/base/multidim/multiDimImplementation.h>
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
Defines a discrete random variable over an integer interval.
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities