aGrUM 2.3.2
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-2025 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-2025 *
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
58namespace gum {
59
80 template < typename GUM_SCALAR >
82 public:
83 // ==========================================================================
85 // ==========================================================================
87
94 explicit BayesNetFactory(BayesNet< GUM_SCALAR >* bn);
95
106
116 virtual ~BayesNetFactory();
117
119 // ==========================================================================
121 // ==========================================================================
123
129 BayesNet< GUM_SCALAR >* bayesNet();
130
132 const DiscreteVariable& varInBN(NodeId id) final;
133
135 factory_state state() const final;
136
139 NodeId variableId(const std::string& name) const final;
140
143 const DiscreteVariable& variable(const std::string& name) const;
144
147 Size cptDomainSize(NodeId n) const final;
148
150 // ==========================================================================
152 // ==========================================================================
154
156 void startNetworkDeclaration() final;
157
159 void variableType(const VarType& type);
160
162 void addNetworkProperty(const std::string& propName, const std::string& propValue) final;
163
165 void endNetworkDeclaration() final;
166
168 // ==========================================================================
170 // ==========================================================================
172
174 void startVariableDeclaration() final;
175
179 void variableName(const std::string& name) final;
180
182 void variableDescription(const std::string& desc) final;
183
185 void addModality(const std::string& name) final;
186
188 void addMin(const long& min);
189
191 void addMax(const long& max);
192
194 void addTick(const GUM_SCALAR& tick);
195
212 void setVariableCPTImplementation(MultiDimAdressable* adressable) final;
213
221
223 // ==========================================================================
225 // ==========================================================================
227
231 void startParentsDeclaration(const std::string& var) final;
232
236 void addParent(const std::string& var) final;
237
246 void endParentsDeclaration() final;
247
249 // ==========================================================================
251 // ==========================================================================
253
257 void startRawProbabilityDeclaration(const std::string& var) final;
258
279 void rawConditionalTable(const std::vector< std::string >& variables,
280 const std::vector< float >& rawTable) final;
281
289 void rawConditionalTable(const std::vector< float >& rawTable) final;
290
293 void endRawProbabilityDeclaration() final;
294
296 // ==========================================================================
298 // (NONE -> RAW_CPT)
299 // ==========================================================================
301
303 void startFactorizedProbabilityDeclaration(const std::string& var) final;
304
307 void startFactorizedEntry() final;
308
311 void endFactorizedEntry() final;
312
315 void setParentModality(const std::string& parent, const std::string& modality) final;
316
347 void setVariableValuesUnchecked(const std::vector< float >& values) final;
348
354 void setVariableValues(const std::vector< float >& values) final;
355
359
361 // ==========================================================================
364 // ==========================================================================
366
382 void setVariable(const DiscreteVariable& var) final;
383
406 void setVariableCPT(const std::string& varName,
407 MultiDimAdressable* table,
408 bool redefineParents) final;
409
411
412 private:
413 // ==========================================================================
415 // ==========================================================================
417
421
425
427 std::vector< std::string > _stringBag_;
428
431
435
437
439 std::vector< factory_state > _states_;
440
442 BayesNet< GUM_SCALAR >* _bn_;
443
446
449
451 void _illegalStateError_(const std::string& s);
452
455 void _checkVariableName_(const std::string& name) const;
456
459 Idx _checkVariableModality_(const std::string& name, const std::string& mod);
460
462 void _checkModalityInBag_(const std::string& mod);
463
466 void _setCPTAndParents_(const DiscreteVariable& var, Tensor< GUM_SCALAR >* table);
467
469 void _resetParts_();
470
472 void _fillProbaWithValuesTable_(const std::vector< std::string >& variables,
473 const std::vector< float >& rawTable);
475 void _fillProbaWithValuesTable_(const std::vector< float >& rawTable);
476
478 bool _increment_(std::vector< gum::Idx >& modCounter,
479 List< const DiscreteVariable* >& varList) const;
480 };
481
482
483#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
484 extern template class BayesNetFactory< double >;
485#endif
486
487} /* namespace gum */
488
490
491#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.
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.
BayesNetFactory< GUM_SCALAR > & operator=(const BayesNetFactory< GUM_SCALAR > &source)=delete
Copy operator is illegal, use only copy constructor.
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.
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:379
Abstract base class for all multi dimensionnal addressable.
<agrum/base/multidim/multiDimImplementation.h>
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:60