Skip to content

#32: Fix DiscreteHazard firing multiple times per timestep - #47

Open
sdwfrost wants to merge 2 commits into
AlgebraicJulia:mainfrom
epirecipes:pr/fix-discrete-hazard-main
Open

sdwfrost wants to merge 2 commits into
AlgebraicJulia:mainfrom
epirecipes:pr/fix-discrete-hazard-main

Conversation

@sdwfrost

@sdwfrost sdwfrost commented Mar 30, 2026

Copy link
Copy Markdown

Problem

DiscreteHazard rules can be scheduled more than once in the same timestep when a fired match persists after the rewrite. This shows up most readily with attribute-containing schemas.

The failure mode is that the incremental hom-set update can discover the same logical match under a new key during the addition phase, and then the later re-enable step can also re-enable the original fired key. That leaves two sampler entries for one logical match. With Dirac-style discrete hazards, both entries fire at the same time, so the number of events can grow explosively across timesteps (1, 2, 4, 8, ...).

Fix

This patch tracks which (rule, key) entries have already been re-enabled during the update pass.

When add_new discovers a match, the code now checks whether an equivalent match was already enabled for that rule and skips the duplicate if so. Likewise, when previously fired matches are re-enabled at the end of the simultaneous-event update, the code checks whether an equivalent match was already scheduled during the update pass and avoids scheduling it twice.

In other words, the sampler keeps one entry per logical match, even if the incremental hom-set temporarily represents that match with multiple keys.

Testing

  • added regression coverage for the repeated-firing scenario reported in #32
  • ran julia --project -e 'using Pkg; Pkg.instantiate(); include("test/ABMs.jl")'

Closes #32.

)

When a rule with DiscreteHazard fires and the match persists (common with
attribute-containing schemas), the incremental hom-set's addition! phase
creates a phantom match in a new component with a different key. The
re-enable loop then also re-enables the original fired key, producing two
sampler entries for the same logical match. With Dirac distributions, both
schedule at the exact same time, causing exponential blowup (1,2,4,8,...
events per timestep).

Fix: track which (rule, key) pairs are enabled during the update phase.
In the add_new loop, check if a newly-discovered match duplicates one
already enabled. In the re-enable loop, check if an equivalent match was
already scheduled by the update phase before re-enabling.

(cherry picked from commit 2fb09aa)
@kris-brown

Copy link
Copy Markdown
Collaborator

Thank you for catching this! Can you add some tests that reproduce the problem?

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sdwfrost

Copy link
Copy Markdown
Author

Added a regression test for the reported failure mode.

The new test builds a one-object attributed model with a DiscreteHazard(1.) rule that increments an integer attribute while preserving the same logical match. It asserts that the rule fires exactly once at each of times 1.0, 2.0, and 3.0, and that the final counter value is 3.

That is the behavior that was previously violated by the duplicate scheduling bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DiscreteHazard firing too many times

2 participants