16constexpr std::size_t kFailed = 0;
17constexpr std::size_t kWorking = 1;
20bool gateFails(
const Node &node, std::size_t failed_inputs, std::size_t n) {
25 return failed_inputs >=
static_cast<std::size_t
>(node.k);
29 throw FaultTreeError(
"gate '" + node.name +
"' has no deterministic table");
42 graph_ = std::make_unique<bncore::Graph>();
47 const auto id =
graph_->add_variable(node.name, {
"Failed",
"Working"});
48 var_of_node_.emplace(node.name,
static_cast<std::size_t
>(
id));
55 for (
const auto &input : node.children) {
65 graph_->set_cpt(
id, {node.probability, 1.0 - node.probability});
69 const std::size_t n = node.children.size();
71 std::ostringstream out;
72 out <<
"gate '" << node.name <<
"' has " << n <<
" inputs, above the "
80 const std::size_t combinations = std::size_t{1} << n;
81 std::vector<double> cpt(combinations * 2);
82 for (std::size_t combo = 0; combo < combinations; ++combo) {
83 std::size_t failed_inputs = 0;
84 for (std::size_t i = 0; i < n; ++i) {
85 const std::size_t state = (combo >> (n - 1 - i)) & 1u;
86 if (state == kFailed) { ++failed_inputs; }
88 const bool fails = gateFails(node, failed_inputs, n);
89 cpt[combo * 2 + kFailed] = fails ? 1.0 : 0.0;
90 cpt[combo * 2 + kWorking] = fails ? 0.0 : 1.0;
105 const std::size_t num_vars =
tree_.
size();
106 std::vector<int> evidence(num_vars, -1);
107 double marginal[2] = {0.0, 0.0};
108 engine_->evaluate(evidence.data(), 1, num_vars, marginal, it->second);
109 return marginal[kFailed];
117 double probability) {
123 {probability, 1.0 - probability});
124 engine_->invalidate_workspace_cache();
static std::unique_ptr< JunctionTree > compile(const Graph &graph, const std::string &heuristic="min_fill")
Compiles a junction tree.
Exception type of the fault tree model, the .fta reader and the quantifier.
A fault tree: nodes keyed by name, plus a designated top event.
void setBasicEventProbability(const std::string &name, double probability)
const std::string & top() const
Name of the top event.
bool hasNode(const std::string &name) const
Tests whether a node of this name has been added.
std::vector< std::string > basicEventNames() const
std::size_t size() const
Number of nodes (basic events plus gates).
const std::vector< Node > & nodes() const
All nodes, basic events and gates, in the order they were added.
bool isDefined(const std::string &name) const
double probabilityOf(const std::string &name) const
std::unordered_map< std::string, std::size_t > var_of_node_
node name to BNCore variable id
std::vector< std::string > basicEventNames() const
Names of the basic events, in definition order.
std::unique_ptr< bncore::Graph > graph_
one binary variable per node
std::unique_ptr< bncore::BatchExecutionEngine > engine_
refers to junction_tree_
FaultTree tree_
validated private copy of the model
void setBasicEventProbability(const std::string &name, double probability)
std::unique_ptr< bncore::JunctionTree > junction_tree_
points at graph_
Quantifier(const FaultTree &tree)
double topEventProbability() const
bncore::JunctionTreeCompiler, which turns a Graph into the JunctionTree that the inference engine run...
bncore::BatchExecutionEngine, the query interface of BNCore: exact posterior marginals and MAP 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.
@ Or
fails when any input fails (.fta type 1)
@ KofN
fails when at least k inputs fail (.fta type 6)
@ Basic
basic event, quantified by probability
@ Not
fails when its single input does not (.fta type 7)
@ And
fails when every input fails (.fta type 0)
constexpr std::size_t kMaxGateInputs
Largest number of inputs a single gate may have.
Exact fault tree quantification: faulttree::Quantifier compiles a FaultTree into a BNCore Bayesian ne...