Deep Q Network是DeepMind最早(2013年)提出来的,是深度强化学习方法。最开始AI什么也不会,通过给它提供游戏界面像素和分数,慢慢把它训练成游戏高手。这里首先给出一个基本的游戏例子,然后再给出强化学习方法。
1.基本游戏
#coding=utf-8
import pygame
from pygame.locals import *
import sys
BLACK =(0,0,0)
WHITE = (255,255,255)
SCREEN_SIZE = [320,400]#屏幕大小
BAR_SIZE = [20,5]#挡板大小
BALL_SIZE = [15,15]#球的尺寸
class Game(object):
def __init__(self):
pygame.init()
self.clock = pygame.time.Clock()#定时器
self.screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('Simple Game')
self.ball_pos_x = SCREEN_SIZE[0]//2 - BALL_SIZE[0]/2
self.ball_pos_y = SCREEN_SIZE[1]//2 - BALL_SIZE[1]/2
#ball 移动方向
self.ball_dir_x = -1 #-1:left 1:right
self.ball_dir_y = -1# -1:up
self.ball_pos = pygame.Rect(self.ball_pos_x,self.ball_pos_y,BALL_SIZE[0],BALL_SIZE[1])
self.score =0
self.bar_pos_x = SCREEN_SIZE[0]//2 - BAR_SIZE[0]//2
self.bar_pos = pygame.Rect(self.bar_pos_x,SCREEN_SIZE[1]-BAR_SIZE[1],BAR_SIZE[0],BALL_SIZE[1])
def bar_move_left(self):#左移
self.bar_pos_x = self.bar_pos_x - 2
def bar_move_right(self):
self.bar_pos_x = self.bar_pos_x + 2
def run(self):
pygame.mouse.set_visible(0) #移动鼠标不可见
bar_move_left =False
bar_move_right = False
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()#接收到退出事件后退出程序
elif event.type == pygame.MOUSEBUTTONDOWN and event.button ==1:#鼠标左键按下
bar_move_left = True
elif event.type == pygame.MOUSEBUTTONUP and event.button == 1: #左键弹起
bar_move_left = False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:#右键
bar_move_right = True
elif event.type == pygame.MOUSEBUTTONUP and event.button &#