```import math
import sys
import numpy as np
import pygame
class Maze(object):
state_dim = 4
action_dim = 1
action_bound = [0, 2 * math.pi]
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((600, 500))
# self.a = np.random.random_sample() * (2 * math.pi) # action 取值范围[0, 2π]
self.env = pygame.draw.rect(self.screen, (0, 0, 0), (45, 30, 500, 450), 2)
# 初始化agent的信息
self.agent_pos_x = 48
self.agent_pos_y = 34
self.v = 0.7
self.vel_x = 0
self.vel_y = 0
self.agent_pos = self.agent_pos_x, self.agent_pos_y, 50, 45
# start point
self.start_pos_x = 48
self.start_pos_y = 34
self.start_pos = self.start_pos_x, self.start_pos_y, 50, 45
# hell位置信息
self.hell_pos_x = 300 # hell initial position
self.hell_pos_y = 250
self.vel_xh = 0 # .1 * np.random.random_sample() # make hell move
self.vel_yh = 0 # .1 * np.random.random_sample()
# hell point
self.hell_pos_x += self.vel_xh
self.hell_pos_y += self.vel_yh
self.hell_pos = self.hell_pos_x, self.hell_pos_y, 50, 45
if self.hell_pos_x > 490 or self.hell_pos_x < 48:
self.vel_xh = -self.vel_xh
if self.hell_pos_y > 430 or self.hell_pos_y < 34:
self.vel_yh = -self.vel_yh
# end point
self.end_pos_x = 490
self.end_pos_y = 430
self.end_pos = self.end_pos_x, self.end_pos_y, 50, 45
# self.end = pygame.draw.rect(self.screen, (255, 0, 0), self.end_pos, 0)
# pygame.display.set_caption("Simple Moving")
###########################################################################################
def step(self, action):
done = False
# action = np.clip(action, a_min=0, a_max=2 * math.pi)
action = np.clip(action, *self.action_bound)
self.vel_x = self.v * math.cos(action)
self.vel_y = self.v * math.sin(action)
self.agent_pos_x = self.agent_pos_x + self.vel_x
self.agent_pos_y = self.agent_pos_y + self.vel_y
# def colliderect(self):
# self.agent = pygame.draw.rect(self.screen, (255, 255, 0), (self.agent_pos_x, self.agent_pos_y, 50, 45), 0)
# normalize features
dist1 = [(self.end_pos_x - self.agent_pos_x), (self.end_pos_y - self.agent_pos_y)]
if self.agent.colliderect(self.hell): # 智能体与障碍物是否相碰
reward = -1
print(reward)
print("misson failed")
done = True
elif self.agent.colliderect(self.end):
reward = 1
print("victory")
done = True
else:
reward = 0
done = False
s = np.concatenate(([self.agent_pos_x, self.agent_pos_y], dist1))
if done:
pygame.quit()
sys.exit()
return s, reward, done
def outside(self):
if self.agent_pos_y > 490 or self.agent_pos_y < 48: # 碰到边界线反弹
self.vel_x = -self.vel_x
if self.agent_pos_y > 430 or self.agent_pos_y < 34:
self.vel_y = -self.vel_y
def reset(self):
self.agent_pos_x = 48
self.agent_pos_y = 34
self.vel_x = 0
self.vel_y = 0
self.agent_pos = self.agent_pos_x, self.agent_pos_y, 50, 45
self.agent = pygame.draw.rect(self.screen, (255, 255, 0), (self.agent_pos_x, self.agent_pos_y, 50, 45), 0)
dist1 = [(self.end_pos_x - self.agent_pos_x), (self.end_pos_y - self.agent_pos_y)]
s = np.concatenate(([self.agent_pos_x, self.agent_pos_y], dist1))
return s
def render(self):
self.screen = pygame.display.set_mode((600, 500))
self.screen.fill((255, 255, 255))
pygame.draw.rect(self.screen, (0, 0, 0), (45, 30, 500, 450), 2)
pygame.draw.rect(self.screen, (0, 0, 0), self.start_pos, 2)
self.hell = pygame.draw.rect(self.screen, (0, 0, 0), self.hell_pos, 0)
self.end = pygame.draw.rect(self.screen, (255, 0, 0), self.end_pos, 0)
self.agent = pygame.draw.rect(self.screen, (255, 255, 0), (self.agent_pos_x, self.agent_pos_y, 50, 45), 0)
pygame.display.flip()
def sample_action(self):
return np.random.random_sample() * (2 * math.pi) # action 取值范围[0, 2π]
# def run(self):
# env = Maze()
# ''''''''''''''''''''''''''
# while True:
# for event in pygame.event.get():
# if event.type == pygame.QUIT:
# pygame.quit()
# exit(0)
# env.render()
# env.step(env.sample_action())
# # if env.colliderect() is True:
# # env.reset()
if __name__ == '__main__':
env = Maze()
''''''''''''''''''''''''''
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
env.render()
env.outside()
env.step(env.sample_action())
1. ****
网上大多碰到边缘都是按照我写的outside进行的,但是我的怎么运行还是会出边界,求解