Trajectories and advantages¶
gympn.data.TrajectoryBuffer ¶
Episodic rollout buffer.
Stores per step the observation (dict with 'graph'), the action, the raw
reward, the log-probability of the chosen action, the value prediction,
the old policy's log-probabilities over every action node, and the
simulator clock at the decision. finish() closes an episode and
computes its advantages and value targets; get() hands back the
training batches.
Advantages:
- default: GAE with a constant discount gam;
- smdp_discount=True: SMDP-GAE, the continuation after a decision is
discounted by e^{-beta * tau}, tau the time to the next decision;
- nfgae=True: net-factored GAE, one SMDP-GAE per net component on the
component's own decision clock (suite/paper/NFGAE_THEORY.md). Needs
_nf_comp/_nf_rew set by the agent before finish().
store ¶
store(state, action, reward, logprob, value, logpis, time: Optional[float] = None)
Append one interaction.
finish ¶
finish()
Close the current episode [start:end) and compute its advantages and value targets (the GAE / TD(lambda) return, advantage + V).
subset ¶
subset(idx: List[int]) -> TrajectoryBuffer
A new buffer holding the finished steps idx (in that order), with their advantages and value targets as computed here. Used to hand each component's agent its own decisions (gympn.component_agents).
get ¶
get(batch_size=64, normalize_advantages=True, normalize_returns=False, sort=True, drop_remainder=False)
Build the training batches. Each sample carries y (action), advantage, value (target), logprobs (chosen action) and the old policy's logpis split per action-node type (a_transition, postpone).
gympn.data.compute_advantages ¶
compute_advantages(rewards: Tensor, values: Tensor, gamma: float, lam: float, dones: Optional[Tensor] = None, last_value: float = 0.0) -> torch.Tensor
Compute generalized advantage estimation (GAE).
Optimized to avoid tensor allocation in loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rewards
|
(Tensor, shape(T))
|
Step rewards |
required |
values
|
(Tensor, shape(T) or (T + 1,))
|
Value function estimates at each step |
required |
gamma
|
float
|
Discount factor |
required |
lam
|
float
|
GAE lambda parameter |
required |
dones
|
(Tensor, optional, shape(T))
|
Done flags (True = episode ended) |
None
|
last_value
|
float
|
Bootstrap value for next state |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
advantages |
(Tensor, shape(T))
|
Computed advantages |
gympn.data.smdp_gae ¶
smdp_gae(rewards: Tensor, values: Tensor, discounts: Tensor, lam: float, dones: Optional[Tensor] = None) -> Tensor
GAE with a PER-STEP (SMDP) discount instead of a constant gamma.
delta_t = r_t + d_t * V(s_{t+1}) - V(s_t)
A_t = delta_t + d_t * lam * A_{t+1}
where d_t = discounts[t] * mask_t (mask=0 at a terminal step, dropping the bootstrap). This is the time-aware advantage of SMDP actor-critic: the continuation V(s_{t+1}) is discounted by the actual sojourn, so a decision that only spends time (e.g. postpone) carries its opportunity cost.