Build a MarkovChain incrementally instead of all at once#458
Open
peterrrock2 wants to merge 3 commits into
Open
Build a MarkovChain incrementally instead of all at once#458peterrrock2 wants to merge 3 commits into
MarkovChain incrementally instead of all at once#458peterrrock2 wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR lets users create a
MarkovChainfirst and configure it piece by piece, rather thanassembling every argument before the chain can exist. Every constructor parameter is now optional,
and the chain can be completed by assigning attributes and calling small helper methods afterwards.
The old all-at-once construction still works unchanged; this only adds the incremental path.
Incremental configuration
MarkovChain.__init__has been updated to make it so that most argruments are optional. Thisallows legacy workflows to still work, but also enables a "Chain-first" paradigm:
Assignment order does not matter. The full configuration is validated by
check_validwheniteration begins, so an incomplete or inconsistent chain is reported at the point it is run rather
than silently misbehaving. Attributes are also locked during iteration to prevent mid-run mutation.
New helper methods
add_updater(name, updater)/add_updaters(mapping): register updaters on the chain. They areapplied to the initial partition immediately if one is set, otherwise as soon as one is assigned,
and every partition the chain produces inherits them.
add_constraint(constraint)/add_constraints(iterable): append to the chain's constraints.Each new constraint is checked against the initial partition as soon as it is added, so a starting
plan that violates one is reported immediately.
check_valid(): validate the complete configuration on demand.While a run is in progress the chain is locked, and these mutators raise a clear error rather than
corrupting an in-flight iteration.
Supporting changes
ConstraintFntype alias (Callable[[Partition], bool]) is introduced and used across theconstraint and optimization APIs to slim down repeated signatures.
gingleator,optimization) andmetagraphare updated to the new chainsurface.
Testing
tests/test_chain.pygains coverage for building a chain incrementally, adding updaters andconstraints before and after the initial partition is set, deferred validation at iteration time,
and the locking behavior that prevents mutation mid-run.
Verified with
make test.