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)
class AnnRunner(object): """Wraps up the gross reality of running a ``print'' using the printer simulation (controlled by a neural network)""" camera_size = 3 def __init__(self, ideal_grid_path, cell_size, units_per_cell=10): """Sets up all the pieces needed to perform a print with the simulated 3d printer (controlled by the neural network). Takes in a path to an a ``goal'' or ``ideal'' grid, and constructs the GridWorld based on the dimensions of that goal grid. Understands both a ``camera'', which observes the actual world (around the print head) and an ``ideal camera'' which observes the same location but based on the ``goal grid'' """ ideal_grid = Grid(path=ideal_grid_path, scale=cell_size) self.ideal_grid = ideal_grid self.gridworld = GridWorld(ideal_grid.width, ideal_grid.height, cell_size) self.gridworld.set_ideal_grid(ideal_grid) self.printer = Printer(10, 10, 9, self.gridworld, units_per_cell) #TODO: shouldn't be giving location values here when it's determined somewhere else. that smells a lot self.camera = Camera(self.gridworld.grid, self.printer, self.camera_size) self.ideal_camera = Camera(self.gridworld.ideal_grid, self.printer, self.camera_size) def run(self, n, iterations=10000): """Runs a simulated print run with the printer simulation (controlled by an ANN. Starts the printer in the location provided by the ideal grid spec """ #set the printer location to the starting postition as defined by the ideal_grid spec self.printer.set_position_on_grid(*self.gridworld.get_starting_position()) for i in xrange(iterations): self.printer.setPenDown() actual = self.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.set_printer_direction(self.get_velocity(result[:2]), self.get_velocity(result[2:])) self.printer.simulate() self.update() return (self.ideal_grid, self.gridworld.grid) def update(self): return def get_velocity(self, instruction): """Translates between the output of the neural network and direction instructions for the printer. leftright and updown are translated separately""" if instruction == "10": return -1 elif instruction == "01": return 1 else: return 0
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 AnnRunner(object): camera_size = 3 def __init__(self, ideal_grid, units_per_cell=10): self.gridworld = GridWorld(ideal_grid.width, ideal_grid.height, ideal_grid.gridsize) self.gridworld.set_ideal_grid(ideal_grid) self.printer = Printer(10, 10, 9, 1, self.gridworld, units_per_cell) #TODO: shouldn't be giving location values here when it's determined somewhere else. that smells a lot self.camera = Camera(self.gridworld.grid, self.printer, self.camera_size) self.ideal_grid = self.gridworld.ideal_grid self.ideal_camera = Camera(self.gridworld.ideal_grid, self.printer, self.camera_size) width = self.gridworld.width() * self.gridworld.gridsize() height = self.gridworld.height() * self.gridworld.gridsize() def run(self, n, iterations=10000): self.printer.set_position_on_grid(self.ideal_grid.starting_point[0], self.ideal_grid.starting_point[1]) for i in xrange(iterations): self.printer.setPenDown() actual = self.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() self.update() return (self.ideal_grid, self.gridworld.grid) def update(self): return def get_velocity(self, instruction): if instruction == "10": return -1 elif instruction == "01": return 1 else: return 0
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)