示例#1
0
	def __init__(self, parent, controller):
		tk.Frame.__init__(self, parent)
		controller.title('Genetic Algorithm')
		canvas = tk.Canvas(self, width=canvas_width, height=canvas_height)
		canvas.pack(expand=tk.YES, fill=tk.BOTH, padx=20, pady=20)
		population = search.init_population(max_population, gene_pool, len(target))
		for generation in range(ngen):
			population = [search.mutate(search.recombine(*search.select(2, population, fitness_fn)), gene_pool, mutation_rate) for i in range(len(population))]
			current_best = ''.join(argmax(population, key=fitness_fn))
			members = [''.join(x) for x in population][:48]

			canvas.delete('all')
			canvas.create_text(canvas_width / 2, 40, fill=p_blue, font='Consolas 46 bold', text=current_best)

			for i in range(len(members) // 3):
				canvas.create_text((canvas_width * .175), (canvas_height * .25 + (25 * i)), fill=p_blue, font='Consolas 16', text=members[3 * i])
				canvas.create_text((canvas_width * .500), (canvas_height * .25 + (25 * i)), fill=p_blue, font='Consolas 16', text=members[3 * i + 1])
				canvas.create_text((canvas_width * .825), (canvas_height * .25 + (25 * i)), fill=p_blue, font='Consolas 16', text=members[3 * i + 2])

			canvas.create_text((canvas_width * .5), (canvas_height * 0.95), fill=p_blue, font='Consolas 18 bold', text=f'Generation {generation}')
			canvas.update()

			fittest_individual = search.fitness_threshold(fitness_fn, f_thres, population)
			if fittest_individual:
				break
def genetic_algorithm_stepwise(population):
	root.title('Genetic Algorithm')
	for generation in range(ngen):
		# generating new population after selecting, recombining and mutating the existing population
		population = [search.mutate(search.recombine(*search.select(2, population, fitness_fn)), gene_pool, mutation_rate) for i in range(len(population))]
		# genome with the highest fitness in the current generation
		current_best = ''.join(argmax(population, key=fitness_fn))
		# collecting first few examples from the current population
		members = [''.join(x) for x in population][:48]

		# clear the canvas
		canvas.delete('all')
		# displays current best on top of the screen
		canvas.create_text(canvas_width / 2, 40, fill=p_blue, font='Consolas 46 bold', text=current_best)

		# displaying a part of the population on the screen
		for i in range(len(members) // 3):
			canvas.create_text((canvas_width * .175), (canvas_height * .25 + (25 * i)), fill=lp_blue, font='Consolas 16', text=members[3 * i])
			canvas.create_text((canvas_width * .500), (canvas_height * .25 + (25 * i)), fill=lp_blue, font='Consolas 16', text=members[3 * i + 1])
			canvas.create_text((canvas_width * .825), (canvas_height * .25 + (25 * i)), fill=lp_blue, font='Consolas 16', text=members[3 * i + 2])

		# displays current generation number
		canvas.create_text((canvas_width * .5), (canvas_height * 0.95), fill=p_blue, font='Consolas 18 bold', text=f'Generation {generation}')

		# displays blue bar that indicates current maximum fitness compared to maximum possible fitness
		scaling_factor = fitness_fn(current_best) / len(target)
		canvas.create_rectangle(canvas_width * 0.1, 90, canvas_width * 0.9, 100, outline=p_blue)
		canvas.create_rectangle(canvas_width * 0.1, 90, canvas_width * 0.1 + scaling_factor * canvas_width * 0.8, 100, fill=lp_blue)
		canvas.update()

		# checks for completion
		fittest_individual = search.fitness_threshold(fitness_fn, f_thres, population)
		if fittest_individual:
			break
def genetic_algorithm_stepwise(population, fitness_fn, gene_pool=[0, 1], f_thres=None, ngen=1200, pmut=0.1):
	for i in range(ngen):
		population = [search.mutate(search.recombine(*search.select(2, population, fitness_fn)), gene_pool, pmut) for i in range(len(population))]
		current_best = ''.join(argmax(population, key=fitness_fn))
		print(f'Current best: {current_best}\tIteration: {str(i)}\tFitness: {fitness_fn(current_best)}\r', end='')

		fittest_individual = search.fitness_threshold(fitness_fn, f_thres, population)
		if fittest_individual:
			return fittest_individual, i

	return argmax(population, key=fitness_fn), i
示例#4
0
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        controller.title('Genetic Algorithm')
        w = tk.Canvas(self, width=800, height=600)
        w.pack(expand=tk.YES, fill=tk.BOTH)
        # var = tk.StringVar()
        # var.set('')
        # label = tk.Label(self, textvariable=var)
        # label.pack()
        population = search.init_population(max_population, gene_pool,
                                            len(target))

        for i in range(100):
            print(i)
            # time.sleep(0.2)
            population = [
                search.mutate(
                    search.recombine(
                        *search.select(2, population, fitness_fn)), gene_pool,
                    mutation_rate) for i in range(len(population))
            ]
            current_best = ''.join(argmax(population, key=fitness_fn))
            w.create_text(100,
                          10,
                          fill='darkblue',
                          font='Times 20 italic bold',
                          text=current_best)
            w.update()
            # var.set(current_best)
            # parent.update_idletasks()

            # checks for completion
            fittest_individual = search.fitness_threshold(
                fitness_fn, f_thres, population)
            if fittest_individual:
                break

        # 	v.set(current_best)
        # 	self.update_idletasks()
        # label.configure(text=current_best)
        # label.update()
        # solution, generations = genetic_algorithm_stepwise(self, population, fitness_fn, gene_pool, f_thres, ngen, mutation_rate)
        controller.mainloop()
def game_loop(population, fitness_fn, gene_pool=[0, 1], f_thres=None, ngen=1200, pmut=0.1):
	global generation
	generation = 0
	running = True
	finished = False
	while running:
		for event in pygame.event.get():
			# defining functions to execute on keypresses
			if event.type == pygame.QUIT:
				running = False
			elif event.type == KEYDOWN and event.key == K_ESCAPE:
				running = False
			elif event.type == KEYDOWN and event.key == K_p:
				pygame.image.save(screen, 'genetic_algorithm_phrase_gen.png') # screenshot

		# set finished to True if we exceed the maximum number of generations
		if generation >= ngen:
			finished = True

		if not finished:
			screen.fill(THECOLORS['white'])
			generation += 1
			# generating new population after selecting, recombining and mutating the existing population
			population = [search.mutate(search.recombine(*search.select(2, population, fitness_fn)), gene_pool, pmut) for i in range(len(population))]
			# genome with the highest fitness in the current generation
			current_best = ''.join(argmax(population, key=fitness_fn))

			# checks for completion
			fittest_individual = search.fitness_threshold(fitness_fn, f_thres, population)
			if fittest_individual:
				finished = True
				# return fittest_individual

			# displays current best on top of the screen
			large_text = pygame.font.SysFont('Consolas', 80, bold=True)
			m_text_surface, m_text_rect = text_objects(current_best, large_text, p_blue)
			m_text_rect.center = ((display_width/2), (display_height * 0.1))
			screen.blit(m_text_surface, m_text_rect)
			
			# collecting first few examples from the current population
			members = [''.join(x) for x in population][:48]
			small_text = pygame.font.SysFont('Consolas', 20)
			# displaying a part of the population on the screen
			for i in range(len(members) // 3):
				m_text_surface1, m_text_rect1 = text_objects(members[3*i], small_text, light_p_blue)
				m_text_surface2, m_text_rect2 = text_objects(members[3*i+1], small_text, light_p_blue)
				m_text_surface3, m_text_rect3 = text_objects(members[3*i+2], small_text, light_p_blue)
				m_text_rect1.center = ((display_width * .175), (display_height * 0.25 + (25 * i)))
				m_text_rect3.center = ((display_width * .500), (display_height * 0.25 + (25 * i)))
				m_text_rect2.center = ((display_width * .825), (display_height * 0.25 + (25 * i)))
				screen.blit(m_text_surface1, m_text_rect1)
				screen.blit(m_text_surface2, m_text_rect2)
				screen.blit(m_text_surface3, m_text_rect3)

			# displays blue bar that indicates current maximum fitness compared to maximum possible fitness
			scaling_factor = fitness_fn(current_best) / len(target)
			pygame.draw.rect(screen, p_blue, (m_text_rect[0], m_text_rect[1] + 85, m_text_rect[2], 10), 2)
			pygame.draw.rect(screen, (12 - 12 * scaling_factor, 57 - 57 *scaling_factor, 76 - 76 * scaling_factor), (m_text_rect[0], m_text_rect[1] + 85, m_text_rect[2] * scaling_factor, 10))

			# displays current generation number
			g_text_surface, g_text_rect = text_objects(f'Generation {generation}', pygame.font.SysFont('Consolas', 20, bold=True), light_p_blue)
			g_text_rect.center = ((display_width * 0.5), (display_height * 0.95))
			screen.blit(g_text_surface, g_text_rect)

		else:
			button('NEXT', display_width * 0.9, display_height * 0.920, display_width * 0.070, display_height * 0.05, p_blue, light_p_blue, game_intro)

		# updates the screen
		pygame.display.update()
		clock.tick(60)