Esempio n. 1
0
def f(chromossome):
    route = Chromossome.get_fenotype(chromossome.get_genes())

    total_weight = 0
    total_vertexes = len(GRAPH)

    for i in range(total_vertexes):
        vertex = route[i]
        next_vertex = route[i + 1]

        edge_weight = GRAPH[vertex][next_vertex]

        if edge_weight is None:
            edge_weight = NON_ADJACENT_WEIGHT

        total_weight += edge_weight

    return total_weight
Esempio n. 2
0
def f_chromossome(chromossome):
    x, y = Chromossome.get_fenotype(chromossome.get_genes())

    return f(x, y)
Esempio n. 3
0
        operators.mutation(population)
        operators.elitism(population)

        generation += 1
        population_score = problem.g_average(population)

        generation_plot.append(generation)
        population_score_plot.append(population_score)

        print(
            f"Generation # {generation} -> Average population score = {population_score:.3f}\n"
        )

    best_chromossome = utils.find_best_chromossome(population)
    print(f"Best individual: {utils.format_chromossome(best_chromossome)}")

    board = Chromossome.get_fenotype(best_chromossome.get_genes())

    for row in range(problem.N_QUEENS):
        row_str = ""

        for column in range(problem.N_QUEENS):
            row_str += str(int(board[row][column])) + " "

        print(row_str)

    plt.gca().set_xlabel("Generation")
    plt.gca().set_ylabel("Average fitness")
    plt.gca().set_title("Average fitness per generation")
    plt.plot(generation_plot, population_score_plot)
    plt.show()