6 const std::vector<std::string> &states) {
8 throw std::invalid_argument(
"Variable with name '" + name +
23 const std::string &new_state1,
24 const std::string &new_state2) {
26 throw std::out_of_range(
"Invalid NodeId.");
28 if (state_idx >= var.states.size())
29 throw std::out_of_range(
"Invalid state index.");
31 var.states.erase(var.states.begin() + state_idx);
32 var.states.insert(var.states.begin() + state_idx, new_state2);
33 var.states.insert(var.states.begin() + state_idx, new_state1);
38 throw std::out_of_range(
"Invalid NodeId provided for edge.");
45 const std::string &child_name) {
51 throw std::out_of_range(
"Invalid NodeId");
53 std::size_t family_states =
variables_[id].states.size();
59 throw std::invalid_argument(
"CPT for variable '" +
variables_[
id].name +
60 "' cannot be empty. Expected scalar size " +
61 std::to_string(family_states) +
63 std::to_string(family_states) +
" * B.");
66 if (cpt.size() == family_states) {
71 if (cpt.size() % family_states != 0) {
72 throw std::invalid_argument(
73 "CPT size mismatch for variable '" +
variables_[
id].name +
74 "'. Expected scalar size " + std::to_string(family_states) +
75 " or batched size " + std::to_string(family_states) +
76 " * B (B > 0), but got " + std::to_string(cpt.size()) +
".");
79 std::size_t batch_size = cpt.size() / family_states;
80 if (batch_size == 0) {
81 throw std::invalid_argument(
"CPT for variable '" +
variables_[
id].name +
82 "' has invalid batch size 0.");
88void Graph::set_cpt(
const std::string &name,
const std::vector<double> &cpt) {
94 throw std::out_of_range(
"Invalid NodeId.");
101 throw std::invalid_argument(
"Variable not found.");
107 throw std::out_of_range(
"Invalid NodeId.");
113 throw std::out_of_range(
"Invalid NodeId.");
void add_edge(NodeId parent, NodeId child)
Adds a directed edge from parent to child.
const std::vector< NodeId > & get_parents(NodeId id) const
Parents of a variable, in the order their edges were added.
std::unordered_map< std::string, NodeId > name_to_id_
std::vector< VariableMetadata > variables_
indexed by NodeId
std::vector< std::vector< NodeId > > parents_
parents_[child], in edge insertion order
void set_cpt(NodeId id, const std::vector< double > &cpt)
Stores the conditional probability table of a variable.
std::vector< std::vector< NodeId > > children_
children_[parent], in edge insertion order
std::size_t num_variables() const
Number of variables added so far.
const std::vector< NodeId > & get_children(NodeId id) const
Children of a variable, in the order their edges were added.
const VariableMetadata & get_variable(NodeId id) const
Metadata of a variable.
NodeId add_variable(const std::string &name, const std::vector< std::string > &states)
Adds a variable.
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.
bncore::Graph, the discrete Bayesian network model: variables, their states, directed edges and condi...
std::uint32_t NodeId
Handle of a variable within one Graph.