def create_horizontal_pipes(self, pipes, neigh_from, neigh_to, intersection): distance_x = neigh_to.rect.centerx - neigh_from.rect.centerx base_x = intersection.rect.x - 64 base_y = intersection.rect.y direction = -1 if distance_x < 0: base_x = intersection.rect.right - 24 direction = 1 pipe_amount = int(abs(distance_x) / 64) for n in range(pipe_amount): pipe = Pipe() pos_x = base_x + pipe.rect.width * n * direction pipe.set_pos((pos_x, base_y)) if direction == 1: pipe.angle = 180 pipes.insert(0, pipe)
def create_vertical_pipes(self, neigh_from, neigh_to, intersection): pipes = [] distance_y = neigh_to.rect.centery - neigh_from.rect.centery base_x = intersection.rect.x base_y = intersection.rect.y - 64 direction = -1 if distance_y > 0: base_y = intersection.rect.bottom direction = 1 pipe_amount = int(abs(distance_y) / 64) for n in range(pipe_amount): pipe = Pipe() pos_y = base_y + pipe.rect.width * n * direction pipe.set_pos((base_x, pos_y)) pipe.angle = -direction * 90 pipes.append(pipe) return pipes
def get_random_pipes(x_position): size = random.randint(100, 300) pipe = Pipe(PIPE_WIDTH, PIPE_HEIGHT, False, x_position, size, SCREEN_HEIGHT, GAME_SPEED) pipe_inverted = Pipe(PIPE_WIDTH, PIPE_HEIGHT, True, x_position, SCREEN_HEIGHT - size - PIPE_GAP, SCREEN_HEIGHT, GAME_SPEED) return (pipe, pipe_inverted)
def __init__(self, Name, Parent=None, Description="", data={}, node_tree={}): Pipe.__init__(self, Name, Parent=Parent, Description=Description, data=data, node_tree=node_tree)
class PipelineManager(): pipeline = { 'load': Pipe('load', 1, 'validate'), 'validate': Pipe('validate', 2, 'summarize'), 'summarize': Pipe('summarize', 3, 'output'), 'output': Pipe('output', 4, None) } def __init__(self, src_file: str, max_date: str): self.step = 0 self.src_file = src_file self.max_date = max_date self.raw_data = None @staticmethod def first_pipe() -> Pipe: for _, p in PipelineManager.pipeline.items(): if p.position == 1: return p def __params_valid(self): if not self.src_file or not self.max_date: raise Exception( 'File source or Max date were not provided to this pipeline') def send_next(self, pipe: Pipe): self.__params_valid() if pipe: self.raw_data = eval(pipe.func).run(self.raw_data, self.src_file, self.max_date) if pipe.next: next_pipe = PipelineManager.pipeline[pipe.next] self.send_next(next_pipe) else: raise Exception('Invalid pipeline step') def start(self): self.__params_valid() self.send_next(self.first_pipe())
def create_intersection(self, neigh_from, neigh_to): pos_x = neigh_to.rect.centerx - 20 pos_y = neigh_from.rect.centery - 20 pipe = Pipe() pipe.is_curve = True pipe.was_animated = True distance_x = neigh_to.rect.centerx - neigh_from.rect.centerx distance_y = neigh_to.rect.centery - neigh_from.rect.centery pipe.flip_vertical = distance_x < 0 pipe.flip_horizontal = distance_y > 0 pipe.set_pos((pos_x, pos_y)) return pipe
def main(genomes, config): global gen gen += 1 # start by creating lists holding the genome itself, the # neural network associated with the genome and the # bird object that uses that network to play nets = [] ge = [] birds = [] for genome_id, genome in genomes: genome.fitness = 0 # start with fitness level of 0 net = neat.nn.FeedForwardNetwork.create(genome, config) nets.append(net) birds.append(Bird(230, 350)) ge.append(genome) pipes = [Pipe(700)] ground = Ground(FLOOR) score = 0 win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) clock = pygame.time.Clock() while True: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() break pipe_ind = 0 if len(birds) > 0: # Determine whether to use the first or second if len(pipes) > 1 and birds[0].x > \ pipes[0].x + pipes[0].PIPE_TOP.get_width(): # Pipe on the screen for neural network input pipe_ind = 1 # Give each bird a fitness of 0.1 for each frame it stays alive for x, bird in enumerate(birds): ge[x].fitness += 0.1 bird.move() # send bird location, top pipe location and bottom pipe location # and determine from network whether to jump or not output = nets[birds.index(bird)].activate( (bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom))) # We use a tanh activation function so result will be # between -1 and 1. if over 0.5 jump if output[0] > 0.5: bird.jump() ground.move() residue = [] add_pipe = False for pipe in pipes: pipe.move() # Check for collisions for bird in birds: if pipe.collide(bird): ge[birds.index(bird)].fitness -= 1 nets.pop(birds.index(bird)) ge.pop(birds.index(bird)) birds.pop(birds.index(bird)) if pipe.x + pipe.PIPE_TOP.get_width() < 0: residue.append(pipe) if not pipe.passed and pipe.x < bird.x: pipe.passed = True add_pipe = True if add_pipe: score += 1 # Add this line to give more reward for passing pipe for genome in ge: genome.fitness += 5 pipes.append(Pipe(700)) for res in residue: pipes.remove(res) for bird in birds: if bird.y + bird.img.get_height() - 10 >= FLOOR or bird.y < -50: nets.pop(birds.index(bird)) ge.pop(birds.index(bird)) birds.pop(birds.index(bird)) draw_window(win, birds, pipes, ground, score, gen, pipe_ind)
def DeclareEquations(self): Pipe.DeclareEquations(self)
def DeclareEquations(self): Pipe.DeclareEquations(self) Carbfilm.DeclareEquations(self)
def define_variables(self): Pipe.define_variables(self) Carbfilm.define_variables(self)
def DeclareEquations(self): Pipe.DeclareEquations(self) ExternalFilmCondensation.DeclareEquations(self)
def define_variables(self): Pipe.define_variables(self) ExternalFilmCondensation.define_variables(self)
def define_parameters(self): Pipe.define_parameters(self) ExternalFilmCondensation.define_parameters(self)
def __init__(self, Name, Parent=None, Description=""): Pipe.__init__(self, Name, Parent=Parent, Description=Description)
def define_parameters(self): Pipe.define_parameters(self) FixedExternalTemperature.define_parameters(self)
def define_constants(self): Pipe.define_constants(self) Carbfilm.define_constants(self)
def define_variables(self): Pipe.define_variables(self) FixedExternalTemperature.define_variables(self)
def define_parameters(self): Pipe.define_parameters(self) Carbfilm.define_parameters(self)
def DeclareEquations(self): Pipe.DeclareEquations(self) FixedExternalTemperature.DeclareEquations(self)
def game_loop(genomes, config): global GEN global VEL GEN += 1 nets = [] ge = [] birds = [] for _, g in genomes: net = neat.nn.FeedForwardNetwork.create(g, config) nets.append(net) birds.append(Bird(230, 350)) g.fitness = 0 ge.append(g) base = Base(730) pipes = [Pipe(600)] win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT)) clock = pygame.time.Clock() score = 0 run = True while run: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False pygame.quit() quit() pipe_ind = 0 if len(birds) > 0: if len(pipes) > 1 and birds[ 0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width(): pipe_ind = 1 else: run = False break for x, bird in enumerate(birds): bird.move() ge[x].fitness += 0.1 output = nets[x].activate( (bird.y, abs(bird.y - pipes[pipe_ind].height), abs(bird.y - pipes[pipe_ind].bottom))) # Only 1 output neuron if output[0] > 0.5: bird.jump() add_pipe = False rem = [] for pipe in pipes: for x, bird in enumerate(birds): if pipe.collide(bird): ge[x].fitness -= 1 birds.pop(x) nets.pop(x) ge.pop(x) if not pipe.passed and pipe.x < bird.x: pipe.passed = True add_pipe = True if pipe.x + pipe.PIPE_TOP.get_width() < 0: rem.append(pipe) pipe.move(VEL) if add_pipe: score += 1 for g in ge: g.fitness += 5 pipes.append(Pipe(600)) for r in rem: pipes.remove(r) for x, bird in enumerate(birds): if bird.y + bird.img.get_height() >= 730 or bird.y < 0: birds.pop(x) nets.pop(x) ge.pop(x) base.move(VEL) draw_window(win, birds, pipes, base, score, GEN, VEL)
def define_domains(self): Pipe.define_domains(self) self.y = daeDomain("y", self, unit(), "Y tube position")