aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
BayesNetFactory.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
48
49#ifndef GUM_BAYESNET_FACTORY_H
50#define GUM_BAYESNET_FACTORY_H
51
52#include <string>
53#include <vector>
54
55#include <agrum/BN/BayesNet.h>
57
58#include <string_view>
59
60namespace gum {
61
82 template < GUM_Numeric GUM_SCALAR >
84 public:
85 // ==========================================================================
87 // ==========================================================================
89
96 explicit BayesNetFactory(BayesNet< GUM_SCALAR >* bn);
97
108
118 ~BayesNetFactory() override;
119
121 // ==========================================================================
123 // ==========================================================================
125
131 BayesNet< GUM_SCALAR >* bayesNet();
132
134 const DiscreteVariable& varInBN(NodeId id) final;
135
137 factory_state state() const final;
138
141 NodeId variableId(std::string_view name) const final;
142
145 const DiscreteVariable& variable(std::string_view name) const;
146
149 Size cptDomainSize(NodeId n) const final;
150
152 // ==========================================================================
154 // ==========================================================================
156
158 void startNetworkDeclaration() final;
159
161 void variableType(const VarType& type) override;
162
164 void addNetworkProperty(std::string_view propName, std::string_view propValue) final;
165
167 void endNetworkDeclaration() final;
168
170 // ==========================================================================
172 // ==========================================================================
174
176 void startVariableDeclaration() final;
177
181 void variableName(std::string_view name) final;
182
184 void variableDescription(std::string_view desc) final;
185
187 void addModality(std::string_view name) final;
188
190 void addMin(const long& min) override;
191
193 void addMax(const long& max) override;
194
196 void addTick(const GUM_SCALAR& tick);
197
214 void setVariableCPTImplementation(MultiDimAdressable* adressable) final;
215
223
225 // ==========================================================================
227 // ==========================================================================
229
233 void startParentsDeclaration(std::string_view var) final;
234
238 void addParent(std::string_view var) final;
239
248 void endParentsDeclaration() final;
249
251 // ==========================================================================
253 // ==========================================================================
255
259 void startRawProbabilityDeclaration(std::string_view var) final;
260
281 void rawConditionalTable(const std::vector< std::string >& variables,
282 const std::vector< float >& rawTable) final;
283
291 void rawConditionalTable(const std::vector< float >& rawTable) final;
292
295 void endRawProbabilityDeclaration() final;
296
298 // ==========================================================================
300 // (NONE -> RAW_CPT)
301 // ==========================================================================
303
305 void startFactorizedProbabilityDeclaration(std::string_view var) final;
306
309 void startFactorizedEntry() final;
310
313 void endFactorizedEntry() final;
314
317 void setParentModality(std::string_view parent, std::string_view modality) final;
318
349 void setVariableValuesUnchecked(const std::vector< float >& values) final;
350
356 void setVariableValues(const std::vector< float >& values) final;
357
361
363 // ==========================================================================
366 // ==========================================================================
368
384 void setVariable(const DiscreteVariable& var) final;
385
408 void setVariableCPT(std::string_view varName,
409 MultiDimAdressable* table,
410 bool redefineParents) final;
411
413
414 private:
415 // ==========================================================================
417 // ==========================================================================
419
423
427
429 std::vector< std::string > _stringBag_;
430
433
437
439
441 std::vector< factory_state > _states_;
442
444 BayesNet< GUM_SCALAR >* _bn_;
445
448
451
453 void _illegalStateError_(std::string_view s);
454
457 void _checkVariableName_(std::string_view name) const;
458
461 Idx _checkVariableModality_(std::string_view name, std::string_view mod);
462
464 void _checkModalityInBag_(std::string_view mod);
465
468 void _setCPTAndParents_(const DiscreteVariable& var, Tensor< GUM_SCALAR >* table);
469
471 void _resetParts_();
472
474 void _fillProbaWithValuesTable_(const std::vector< std::string >& variables,
475 const std::vector< float >& rawTable);
477 void _fillProbaWithValuesTable_(const std::vector< float >& rawTable);
478
480 bool _increment_(std::vector< gum::Idx >& modCounter,
481 List< const DiscreteVariable* >& varList) const;
482 };
483
484
485#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
486 extern template class BayesNetFactory< double >;
487#endif
488
489} /* namespace gum */
490
492
493#endif // GUM_BAYESNET_FACTORY_H
Implementation of the BayesNetFactory class.
Class representing Bayesian networks.
Header of the IBayesNetFactory class.
A factory class to ease BayesNet construction.
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.
BayesNetFactory< GUM_SCALAR > & operator=(const BayesNetFactory< GUM_SCALAR > &source)=delete
Copy operator is illegal, use only copy constructor.
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.
factory_state
The enumeration of states in which the factory can be in.
Class for assigning/browsing values to tuples of discrete variables.
Generic doubly linked lists.
Definition list.h:378
Abstract base class for all multi dimensionnal addressable.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
Size NodeId
Type for node ids.
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
VarType
Definition variable.h:62