Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Redchards/22410248cc5a8fac33a234c16585d3ec to your computer and use it in GitHub Desktop.
Save Redchards/22410248cc5a8fac33a234c16585d3ec to your computer and use it in GitHub Desktop.
Reinforcement learning project based on the paper "Self-Improving Reactive Agents Based on Reinforcement Learning, Planning and Teaching"
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.6" project-jdk-type="Python SDK" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/SelfImprovingAgents.iml" filepath="$PROJECT_DIR$/.idea/SelfImprovingAgents.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PySciProjectComponent">
<option name="PY_SCI_VIEW" value="true" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="jdk" jdkName="Python 3.6" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="PyDocumentationSettings">
<option name="renderExternalDocumentation" value="true" />
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module>
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 19:26:53 2018
@author: Loic
"""
from renderer import Renderer
from simulator import MockSimulator
if __name__ == "__main__":
renderer = Renderer(simulator=MockSimulator())
renderer.render()
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 19:39:14 2018
@author: Loic
"""
class DefaultColorManager:
def __init__(self):
self.BLACK = (0, 0, 0)
self.WHITE = (255, 255, 255)
self.cell_color = self.WHITE
self.margin_color = self.BLACK
import pygame
from renderable import Renderable
class EmptyEntity:
type = 'empty'
def __init__(self):
self.type = None
self.id = -1
self.renderable = Renderable(pygame.image.load('resources/empty.png'))
def get_visual(self):
return self.renderable.get_visual()
class EnemyAgent(EmptyEntity):
type = 'enemy'
def __init__(self, entity_id):
self.entity_id = entity_id
self.renderable = Renderable(pygame.image.load('resources/enemy.jpg'))
class Obstacle(EmptyEntity):
type = 'obstacle'
def __init__(self, entity_id):
self.entity_id = entity_id
self.renderable = Renderable(pygame.image.load('resources/obstacle.png'))
class Food(EmptyEntity):
type = 'food'
def __init__(self, entity_id):
self.entity_id = entity_id
self.renderable = Renderable(pygame.image.load('resources/food.png'))
class PlayerAgent(EmptyEntity):
type = 'player'
def __init__(self, entity_id):
self.entity_id = entity_id
self.renderable = Renderable(pygame.image.load('resources/player.jpg'))
import entity
import csv
class MapLoader:
def __init__(self):
self.entities = [entity.PlayerAgent, entity.EnemyAgent, entity.Food, entity.Obstacle]
self.entity_dict = {e.type: e for e in self.entities}
def parse_map(self, filename):
with open(filename, newline='') as f:
mapreader = csv.reader(f, delimiter=',')
map = []
current_ids = {e.type: 1 for e in self.entities}
for row in mapreader:
map.append([])
for elem in row:
if elem == '#':
map[-1].append(entity.EmptyEntity())
else:
map[-1].append(self.entity_dict[elem](current_ids[elem]))
current_ids[elem] += 1
return map
class Renderable:
def __init__(self, visual):
self.visual = visual
def get_visual(self):
return self.visual
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 19:17:26 2018
@author: Loic
"""
import pygame
from pygame.locals import *
from simulator import DefaultSimulator
from color import DefaultColorManager
class Renderer:
def __init__(self, window_dimensions=(480, 480), window_name="Default Renderer", simulator=DefaultSimulator(), cell_margin=1):
self.window_dimensions = window_dimensions
self.window_name = window_name
self.simulator = simulator
self.cell_margin = 1
self.running = False
self.screen = None
self.color_manager = DefaultColorManager()
self.cell_dim = self.compute_cell_dimensions()
self.event_handlers = {
pygame.QUIT: lambda evt: self.stop(),
pygame.VIDEORESIZE: lambda evt: self.set_screen_dimensions(evt.dict['size'])
}
def render(self):
self.running = True
pygame.init()
self.screen = pygame.display.set_mode(self.window_dimensions, HWSURFACE | DOUBLEBUF | RESIZABLE)
pygame.display.set_caption(self.window_name)
clock = pygame.time.Clock()
while self.running:
self.handle_events()
self.draw_grid(self.simulator.step(), self.cell_dim)
clock.tick(60)
pygame.display.flip()
pygame.quit()
def stop(self):
self.running = False
def is_running(self):
return self.running
def compute_cell_dimensions(self):
grid_width, grid_height = self.simulator.dim
if grid_width == 0 or grid_height == 0:
return self.window_dimensions
else:
return self.window_dimensions[0] // grid_width, self.window_dimensions[1] // grid_height
def draw_grid(self, grid, cell_dim):
cell_width, cell_height = cell_dim
nb_col = 1 if len(grid) == 0 or len(grid[0]) == 0 else len(grid[0])
nb_row = 1 if len(grid) == 0 else len(grid)
self.screen.fill(self.color_manager.margin_color)
for row in range(nb_row):
for col in range(nb_col):
self.screen.blit(pygame.transform.scale(grid[row][col].get_visual(), (cell_width - self.cell_margin, cell_height - self.cell_margin)),
(cell_width * col + self.cell_margin, cell_height * row + self.cell_margin))
def handle_events(self):
for evt in pygame.event.get():
if self.has_handler_for(evt):
self.event_handlers[evt.type](evt)
def has_handler_for(self, evt):
return evt.type in self.event_handlers
def set_screen_dimensions(self, dim):
self.window_dimensions = dim
self.cell_dim = self.compute_cell_dimensions()
self.screen = pygame.display.set_mode(self.window_dimensions, HWSURFACE | DOUBLEBUF | RESIZABLE)
obstacle obstacle obstacle obstacle obstacle
obstacle obstacle # # obstacle
obstacle # # # obstacle
obstacle player # enemy obstacle
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 19:15:50 2018
@author: Loic
"""
import pygame
from renderable import Renderable
import entity
from map_loader import MapLoader
class DefaultSimulator:
def __init__(self):
self.dim = (0, 0)
def step(self):
return []
class MockSimulator(DefaultSimulator):
def __init__(self):
img = pygame.image.load("resources/star.png")
#self.mockentity = entity.PlayerAgent(42)
self.map = MapLoader().parse_map('resources/map_test.csv')
self.dim = (len(self.map[0]), len(self.map))
def step(self):
#return [[self.mockentity.renderable for i in range(20)] for _ in range(20)]
return self.map
class Simulator(DefaultSimulator):
def __init__(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment