import random

from ch9.knapsack.individual import Individual
from ch9.knapsack.random_set_generator import random_set_generator

if __name__ == '__main__':
    random.seed(1)

    items = random_set_generator(1, 100, 0.1, 7, 200)
    Individual.set_items(items)
    Individual.set_max_weight(10)

    population = [Individual.create_random() for _ in range(1000)]
    average_weight = sum([ind.total_weight()
                          for ind in population]) / len(population)
    print(f'Average weight of population: {average_weight}')
    stats,
    plot_stats,
)


def crossover(parent1, parent2):
    child1_genes, child2_genes = crossover_one_point(parent1.gene_list, parent2.gene_list)
    return Individual(child1_genes), Individual(child2_genes)


def mutate(ind):
    mutated_gene = mutation_bit_flip(ind.gene_list)
    return Individual(mutated_gene)


Individual.set_items(get_items_from_my_room())
Individual.set_max_weight(10)

random.seed(63)
POPULATION_SIZE = 8
CROSSOVER_PROBABILITY = .7
MUTATION_PROBABILITY = .2
MAX_GENERATIONS = 20

first_population = [Individual.create_random() for _ in range(POPULATION_SIZE)]
population = first_population.copy()
fitness_list = [ind.fitness for ind in population]
fit_avg = [sum(fitness_list) / len(population)]
fit_best = [max(fitness_list)]
fit_best_ever = [max(fitness_list + fit_best)]
best_ind = random.choice(first_population)