kups.core.schedule
¶
Property scheduling for time-dependent simulation parameters.
This module provides a framework for scheduling property changes during simulation, such as temperature annealing, pressure ramps, and time step adaptation.
Key components:
- Schedule: Protocol for property schedules
- Scheduler: Protocol for state schedulers
- PropertyScheduler: Applies schedules to state properties
- ConstantSchedule: Returns a fixed value
- IncrementSchedule: Increments current value by fixed amount
- LinearSchedule: Linear interpolation between values
- ExponentialSchedule: Exponential decay/growth
- StepFunctionSchedule: Discrete step changes
- CosineAnnealingSchedule: Smooth cosine-based annealing
The Input and Value types are generic, enabling both simple step-based scheduling
(where Value = Array) and complex acceptance-based scheduling (where Value = ParameterSchedulerState).
ComposedSchedule
¶
Bases: Schedule[Array, Value]
Compose two schedules sequentially.
Uses the first schedule for inputs below transition_input, then switches
to the second schedule (with input offset by transition_input).
Supports PyTree values - both schedules must return the same PyTree structure.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
first |
Schedule[Array, Value]
|
Schedule to use before transition |
second |
Schedule[Array, Value]
|
Schedule to use after transition |
transition_input |
Array
|
Input value at which to switch schedules |
Example
Source code in src/kups/core/schedule.py
ConstantSchedule
¶
Bases: Schedule[Array, Value]
Schedule that returns a constant value.
Ignores both input and current value, always returning the specified constant.
Supports PyTree values.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
value |
Value
|
The constant value to return (can be any PyTree) |
Example
Source code in src/kups/core/schedule.py
CosineAnnealingSchedule
¶
Bases: Schedule[Array, Value]
Cosine annealing schedule for smooth value transitions.
Computes: min_value + 0.5 * (max_value - min_value) * (1 + cos(pi * input / total_steps))
Provides smooth transitions that slow down at the extremes, starting at
max_value and annealing to min_value over total_steps.
Supports PyTree values - all leaves are annealed uniformly.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
min_value |
Value
|
Minimum (target) value (PyTree structure must match max_value) |
max_value |
Value
|
Maximum (starting) value (PyTree structure must match min_value) |
total_steps |
Array
|
Period of one cosine cycle |
Example
Source code in src/kups/core/schedule.py
ExponentialSchedule
¶
Bases: Schedule[Array, Value]
Exponential decay or growth of a property.
Computes: clamp(current * rate, min_value, max_value)
Each call multiplies the current value by the rate, enabling exponential decay (rate < 1) or growth (rate > 1).
Supports PyTree values - all leaves are scaled by the same rate.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
rate |
Array
|
Multiplicative factor per step (< 1 for decay, > 1 for growth) |
bounds |
tuple[Value | None, Value | None]
|
Optional (min, max) bounds to clamp the result (PyTree or None) |
Example
Source code in src/kups/core/schedule.py
IncrementSchedule
¶
Bases: Schedule[Any, Value]
Schedule that increments the current value by a fixed amount.
Ignores the input and returns current + increment.
Supports PyTree values - all leaves are incremented by the same amount.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
increment |
Value
|
The amount to add to the current value |
Example
Source code in src/kups/core/schedule.py
LinearSchedule
¶
Bases: Schedule[Array, Value]
Linear interpolation between start and end values.
Computes: start + (end - start) * clamp(input / total_steps, 0, 1)
The value transitions linearly from start to end over total_steps,
then remains at end for all subsequent steps.
Supports PyTree values - all leaves are interpolated uniformly.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
start |
Value
|
Initial value at step 0 (PyTree structure must match end) |
end |
Value
|
Final value at total_steps (PyTree structure must match start) |
total_steps |
Array
|
Number of steps over which to interpolate |
Example
# Temperature ramp from 300K to 500K over 10000 steps
schedule = LinearSchedule(
start=jnp.array(300.0),
end=jnp.array(500.0),
total_steps=jnp.array(10000)
)
temp = schedule(jnp.array(5000), current) # Returns 400.0
# Can also interpolate PyTrees
schedule = LinearSchedule(
start={"temp": jnp.array(300.0), "pressure": jnp.array(1.0)},
end={"temp": jnp.array(500.0), "pressure": jnp.array(2.0)},
total_steps=jnp.array(10000)
)
Source code in src/kups/core/schedule.py
PropertyScheduler
¶
Bases: Scheduler[State, Input]
Applies a Schedule to update a property in state.
This is a generic scheduler that works with any Schedule implementation. It reads the current value via a lens, applies the schedule, and writes the result back.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Input
|
Schedule input type |
required | |
Value
|
Type of value being scheduled |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
lens |
Lens[State, Value]
|
Lens to access and update the scheduled property |
schedule |
Schedule[Input, Value]
|
Schedule function that computes new values |
Example
# Step-based temperature scheduling
temp_scheduler = PropertyScheduler(
lens=lens(lambda s: s.temperature),
schedule=LinearSchedule(
start=jnp.array(500.0),
end=jnp.array(300.0),
total_steps=jnp.array(10000)
)
)
# Apply with explicit input
state = temp_scheduler(state, step)
# Acceptance-based scheduling (for MCMC)
step_size_scheduler = PropertyScheduler(
lens=lens(lambda s: s.scheduler_state),
schedule=acceptance_target_schedule
)
# Called in MCMC loop
state = step_size_scheduler(state, acceptance)
Source code in src/kups/core/schedule.py
__call__(state, input)
¶
Apply the schedule to update the state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current simulation state |
required |
input
|
Input
|
Scheduling input (step, acceptance, etc.) |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state with scheduled property modified |
Source code in src/kups/core/schedule.py
Schedule
¶
Bases: Protocol
Protocol for property schedules.
A schedule defines how a property value changes based on some input.
Both Input and Value are generic, allowing flexible scheduling strategies.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Input
|
Type of scheduling input (step number, acceptance rate, time, etc.) |
required | |
Value
|
Type of value being scheduled (Array, ParameterSchedulerState, etc.) |
required |
Example
# Simple step-based schedule
class MySchedule:
def __call__(self, step: Array, current: Array) -> Array:
return current * 0.999 # 0.1% decay per step
# Complex state-based schedule
class AcceptanceSchedule:
def __call__(self, acceptance: Array, state: SchedulerState) -> SchedulerState:
# Update state based on acceptance rate
...
Source code in src/kups/core/schedule.py
__call__(input, current)
¶
Compute the scheduled value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Input
|
Scheduling input (e.g., step number, acceptance rate) |
required |
current
|
Value
|
Current value of the property |
required |
Returns:
| Type | Description |
|---|---|
Value
|
New value for the property |
Source code in src/kups/core/schedule.py
Scheduler
¶
Bases: Protocol
Protocol for schedulers that update state based on input.
This protocol defines the interface for any scheduler that can be used with MCMCPropagator or other components that need to adjust parameters based on some input signal.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
Simulation state type |
required | |
Input
|
Type of input signal (e.g., acceptance flags, step number) |
required |
Example
Source code in src/kups/core/schedule.py
__call__(state, input)
¶
Update state based on input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current simulation state |
required |
input
|
Input
|
Input signal (e.g., acceptance flags) |
required |
Returns:
| Type | Description |
|---|---|
State
|
Updated state |
StepFunctionSchedule
¶
Bases: Schedule[Array, Value]
Step function schedule with discrete value changes.
Returns the value corresponding to the largest step threshold not exceeding the current input.
Supports PyTree values - the values attribute should be a list/tuple of
PyTrees with matching structure.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
Type of value being scheduled (Array or PyTree of Arrays) |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
steps |
Array
|
Array of step thresholds (must be sorted ascending) |
values |
tuple[Value, ...]
|
Tuple of values corresponding to each threshold (PyTree structure) |
Example
# Change temperature at specific steps
schedule = StepFunctionSchedule(
steps=jnp.array([0, 1000, 5000, 10000]),
values=(
jnp.array(300.0),
jnp.array(350.0),
jnp.array(400.0),
jnp.array(300.0),
)
)
# step 0-999: 300.0
# step 1000-4999: 350.0
# step 5000-9999: 400.0
# step 10000+: 300.0
# Can also use PyTree values
schedule = StepFunctionSchedule(
steps=jnp.array([0, 1000]),
values=(
{"temp": jnp.array(300.0), "pressure": jnp.array(1.0)},
{"temp": jnp.array(400.0), "pressure": jnp.array(2.0)},
)
)