class AnnRunner: def __init__(self, ideal_grid): self.gridworld = GridWorld(ideal_grid.width, ideal_grid.height, ideal_grid.gridsize) self.gridworld.set_ideal_grid(ideal_grid) self.printer = VirtualPrinter(10, 10, 9, 1, pygame.color.Color("darkorange"), self.gridworld) self.camera = VisualCamera(self.gridworld, self.printer, 3) self.ideal_camera = Camera(self.gridworld.ideal_grid, self.printer, 3) #gui stuff pygame.init() width = self.gridworld.width() * self.gridworld.gridsize() height = self.gridworld.height() * self.gridworld.gridsize() self.window = pygame.display.set_mode((width, height)) def run(self, n): self.printer.position = Vector(270, 150) while True: self.printer.setPenDown() actual = self.camera.camera.all_cell_values() ideal = self.ideal_camera.all_cell_values() pattern = [i - a for i,a in zip(actual, ideal)] result = n.propagate(pattern) result = [int(round(x)) for x in result] result = ''.join(map(str, result)) self.printer.v = Vector(self.get_velocity(result[:2]), self.get_velocity(result[2:])) self.printer.simulate(1) self.redraw() pygame.display.update() def get_velocity(self, instruction): if instruction == "10": return -100 elif instruction == "01": return 100 else: return 0 def redraw(self): self.gridworld.draw(self.window) self.printer.draw(self.window) self.camera.draw(self.window)
class Generator: movement_constant = 3 aquire_data = True def __init__(self, ideal_grid=None, ideal_grid_path=None): """ Set pygame stuff up for running the simulation.""" assert ideal_grid or ideal_grid_path, "must provide at least one ideal grid" self.gridworld = GridWorld(ideal_grid.width, ideal_grid.height, ideal_grid.gridsize) self.gridworld.set_ideal_grid(ideal_grid) self.printer = VirtualPrinter(10, 10, 9, 1, pygame.color.Color("darkorange"), self.gridworld) self.camera = VisualCamera(self.gridworld, self.printer, 3) self.ideal_camera = Camera(self.gridworld.ideal_grid, self.printer, 3) #gui stuff pygame.init() width = self.gridworld.width() * self.gridworld.gridsize() height = self.gridworld.height() * self.gridworld.gridsize() self.window = pygame.display.set_mode((width, height)) def generate(self, outputfile): inputs = [] outputs = [] self.printer.setPenDown() self.printer.v = Vector(0, 0) self.printer.position = Vector(270, 130) while self.aquire_data: actual = self.camera.camera.all_cell_values() ideal = self.ideal_camera.all_cell_values() inputs.append([i - a for a,i in zip(actual, ideal)]) outputs.append([self.printer.v.x, self.printer.v.y]) self.act_and_refresh() outputs = [[self.encode(x) + self.encode(y)] for x,y in outputs] self.aquire_data = True with open(outputfile, 'w') as output: writer = csv.writer(output) writer.writerow(camera_headers + output_headers) for inval, outval in zip(inputs, outputs): writer.writerow(inval + outval) def encode(self, velocity): if velocity >= 100: return "01" elif velocity <= -100: return "10" else: return "00" def act_and_refresh(self): self.act_on_key_input() self.printer.simulate(1) self.redraw() pygame.display.update() def act_on_key_input(self): for event in pygame.event.get(pygame.KEYUP): if event.key == pygame.K_p: self.print_all_camera_values() keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.printer.v = Vector(-100, 0) if keys[pygame.K_RIGHT]: self.printer.v = Vector(100, 0) if keys[pygame.K_UP]: self.printer.v = Vector(0, -100) if keys[pygame.K_DOWN]: self.printer.v = Vector(0, 100) if keys[pygame.K_SPACE]: self.printer.v = Vector(0, 0) if keys[pygame.K_q]: self.aquire_data = False def redraw(self): self.gridworld.draw(self.window) self.printer.draw(self.window) self.camera.draw(self.window)
class Runner: """Runner for printer test""" movement_constant = 3 def __init__(self): """ Set pygame stuff up for running the simulation.""" pygame.init() grid = GridWorld(20, 20, 30) ideal_grid = Grid(20, 20, 30) ideal_grid.grid = [[1 if x <= 10 else 0 for x in range(20)] for _ in range(20)] grid.set_ideal_grid(ideal_grid) width = grid.width() * grid.gridsize() height = grid.height() * grid.gridsize() self.grid = grid self.window = pygame.display.set_mode((width, height)) self.printer = VirtualPrinter(10, 10, 9, 1, pygame.color.Color("darkorange"), grid) self.camera = VisualCamera(self.grid, self.printer, 3) self.grid.draw(self.window) def run(self): self.printer.setPenDown() self.printer.v = Vector(0, 0) while (True): self.act_on_key_input() self.printer.simulate(1) self.redraw() pygame.display.update() def run_auto(self): printer = self.printer printer.setPenDown() printer.position = Vector(45, 45) printer.v = Vector(500, 0) while printer.position.x < ((self.grid.width() - 2) * self.grid.gridsize() - 15): self.printer.simulate(1) self.redraw() pygame.display.update() printer.v = Vector(0, 500) while printer.position.y < ((self.grid.height() -2) * self.grid.gridsize() - 15): self.printer.simulate(1) self.redraw() pygame.display.update() printer.v = Vector(-500, 0) while printer.position.x > ((2 * self.grid.gridsize()) + 15): self.printer.simulate(1) self.redraw() pygame.display.update() def act_on_key_input(self): for event in pygame.event.get(pygame.KEYUP): if event.key == pygame.K_p: self.print_all_camera_values() keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.printer.v.x += self.movement_constant * -1 if keys[pygame.K_RIGHT]: self.printer.v.x += self.movement_constant if keys[pygame.K_UP]: self.printer.v.y += self.movement_constant * -1 if keys[pygame.K_DOWN]: self.printer.v.y += self.movement_constant if keys[pygame.K_SPACE]: self.printer.v = Vector(0, 0) def print_all_camera_values(self): val_matrix = self.camera.camera.all_cell_values() for row in val_matrix: output = '' for val in row: output += str(val) + ' ' print output print '*' * 10 def redraw(self): self.grid.draw(self.window) self.printer.draw(self.window) self.camera.draw(self.window)