60 template < GUM_Numeric GUM_SCALAR >
66 for (
auto node: bn->nodes()) {
78 template < GUM_Numeric GUM_SCALAR >
87 _bn_ =
new BayesNet< GUM_SCALAR >(*(source.
_bn_));
92 template < GUM_Numeric GUM_SCALAR >
101 std::cerr <<
"[BN factory] Implementation defined for a variable but not used. "
102 "You should call endVariableDeclaration() before "
110 template < GUM_Numeric GUM_SCALAR >
115 template < GUM_Numeric GUM_SCALAR >
117 return _bn_->variable(
id);
121 template < GUM_Numeric GUM_SCALAR >
129 template < GUM_Numeric GUM_SCALAR >
138 template < GUM_Numeric GUM_SCALAR >
142 return _bn_->variable(*p);
148 template < GUM_Numeric GUM_SCALAR >
150 return _bn_->cpt(n).domainSize();
154 template < GUM_Numeric GUM_SCALAR >
164 template < GUM_Numeric GUM_SCALAR >
166 std::string_view propValue) {
167 _bn_->setProperty(propName, propValue);
171 template < GUM_Numeric GUM_SCALAR >
182 template < GUM_Numeric GUM_SCALAR >
195 template < GUM_Numeric GUM_SCALAR >
208 template < GUM_Numeric GUM_SCALAR >
223 template < GUM_Numeric GUM_SCALAR >
236 +
") are not supported in Bayesian networks.")
245 template < GUM_Numeric GUM_SCALAR >
258 template < GUM_Numeric GUM_SCALAR >
270 template < GUM_Numeric GUM_SCALAR >
282 template < GUM_Numeric GUM_SCALAR >
301 template < GUM_Numeric GUM_SCALAR >
308 if (impl ==
nullptr) {
310 "An implementation for this variable is already "
312 }
else if (impl->nbrDim() > 0) {
321 template < GUM_Numeric GUM_SCALAR >
340 std::vector< int > domain;
367 if (var ==
nullptr) {
388 = std::format(
"Not enough modalities ({}) declared for variable {}",
403 template < GUM_Numeric GUM_SCALAR >
417 template < GUM_Numeric GUM_SCALAR >
431 template < GUM_Numeric GUM_SCALAR >
440 for (
size_t i =
_stringBag_.size() - 1; i > 0; --i) {
453 template < GUM_Numeric GUM_SCALAR >
475 template < GUM_Numeric GUM_SCALAR >
477 const std::vector< std::string >& variables,
478 const std::vector< float >& rawTable) {
486 template < GUM_Numeric GUM_SCALAR >
488 const std::vector< std::string >& variables,
489 const std::vector< float >& rawTable) {
494 table.fillWith(GUM_SCALAR(0.0));
496 for (
size_t i = 0; i < variables.size(); ++i) {
502 std::vector< Idx > modCounter;
505 for (
NodeId i = 0; i < nbrVar; i++) {
506 modCounter.push_back(
Idx(0));
509 for (
Idx j = 0; j < rawTable.size(); j++) {
510 for (
NodeId i = 0; i < nbrVar; i++) {
511 cptInst.
chgVal(*(varList[i]), modCounter[i]);
514 table.set(cptInst,
static_cast< GUM_SCALAR
>(rawTable[j]));
519 template < GUM_Numeric GUM_SCALAR >
528 template < GUM_Numeric GUM_SCALAR >
530 const std::vector< float >& rawTable) {
543 (j < rawTable.size()) ?
static_cast< GUM_SCALAR
>(rawTable[j++])
544 :
static_cast< GUM_SCALAR
>(0));
550 template < GUM_Numeric GUM_SCALAR >
555 for (
NodeId j = 0; j < modCounter.size(); j++) {
556 last = (modCounter[j] == (varList[j]->domainSize() - 1)) && last;
561 if (last) {
return false; }
568 if (modCounter[i] == (varList[i]->domainSize() - 1)) {
584 template < GUM_Numeric GUM_SCALAR >
595 template < GUM_Numeric GUM_SCALAR >
608 template < GUM_Numeric GUM_SCALAR >
620 template < GUM_Numeric GUM_SCALAR >
633 template < GUM_Numeric GUM_SCALAR >
635 std::string_view modality) {
642 (*_parents_) << parentVar;
673 template < GUM_Numeric GUM_SCALAR >
675 const std::vector< float >& values) {
690 for (
auto node:
_bn_->parents(varId)) {
691 if (!
_parents_->contains(
_bn_->variable(node))) { inst_default <<
_bn_->variable(node); }
698 inst.
val(var) < values.size() ?
static_cast< GUM_SCALAR
>(values[inst.
val(var)])
699 :
static_cast< GUM_SCALAR
>(0));
706 for (var_inst.
setFirst(); !var_inst.
end(); ++var_inst) {
712 inst.
val(var) < values.size()
713 ?
static_cast< GUM_SCALAR
>(values[inst.
val(var)])
714 :
static_cast< GUM_SCALAR
>(0));
721 template < GUM_Numeric GUM_SCALAR >
728 values.size() != var.domainSize()) {
730 var.name() <<
" : invalid number of modalities: found " << values.size()
731 <<
" while needed " << var.domainSize())
740 template < GUM_Numeric GUM_SCALAR >
761 template < GUM_Numeric GUM_SCALAR >
792 template < GUM_Numeric GUM_SCALAR >
795 bool redefineParents) {
796 auto pot =
dynamic_cast< Tensor< GUM_SCALAR >*
>(table);
807 if (redefineParents) {
809 }
else if (pot->contains(var)) {
810 for (
auto node:
_bn_->parents(varId)) {
811 if (!pot->contains(
_bn_->variable(node))) {
817 _bn_->_unsafeChangeTensor_(varId, pot);
823 template < GUM_Numeric GUM_SCALAR >
825 std::string msg =
"Illegal state call (";
827 msg +=
") in state ";
866 msg +=
"Unknown state";
875 template < GUM_Numeric GUM_SCALAR >
882 template < GUM_Numeric GUM_SCALAR >
884 std::string_view mod) {
890 if (mod == var.
label(i)) {
return i; }
897 template < GUM_Numeric GUM_SCALAR >
906 template < GUM_Numeric GUM_SCALAR >
908 Tensor< GUM_SCALAR >* table) {
910 _bn_->dag_.eraseParents(varId);
912 for (
auto v: table->variablesSequence()) {
920 _bn_->_unsafeChangeTensor_(varId, table);
924 template < GUM_Numeric GUM_SCALAR >
Headers of the BayesNetFactory class.
const DiscreteVariable & varInBN(NodeId id) final
short-cut accessor for a DiscreveVariable in the BN
std::vector< factory_state > _states_
State stack.
void startFactorizedProbabilityDeclaration(std::string_view var) final
Tells the factory that we're starting a factorized declaration.
void _checkVariableName_(std::string_view name) const
Check if a variable with the given name exists, if not raise an NotFound exception.
bool _bar_flag_
Depending on the context this flag is used for some VERY important reasons.
void endParentsDeclaration() final
Tells the factory that we've finished declaring parents for some variable.
BayesNet< GUM_SCALAR > * bayesNet()
Returns the BayesNet created by this factory.
Size cptDomainSize(NodeId n) const final
Returns the domainSize of the cpt for the node n.
bool _increment_(std::vector< gum::Idx > &modCounter, List< const DiscreteVariable * > &varList) const
Increment a modality counter for the fillProbaWithValuesTable method.
void _fillProbaWithValuesTable_(const std::vector< std::string > &variables, const std::vector< float > &rawTable)
Fill a tensor from a raw CPT.
HashTable< std::string, NodeId > _varNameMap_
Mapping between a declared variable's name and it's node id.
void setVariableCPT(std::string_view varName, MultiDimAdressable *table, bool redefineParents) final
Define a variable's CPT.
void setVariableValuesUnchecked(const std::vector< float > &values) final
Gives the values of the variable with respect to precedent parents modality.
void addModality(std::string_view name) final
Adds a modality to the current labelized variable.
void endNetworkDeclaration() final
Tells the factory that we're out of a network declaration.
void rawConditionalTable(const std::vector< std::string > &variables, const std::vector< float > &rawTable) final
Fills the variable's table with the values in rawTable.
BayesNet< GUM_SCALAR > * _bn_
The constructed BayesNet.
~BayesNetFactory() override
Destructor.
void _setCPTAndParents_(const DiscreteVariable &var, Tensor< GUM_SCALAR > *table)
Sub method of setVariableCPT() which redefine the BayesNet's DAG with respect to table.
const DiscreteVariable & variable(std::string_view name) const
Returns a constant reference on a variable given it's name.
void addParent(std::string_view var) final
Tells the factory for which variable we're declaring parents.
void endFactorizedProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
void setVariable(const DiscreteVariable &var) final
Define a variable.
std::vector< std::string > _stringBag_
Just to keep track of strings between two start/end calls.
NodeId variableId(std::string_view name) const final
Returns the NodeId of a variable given it's name.
void startFactorizedEntry() final
Tells the factory that we start an entry of a factorized conditional probability table.
void startRawProbabilityDeclaration(std::string_view var) final
Tells the factory that we're declaring a conditional probability table for some variable.
void endFactorizedEntry() final
Tells the factory that we end an entry of a factorized conditional probability table.
bool _foo_flag_
Depending on the context this flag is used for some VERY important reasons.
void _resetParts_()
Reset the different parts used to constructed the BayesNet.
factory_state state() const final
Returns the current state of the factory.
void _checkModalityInBag_(std::string_view mod)
Check if in stringBag there is no other modality with the same name.
void addMax(const long &max) override
Adds the max value of the current range variable.
Idx _checkVariableModality_(std::string_view name, std::string_view mod)
Check if var exists and if mod is one of it's modality, if not raise an NotFound exception.
Instantiation * _parents_
Used when a factorized CPT is built.
void endRawProbabilityDeclaration() final
Tells the factory that we finished declaring a conditional probability table.
void variableType(const VarType &type) override
Tells the factory the current variable's type.
void setVariableValues(const std::vector< float > &values) final
same than below with gum::OperationNotAllowed exception if value's size not OK.
void startNetworkDeclaration() final
Tells the factory that we're in a network declaration.
void startParentsDeclaration(std::string_view var) final
Tells the factory that we're declaring parents for some variable.
void variableDescription(std::string_view desc) final
Tells the factory the current variable's description.
void addNetworkProperty(std::string_view propName, std::string_view propValue) final
Tells the factory to add a property to the current network.
void setVariableCPTImplementation(MultiDimAdressable *adressable) final
Defines the implementation to use for var's Tensor.
MultiDimImplementation< GUM_SCALAR > * _impl_
Implementation of variable between two startVariableDeclaration/endVariableDeclaration calls.
void addTick(const GUM_SCALAR &tick)
Adds a tick to the current Discretized variable.
void startVariableDeclaration() final
Tells the factory that we're in a variable declaration.
void addMin(const long &min) override
Adds the min value of the current range variable.
void _illegalStateError_(std::string_view s)
Raise an OperationNotAllowed with the message "Illegal state.".
void setParentModality(std::string_view parent, std::string_view modality) final
Tells the factory on which modality we want to instantiate one of variable's parent.
NodeId endVariableDeclaration() final
Tells the factory that we're out of a variable declaration.
void variableName(std::string_view name) final
Tells the factory the current variable's name.
BayesNetFactory(BayesNet< GUM_SCALAR > *bn)
Use this constructor if you want to use an already created BayesNet.
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.
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 setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
void incNotVar(const DiscreteVariable &v)
Operator increment for vars which are not v.
void incIn(const Instantiation &i)
Operator increment for the variables in i.
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.
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