State Machine¶
The state machine is the core of JobShopLab, responsible for simulating the execution of job shop scheduling problems. It follows functional programming principles, operating on immutable data structures to ensure predictability and reproducibility.
State Machine Architecture¶
The state machine implements a pure functional architecture with these key components:
State: Immutable data structure representing the complete system state
Handlers: Pure functions that process transitions and update state
Transitions: Events that transform the system from one state to another
Validators: Functions that check transition validity
State Representation¶
The state is represented by an immutable State dataclass with these components:
Time: Current simulation time
Machines: Collection of machine states (IDLE, PROCESSING, etc.)
Jobs: Collection of job states with operation completion status
Buffers: Optional buffer capacities and contents
Transport: Optional transport resources and locations
# Example state structure (simplified)
@dataclass(frozen=True)
class State:
time: Time
machines: Tuple[MachineState, ...]
jobs: Tuple[JobState, ...]
buffers: Optional[Tuple[BufferState, ...]] = None
transport: Optional[Tuple[TransportState, ...]] = None
Transition System¶
Transitions represent events that can change the state, such as:
Starting a job operation on a machine
Completing an operation
Moving a job between machines
Transporting materials
Each transition is validated before being applied to ensure it respects system constraints.
Handler Functions¶
Handlers are pure functions that implement transitions:
def handle_start_operation(
state: State,
job_id: str,
machine_id: str,
operation_id: str
) -> State:
"""Handle starting an operation for a job on a machine."""
# Create new machine states tuple with updated machine
# Create new job states tuple with updated job
# Return new state with updated collections
return State(
time=state.time,
machines=new_machines,
jobs=new_jobs,
buffers=state.buffers,
transport=state.transport
)
Time Management¶
The state machine supports different time progression mechanisms:
Event-based: Time jumps to the next event (operation completion)
Continuous: Time progresses in fixed increments
Stochastic: Handles probabilistic completion times
These are implemented as time machines that calculate the next state based on the current one.
Middleware Integration¶
The state machine interacts with the rest of the framework through middleware:
Middleware translates gym actions into state machine transitions
State machine executes transitions and returns new states
Middleware converts state into observations for the agent
This separation of concerns keeps the state machine focused purely on simulating job shop dynamics, while middleware handles RL-specific aspects.