## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(dist.structure)
library(algebraic.dist)

## -----------------------------------------------------------------------------
sys <- series_dist(list(exponential(1), exponential(1), exponential(1)))
phi(sys, c(1, 1, 1))                   # all up: series functions
phi(sys, c(1, 0, 1))                   # one down: series fails

## -----------------------------------------------------------------------------
par_sys <- parallel_dist(list(exponential(1), exponential(1)))
phi(par_sys, c(0, 1))                  # one up: parallel functions
phi(par_sys, c(0, 0))                  # all down: parallel fails

## -----------------------------------------------------------------------------
min_paths(sys)                         # series: one path, all components
min_cuts(sys)                          # series: m singleton cuts

## -----------------------------------------------------------------------------
min_paths(par_sys)                     # parallel: m singleton paths
min_cuts(par_sys)                      # parallel: one cut, all components

## -----------------------------------------------------------------------------
sys_kofn <- kofn_dist(k = 2,
  components = list(exponential(1), exponential(1), exponential(1)))
length(min_paths(sys_kofn))            # choose(3, 2) = 3 paths
length(min_cuts(sys_kofn))             # choose(3, 2) = 3 cuts (pairs)

## -----------------------------------------------------------------------------
bridge <- bridge_dist(replicate(5, exponential(1), simplify = FALSE))
length(min_paths(bridge))              # 4 minimal paths
length(min_cuts(bridge))               # 4 minimal cuts
system_signature(bridge)               # (0, 1/5, 3/5, 1/5, 0)

## -----------------------------------------------------------------------------
# Two parallel sub-systems combined in series:
# Paths: any path picks one component from each block.
custom <- coherent_dist(
  min_paths = list(c(1, 3), c(1, 4), c(2, 3), c(2, 4)),
  components = replicate(4, exponential(1), simplify = FALSE)
)
ncomponents(custom)
length(min_paths(custom))
length(min_cuts(custom))

## -----------------------------------------------------------------------------
structural_importance(sys_kofn, j = 1)     # symmetric: same for all j

## -----------------------------------------------------------------------------
for (p in c(0.1, 0.5, 0.9)) {
  cat(sprintf("p = %.1f: reliability = %.4f\n",
              p, reliability(sys, p)))
}

## -----------------------------------------------------------------------------
# 2-of-3 at p = 0.8: P(Binom(3, 0.8) >= 2)
reliability(sys_kofn, 0.8)
sum(dbinom(2:3, size = 3, prob = 0.8))

## -----------------------------------------------------------------------------
crit <- critical_states(sys_kofn, j = 1)
nrow(crit)                             # 2 (= choose(2, 1) for 2-of-3)
crit                                   # each row: a state of components 2, 3

## -----------------------------------------------------------------------------
dsys <- dual(sys)
phi(dsys, c(0, 0, 0))                  # like parallel: fails only if all down
phi(dsys, c(1, 0, 0))                  # functions if any up

## -----------------------------------------------------------------------------
is_coherent(sys)                       # TRUE
is_coherent(bridge)                    # TRUE

