petri_net_nn.petri_net¶
petri_net ¶
Petri net data structure.
A finite Petri net N = (P, T, F, M_0) plus a number of classical extensions: arc multiplicities (multi-token markings), inhibitor arcs, transition durations, firing rates, and coloured tokens (per-token scalar values with optional transition guards reading those values).
Every extension is sparse — entries are recorded only when they deviate from the trivial default, so nets that don't use the extension behave identically to the basic four-tuple definition.
PetriNet
dataclass
¶
PetriNet(places=set(), transitions=set(), flow=set(), initial_marking=dict(), place_labels=dict(), transition_labels=dict(), arc_multiplicities=dict(), inhibitor_arcs=set(), transition_durations=dict(), transition_rates=dict(), transition_guards=dict(), arc_output_values=dict(), transition_structural_guards=dict(), transition_torch_guards=dict(), arc_torch_output_values=dict(), silent_transitions=set())
add_transition ¶
add_transition(tid, *, label=None, duration=1, rate=1.0, guard=None, structural_guard=None, torch_guard=None, silent=False)
Add a transition.
duration is the number of time-unrolled steps the
transition takes to produce its output once it has fired
(default 1 = immediate). Durations only have effect in the
compiler's time-unrolled forward pass.
rate is a per-transition firing-rate multiplier applied
to the pre-activation by the compiler. The default 1.0
leaves behaviour unchanged. A rate of 3.0 makes this
transition fire roughly three times as eagerly as its
siblings for the same inputs; 0.3 makes it three times less
eager. Lets the modeller encode prior knowledge about
transition propensity (priority, stochastic rate, etc.)
alongside the learnable weights and thresholds.
guard is an optional callable used by the coloured
token-game (is_enabled_coloured / fire_coloured).
It receives a dict mapping each input place to the value of
the token that would be consumed there and returns True
when the transition is allowed to fire. Guards have no
effect on the count-based is_enabled / fire path.
structural_guard is the matching declarative record for
the simple comparison-form guard — {"place": ..., "op":
..., "value": ...}. When present, the compiler picks it
up and builds a differentiable soft guard with a learnable
threshold parameter initialised at value. The token-game
path keeps using guard unchanged, so the two views stay
in sync: the declarative record is the trainable face of the
same rule the callable encodes.
torch_guard is a user-supplied torch-friendly soft gate:
a callable taking dict[place_id, Tensor(batch,)] of input
values and returning a Tensor(batch,) gate in [0, 1].
The compiler multiplies the transition's firing strength by
this gate. Use it for routing logic the structural form
can't express — multi-input comparisons, compound predicates,
custom learnable sub-networks. If both structural_guard
and torch_guard are supplied, the torch callable wins in
the compiler (it's strictly more expressive). The token-game
is unaffected by torch_guard either way.
silent=True marks the transition as a τ (silent) step
for weak-bisimulation purposes. The transition still fires
normally in the token-game and contributes to the trained
network the same way every other transition does — silence
is only a property the weak-bisimulation checker reads
when deciding whether two nets exhibit the same observable
behaviour. Use it for internal logging steps, no-op gates,
and other refactoring artefacts that shouldn't count as
visible actions in an equivalence check.
Source code in petri_net_nn/petri_net.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
duration ¶
The transition's firing duration in time-unrolled steps. Returns 1 (immediate) for transitions added without an explicit duration.
rate ¶
The transition's firing-rate multiplier. Returns 1.0 for transitions added without an explicit rate.
add_arc ¶
Add an arc.
weight is the multiplicity — how many tokens this arc
moves per firing (default 1).
output_value only applies to arcs from transitions to
places. It specifies the value of the token the firing
produces — a constant float, or a callable receiving the
input-place values bound at the transition and returning
a float. If omitted, the produced token carries value 1.0.
The float / callable forms are evaluated by the coloured
token-game (fire_coloured); count-based firing ignores
them, and the compiler honours only the constant form.
torch_output_value is the torch-friendly counterpart:
a callable taking dict[place_id, Tensor(batch,)] of bound
input values and returning a Tensor(batch,) that the
compiler uses as the value carried by the produced token in
the per-place value channel. Only applies to transition →
place arcs. Doesn't participate in the discrete token-game
(that's what output_value is for); the two can coexist —
the token-game uses output_value and the compiler uses
torch_output_value when present.
Source code in petri_net_nn/petri_net.py
weight ¶
How many tokens this arc moves per firing. 1 unless an
explicit weight was supplied to add_arc.
add_inhibitor_arc ¶
Add an inhibitor arc from place to transition. The
transition can only fire when the place is empty; the
transition does not consume tokens from the inhibitor place.
Used to model mutual exclusion, negative preconditions, and
guard-against-already-running patterns.
Source code in petri_net_nn/petri_net.py
inhibitor_preset ¶
Places that inhibit transition — must be empty for it
to fire.
is_enabled_coloured ¶
Coloured-token enablement: a transition is enabled iff every
input place holds at least weight tokens, every inhibitor
place is empty, and the transition's guard (if any) returns
True for the values of the tokens that would be consumed.
Source code in petri_net_nn/petri_net.py
fire_coloured ¶
Fire transition against a coloured marking. Consumes
weight tokens FIFO from each input place; produces tokens
whose values come from each output arc's output_value
spec (constant float, or a callable receiving the bound input
values). Returns a new marking — the input is not mutated.
Source code in petri_net_nn/petri_net.py
validate ¶
Return a list of structural issues. An empty list means the net is well-formed in the elementary sense: every arc connects known nodes, every transition has at least one input and one output, and the initial marking is supported on known places.
Source code in petri_net_nn/petri_net.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |