|
ADS-IDAC Engine 5.0.0
|
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... | |
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.
|
default |
| 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.
| parent_name | Name of the parent variable. |
| child_name | Name of the child variable. |
| std::invalid_argument | if either name is unknown. |
Definition at line 44 of file graph.cpp.
References add_edge(), and get_variable().
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.
| parent | Id of the parent variable. |
| child | Id of the child variable. |
| std::out_of_range | if either id is invalid. |
Definition at line 36 of file graph.cpp.
References children_, parents_, and variables_.
Referenced by add_edge().
| NodeId bncore::Graph::add_variable | ( | const std::string & | name, |
| const std::vector< std::string > & | states | ||
| ) |
Adds a variable.
| name | Unique name of the variable. |
| states | State labels. The position of a label is its state index. |
| std::invalid_argument | if a variable of that name already exists. |
Definition at line 5 of file graph.cpp.
References children_, name_to_id_, parents_, and variables_.
Children of a variable, in the order their edges were added.
| id | Variable id. |
| std::out_of_range | if id is invalid. |
Definition at line 111 of file graph.cpp.
References children_.
Referenced by bncore::JunctionTreeCompiler::moralize().
Parents of a variable, in the order their edges were added.
| id | Variable id. |
| std::out_of_range | if 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().
| const VariableMetadata & bncore::Graph::get_variable | ( | const std::string & | name | ) | const |
Metadata of a variable given by name.
| name | Variable name, compared exactly. |
| std::invalid_argument | if the name is unknown. |
Definition at line 98 of file graph.cpp.
References name_to_id_, and variables_.
| const VariableMetadata & bncore::Graph::get_variable | ( | NodeId | id | ) | const |
Metadata of a variable.
| id | Variable id. |
| std::out_of_range | if id is invalid. |
Definition at line 92 of file graph.cpp.
References variables_.
Referenced by add_edge(), bncore::BatchWorkspace::apply_soft_evidence_to_clique(), bncore::BatchExecutionEngine::apply_soft_evidence_to_workspace(), bncore::BatchWorkspace::build_message_schedule(), bncore::JunctionTreeCompiler::compile(), bncore::BatchExecutionEngine::evaluate(), bncore::BatchWorkspace::query_map(), bncore::BatchWorkspace::query_marginal(), bncore::BatchWorkspace::query_marginals_multi(), bncore::BatchWorkspace::rebuild_clique_potentials(), bncore::BatchWorkspace::set_soft_evidence(), bncore::BatchWorkspace::set_soft_evidence_matrix(), bncore::DiscretizationManager::should_split(), and bncore::DiscretizationManager::split_bin().
| 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().
| 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> &).
| name | Name of the variable. |
| cpt | Table values. |
| std::out_of_range | if the name is unknown. |
| std::invalid_argument | if cpt is empty or has the wrong size. |
Definition at line 88 of file graph.cpp.
References name_to_id_, and set_cpt().
| 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.
| id | Variable whose table is set. |
| cpt | Table values in the layout above. |
| std::out_of_range | if id is invalid. |
| std::invalid_argument | if 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().
| 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.
| id | Variable to change. |
| state_idx | Index of the state to replace, less than the variable's state count. |
| new_state1 | Label of the first replacement state. |
| new_state2 | Label of the second replacement state. |
| std::out_of_range | if id or state_idx is invalid. |
Definition at line 22 of file graph.cpp.
References variables_.
Referenced by bncore::DiscretizationManager::split_bin().
|
private |
children_[parent], in edge insertion order
Definition at line 139 of file graph.hpp.
Referenced by add_edge(), add_variable(), and get_children().
|
private |
Definition at line 135 of file graph.hpp.
Referenced by add_variable(), get_variable(), and set_cpt().
|
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().
|
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().