68#include <unordered_set>
97 template < std::
integral T >
196 template < GUM_Numeric GUM_SCALAR >
219 KTBN(
const KTBN< GUM_SCALAR >& source);
222 KTBN(KTBN< GUM_SCALAR >&& source)
noexcept;
225 KTBN< GUM_SCALAR >&
operator=(
const KTBN< GUM_SCALAR >& source);
228 KTBN< GUM_SCALAR >&
operator=(KTBN< GUM_SCALAR >&& source)
noexcept;
276 void add(std::string_view fast_description,
277 bool temporal =
true,
278 unsigned int default_nbrmod = 2);
286 void addTemporal(std::string_view fast_description,
unsigned int default_nbrmod = 2);
288 void addAtemporal(std::string_view fast_description,
unsigned int default_nbrmod = 2);
298 void erase(std::string_view base);
319 bool exists(std::string_view base)
const;
334 std::vector< std::pair< std::string, int > >
nodes()
const;
337 std::vector< std::pair< std::string, int > >
parents(std::string_view base,
int slice)
const;
339 std::vector< std::pair< std::string, int > >
parents(std::string_view node_name)
const;
342 std::vector< std::pair< std::string, int > >
children(std::string_view base,
int slice)
const;
344 std::vector< std::pair< std::string, int > >
children(std::string_view node_name)
const;
390 void addArc(std::string_view tailBase,
int tailSlice, std::string_view headBase,
int headSlice);
397 void addArc(std::string_view tail, std::string_view head);
408 void eraseArc(std::string_view tailBase,
410 std::string_view headBase,
415 void eraseArc(std::string_view tail, std::string_view head);
418 bool existsArc(std::string_view tailBase,
420 std::string_view headBase,
421 int headSlice)
const;
425 bool existsArc(std::string_view tail, std::string_view head)
const;
428 std::vector< std::pair< std::pair< std::string, int >, std::pair< std::string, int > > >
452 const Tensor< GUM_SCALAR >&
cpt(std::string_view base,
int slice)
const;
454 const Tensor< GUM_SCALAR >&
cpt(std::string_view node_name)
const;
494 void fillCPT(std::string_view base,
497 const std::vector< GUM_SCALAR >& distribution)
const;
535 void fillCPT(std::string_view node_name,
536 const std::map< std::variant< std::string, std::pair< std::string, int > >,
538 const std::vector< GUM_SCALAR >& distribution)
const;
544 void generateCPT(std::string_view base,
int slice)
const;
546 void generateCPT(std::string_view node_name)
const;
555 BayesNet< GUM_SCALAR >
toBN()
const;
571 BayesNet< GUM_SCALAR >
unroll(
Size nbTimeSlices)
const;
591 void save(std::string_view filename)
const;
605 static KTBN< GUM_SCALAR >
load(std::string_view filename);
649 static KTBN< GUM_SCALAR >
fromBN(
const BayesNet< GUM_SCALAR >& bn,
650 const std::unordered_set< std::string >& atemporalNodes = {},
651 std::vector< std::string >* warnings =
nullptr);
663 std::string
toDot()
const;
717 std::string
_encode_(std::string_view base,
int slice)
const;
722 std::pair< std::string, int >
_decodeName_(std::string_view name)
const;
727 std::pair< std::string, int >
_determineNode_(
const std::string& name)
const;
738 void _validateAdd_(
const std::string& base,
bool temporal)
const;
743 std::vector< std::string >* warnings);
753 std::string
_timeSlicesToDot_(
const BayesNet< GUM_SCALAR >& bn,
bool highlightReplicated)
const;
757 static std::string
_escapeDot_(std::string_view name);
760#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
765 template < GUM_Numeric GUM_SCALAR >
766 std::ostream&
operator<<(std::ostream& output,
const KTBN< GUM_SCALAR >& kdbn);
Class representing Bayesian networks.
Inline implementations of gum::KTBNModality's non-template members.
Template implementation of the gum::KTBN class.
Base class for discrete random variable.
std::vector< std::pair< std::string, int > > children(std::string_view base, int slice) const
Children of a node as (base, slice) pairs (ATEMPORAL if atemporal).
void clear()
Removes all variables and arcs, keeping the order .
void addTemporal(const DiscreteVariable &var)
Convenience shortcut for add(var, true).
const std::unordered_set< std::string > & temporalVarNames() const
void addAtemporal(const DiscreteVariable &var)
Convenience shortcut for add(var, false).
BayesNet< GUM_SCALAR > toBN() const
void addArc(std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)
Adds an arc between two (process, slice) endpoints.
std::vector< std::pair< std::pair< std::string, int >, std::pair< std::string, int > > > arcs() const
std::pair< std::string, int > _decodeName_(std::string_view name) const
Purely syntactic parse of an engine name → (base, slice). Slice is ATEMPORAL when there is no [digits...
std::vector< std::pair< std::string, int > > nodes() const
BayesNet< GUM_SCALAR > _bn_
The underlying Bayesian network used as a storage engine for the template.
static KTBN< GUM_SCALAR > load(std::string_view filename)
Loads a k-DBN from a GUM file produced by save().
void erase(std::string_view base)
Removes a variable and all its incident arcs.
const DiscreteVariable & variable(std::string_view base, int slice) const
Returns the gum::DiscreteVariable of a (process, slice) couple.
std::string summaryGraph() const
Returns the Graphviz DOT string of the summary graph: the projection of the transition kernel alone (...
bool existsArc(std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice) const
void save(std::string_view filename) const
Saves the template in the GUM format (text .jgum or binary .bgum).
static std::string _escapeDot_(std::string_view name)
Escapes double quotes for a DOT identifier or label. Shared by timeSlicesToDot() and summaryGraph().
NodeId _validateVariable_(std::string_view base, int slice) const
Resolves and validates a (base, slice) endpoint into its NodeId.
std::unordered_set< std::string > _temporal_
Base names of the registered temporal processes.
void eraseArc(std::string_view tailBase, int tailSlice, std::string_view headBase, int headSlice)
Removes an arc between two (process, slice) endpoints.
std::vector< std::pair< std::string, int > > parents(std::string_view base, int slice) const
Parents of a node as (base, slice) pairs (ATEMPORAL if atemporal).
const std::unordered_set< std::string > & atemporalVarNames() const
void generateCPT(std::string_view base, int slice) const
Randomly generates the CPT of a single node.
Size nbAtemporalVars() const
std::string _encode_(std::string_view base, int slice) const
Encodes (base, slice) → engine name: base[t], or base if atemporal.
const Tensor< GUM_SCALAR > & cpt(std::string_view base, int slice) const
Returns the CPT of a (process, slice) couple.
void generateCPTs() const
Randomly generates every CPT of the template.
std::unordered_set< std::string > _atemporal_
Base names of the registered atemporal variables.
std::string baseName(const DiscreteVariable &var) const
Returns the base name (without bracket encoding) of var.
static constexpr int ATEMPORAL
Conventional time-slice value denoting an atemporal (static) variable.
std::string toUnrolledDot(Size T, bool highlightReplicated=false) const
Returns a Graphviz DOT string of the k-DBN unrolled over T time slices.
std::string bnToDot() const
Returns the Graphviz DOT string of the underlying storage BayesNet.
KTBN< GUM_SCALAR > & operator=(const KTBN< GUM_SCALAR > &source)
Copy assignment operator.
int timeSlice(const DiscreteVariable &var) const
The time slice of var, or ATEMPORAL if it is atemporal.
void changeVariableName(std::string_view oldBase, std::string_view newBase)
Renames a variable (temporal process or atemporal variable).
std::string toDot() const
Returns a Graphviz DOT string with one cluster per time slice.
std::pair< std::string, int > _determineNode_(const std::string &name) const
Cache-aware classification of a node name → (base, slice): nodes registered in _atemporal_ (atemporal...
BayesNet< GUM_SCALAR > unroll(Size nbTimeSlices) const
Unrolls the k-DBN into a standard gum::BayesNet.
KTBN(Size k=2)
Default constructor.
void fillCPT(std::string_view base, int slice, const std::map< std::pair< std::string, int >, KTBNModality > &parents, const std::vector< GUM_SCALAR > &distribution) const
Fills one conditional distribution P(node | parent configuration).
std::string _timeSlicesToDot_(const BayesNet< GUM_SCALAR > &bn, bool highlightReplicated) const
Renders bn as time-slice-clustered DOT. Shared engine behind toDot() (on _bn_) and toUnrolledDot() (o...
Size _k_
The order (number of time slices in the template).
Size nbTemporalVars() const
std::vector< std::pair< std::string, int > > _determineNodeSet_(const NodeSet &ids) const
Maps a set of node ids to (base, slice) pairs (via determineNode).
bool exists(std::string_view base) const
void _validateAdd_(const std::string &base, bool temporal) const
Checks that a variable named base can be added.
std::string toString() const
void add(const DiscreteVariable &var, bool temporal=true)
Adds a variable to the k-DBN.
static std::pair< std::string, bool > _resolveGumFormat_(std::string_view filename)
Resolves a user filename to (filepath, binary): ensures a .jgum/.bgum extension (....
static KTBN< GUM_SCALAR > fromBN(const BayesNet< GUM_SCALAR > &bn, const std::unordered_set< std::string > &atemporalNodes={}, std::vector< std::string > *warnings=nullptr)
Builds a k-DBN from an existing gum::BayesNet, reading its node names under one of two mutually exclu...
void _determineNodesFromBN_(const std::unordered_set< std::string > &atemporalNodes, std::vector< std::string > *warnings)
Rebuilds the cached name sets from the storage engine content (used by fromBN()/load(); decodes names...
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
gum is the global namespace for all aGrUM entities
std::ostream & operator<<(std::ostream &stream, const AVLTree< Val, Cmp > &tree)
display the content of a tree
template class GUM_PUBLIC_KTBN KTBN< double >
A parent's value in gum::KTBN::fillCPT(): a modality index or a modality label.
Idx index
The index, when isLabel is false.
std::string label
The label, when isLabel is true.
bool isLabel
Whether the value was spelled as a label rather than an index.
KTBNModality(T modality)
From a modality index.
KTBNModality(const char *modality)
From a modality label.
KTBNModality(std::string modality)
From a modality label.
Idx toIndex(const DiscreteVariable &var) const
Resolves this value to a modality index of var, looking the label up when one was given.