工作笔记|基于tensorflow的强化学习小游戏CartPole-v0代码实现

本文通过TensorFlow实现强化学习小游戏CartPole-v0,展示训练过程及结果,直接呈现核心代码。
摘要由CSDN通过智能技术生成

游戏图像

训练结果

废话不说直接上代码

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
# import tensorflow.compat.v1 as tf
# tf.disable_v2_behavior()
# tf.compat.v1.disable_eager_execution()

# 摆脱Gym提供的仿真环境,自己创建CartPole仿真环境
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 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值