Skip to content

Conversation

@bcorfman
Copy link

Add support for weighted transitions that allow non-deterministic state transitions based on configurable probabilities. This is useful for game AI, simulations, and randomized workflows.

Key features:

  • Add optional 'weight' parameter to Transition class
  • Add optional 'random_seed' parameter to StateMachine for deterministic testing
  • Implement weighted selection in both sync and async engines
  • Automatically display probability percentages in state diagrams
  • Full backward compatibility (no weights = original first-match behavior)
  • Zero/negative weights are ignored
  • Conditions (guards/validators) work seamlessly with weighted transitions
  • Complete pickling support

Changes:

  • statemachine/transition.py: Add weight parameter and repr support
  • statemachine/statemachine.py: Add random_seed and Random instance
  • statemachine/engines/sync.py: Implement weighted selection logic
  • statemachine/engines/async_.py: Implement weighted selection logic
  • statemachine/contrib/diagram.py: Add probability labels to diagrams
  • README.md: Add probabilistic transitions to features list
  • docs/transitions.md: Add comprehensive documentation with examples
  • tests/test_probabilistic_transitions.py: 20 comprehensive tests
  • tests/examples/game_character_idle_machine.py: Working example

All 348 existing tests pass + 20 new tests = 368 total passing tests

Add support for weighted transitions that allow non-deterministic state
transitions based on configurable probabilities. This is useful for game AI,
simulations, and randomized workflows.

Key features:
- Add optional 'weight' parameter to Transition class
- Add optional 'random_seed' parameter to StateMachine for deterministic testing
- Implement weighted selection in both sync and async engines
- Automatically display probability percentages in state diagrams
- Full backward compatibility (no weights = original first-match behavior)
- Zero/negative weights are ignored
- Conditions (guards/validators) work seamlessly with weighted transitions
- Complete pickling support

Changes:
- statemachine/transition.py: Add weight parameter and repr support
- statemachine/statemachine.py: Add random_seed and Random instance
- statemachine/engines/sync.py: Implement weighted selection logic
- statemachine/engines/async_.py: Implement weighted selection logic
- statemachine/contrib/diagram.py: Add probability labels to diagrams
- README.md: Add probabilistic transitions to features list
- docs/transitions.md: Add comprehensive documentation with examples
- tests/test_probabilistic_transitions.py: 20 comprehensive tests
- tests/examples/game_character_idle_machine.py: Working example

All 348 existing tests pass + 20 new tests = 368 total passing tests
@sonarqubecloud
Copy link

@bcorfman
Copy link
Author

Closing to reopen from clean branch

@bcorfman bcorfman closed this Oct 13, 2025
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.

1 participant