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

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

## -----------------------------------------------------------------------------
sys <- parallel_dist(list(
  exponential(1),
  exponential(1),
  exponential(1)
))

# Survival at t = 1 for the all-exponential parallel:
algebraic.dist::surv(sys)(1)

sys2 <- substitute_component(sys, j = 1,
  new_component = weibull_dist(shape = 2, scale = 2))

# Survival improves because component 1 is now longer-lived:
algebraic.dist::surv(sys2)(1)

## -----------------------------------------------------------------------------
length(min_paths(sys2))                # still 3 (parallel)
ncomponents(sys2)                      # still 3

## -----------------------------------------------------------------------------
inner_a <- parallel_dist(list(exponential(1), exponential(1)))
inner_b <- parallel_dist(list(exponential(1), exponential(1)))

# Outer: series of 2 "slots" (the components are placeholder; they get
# replaced by the inners during compose_systems).
outer <- series_dist(list(exponential(1), exponential(1)))

sys <- compose_systems(outer, list(inner_a, inner_b))
ncomponents(sys)                       # 4 total leaf components
length(min_paths(sys))                 # 4 composed paths

## -----------------------------------------------------------------------------
min_paths(sys)

## -----------------------------------------------------------------------------
inner_a <- series_dist(list(exponential(1), exponential(1)))
inner_b <- series_dist(list(exponential(1), exponential(1)))

outer <- parallel_dist(list(exponential(1), exponential(1)))

sys <- compose_systems(outer, list(inner_a, inner_b))
ncomponents(sys)                       # 4 total
length(min_paths(sys))                 # 2 composed paths
min_paths(sys)                         # {1, 2} and {3, 4}

## -----------------------------------------------------------------------------
# Series of: (a 2-parallel block) + (a single exponential)
inner_par <- parallel_dist(list(exponential(1), exponential(1)))
outer <- series_dist(list(exponential(1), exponential(1)))

sys <- compose_systems(outer, list(inner_par, exponential(1)))
ncomponents(sys)                       # 3: two from parallel + one single
min_paths(sys)                         # {1, 3} and {2, 3}

## -----------------------------------------------------------------------------
# A parallel of two different sub-systems:
sub_a <- series_dist(list(exponential(1), exponential(1)))     # simple series
sub_b <- bridge_dist(replicate(5, exponential(1), simplify = FALSE))  # bridge

outer <- parallel_dist(list(exponential(1), exponential(1)))
combined <- compose_systems(outer, list(sub_a, sub_b))

ncomponents(combined)                  # 2 + 5 = 7 total
length(min_paths(combined))            # = 1 + 4 (series has 1 path, bridge has 4)

