← Documentation
ADS-IDAC Engine 5.0.0
bncore::Graph Class Reference

Directed graphical model over discrete variables. More...

#include <graph.hpp>

Public Member Functions

 Graph ()=default
 
NodeId add_variable (const std::string &name, const std::vector< std::string > &states)
 Adds a variable. More...
 
void split_state (NodeId id, std::size_t state_idx, const std::string &new_state1, const std::string &new_state2)
 Replaces one state of a variable by two states. More...
 
void add_edge (NodeId parent, NodeId child)
 Adds a directed edge from parent to child. More...
 
void add_edge (const std::string &parent_name, const std::string &child_name)
 Adds a directed edge between two variables given by name. More...
 
void set_cpt (NodeId id, const std::vector< double > &cpt)
 Stores the conditional probability table of a variable. More...
 
void set_cpt (const std::string &name, const std::vector< double > &cpt)
 Stores the conditional probability table of a variable given by name. See set_cpt(NodeId, const std::vector<double> &). More...
 
const VariableMetadata & get_variable (NodeId id) const
 Metadata of a variable. More...
 
const VariableMetadata & get_variable (const std::string &name) const
 Metadata of a variable given by name. More...
 
const std::vector< NodeId > & get_parents (NodeId id) const
 Parents of a variable, in the order their edges were added. More...
 
const std::vector< NodeId > & get_children (NodeId id) const
 Children of a variable, in the order their edges were added. More...
 
std::size_t num_variables () const
 Number of variables added so far. More...
 

Private Attributes

std::vector< VariableMetadata > variables_
 indexed by NodeId More...
 
std::unordered_map< std::string, NodeId > name_to_id_
 
std::vector< std::vector< NodeId > > parents_
 parents_[child], in edge insertion order More...
 
std::vector< std::vector< NodeId > > children_
 children_[parent], in edge insertion order More...
 

Detailed Description

Directed graphical model over discrete variables.

Variables are identified by a NodeId (insertion order, starting at 0) and by a unique name. The order in which edges into a variable are added is the order of its parents, and that order fixes the layout of its CPT (see set_cpt()).

The class stores what it is given. add_edge() does not detect duplicate edges or cycles, and set_cpt() checks the size of a table but not its values.

Definition at line 28 of file graph.hpp.

Constructor & Destructor Documentation

◆ Graph()

bncore::Graph::Graph ( )
default

Member Function Documentation

◆ add_edge() [1/2]

void bncore::Graph::add_edge ( const std::string &  parent_name,
const std::string &  child_name 
)

Adds a directed edge between two variables given by name.

Parameters
parent_nameName of the parent variable.
child_nameName of the child variable.
Exceptions
std::invalid_argumentif either name is unknown.

Definition at line 44 of file graph.cpp.

References add_edge(), and get_variable().

Here is the call graph for this function:

◆ add_edge() [2/2]

void bncore::Graph::add_edge ( NodeId  parent,
NodeId  child 
)

Adds a directed edge from parent to child.

The parent is appended to the child's parent list, which defines the dimension order of the child's CPT. Add all edges into a variable before calling set_cpt() for it.

Parameters
parentId of the parent variable.
childId of the child variable.
Exceptions
std::out_of_rangeif either id is invalid.

Definition at line 36 of file graph.cpp.

References children_, parents_, and variables_.

Referenced by add_edge().

Here is the caller graph for this function:

◆ add_variable()

NodeId bncore::Graph::add_variable ( const std::string &  name,
const std::vector< std::string > &  states 
)

Adds a variable.

Parameters
nameUnique name of the variable.
statesState labels. The position of a label is its state index.
Returns
Id of the new variable, equal to num_variables() before the call.
Exceptions
std::invalid_argumentif a variable of that name already exists.

Definition at line 5 of file graph.cpp.

References children_, name_to_id_, parents_, and variables_.

◆ get_children()

const std::vector< NodeId > & bncore::Graph::get_children ( NodeId  id) const

Children of a variable, in the order their edges were added.

Parameters
idVariable id.
Exceptions
std::out_of_rangeif id is invalid.

Definition at line 111 of file graph.cpp.

References children_.

Referenced by bncore::JunctionTreeCompiler::moralize().

Here is the caller graph for this function:

◆ get_parents()

const std::vector< NodeId > & bncore::Graph::get_parents ( NodeId  id) const

Parents of a variable, in the order their edges were added.

Parameters
idVariable id.
Exceptions
std::out_of_rangeif id is invalid.

Definition at line 105 of file graph.cpp.

References parents_.

Referenced by bncore::JunctionTreeCompiler::compile(), bncore::JunctionTreeCompiler::moralize(), and bncore::BatchWorkspace::rebuild_clique_potentials().

Here is the caller graph for this function:

◆ get_variable() [1/2]

const VariableMetadata & bncore::Graph::get_variable ( const std::string &  name) const

Metadata of a variable given by name.

Parameters
nameVariable name, compared exactly.
Returns
Reference into the graph's storage. A later add_variable() can invalidate it.
Exceptions
std::invalid_argumentif the name is unknown.

Definition at line 98 of file graph.cpp.

References name_to_id_, and variables_.

◆ get_variable() [2/2]

const VariableMetadata & bncore::Graph::get_variable ( NodeId  id) const

◆ num_variables()

std::size_t bncore::Graph::num_variables ( ) const

Number of variables added so far.

Definition at line 117 of file graph.cpp.

References variables_.

Referenced by bncore::JunctionTreeCompiler::compile(), bncore::BatchExecutionEngine::evaluate_map(), and bncore::JunctionTreeCompiler::moralize().

Here is the caller graph for this function:

◆ set_cpt() [1/2]

void bncore::Graph::set_cpt ( const std::string &  name,
const std::vector< double > &  cpt 
)

Stores the conditional probability table of a variable given by name. See set_cpt(NodeId, const std::vector<double> &).

Parameters
nameName of the variable.
cptTable values.
Exceptions
std::out_of_rangeif the name is unknown.
std::invalid_argumentif cpt is empty or has the wrong size.

Definition at line 88 of file graph.cpp.

References name_to_id_, and set_cpt().

Here is the call graph for this function:

◆ set_cpt() [2/2]

void bncore::Graph::set_cpt ( NodeId  id,
const std::vector< double > &  cpt 
)

Stores the conditional probability table of a variable.

The table is flat and row-major over [parent 0, ..., parent n-1, self], with parents in the order their edges were added. The variable's own state varies fastest and parent 0 slowest. A variable without parents takes its prior, one value per state.

Let F be the product of the state counts of the parents and of the variable, computed from the parents known at the time of the call. A table of F values applies to every batch row. A table of F * B values is a batched table with a separate CPT per batch row, laid out as [F x B]: the value for family configuration f and batch row b is at f * B + b.

Values are stored as given. They are not normalized or range checked. A BatchWorkspace reads the table when it builds its potentials and then keeps them. After changing a table, call BatchExecutionEngine::invalidate_workspace_cache() on an engine that has already evaluated, so that its cached workspace is rebuilt.

Parameters
idVariable whose table is set.
cptTable values in the layout above.
Exceptions
std::out_of_rangeif id is invalid.
std::invalid_argumentif cpt is empty or its size is not a multiple of F.

Definition at line 49 of file graph.cpp.

References parents_, and variables_.

Referenced by set_cpt().

Here is the caller graph for this function:

◆ split_state()

void bncore::Graph::split_state ( NodeId  id,
std::size_t  state_idx,
const std::string &  new_state1,
const std::string &  new_state2 
)

Replaces one state of a variable by two states.

new_state1 takes the position state_idx and new_state2 the position after it; later states move up by one. Only the state labels change: the CPT stored for the variable and the CPTs of its children keep their sizes, so they have to be set again before inference.

Parameters
idVariable to change.
state_idxIndex of the state to replace, less than the variable's state count.
new_state1Label of the first replacement state.
new_state2Label of the second replacement state.
Exceptions
std::out_of_rangeif id or state_idx is invalid.

Definition at line 22 of file graph.cpp.

References variables_.

Referenced by bncore::DiscretizationManager::split_bin().

Here is the caller graph for this function:

Member Data Documentation

◆ children_

std::vector<std::vector<NodeId> > bncore::Graph::children_
private

children_[parent], in edge insertion order

Definition at line 139 of file graph.hpp.

Referenced by add_edge(), add_variable(), and get_children().

◆ name_to_id_

std::unordered_map<std::string, NodeId> bncore::Graph::name_to_id_
private

Definition at line 135 of file graph.hpp.

Referenced by add_variable(), get_variable(), and set_cpt().

◆ parents_

std::vector<std::vector<NodeId> > bncore::Graph::parents_
private

parents_[child], in edge insertion order

Definition at line 138 of file graph.hpp.

Referenced by add_edge(), add_variable(), get_parents(), and set_cpt().

◆ variables_

std::vector<VariableMetadata> bncore::Graph::variables_
private

indexed by NodeId

Definition at line 134 of file graph.hpp.

Referenced by add_edge(), add_variable(), get_variable(), num_variables(), set_cpt(), and split_state().


The documentation for this class was generated from the following files: