您当前的位置: 首页 >  ar

FPGA硅农

暂无认证

  • 0浏览

    0关注

    282博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

【强化学习】之Q-Learning

FPGA硅农 发布时间:2021-12-15 14:47:05 ,浏览量:0

问题描述

在这里插入图片描述 如图所示,从左上角出发,每次只能往上下左右四个方向移动1个单位,要求设计一个路径,尽可能避免红色障碍,到达蓝色方格处。 首先,我们将问题抽象化,25个方格位置分别编号为0-24,表示25个状态,上、下、左、右为四个动作,如下图所示: 在这里插入图片描述

奖赏机制
def get_init_feedback_table(S,a):
    tab=np.ones((25,4))
    tab[8][1]=-10;tab[4][3]=-10;tab[14][2]=-10
    tab[11][1]=-10;tab[13][0]=-10;tab[7][3]=-10;tab[17][2]=-10
    tab[16][0]=-10;tab[20][2]=-10;tab[10][3]=-10;
    tab[18][0]=-10;tab[16][1]=-10;tab[22][2]=-10;tab[12][3]=-10
    tab[23][1]=50;tab[19][3]=50
    return tab[S,a]

如代码所示,当某个动作导致下一状态为红色障碍物时,R=-10,若进入蓝色终点,则R=50,否则R=1,需要注意的是,这里的R和Q表的Q(S,A)是不一样的,R是状态S采用动作A后得到的即时奖励。

Q-learning算法

在这里插入图片描述

实验代码
import numpy as np
import pandas as pd
import time

N_STATES = 25   # the length of the 2 dimensional world
ACTIONS = ['left', 'right','up','down']     # available actions
EPSILON = 0.3   # greedy police
ALPHA = 0.8     # learning rate
GAMMA = 0.9    # discount factor
MAX_EPISODES = 1000   # maximum episodes
FRESH_TIME = 0.00001    # fresh time for one move

def build_q_table(n_states, actions):
    table = pd.DataFrame(
        np.zeros((n_states, len(actions))),     # q_table initial values
        columns=actions,    # actions's name
    )
    return table

def choose_action(state, q_table):
    state_actions = q_table.iloc[state, :]
    if (np.random.uniform() > EPSILON) or ((state_actions == 0).all()):  # act non-greedy or state-action have no value
        if state==0:
            action_name=np.random.choice(['right','down'])
        elif state>0 and state20 and state            
关注
打赏
1658642721
查看更多评论
0.0366s