INNER CODE UNIT · Python
policy_improvement
rlcode/reinforcement-learning · 1-grid-world/1-policy_iteration.py:41
def policy_improvement(self):
next_policy = self.policy_table
for state in self.env.get_all_states():
if state == [2, 2]:
continue
value = -99999
max_index = []
result = [0.0, 0.0, 0.0, 0.0] # initialize the policy
# for every actions, calculate
# [reward + (discount factor) * (next state value function)]
for index, action in enumerate(self.env.possible_actions):
next_state = self.env.state_after_action(state, action)
reward = self.env.get_reward(state, action)
next_value = self.get_value(next_state)
temp = reward + self.discount_factor * next_value
# We normally can't pick multiple actions in greedy policy.