def convert(self, individual): cm = np.zeros((self.substrate.num_nodes, self.substrate.num_nodes)) for (i,j), coords, conn_id, expr_id in self.substrate.get_connection_list(self.add_deltas): # Add a bias (translation) coords = np.hstack((coords, [1])) w = sum(weight * gabor_opt(*(np.dot(mat, coords)), sigma=sigma) for (weight, sigma, mat) in individual.wavelets[conn_id]) cm[j,i] = w # Rescale weights cm[np.abs(cm) < self.min_weight] = 0 cm -= (np.sign(cm) * self.min_weight) cm *= self.weight_range / (self.weight_range - self.min_weight) # Clip highest weights cm = np.clip(cm, -self.weight_range, self.weight_range) net = NeuralNetwork().from_matrix(cm, node_types=[self.node_type]) if self.sandwich: net.make_sandwich() if self.feedforward: net.make_feedforward() return net
def evaluate(self, network, verbose=False): if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) network.make_feedforward() if not network.node_types[-1](-1000) < -0.95: raise Exception("Network should be able to output value of -1, e.g. using a tanh node.") pairs = list(zip(self.INPUTS, self.OUTPUTS)) random.shuffle(pairs) if not self.do_all: pairs = [random.choice(pairs)] rmse = 0.0 for (i, target) in pairs: # Feed with bias output = network.feed(i) # Grab the output output = output[-len(target):] err = (target - output) err[abs(err) < self.EPSILON] = 0; err = (err ** 2).mean() # Add error if verbose: print("%r -> %r (%.2f)" % (i, output, err)) rmse += err score = 1/(1+np.sqrt(rmse / len(pairs))) return {'fitness': score}
def evaluate(self, network, verbose=False): if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) network.make_feedforward() if not network.node_types[-1](-1000) < -0.95: raise Exception( "Network should be able to output value of -1, e.g. using a tanh node." ) pairs = list(zip(self.INPUTS, self.OUTPUTS)) random.shuffle(pairs) if not self.do_all: pairs = [random.choice(pairs)] rmse = 0.0 for (i, target) in pairs: # Feed with bias output = network.feed(i) # Grab the output output = output[-len(target):] err = (target - output) err[abs(err) < self.EPSILON] = 0 err = (err**2).mean() # Add error if verbose: print("%r -> %r (%.2f)" % (i, output, err)) rmse += err score = 1 / (1 + np.sqrt(rmse / len(pairs))) return {'fitness': score}
def _step(self, evaluee, callback): while not self.camera.img_ready: time.sleep(.01) presence_box = self.camera.readPuckPresence() presence_goal = self.camera.readGoalPresence() self.camera.img_ready = False # print presence_box, presence_goal if presence_goal and presence_box: # self.frame_rate_counter += 1 # print 'Camera test', self.frame_rate_counter self.prev_presence = self.presence self.presence = tuple(presence_box) + tuple(presence_goal) has_box = 1 if presence_box[0] == 0 else 0 inputs = np.hstack( ([x if not x == -np.inf else -10000 for x in self.presence], has_box, 1)) inputs[::2] = inputs[::2] / self.camera.MAX_DISTANCE out = NeuralNetwork(evaluee).feed(inputs) left, right = list(out[-2:]) self.motorspeed = {'left': left, 'right': right} writeMotorSpeed(self.thymioController, self.motorspeed, max_speed=MAX_MOTOR_SPEED) else: time.sleep(.001) callback(self.getEnergyDelta()) return True
def evaluate(self, network): if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) score, steps = self._loop(network, test=True) score, steps = self._loop(network) return {'fitness': score / self.max_score, 'steps': steps}
def solve(self, network): if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) score, steps = self._loop(network) if score < self.max_score: print("Failed... Score: ", score / self.max_score, " in ", steps, " Steps") return 0 return int(score > self.max_score)
def convert(self, individual): cm = np.zeros((self.substrate.num_nodes, self.substrate.num_nodes)) for (i, j ), coords, conn_id, expr_id in self.substrate.get_connection_list( self.add_deltas): # Add a bias (translation) coords = np.hstack((coords, [1])) w = sum(weight * gabor_opt(*(np.dot(mat, coords)), sigma=sigma) for (weight, sigma, mat) in individual.wavelets[conn_id]) cm[j, i] = w # Rescale weights cm[np.abs(cm) < self.min_weight] = 0 cm -= (np.sign(cm) * self.min_weight) cm *= self.weight_range / (self.weight_range - self.min_weight) # Clip highest weights cm = np.clip(cm, -self.weight_range, self.weight_range) net = NeuralNetwork().from_matrix(cm, node_types=[self.node_type]) if self.sandwich: net.make_sandwich() if self.feedforward: net.make_feedforward() return net
def ok_call(psValues): selectedValues = [ psValues[ind] / self.sensors_max[ind] for ind in PROX_SENSOR_NO ] selectedValues.append(1) net_inputs = np.array(selectedValues) left, right = list(NeuralNetwork(evaluee).feed(net_inputs)[-2:]) motorspeed = {'left': left, 'right': right} writeMotorSpeed(self.thymioController, motorspeed) callback(self.getFitness(motorspeed, net_inputs))
def ok_call(psValues): psValues = np.array([psValues[0], psValues[2], psValues[4], psValues[5], psValues[6], 1],dtype='f') psValues[0:5] = [(float(x) - float(pr.SENSOR_MAX[0]/2))/float(pr.SENSOR_MAX[0]/2) for x in psValues[0:5]] left, right = list(NeuralNetwork(evaluee).feed(psValues)[-2:]) motorspeed = { 'left': left, 'right': right } try: writeMotorSpeed(self.thymioController, motorspeed) except Exception as e: print str(e) # print "Sensor values: ", psValues, " sensor max: ", SENSOR_MAX if not self.atWall and any(i >= 1 for i in psValues[0:5]): self.atWall = True self.hitWallCounter += 1 elif not any(i >= 1 for i in psValues[0:5]): self.atWall = False callback(self.getFitness(motorspeed, psValues))
def _step(self, evaluee, callback): self.motorLock.acquire() getProxReadings(self.thymioController, self.prox_readings_ok_call, self.prox_readings_nok_call) # time.sleep(0.05) self.motorLock.release() presence_box = self.presenceValues["puck"] presence_goal = self.presenceValues["target"] if presence_goal and presence_box: self.prev_presence = self.presence self.presence = tuple(presence_box) + tuple(presence_goal) has_box = 1 if presence_box[0] == 0 else 0 #inputs consist of: # 2 values for distance and angle of the puck # 2 values for distance and angle of the target # 2 values for the proximity sensors # boolean whether robot currently has the puck # a constant value of 1 (bias) inputs = np.hstack(([x if not x == -np.inf else -self.camera.MAX_DISTANCE for x in self.presence])) inputs[::2] = map(lambda dist: dist/float(self.camera.MAX_DISTANCE), inputs[::2]) inputs = np.concatenate((inputs, self.proxvalues), 0) inputs = np.hstack((inputs, has_box, 1)) #print "Inputs: ", inputs out = NeuralNetwork(evaluee).feed(inputs) left, right = list(out[-2:]) self.motorspeed = { 'left': left, 'right': right } self.motorLock.acquire() writeMotorSpeed(self.thymioController, self.motorspeed, max_speed=MAX_MOTOR_SPEED) self.motorLock.release() else: time.sleep(.001) callback(self.getEnergyDelta2()) return True
def evaluate(self, network): if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) if network.cm.shape != self.target.shape: raise Exception( "Network shape (%s) does not match target shape (%s)." % (network.cm.shape, self.target.shape)) diff = np.abs(network.cm - self.target) err = ((network.cm - self.target)**2).sum() x, res, _, _ = lstsq(self.locs, self.target.flat) # print self.target.flatten() # print res # print x res = res[0] diff_lsq = np.abs(np.dot(self.locs, x) - self.target.flat) diff_nonlin = diff_lsq.mean() - diff.mean() correct = (diff < (self.max_weight / 10.0)).mean() nonlinear = res - err if self.fitnessmeasure == 'absdiff': fitness = 2**((2 * self.max_weight) - diff).mean() elif self.fitnessmeasure == 'sqerr': fitness = 1 / (1 + err) return { 'fitness': fitness, 'error': err, 'diff': diff.mean(), 'diff_lsq': diff_lsq.mean(), 'correct': correct, 'err_nonlin': nonlinear, 'diff_nonlin': diff_nonlin, 'residue': res }
def _step(self, evaluee, callback): presence_box = self.presenceValues["puck"] presence_goal = self.presenceValues["target"] if presence_goal and presence_box: self.prev_presence = self.presence self.presence = tuple(presence_box) + tuple(presence_goal) has_box = 1 if presence_box[0] == 0 else 0 #inputs consist of: # 2 values for distance and angle of the puck # 2 values for distance and angle of the target # # boolean whether robot currently has the puck # a constant value of 1 (bias) inputs = np.hstack( ([x if not x == -np.inf else -10000 for x in self.presence], has_box, 1)) inputs[::2] = inputs[::2] / self.camera.MAX_DISTANCE out = NeuralNetwork(evaluee).feed(inputs) left, right = list(out[-2:]) self.motorspeed = {'left': left, 'right': right} self.motorLock.acquire() writeMotorSpeed(self.thymioController, self.motorspeed, max_speed=MAX_MOTOR_SPEED) self.motorLock.release() else: time.sleep(.001) callback(self.getEnergyDelta2()) return True
def evaluate(self, network, draw=False): """ Evaluate the efficiency of the given network. Returns the distance that the walker ran in the given time (max_steps). """ if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) if draw: import pygame pygame.init() screen = pygame.display.set_mode((self.track_length, 200)) pygame.display.set_caption("Simulation") clock = pygame.time.Clock() running = True font = pygame.font.Font(pygame.font.get_default_font(), 8) # Initialize pymunk self.space = space = pymunk.Space() space.gravity = (0.0, 900.0) space.damping = 0.7 self.touching_floor = False # Create objects # Floor floor = pymunk.Body() floor.position = pymunk.Vec2d(self.track_length / 2.0, 210) sfloor = pymunk.Poly.create_box(floor, (self.track_length, 40)) sfloor.friction = 1.0 sfloor.collision_type = 1 space.add_static(sfloor) # Torso torsolength = 20 + (self.num_legs // 2 - 1) * self.leg_spacing mass = torsolength * self.torso_height * self.torso_density torso = pymunk.Body( mass, pymunk.moment_for_box(mass, torsolength, self.torso_height)) torso.position = pymunk.Vec2d( 200, 200 - self.leg_length * 2 - self.torso_height) storso = pymunk.Poly.create_box(torso, (torsolength, self.torso_height)) storso.group = 1 storso.collision_type = 1 storso.friction = 2.0 # space.add_static(storso) space.add(torso, storso) # Legs legs = [] for i in range(self.num_legs // 2): x = 10 - torsolength / 2.0 + i * self.leg_spacing y = self.torso_height / 2.0 - 10 legs.append(Leg(torso, (x, y), self)) legs.append(Leg(torso, (x, y), self)) # Collision callback def oncollide(space, arb): self.touching_floor = True space.add_collision_handler(1, 1, post_solve=oncollide) for step in range(self.max_steps): # Query network input_width = max(len(legs), 4) net_input = np.zeros((3, input_width)) torso_y = torso.position.y torso_a = torso.angle sine = np.sin(step / 10.0) hip_angles = [leg.hip.angle() for leg in legs] knee_angles = [leg.knee.angle() for leg in legs] other = [torso_y, torso_a, sine, 1.0] # Build a 2d input grid, # as in Clune 2009 Evolving Quadruped Gaits, p4 net_input[0, :len(legs)] = hip_angles net_input[1, :len(legs)] = knee_angles net_input[2, :4] = other act = network.feed(net_input, add_bias=False) output = np.clip(act[-self.num_legs * 2:] * self.max_rate, -1.0, 1.0) / 2.0 + 0.5 for i, leg in enumerate(legs): leg.hip.set_target(output[i * 2]) leg.knee.set_target(output[i * 2 + 1]) # Advance simulation space.step(1 / 50.0) # Check for success/failure if torso.position.x < 0: break if torso.position.x > self.track_length - 50: break if self.touching_floor: break # Draw if draw: print(act) # Clear screen.fill((255, 255, 255)) # Do all drawing txt = font.render('%d' % step, False, (0, 0, 0)) screen.blit(txt, (0, 0)) # Draw objects for o in space.shapes + space.static_shapes: if isinstance(o, pymunk.Circle): pygame.draw.circle( screen, (0, 0, 0), (int(o.body.position.x), int(o.body.position.y)), int(o.radius)) else: pygame.draw.lines(screen, (0, 0, 0), True, [(int(p.x), int(p.y)) for p in o.get_points()]) # Flip buffers pygame.display.flip() clock.tick(50) if draw: pygame.quit() distance = torso.position.x # print "Travelled %.2f in %d steps." % (distance, step) return {'fitness': distance}
def convert(self, network): """ Performs conversion. :param network: Any object that is convertible to a :class:`~peas.networks.NeuralNetwork`. """ # Cast input to a neuralnetwork if it isn't if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) # Since Stanley mentions to "fully activate" the CPPN, # I assume this means it's a feedforward net, since otherwise # there is no clear definition of "full activation". # In an FF network, activating each node once leads to a stable condition. # Check if the network has enough inputs. required_inputs = 2 * self.substrate.dimensions + 1 if self.add_deltas: required_inputs += self.substrate.dimensions if network.cm.shape[0] <= required_inputs: raise Exception("Network does not have enough inputs. Has %d, needs %d" % (network.cm.shape[0], cm_dims+1)) # Initialize connectivity matrix cm = np.zeros((self.substrate.num_nodes, self.substrate.num_nodes)) for (i,j), coords, conn_id, expr_id in self.substrate.get_connection_list(self.add_deltas): expression = True if expr_id is not None: network.flush() expression = network.feed(coords, self.activation_steps)[expr_id] > 0 if expression: network.flush() weight = network.feed(coords, self.activation_steps)[conn_id] cm[j, i] = weight # Rescale the CM cm[np.abs(cm) < self.min_weight] = 0 cm -= (np.sign(cm) * self.min_weight) cm *= self.weight_range / (self.weight_range - self.min_weight) # Clip highest weights cm = np.clip(cm, -self.weight_range, self.weight_range) net = NeuralNetwork().from_matrix(cm, node_types=[self.node_type]) if self.sandwich: net.make_sandwich() if self.feedforward: net.make_feedforward() if not np.all(np.isfinite(net.cm)): raise Exception("Network contains NaN/inf weights.") return net
def convert(self, network): """ Performs conversion. :param network: Any object that is convertible to a :class:`~peas.networks.NeuralNetwork`. """ # Cast input to a neuralnetwork if it isn't if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) # Since Stanley mentions to "fully activate" the CPPN, # I assume this means it's a feedforward net, since otherwise # there is no clear definition of "full activation". # In an FF network, activating each node once leads to a stable condition. # Check if the network has enough inputs. required_inputs = 2 * self.substrate.dimensions + 1 if self.add_deltas: required_inputs += self.substrate.dimensions if network.cm.shape[0] <= required_inputs: raise Exception( "Network does not have enough inputs. Has %d, needs %d" % (network.cm.shape[0], cm_dims + 1)) # Initialize connectivity matrix cm = np.zeros((self.substrate.num_nodes, self.substrate.num_nodes)) for (i, j ), coords, conn_id, expr_id in self.substrate.get_connection_list( self.add_deltas): expression = True if expr_id is not None: network.flush() expression = network.feed(coords, self.activation_steps)[expr_id] > 0 if expression: network.flush() weight = network.feed(coords, self.activation_steps)[conn_id] cm[j, i] = weight # Rescale the CM cm[np.abs(cm) < self.min_weight] = 0 cm -= (np.sign(cm) * self.min_weight) cm *= self.weight_range / (self.weight_range - self.min_weight) # Clip highest weights cm = np.clip(cm, -self.weight_range, self.weight_range) net = NeuralNetwork().from_matrix(cm, node_types=[self.node_type]) if self.sandwich: net.make_sandwich() if self.feedforward: net.make_feedforward() if not np.all(np.isfinite(net.cm)): raise Exception("Network contains NaN/inf weights.") return net
def evaluate(self, network, draw=False, drawname='Simulation'): """ Evaluate the efficiency of the given network. Returns the distance that the bot ran """ if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) h, w = self.field_friction.shape if draw: import pygame pygame.init() screen = pygame.display.set_mode((w, h)) pygame.display.set_caption(drawname) clock = pygame.time.Clock() running = True font = pygame.font.Font(pygame.font.get_default_font(), 12) field_image = pygame.image.load(self.observationpath) # Initialize pymunk self.space = space = pymunk.Space() space.gravity = (0, 0) space.damping = self.damping # Create objects robot = Robot(space, self.field_friction, self.field_observation, self.initial_pos, friction_scale=self.friction_scale, motor_torque=self.motor_torque, force_global=self.force_global) path = [(robot.body.position.int_tuple)] cells = [] if network.cm.shape[0] != (3 * 5 + 3 * 3 + 1): raise Exception( "Network shape must be a 2 layer controller: 3x5 input + 3x3 hidden + 1 bias. Has %d." % network.cm.shape[0]) for step in range(self.max_steps): net_input = np.array(list(robot.sensor_response())) # The nodes used for output are somewhere in the middle of the network # so we extract them using -4 and -6 action = network.feed(net_input)[[-4, -6]] if self.flush_each_step: network.flush() robot.drive(*action) robot.apply_friction() space.step(1 / 50.0) new_cell_covered = False current_cell = int(robot.body.position.x // 32), int( robot.body.position.y // 32) if current_cell not in cells: cells.append(current_cell) new_cell_covered = True if len(cells) > self.coverage_memory: cells.pop(0) elif cells[-1] == current_cell: new_cell_covered = True if step % self.path_resolution == 0 and (not self.check_coverage or new_cell_covered): path.append((robot.body.position.int_tuple)) if draw: screen.fill((255, 255, 255)) screen.blit(field_image, (0, 0)) txt = font.render('%d - %.0f' % (step, path_length(path)), False, (0, 0, 0)) screen.blit(txt, (0, 0)) # Draw path if len(path) > 1: pygame.draw.lines(screen, (0, 0, 255), False, path, 3) for cell in cells: pygame.draw.rect(screen, (200, 200, 0), (cell[0] * 32, cell[1] * 32, 32, 32), 2) robot.draw(screen) if pygame.event.get(pygame.QUIT): break for k in pygame.event.get(pygame.KEYDOWN): if k.key == pygame.K_SPACE: break pygame.display.flip() # clock.tick(50) if draw: pygame.quit() self.last_path = path dist = path_length(path) speed = dist / self.max_steps return {'fitness': 1 + dist**2, 'dist': dist, 'speed': speed}
def evaluate(self, network, draw=False, drawname='Simulation'): """ Evaluate the efficiency of the given network. Returns the distance that the bot ran """ if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) h,w = self.field_friction.shape if draw: import pygame pygame.init() screen = pygame.display.set_mode((w, h)) pygame.display.set_caption(drawname) clock = pygame.time.Clock() running = True font = pygame.font.Font(pygame.font.get_default_font(), 12) field_image = pygame.image.load(self.observationpath) # Initialize pymunk self.space = space = pymunk.Space() space.gravity = (0,0) space.damping = self.damping # Create objects robot = Robot(space, self.field_friction, self.field_observation, self.initial_pos, friction_scale=self.friction_scale, motor_torque=self.motor_torque, force_global=self.force_global) path = [(robot.body.position.int_tuple)] cells = [] if network.cm.shape[0] != (3*5 + 3*3 + 1): raise Exception("Network shape must be a 2 layer controller: 3x5 input + 3x3 hidden + 1 bias. Has %d." % network.cm.shape[0]) for step in range(self.max_steps): net_input = np.array(list(robot.sensor_response())) # The nodes used for output are somewhere in the middle of the network # so we extract them using -4 and -6 action = network.feed(net_input)[[-4,-6]] if self.flush_each_step: network.flush() robot.drive(*action) robot.apply_friction() space.step(1/50.0) new_cell_covered = False current_cell = int(robot.body.position.x // 32), int(robot.body.position.y // 32) if current_cell not in cells: cells.append(current_cell) new_cell_covered = True if len(cells) > self.coverage_memory: cells.pop(0) elif cells[-1] == current_cell: new_cell_covered = True if step % self.path_resolution == 0 and (not self.check_coverage or new_cell_covered): path.append((robot.body.position.int_tuple)) if draw: screen.fill((255, 255, 255)) screen.blit(field_image, (0,0)) txt = font.render('%d - %.0f' % (step, path_length(path)), False, (0,0,0) ) screen.blit(txt, (0,0)) # Draw path if len(path) > 1: pygame.draw.lines(screen, (0,0,255), False, path, 3) for cell in cells: pygame.draw.rect(screen, (200,200,0), (cell[0]*32, cell[1]*32, 32, 32), 2) robot.draw(screen) if pygame.event.get(pygame.QUIT): break for k in pygame.event.get(pygame.KEYDOWN): if k.key == pygame.K_SPACE: break pygame.display.flip() # clock.tick(50) if draw: pygame.quit() self.last_path = path dist = path_length(path) speed = dist / self.max_steps return {'fitness':1 + dist**2, 'dist':dist, 'speed':speed}
def evaluate(self, network, draw=False): """ Evaluate the efficiency of the given network. Returns the distance that the walker ran in the given time (max_steps). """ if not isinstance(network, NeuralNetwork): network = NeuralNetwork(network) if draw: import pygame pygame.init() screen = pygame.display.set_mode((self.track_length, 200)) pygame.display.set_caption("Simulation") clock = pygame.time.Clock() running = True font = pygame.font.Font(pygame.font.get_default_font(), 8) # Initialize pymunk self.space = space = pymunk.Space() space.gravity = (0.0, 900.0) space.damping = 0.7 self.touching_floor = False # Create objects # Floor floor = pymunk.Body() floor.position = pymunk.Vec2d(self.track_length/2.0 , 210) sfloor = pymunk.Poly.create_box(floor, (self.track_length, 40)) sfloor.friction = 1.0 sfloor.collision_type = 1 space.add_static(sfloor) # Torso torsolength = 20 + (self.num_legs // 2 - 1) * self.leg_spacing mass = torsolength * self.torso_height * self.torso_density torso = pymunk.Body(mass, pymunk.moment_for_box(mass, torsolength, self.torso_height)) torso.position = pymunk.Vec2d(200, 200 - self.leg_length * 2 - self.torso_height) storso = pymunk.Poly.create_box(torso, (torsolength, self.torso_height)) storso.group = 1 storso.collision_type = 1 storso.friction = 2.0 # space.add_static(storso) space.add(torso, storso) # Legs legs = [] for i in range(self.num_legs // 2): x = 10 - torsolength / 2.0 + i * self.leg_spacing y = self.torso_height / 2.0 - 10 legs.append( Leg(torso, (x,y), self) ) legs.append( Leg(torso, (x,y), self) ) # Collision callback def oncollide(space, arb): self.touching_floor = True space.add_collision_handler(1, 1, post_solve=oncollide) for step in range(self.max_steps): # Query network input_width = max(len(legs), 4) net_input = np.zeros((3, input_width)) torso_y = torso.position.y torso_a = torso.angle sine = np.sin(step / 10.0) hip_angles = [leg.hip.angle() for leg in legs] knee_angles = [leg.knee.angle() for leg in legs] other = [torso_y, torso_a, sine, 1.0] # Build a 2d input grid, # as in Clune 2009 Evolving Quadruped Gaits, p4 net_input[0, :len(legs)] = hip_angles net_input[1, :len(legs)] = knee_angles net_input[2, :4] = other act = network.feed(net_input, add_bias=False) output = np.clip(act[-self.num_legs*2:] * self.max_rate, -1.0, 1.0) / 2.0 + 0.5 for i, leg in enumerate(legs): leg.hip.set_target( output[i * 2] ) leg.knee.set_target( output[i * 2 + 1] ) # Advance simulation space.step(1/50.0) # Check for success/failure if torso.position.x < 0: break if torso.position.x > self.track_length - 50: break if self.touching_floor: break # Draw if draw: print(act) # Clear screen.fill((255, 255, 255)) # Do all drawing txt = font.render('%d' % step, False, (0,0,0) ) screen.blit(txt, (0,0)) # Draw objects for o in space.shapes + space.static_shapes: if isinstance(o, pymunk.Circle): pygame.draw.circle(screen, (0,0,0), (int(o.body.position.x), int(o.body.position.y)), int(o.radius)) else: pygame.draw.lines(screen, (0,0,0), True, [(int(p.x), int(p.y)) for p in o.get_points()]) # Flip buffers pygame.display.flip() clock.tick(50) if draw: pygame.quit() distance = torso.position.x # print "Travelled %.2f in %d steps." % (distance, step) return {'fitness':distance}
def visualize(self, filename): return NeuralNetwork(self).visualize(filename, inputs=self.inputs, outputs=self.outputs)