Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Covering and hitting: minimal monitoring sets#
Which resources must be monitored so that every activity involves at least one monitored resource? This is a minimum hitting set (transversal) of the activity hyperedges. Which activities must be observed so that every resource is seen at least once? That is a minimum set cover. Both are solved exactly by integer programming and approximately by the greedy rule; the minimal transversals of a small sub-system are enumerated with Berge’s algorithm.
import matplotlib.pyplot as plt
import hatvis as hv
from hatvis import problems
from hatvis.plotting import draw_hypergraph
H = hv.datasets.load_wire_harness()
people = H.restrict_nodes([v for v in H.nodes if H.node_data.loc[v, "type"] in ("operator", "robot", "AGV",
"machine")])
people = people.remove_empty_edges()
# [snippet:cover]
hs_exact = problems.hitting_set(people)
hs_greedy = problems.greedy_hitting_set(people)
sc_exact = problems.set_cover(people)
sc_greedy = problems.greedy_set_cover(people)
# [/snippet:cover]
print(f"hitting set: exact {len(hs_exact)} {hs_exact}, greedy {len(hs_greedy)}")
print(f"set cover: exact {len(sc_exact)}, greedy {len(sc_greedy)}")
small = people.restrict_edges([e for e in people.edges if people.edge_data.loc[e, "step"].startswith("Crimping1")
or people.edge_data.loc[e, "step"].startswith("Assembly1")]).remove_isolated()
tr = problems.minimal_transversals(small)
print("minimal transversals of the line-1 cell:", [sorted(t) for t in tr])
hitting set: exact 9 ['O2', 'O3', 'O4', 'O5', 'O6', 'O7', 'R2', 'R5', 'AGV'], greedy 9
set cover: exact 9, greedy 10
minimal transversals of the line-1 cell: [['M1', 'O3'], ['O2', 'O3']]
fig, ax = plt.subplots(figsize=(8, 6.5))
color = {v: hv.plotting.PALETTE[1] if v in hs_exact else hv.plotting.INK for v in people.nodes}
draw_hypergraph(people, layout="force", style="smooth", node_color=color, edge_color="#9CC3E6", alpha=0.05,
ax=ax, legend=False, pad=0.015, random_state=1,
title=f"minimum hitting set ({len(hs_exact)} resources, orange)")
plt.tight_layout()

Total running time of the script: (0 minutes 1.241 seconds)