第17章強化学習
17.2.8. Implementation of Value Iteration17.2.8 価値反復の実装
We next show how to implement Value Iteration for a navigation problem called FrozenLake from Open AI Gym. We first need to setup the enviroment as shown in the following code.
英語のヒント
次に、Open AI GymのFrozenLakeと呼ばれる移動の問題について、価値反復をどう実装するかを示す。まず、次のコードに示すように環境を用意する必要がある。
%matplotlib inline
import random
import numpy as np
from d2l import torch as d2l
seed = 0 # Random number generator seed
gamma = 0.95 # Discount factor
num_iters = 10 # Number of iterations
random.seed(seed) # Set the random seed to ensure results can be reproduced
np.random.seed(seed)
# Now set up the environment
env_info = d2l.make_env('FrozenLake-v1', seed=seed)In the FrozenLake environment, the robot moves on a grid (these are the states) with actions that are “up” (), “down” (), “left” (), and “right” (). The environment contains a number of holes (H) cells and frozen (F) cells as well as a goal cell (G), all of which are unknown to the robot. To keep the problem simple, we assume the robot has reliable actions, i.e. for all . If the robot reaches the goal, the trial ends and the robot receives a reward of irrespective of the action; the reward at any other state is for all actions. The objective of the robot is to learn a policy that reaches the goal location (G) from a given start location (S) (this is ) to maximize the return.
英語のヒント
FrozenLakeの環境では、ロボットはの格子(これが状態である)の上を、「上」()、「下」()、「左」()、「右」()の行動で動く。環境には、いくつかの穴(H)のマスと凍った(F)マス、そして目標のマス(G)があり、そのどれもロボットにはわからない。問題を簡単にするため、ロボットの行動は確実であると仮定する。つまり、すべてのについてである。ロボットが目標に到達すると試行は終わり、ロボットは行動にかかわらずの報酬を受け取る。それ以外の状態での報酬は、すべての行動についてである。ロボットの目的は、与えられた出発の場所(S)(これがである)から目標の場所(G)に到達する方策を学習して、収益を最大化することである。
The following function implements Value Iteration, where
env_info contains MDP and environment related information and gamma is the discount factor:英語のヒント
次の関数は価値反復を実装する。ここで、
env_infoはMDPと環境に関する情報を含み、gammaは割引率である。def value_iteration(env_info, gamma, num_iters):
env_desc = env_info['desc'] # 2D array shows what each item means
prob_idx = env_info['trans_prob_idx']
nextstate_idx = env_info['nextstate_idx']
reward_idx = env_info['reward_idx']
num_states = env_info['num_states']
num_actions = env_info['num_actions']
mdp = env_info['mdp']
V = np.zeros((num_iters + 1, num_states))
Q = np.zeros((num_iters + 1, num_states, num_actions))
pi = np.zeros((num_iters + 1, num_states))
for k in range(1, num_iters + 1):
for s in range(num_states):
for a in range(num_actions):
# Calculate \sum_{s'} p(s'\mid s,a) [r + \gamma v_k(s')]
for pxrds in mdp[(s,a)]:
# mdp(s,a): [(p1,next1,r1,d1),(p2,next2,r2,d2),..]
pr = pxrds[prob_idx] # p(s'\mid s,a)
nextstate = pxrds[nextstate_idx] # Next state
reward = pxrds[reward_idx] # Reward
Q[k,s,a] += pr * (reward + gamma * V[k - 1, nextstate])
# Record max value and max action
V[k,s] = np.max(Q[k,s,:])
pi[k,s] = np.argmax(Q[k,s,:])
d2l.show_value_function_progress(env_desc, V[:-1], pi[:-1])
value_iteration(env_info=env_info, gamma=gamma, num_iters=num_iters)
The above pictures show the policy (the arrow indicates the action) and value function (the change in color shows how the value function changes over time from the initial value shown by dark color to the optimal value shown by light colors.). As we see, Value Iteration finds the optimal value function after 10 iterations and the goal state (G) can be reached starting from any state as long as it is not an H cell. Another interesting aspect of the implementation is that in addition to finding the optimal value function, we also automatically found the optimal policy corresponding to this value function.
英語のヒント
上の図は、方策(矢印が行動を示す)と価値関数(色の変化は、暗い色で示した初期値から明るい色で示した最適な値へと、価値関数が時間とともにどう変わるかを示す)を表している。見てのとおり、価値反復は10回の反復の後に最適な価値関数を見つけ、Hのマスでないかぎり、どの状態から始めても目標の状態(G)に到達できる。この実装のもう一つの興味深い点は、最適な価値関数を見つけるのに加えて、この価値関数に対応する最適方策も自動的に見つかったことである。