游戏图像
训练结果
废话不说直接上代码
import numpy as np
import pygame
import math
import time
from load import *
from pygame.locals import *
import gym
import matplotlib.pyplot as plt
import tensorflow as tf
class CartPoleEnv:
def __init__(self):
self.actions = [0,1]
self.state = np.random.uniform(-0.05, 0.05, size=(4,))
self.steps_beyond_done = 0
self.viewer = None
self.FPSCLOCK = pygame.time.Clock()
self.screen_size = [400,300]
self.cart_x = 200
self.cart_y = 200
self.theta = -1.5
self.gravity = 9.8
self.mass_cart = 1.0
self.mass_pole = 0.1
self.total_mass = (self.mass_cart + self.mass_pole)
self.length = 0.5
self.pole_mess_length = (self.mass_pole * self.length)
self.force_mag = 10.0
self.tau = 0.02
def reset(self):
n = np.random.randint(1, 1000, 1)
np.random.seed(n)
self.state = np.random.uniform(-0.05, 0.05, size=(4,))
self.steps_beyond_done = 0
return np.array(self.state)
def step(self, action):
state = self.statex, x_dot, theta, theta_dot = stateforce = self.force_mag if action==1 else -self.force_mag
costheta = math.cos(theta)
sintheta = math.sin(theta)
temp = (force+self.pole_mess_length * theta_dot * theta_dot * sintheta)/self.total_mass
thetaacc = (self.gravity * sintheta - costheta * temp)/(self.length * (4.0/3.0-self.mass_pole*costheta*costheta/self.total_mass))
xacc = temp - self.pole_mess_length * thetaacc * costheta / self.total_mass
x = x+self.tau * x_dot
x_dot = x_dot + self.tau * xacc
theta = theta +self.tau * theta_dot
theta_dot = theta_dot * self.tau * thetaacc
self.state