State Machine
A state machine implementation for use in vivarium
simulations.
- vivarium.framework.state_machine.default_probability_function(index)[source]
Transition decision function that always triggers this transition.
- class vivarium.framework.state_machine.Trigger(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]
- NOT_TRIGGERED = 0
- START_INACTIVE = 1
- START_ACTIVE = 2
- class vivarium.framework.state_machine.Transition(input_state, output_state, probability_func=<function Transition.<lambda>>, triggered=Trigger.NOT_TRIGGERED)[source]
A process by which an entity might change into a particular state.
- Parameters:
- class vivarium.framework.state_machine.State(state_id, allow_self_transition=False, initialization_weights=0.0)[source]
An abstract representation of a particular position in a state space.
- state_id
The name of this state. This should be unique
- transition_set
A container for potential transitions out of this state.
- property configuration_defaults: dict[str, Any]
Provides a dictionary containing the defaults for any configurations managed by this component.
These default values will be stored at the component_configs layer of the simulation’s LayeredConfigTree.
- Returns:
A dictionary containing the defaults for any configurations managed by
this component.
- has_initialization_weights()[source]
Determines if state has explicitly defined initialization weights.
- Return type:
- next_state(index, event_time, population_view)[source]
Moves a population between different states.
- Parameters:
index (pd.Index[int]) – An iterable of integer labels for the simulants.
event_time (ClockTime) – When this transition is occurring.
population_view (PopulationView) – A view of the internal state of the simulation.
- Return type:
None
- transition_effect(index, event_time, population_view)[source]
Updates the simulation state and triggers any side-effects associated with entering this state.
- Parameters:
index (pd.Index[int]) – An iterable of integer labels for the simulants.
event_time (ClockTime) – The time at which this transition occurs.
population_view (PopulationView) – A view of the internal state of the simulation.
- Return type:
None
- cleanup_effect(index, event_time)[source]
- Parameters:
index (pd.Index[int])
event_time (ClockTime)
- Return type:
None
- add_transition(transition=None, output_state=None, probability_function=<function default_probability_function>, triggered=Trigger.NOT_TRIGGERED)[source]
Adds a transition to this state and its TransitionSet.
A transition can be added by passing a Transition object or by specifying an output state and a decision function. If a transition is provided, the output state and decision function must not be.
- Parameters:
transition (Transition | None) – The transition to add.
output_state (State | None) – The state to transition to
probability_function (Callable[[pd.Index[int]], pd.Series[float]]) – A function that determines the probability that this transition should happen. By default, this is a function that will produce a probability of 1.0 for all simulants in the state.
triggered (Trigger) – A flag indicating whether this transition is triggered by some event.
- Return type:
- class vivarium.framework.state_machine.Transient[source]
Used to tell _next_state to transition a second time.
- class vivarium.framework.state_machine.TransientState(state_id, allow_self_transition=False, initialization_weights=0.0)[source]
- class vivarium.framework.state_machine.TransitionSet(state_id, *transitions, allow_self_transition=False)[source]
A container for state machine transitions.
- Parameters:
state_id (str)
transitions (Transition)
allow_self_transition (bool)
- state_id
The unique name of the state that instantiated this TransitionSet. Typically a string but any object implementing __str__ will do.
- allow_null_transition
Specified whether it is possible not to transition on a given time-step
- transitions
A list of transitions that can be taken from this state.
- random
The randomness stream.
- property name: str
Returns the name of the component.
By convention, these are in snake case with arguments of the __init__ appended and separated by ..
Names must be unique within a simulation.
The name is created by first converting the name of the class to snake case. Then, the names of the initialization parameters are appended, separated by .. If a parameter is an instance of Component, its name property is used; otherwise, the string representation of the parameter is used. The resulting string is stored in the _name attribute and returned.
IMPORTANT: this property must not be accessed within the __init__ functions of this component or its subclasses or its value may not be initialized correctly.
- Returns:
The name of the component.
- Return type:
- append(transition)[source]
- Return type:
- Parameters:
transition (Transition)
- extend(transitions)[source]
- Return type:
- Parameters:
transitions (Iterable[Transition])
- class vivarium.framework.state_machine.Machine(state_column, states=(), initial_state=None)[source]
A collection of states and transitions between those states.
- states
The collection of states represented by this state machine.
- state_column
A label for the piece of simulation state governed by this state machine.
- property sub_components: Sequence[Component]
Provide components managed by this component.
- Returns:
The sub-components that are managed by this component.
- Return type:
List[Component]
- property columns_created: list[str]
Provides names of columns created by the component.
- Returns:
Names of the columns created by this component, or an empty list if
none.
- property initialization_requirements: list[str | Resource]
A list containing the columns, pipelines, and randomness streams required by this component’s simulant initializer.
- setup(builder)[source]
Defines custom actions this component needs to run during the setup lifecycle phase.
This method is intended to be overridden by subclasses to perform any necessary setup operations specific to the component. By default, it does nothing.
- on_initialize_simulants(pop_data)[source]
Method that vivarium will run during simulant initialization.
This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the simulant initialization phase.
- Return type:
- Parameters:
pop_data (SimulantData) – The data associated with the simulants being initialized.
- Return type:
None
- on_time_step(event)[source]
Method that vivarium will run during the time_step event.
This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the time_step event.
- on_time_step_cleanup(event)[source]
Method that vivarium will run during the time_step__cleanup event.
This method is intended to be overridden by subclasses if there are operations they need to perform specifically during the time_step__cleanup event.
- transition(index, event_time)[source]
Finds the population in each state and moves them to the next state.
- Parameters:
index (pd.Index[int]) – An iterable of integer labels for the simulants.
event_time (ClockTime) – The time at which this transition occurs.
- Return type:
None
- cleanup(index, event_time)[source]
- Parameters:
index (pd.Index[int])
event_time (ClockTime)
- Return type:
None