|
| 1 | +# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python |
| 2 | +# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python |
| 3 | +from __future__ import print_function, division |
| 4 | +from builtins import range |
| 5 | +# Note: you may need to update your version of future |
| 6 | +# sudo pip install -U future |
| 7 | + |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import pandas as pd |
| 11 | +import matplotlib.pyplot as plt |
| 12 | +from grid_world import standard_grid, negative_grid |
| 13 | +from iterative_policy_evaluation import print_values, print_policy |
| 14 | +from sklearn.kernel_approximation import Nystroem, RBFSampler |
| 15 | + |
| 16 | +GAMMA = 0.9 |
| 17 | +ALPHA = 0.1 |
| 18 | +ALL_POSSIBLE_ACTIONS = ('U', 'D', 'L', 'R') |
| 19 | +ACTION2INT = {a: i for i, a in enumerate(ALL_POSSIBLE_ACTIONS)} |
| 20 | +INT2ONEHOT = np.eye(len(ALL_POSSIBLE_ACTIONS)) |
| 21 | + |
| 22 | + |
| 23 | +def epsilon_greedy(model, s, eps=0.1): |
| 24 | + # we'll use epsilon-soft to ensure all states are visited |
| 25 | + # what happens if you don't do this? i.e. eps=0 |
| 26 | + p = np.random.random() |
| 27 | + if p < (1 - eps): |
| 28 | + values = model.predict_all_actions(s) |
| 29 | + return ALL_POSSIBLE_ACTIONS[np.argmax(values)] |
| 30 | + else: |
| 31 | + return np.random.choice(ALL_POSSIBLE_ACTIONS) |
| 32 | + |
| 33 | + |
| 34 | +def one_hot(k): |
| 35 | + return INT2ONEHOT[k] |
| 36 | + |
| 37 | + |
| 38 | +def merge_state_action(s, a): |
| 39 | + ai = one_hot(ACTION2INT[a]) |
| 40 | + return np.concatenate((s, ai)) |
| 41 | + |
| 42 | + |
| 43 | +def gather_samples(grid, n_episodes=1000): |
| 44 | + samples = [] |
| 45 | + for _ in range(n_episodes): |
| 46 | + s = grid.reset() |
| 47 | + while not grid.game_over(): |
| 48 | + a = np.random.choice(ALL_POSSIBLE_ACTIONS) |
| 49 | + sa = merge_state_action(s, a) |
| 50 | + samples.append(sa) |
| 51 | + |
| 52 | + r = grid.move(a) |
| 53 | + s = grid.current_state() |
| 54 | + return samples |
| 55 | + |
| 56 | + |
| 57 | +class Model: |
| 58 | + def __init__(self, grid): |
| 59 | + # fit the featurizer to data |
| 60 | + samples = gather_samples(grid) |
| 61 | + # self.featurizer = Nystroem() |
| 62 | + self.featurizer = RBFSampler() |
| 63 | + self.featurizer.fit(samples) |
| 64 | + dims = self.featurizer.n_components |
| 65 | + |
| 66 | + # initialize linear model weights |
| 67 | + self.w = np.zeros(dims) |
| 68 | + |
| 69 | + def predict(self, s, a): |
| 70 | + sa = merge_state_action(s, a) |
| 71 | + x = self.featurizer.transform([sa])[0] |
| 72 | + return x @ self.w |
| 73 | + |
| 74 | + def predict_all_actions(self, s): |
| 75 | + return [self.predict(s, a) for a in ALL_POSSIBLE_ACTIONS] |
| 76 | + |
| 77 | + def grad(self, s, a): |
| 78 | + sa = merge_state_action(s, a) |
| 79 | + x = self.featurizer.transform([sa])[0] |
| 80 | + return x |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + # use the standard grid again (0 for every step) so that we can compare |
| 85 | + # to iterative policy evaluation |
| 86 | + # grid = standard_grid() |
| 87 | + grid = negative_grid(step_cost=-0.1) |
| 88 | + |
| 89 | + # print rewards |
| 90 | + print("rewards:") |
| 91 | + print_values(grid.rewards, grid) |
| 92 | + |
| 93 | + model = Model(grid) |
| 94 | + reward_per_episode = [] |
| 95 | + state_visit_count = {} |
| 96 | + |
| 97 | + # repeat until convergence |
| 98 | + n_episodes = 20000 |
| 99 | + for it in range(n_episodes): |
| 100 | + if (it + 1) % 100 == 0: |
| 101 | + print(it + 1) |
| 102 | + |
| 103 | + s = grid.reset() |
| 104 | + state_visit_count[s] = state_visit_count.get(s, 0) + 1 |
| 105 | + episode_reward = 0 |
| 106 | + while not grid.game_over(): |
| 107 | + a = epsilon_greedy(model, s) |
| 108 | + r = grid.move(a) |
| 109 | + s2 = grid.current_state() |
| 110 | + state_visit_count[s2] = state_visit_count.get(s2, 0) + 1 |
| 111 | + |
| 112 | + # get the target |
| 113 | + if grid.game_over(): |
| 114 | + target = r |
| 115 | + else: |
| 116 | + values = model.predict_all_actions(s2) |
| 117 | + target = r + GAMMA * np.max(values) |
| 118 | + |
| 119 | + # update the model |
| 120 | + g = model.grad(s, a) |
| 121 | + err = target - model.predict(s, a) |
| 122 | + model.w += ALPHA * err * g |
| 123 | + |
| 124 | + # accumulate reward |
| 125 | + episode_reward += r |
| 126 | + |
| 127 | + # update state |
| 128 | + s = s2 |
| 129 | + |
| 130 | + reward_per_episode.append(episode_reward) |
| 131 | + |
| 132 | + plt.plot(reward_per_episode) |
| 133 | + plt.title("Reward per episode") |
| 134 | + plt.show() |
| 135 | + |
| 136 | + # obtain V* and pi* |
| 137 | + V = {} |
| 138 | + greedy_policy = {} |
| 139 | + states = grid.all_states() |
| 140 | + for s in states: |
| 141 | + if s in grid.actions: |
| 142 | + values = model.predict_all_actions(s) |
| 143 | + V[s] = np.max(values) |
| 144 | + greedy_policy[s] = ALL_POSSIBLE_ACTIONS[np.argmax(values)] |
| 145 | + else: |
| 146 | + # terminal state or state we can't otherwise get to |
| 147 | + V[s] = 0 |
| 148 | + |
| 149 | + print("values:") |
| 150 | + print_values(V, grid) |
| 151 | + print("policy:") |
| 152 | + print_policy(greedy_policy, grid) |
| 153 | + |
| 154 | + |
| 155 | + print("state_visit_count:") |
| 156 | + state_sample_count_arr = np.zeros((grid.rows, grid.cols)) |
| 157 | + for i in range(grid.rows): |
| 158 | + for j in range(grid.cols): |
| 159 | + if (i, j) in state_visit_count: |
| 160 | + state_sample_count_arr[i,j] = state_visit_count[(i, j)] |
| 161 | + df = pd.DataFrame(state_sample_count_arr) |
| 162 | + print(df) |
0 commit comments