RL sampler (experimental)¶
ResidualBanditSampler ¶
ResidualBanditSampler(
domain,
time_domain,
n_x_cells=16,
n_t_cells=8,
policy_fraction=0.5,
temperature=0.5,
probe_cells=32,
probe_points=8,
probe_every=1,
agent=None,
device=None,
)
Collocation sampler whose cell preferences are learned by a DQN bandit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
domain
|
tuple[float, float]
|
|
required |
time_domain
|
tuple[float, float]
|
|
required |
n_x_cells
|
int
|
Number of cells along |
16
|
n_t_cells
|
int
|
Number of cells along |
8
|
policy_fraction
|
float
|
Share of each batch drawn from the policy; the rest is uniform over the full domain. |
0.5
|
temperature
|
float
|
Softmax temperature on the Q-values. |
0.5
|
probe_cells
|
int
|
Cells probed per call to collect rewards. |
32
|
probe_points
|
int
|
Residual evaluations per probed cell. |
8
|
probe_every
|
int
|
Probe (and train the agent) on every |
1
|
agent
|
RLAgent | None
|
Optional pre-built agent; must have |
None
|
device
|
device | None
|
Torch device. |
None
|
Source code in pinnrl/rl/bandit_sampler.py
probe ¶
Probe random cells, push rewards to the agent, and return the state.
probe_points random points in each of min(probe_cells, n_cells)
distinct cells are scored by |residual|. The state is
[log10 mean, log10 max, log10 std, progress] over all probed
values (the mean is the mean of the cell means); each cell's reward
is its mean divided by that overall mean. Each transition is stored
with next_state = state, which gamma = 0 ignores.
Source code in pinnrl/rl/bandit_sampler.py
distribution ¶
ε-mixture of softmax(Q / temperature) and the uniform distribution.
The Q-network is evaluated in eval mode (dropout off), so the same state always gives the same distribution.
Source code in pinnrl/rl/bandit_sampler.py
sample ¶
Return num_points collocation points, as (x, t) columns.
Probes and updates the agent first on every probe_every-th call
(and on the first); other calls reuse the last state. progress in
[0, 1] is the training-progress entry of the state.
Source code in pinnrl/rl/bandit_sampler.py
RLAgent ¶
RLAgent(
state_dim,
action_dim,
hidden_dim,
learning_rate=0.0001,
gamma=0.99,
epsilon_start=1.0,
epsilon_end=0.01,
epsilon_decay=0.995,
memory_size=10000,
batch_size=64,
target_update=100,
device=None,
)
DQN agent with experience replay and an ε schedule that decays per update.
Initialize RL agent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dim
|
int
|
State dimension. |
required |
action_dim
|
int
|
Number of discrete actions. |
required |
hidden_dim
|
int
|
Hidden layer dimension. |
required |
learning_rate
|
float
|
Learning rate. |
0.0001
|
gamma
|
float
|
Discount factor ( |
0.99
|
epsilon_start
|
float
|
Initial exploration rate. |
1.0
|
epsilon_end
|
float
|
Final exploration rate. |
0.01
|
epsilon_decay
|
float
|
Multiplicative ε decay applied on every :meth: |
0.995
|
memory_size
|
int
|
Size of replay buffer. |
10000
|
batch_size
|
int
|
Batch size for training. |
64
|
target_update
|
int
|
Updates between target-network syncs. |
100
|
device
|
device | None
|
Device to place the networks on. |
None
|
Source code in pinnrl/rl/rl_agent.py
update ¶
Store a transition, train on a replayed batch, and decay ε.
Training starts once the buffer holds batch_size transitions; the
target network is synced every target_update calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Tensor
|
Current state. |
required |
action
|
int
|
Action taken. |
required |
reward
|
float
|
Reward received. |
required |
next_state
|
Tensor
|
Next state. |
required |
done
|
bool
|
Whether the episode ended. |
required |
Source code in pinnrl/rl/rl_agent.py
DQNNetwork ¶
Bases: Module
MLP mapping a state to one Q-value per action (Linear-LayerNorm-ReLU-Dropout blocks).
Initialize DQN network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state_dim
|
int
|
State dimension. |
required |
action_dim
|
int
|
Action dimension. |
required |
hidden_dim
|
int
|
Hidden layer dimension. |
required |
num_layers
|
int
|
Number of linear layers, input and output included. |
3
|
dropout
|
float
|
Dropout rate after each hidden block. |
0.1
|
Source code in pinnrl/rl/rl_agent.py
forward ¶
Forward pass through the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Q-values. |
ReplayBuffer ¶
Experience replay buffer for training stability.
Initialize replay buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
Maximum number of experiences to store. |
required |
Source code in pinnrl/rl/rl_agent.py
push ¶
Add experience to buffer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Tensor
|
Current state. |
required |
action
|
int
|
Action taken. |
required |
reward
|
float
|
Reward received. |
required |
next_state
|
Tensor
|
Next state. |
required |
done
|
bool
|
Whether episode is done. |
required |
Source code in pinnrl/rl/rl_agent.py
sample ¶
Sample a batch of experiences without replacement.
Indices come from torch's generator, so torch.manual_seed makes
the draw (and a bandit run) reproducible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch_size
|
int
|
Size of batch to sample. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple]
|
List of experiences. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |