68#include <unordered_set>
101 template < std::
integral T >
200 template < GUM_Numeric GUM_SCALAR >
223 KTBN(
const KTBN< GUM_SCALAR >& source);
226 KTBN(KTBN< GUM_SCALAR >&& source)
noexcept;
229 KTBN< GUM_SCALAR >&
operator=(
const KTBN< GUM_SCALAR >& source);
232 KTBN< GUM_SCALAR >&
operator=(KTBN< GUM_SCALAR >&& source)
noexcept;
280 void add(std::string_view fast_description,
281 bool temporal =
true,
282 unsigned int default_nbrmod = 2);
290 void addTemporal(std::string_view fast_description,
unsigned int default_nbrmod = 2);
292 void addAtemporal(std::string_view fast_description,
unsigned int default_nbrmod = 2);
302 void erase(std::string_view base);
323 bool exists(std::string_view base)
const;
338 std::vector< std::pair< std::string, int > >
nodes()
const;
341 std::vector< std::pair< std::string, int > >
parents(std::string_view base,
int slice)
const;
343 std::vector< std::pair< std::string, int > >
parents(std::string_view node_name)
const;
346 std::vector< std::pair< std::string, int > >
children(std::string_view base,
int slice)
const;
348 std::vector< std::pair< std::string, int > >
children(std::string_view node_name)
const;
394 void addArc(std::string_view tailBase,
int tailSlice, std::string_view headBase,
int headSlice);
401 void addArc(std::string_view tail, std::string_view head);
412 void eraseArc(std::string_view tailBase,
414 std::string_view headBase,
419 void eraseArc(std::string_view tail, std::string_view head);
422 bool existsArc(std::string_view tailBase,
424 std::string_view headBase,
425 int headSlice)
const;
429 bool existsArc(std::string_view tail, std::string_view head)
const;
432 std::vector< std::pair< std::pair< std::string, int >, std::pair< std::string, int > > >
456 const Tensor< GUM_SCALAR >&
cpt(std::string_view base,
int slice)
const;
458 const Tensor< GUM_SCALAR >&
cpt(std::string_view node_name)
const;
498 void fillCPT(std::string_view base,
501 const std::vector< GUM_SCALAR >& distribution)
const;
539 void fillCPT(std::string_view node_name,
540 const std::map< std::variant< std::string, std::pair< std::string, int > >,
542 const std::vector< GUM_SCALAR >& distribution)
const;
548 void generateCPT(std::string_view base,
int slice)
const;
550 void generateCPT(std::string_view node_name)
const;
559 BayesNet< GUM_SCALAR >
toBN()
const;
575 BayesNet< GUM_SCALAR >
unroll(
Size nbTimeSlices)
const;
595 void save(std::string_view filename)
const;
609 static KTBN< GUM_SCALAR >
load(std::string_view filename);
653 static KTBN< GUM_SCALAR >
fromBN(
const BayesNet< GUM_SCALAR >& bn,
654 const std::unordered_set< std::string >& atemporalNodes = {},
655 std::vector< std::string >* warnings =
nullptr);
667 std::string
toDot()
const;
731 std::string
_encode_(std::string_view base,
int slice)
const;
736 std::pair< std::string, int >
_decodeName_(std::string_view name)
const;
741 std::pair< std::string, int >
_determineNode_(
const std::string& name)
const;
752 void _validateAdd_(
const std::string& base,
bool temporal)
const;
757 std::vector< std::string >* warnings);
768 bool highlightReplicated,
772 static std::string
_escapeDot_(std::string_view name);
775#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
780 template < GUM_Numeric GUM_SCALAR >
781 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
std::string _timeSlicesToDot_(const BayesNet< GUM_SCALAR > &bn, bool highlightReplicated, const dot::DotOptions &options) const
Renders bn as time-slice-clustered DOT. Shared engine behind toDot() (on _bn_) and toUnrolledDot() (o...
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. Used by 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).
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...
The options given to a toDot(const DotOptions&).
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.