cleanup algos + adjusted renderer to support "ray casting"

This commit is contained in:
romue
2021-07-27 16:59:24 +02:00
parent 8429e3db9d
commit 9e8d2ac1dc
9 changed files with 102 additions and 254 deletions

View File

@ -3,7 +3,7 @@ import numpy as np
from pathlib import Path
from collections import deque
import pygame
from typing import NamedTuple
from typing import NamedTuple, Any
import time
@ -14,6 +14,7 @@ class Entity(NamedTuple):
value_operation: str = 'none'
state: str = None
id: int = 0
aux:Any=None
class Renderer:
@ -73,6 +74,20 @@ class Renderer:
asset = pygame.transform.smoothscale(asset, (s, s))
return asset
def visibility_rects(self, bp, view):
rects = []
for i in range(-self.view_radius, self.view_radius+1):
for j in range(-self.view_radius, self.view_radius+1):
if bool(view[self.view_radius+j, self.view_radius+i]):
visibility_rect = bp['dest'].copy()
visibility_rect.centerx += i*self.cell_size
visibility_rect.centery += j*self.cell_size
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, self.AGENT_VIEW_COLOR, shape_surf.get_rect())
shape_surf.set_alpha(64)
rects.append(dict(source=shape_surf, dest=visibility_rect))
return rects
def render(self, entities):
for event in pygame.event.get():
if event.type == pygame.QUIT:
@ -88,13 +103,8 @@ class Renderer:
blits.append(bp)
if entity.name.lower() == 'agent':
if self.view_radius > 0:
visibility_rect = bp['dest'].inflate(
(self.view_radius*2)*self.cell_size, (self.view_radius*2)*self.cell_size
)
shape_surf = pygame.Surface(visibility_rect.size, pygame.SRCALPHA)
pygame.draw.rect(shape_surf, self.AGENT_VIEW_COLOR, shape_surf.get_rect())
shape_surf.set_alpha(64)
blits.appendleft(dict(source=shape_surf, dest=visibility_rect))
vis_rects = self.visibility_rects(bp, entity.aux)
blits.extendleft(vis_rects)
if entity.state != 'blank':
agent_state_blits = self.blit_params(
Entity(entity.state, (entity.pos[0]+0.12, entity.pos[1]), 0.48, 'scale')

View File

@ -83,7 +83,7 @@ class SimpleFactory(BaseFactory):
agents = []
for i, agent in enumerate(self._agents):
name, state = asset_str(agent)
agents.append(Entity(name, agent.pos, 1, 'none', state, i+1))
agents.append(Entity(name, agent.pos, 1, 'none', state, i+1, agent.temp_light_map))
doors = []
if self.parse_doors:
for i, door in enumerate(self._doors):
@ -229,7 +229,7 @@ if __name__ == '__main__':
allow_no_op=False)
factory = SimpleFactory(movement_properties=move_props, dirt_properties=dirt_props, n_agents=1,
combin_agent_slices_in_obs=False, level_name='rooms', parse_doors=True,
pomdp_radius=3, cast_shadows=True)
pomdp_radius=2, cast_shadows=True)
n_actions = factory.action_space.n - 1
_ = factory.observation_space

View File

@ -4,5 +4,5 @@ from environments.policy_adaption.natural_rl_environment.imgsource import *
from environments.policy_adaption.natural_rl_environment.natural_env import *
if __name__ == "__main__":
env = make('SpaceInvaders-v0', 'color') # gravitar, breakout, MsPacman, Space Invaders
env = make('SpaceInvaders-v0', 'video') # gravitar, breakout, MsPacman, Space Invaders
play.play(env, zoom=4)