Beispiel #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 raise_frame(frame, init=False, update_target=False, target_entry=None, f_thres_slider=None):
	frame.tkraise()
	global target
	if update_target and target_entry is not None:
		target = target_entry.get()
		f_thres_slider.config(to=len(target))
	if init:
		population = search.init_population(max_population, gene_pool, len(target))
		genetic_algorithm_stepwise(population)
def main():
    population = search.init_population(max_population, gene_pool, len(target))
    mating_pool = []
    for _ in range(1000):
        new_population = [
            search.mutate(
                search.recombine(
                    *select(2, population, fitness_fn, mating_pool)),
                gene_pool, mutation_rate)
        ]
        fittest_individual = argmax(new_population, key=fitness_fn)
        current_best = ''.join(fittest_individual)
        if current_best == target:
            return
        print(current_best)
        population = new_population
Beispiel #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 main():
	population = search.init_population(max_population, gene_pool, len(target))
	solution, iterations = genetic_algorithm_stepwise(population, fitness_fn, f_thres=len(target), gene_pool=gene_pool, pmut=mutation_rate)
def main():
	population = search.init_population(max_population, gene_pool, len(target))
	solution = game_loop(population, fitness_fn, gene_pool=gene_pool, f_thres=f_thres, pmut=mutation_rate, ngen=ngen)
    x2s[1:] = xs[:length - 1]
    x2s[0] = xs[length - 1]

    y2s[1:] = ys[:-1]
    y2s[0] = ys[-1]

    value = np.exp(
        -np.log(np.sum(np.sqrt((x2s - xs)**2 +
                               (y2s - ys)**2)) + 0.00001) * 5 + 35)
    return value


city_num = 15
cities = generate_random_cities(city_num=city_num, length=100)
population = init_population(pop_number=1000,
                             gene_pool=None,
                             state_length=city_num)

path, history = genetic_algorithm(population,
                                  fitness_fn,
                                  gene_pool=[0, 1],
                                  f_thres=None,
                                  ngen=200,
                                  pmut=0.1)

print(path)

xs = []
ys = []
for i in range(len(path)):
    xs.append(cities[path[i]][0])