<jupyter_start><jupyter_text>Exercise 7.1Why do you think a larger random walk task (19 states instead of 5) was used in the examples of this chapter? Would a smaller walk have shifted the advantage to a different value of n? How about the change in left-side outcome from 0 to -1? Would that have made any difference in the best value of n?A small random walk would truncate large n-step to their total returns since episodes will be shorter (i.e. large n would just result in alpha MC methods). Therefore we should expect the advantage at lower n for smaller random walks. With values initialized at 0, if the left-most value terminated in 0 reward, we would need longer episodes for an agent to assign the correct values to the states left of center, since episodes that terminate to the left will not cause any updates initially, only the episodes that terminate to the right end with non-zero reward. Thus I would expect the best value of n to increase. --------- Exercise 7.2Why do you think on-line methods worked better than off-line methods on the example task?Off-line methods generally take random actions with some small probability $\epsilon$. We would expect at least 1-2 random actions in an environment with a minimum of 10 states to termination, depending on $\epsilon$ (assuming $\epsilon$ is between 10-20%). Therefore, even after finding the optimal action-values, these random actions will attribute erroneous rewards to certain actions, leading to higher RMSEs compared to on-line methods; we also see that larger n is more optimal for off-line methods compared to on-line, presumably because larger n reduces noise from the $\epsilon$ greedy actions. ----------- Exercise 7.3In the lower part of Figure  7.2, notice that the plot for n=3 is different from the others, dropping to low performance at a much lower value of $\alpha$ than similar methods. In fact, the same was observed for n=5, n=7, and n=9. Can you explain why this might have been so? In fact, we are not sure ourselves.My hypothesis is that odd values of n have higher RMSE because of the environment. It takes at a minimum, an odd number of steps to reach termination from the starting state. For off-line methods, even after finding the optimal action-values, an agent may still not terminate in an odd number of steps. Therefore my hypothesis is that odd n-step methods are more likely to cause erroneous updates to the $\epsilon$ greedy actions compared to even n-step methods. A quick way to test this, would be to create a random-walk where an agent will terminate at a minimum in an even number of steps, and then to observe the same plots as in Figure 7.2.  ---------- Exercise 7.4  The parameter $\lambda $ characterizes how fast the exponential weighting in Figure  7.4 falls off, and thus how far into the future the $\lambda $-return algorithm looks in determining its backup. But a rate factor such as $\lambda $ is sometimes an awkward way of characterizing the speed of the decay. For some purposes it is better to specify a time constant, or half-life. What is the equation relating $\lambda $ and the half-life, $\tau$, the time by which the weighting sequence will have fallen to half of its initial value?The half life occurs when weighting drops in half:$ \lambda^{n} = 0.5 $,which occurs at,$n = -ln(2) / ln(\lambda) = \tau$ -----Getting (7.3) from the equation above it:$R_t^\lambda = (1 - \lambda) \sum_{n=1}^\infty \lambda^{n-1} R^{(n)}_t$,after $T-t-1$, we sum to infinity but with $R^{T-t-1}_t$, which is just the total return $R_t$, so:$R_t^\lambda = (1 - \lambda) \sum_{n=1}^{T-t-1} \lambda^{n-1} R^{(n)}_t + (1 - \lambda) R_t \sum_{n=T-t-1}^{\infty} \lambda^{n} $We can remove $\lambda^{T-t-1}$ from the last sum to get $ (1 - \lambda) R_t \lambda^{T-t-1} \sum_{n=0}^\infty \lambda^n = (1 - \lambda) R_t \lambda^{T-t-1} \frac{1}{1 - \lambda}$, so that: $R_t^\lambda = (1 - \lambda) \sum_{n=1}^{T-t-1} \lambda^{n} R^{(n)}_t + \lambda^{T-t-1} R_t  $ ---------- Exercise 7.5In order to get TD($\lambda$) to be equivalent to the $\lambda$-return algorithm in the online case, the proposal is that $\delta_t = r_{t+1} + \gamma V_t(s_{t+1}) - V_{t-1}(s_t) $ and the n-step return is $R_t^{(n)} = r_{t+1} + \dots + \gamma^{n-1} r_{t+n} + \gamma^n V_{t+n-1}(s_{t+n}) $. To show that this new TD method is equivalent to the $\lambda$ return, it suffices to show that $\Delta V_t(s_t)$ for the $\lambda$ return is equivalent to the new TD with modified $\delta_t$ and $R_t^{(n)}$.As such, we expand the $\lambda$ return:$\begin{equation}\begin{split}\frac{1}{\alpha} \Delta V_t(s_t) =&  -V_{t-1}(s_t) + R_t^\lambda\\=& -V_{t-1}(s_t) + (1 - \lambda) \lambda^0 [r_{t+1} + \gamma V_t(s_{t+1})] + (1-\lambda) \lambda^1 [r_{t+1} + \gamma r_{t+2} + \gamma^2 V_{t+1}(s_{t+2})] + \dots\\=& -V_{t-1}(s_t) + (\gamma \lambda)^0 [r_{t+1} + \gamma V_t(s_{t+1}) - \gamma \lambda V_t(s_{t+1})] + (\gamma \lambda)^1 [r_{t+2} + \gamma V_{t+1}(s_{t+2}) - \gamma \lambda V_{t+1}(s_{t+2})] + \dots\\=& (\gamma \lambda)^0 [r_{t+1} + \gamma V_t(s_{t+1}) - V_{t-1}(s_t)] + (\gamma \lambda) [r_{t+2} + \gamma V_{t+1}(s_{t+2}) - V_t(s_t+1)] + \dots\\=& \sum_{k=t}^\infty (\gamma \lambda)^{k-t} \delta_k\end{split}\end{equation}$where $\delta_k = r_k + \gamma V_k(s_{k+1}) - V_{k-1}(s_k)$ as defined in the problem. Therefore, for online TD as defined above, the $\lambda$ return is exactly equivalent. ------------- Exercise 7.6In Example 7.5, suppose from state s the wrong action is taken twice before the right action is taken. If accumulating traces are used, then how big must the trace parameter $\lambda $ be in order for the wrong action to end up with a larger eligibility trace than the right action?  The eligibility trace update is $e_t(s) \leftarrow 1 + \gamma \lambda e_{t-1}(s)$ if $s = s_t$ and $e_t(s) \leftarrow \gamma \lambda e_{t-1}(s)$ if $s \neq s_t$. For two wrong actions, then one right action, $e_t(wrong) = (1 + \gamma \lambda) \gamma \lambda $, and $e_t(right) = 1$. If we want $e_t(wrong) \gt e_t(right)$, we need $(1 + \gamma \lambda) \gamma \lambda \gt 1$, or $\gamma \lambda \gt \frac{1}{2} (\sqrt(5) - 1)$. ----------- Exercise 7.7<jupyter_code>class LoopyEnvironment(object):
    def __init__(self):
        self._terminal_state = 5
        self._state = 0
        self._num_actions = 2
    
    @property
    def state(self):
        return self._state
    
    @state.setter
    def state(self, state):
        assert isinstance(state, int)
        assert state >= 0 and state <= self._terminal_state
        self._state = state
    
    @property
    def terminal_state(self):
        return self._terminal_state

    def reinit_state(self):
        self._state = 0
    
    def get_states_list(self):
        return range(self._terminal_state + 1)
    
    def get_actions_list(self):
        return range(self._num_actions)
    
    def is_terminal_state(self):
        return self._state == self._terminal_state
    
    def take_action(self, action):
        """
            action int: 0 or 1
                if action is 0 = wrong, then don't change the state
                if action is 1 = right, then go to the next state

            returns int: reward
        """
        assert action in [0, 1]
        assert self.is_terminal_state() == False
        if action == 1:
            self._state += 1
        if self._state == self._terminal_state:
            return 1
        return 0
import random
from itertools import product

class SARSA_lambda(object):
    def __init__(self, environment):
        states = environment.get_states_list()
        actions = environment.get_actions_list()
        
        self.environment = environment
        self.state_actions = list(product(states, actions))
        self.Q = np.random.random([len(states), len(actions)])
        self.e = np.zeros([len(states), len(actions)])
    
    def _get_epsilon_greedy_action(self, epsilon, p):
        if random.random() <= epsilon:
            action = random.randint(0, len(p) - 1)
            return action
        actions = np.where(p == np.amax(p))[0]
        action = np.random.choice(actions)
        return action
    
    def learn(self, num_episodes=100, Lambda=.9, gamma=.9, epsilon=.05, alpha=0.05,
             replace_trace=False):
        """
        Args:
            num_episodes (int): Number of episodes to train
            Lambda (float): TD(lambda) parameter 
                (if lambda = 1 we have MC or if lambda = 0 we have 1-step TD)
            gamma (float): decay parameter for Bellman equation
            epsilon (float): epsilon greedy decisions
            alpha (float): determines how big should TD update be
        
        Returns:
            list (int): the number of time steps it takes for each episode to terminate
        """
        
        time_steps = []
        for n in xrange(num_episodes):
            time_idx = 0
            self.e = self.e * 0
            self.environment.reinit_state()
            s = self.environment.state
            a = random.randint(0, self.Q.shape[1] - 1)
            while not self.environment.is_terminal_state():
                r = self.environment.take_action(a)
                time_idx += 1

                s_prime = self.environment.state
                a_prime = self._get_epsilon_greedy_action(epsilon, self.Q[s_prime, :])
                delta = r + gamma * self.Q[s_prime, a_prime] - self.Q[s, a]

                if replace_trace:
                    self.e[s, a] = 1
                else:
                    self.e[s, a] = self.e[s, a] + 1
                    
                for s, a in self.state_actions:
                    self.Q[s, a] = self.Q[s, a] + alpha * delta * self.e[s, a]
                    self.e[s, a] = gamma * Lambda * self.e[s, a]
                    
                s = s_prime
                a = a_prime
                
            time_steps.append(time_idx)
        return time_steps

env = LoopyEnvironment()
s = SARSA_lambda(env)<jupyter_output><empty_output><jupyter_text>Run both the replace-trace and the SARSA($\lambda$) regular trace methods for X episodes, and repeat N times. Get the average time length over all X episodes for each iteration for each alpha. In the environment in Figure 7.18, it takes at a minimum, 5 time steps to terminate. This is our baseline.<jupyter_code>def get_results(replace_trace, num_trials, num_episodes):
    alphas = np.linspace(.2, 1, num=10)
    results = np.array([])
    for alpha in alphas:
        res = []
        for i in xrange(num_trials):
            sarsa_lambda = SARSA_lambda(env)
            t = sarsa_lambda.learn(num_episodes=num_episodes, alpha=alpha, 
                                   replace_trace=replace_trace, gamma=0.9,
                                   epsilon=0.05, Lambda=0.9)
            res.append(np.mean(t))

        if results.shape[0] == 0:
            results = np.array([alpha, np.mean(res)])
        else:
            results = np.vstack([results, [alpha, np.mean(res)]])
    return results

num_trials = 100
num_episodes = 20
replace_trace = get_results(True, num_trials, num_episodes)
regular_trace = get_results(False, num_trials, num_episodes)
        
plt.plot(replace_trace[:, 0], replace_trace[:, 1], label='replace')
plt.plot(regular_trace[:, 0], regular_trace[:, 1], label='regular')

plt.legend()
plt.title('Exercise 7.7: First %d episodes averaged %d times' %(num_episodes, num_trials))
plt.xlabel('alpha')
plt.ylabel('Time-steps')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12092214006089604, 'fraction_numerical': 0.01644193127446716, 'mean_word_length': 2.7203883495145633, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '51503', 'license': 'MIT', 'n_tokens_mistral': 3635, 'n_tokens_neox': 3446, 'n_words': 1318, 'path': 'notebooks/chapter7.ipynb', 'repo_name': 'btaba/intro-to-rl', 'size': '11495'}