The REINFORCE algorithm is a foundational policy gradient method for reinforcement learning. Adding a baseline to REINFORCE reduces the variance of the gradient estimate without introducing bias, leading to more stable learning.
Implement a function reinforce_baseline_update that processes a single episode and updates both the softmax policy parameters and linear baseline parameters.
episode (list of tuples): A sequence of (state, action, reward) tuples representing one complete episode. Each state is a list of floats (feature vector), action is an integer, and reward is a float.theta (numpy 2D array): Policy parameters of shape (n_features, n_actions) for a softmax policy.w (numpy 1D array): Baseline parameters of shape (n_features,) for a linear value baseline.gamma (float): Discount factor in [0, 1].alpha_theta (float): Learning rate for the policy parameters.alpha_w (float): Learning rate for the baseline parameters.The policy computes action probabilities as softmax over theta^T @ state for each state.
Process each timestep of the episode sequentially (from t=0 to T-1). At each step, the current (possibly already-updated) parameters should be used.
Return a dictionary with:
'theta': Updated policy parameters as a nested list (2D)'w': Updated baseline parameters as a list (1D)'returns': List of discounted returns G_t for each timestep'advantages': List of advantage values (G_t minus baseline prediction) at each timestepUse numerically stable softmax (subtract the max logit before exponentiating).