Exemple #1
0
    # pop = toolbox.population(n=1000)
    game = TetrisApp(training=True)
    best_ind = []
    best_score = -1

    max_out  = open("max50.txt", "w")
    mean_out = open("mean50.txt", "w")
    min_out  = open("min50.txt", "w")
    std_out  = open("std50.txt", "w")
    var_out  = open("var50.txt", "w")

    for g in range(1, n_gen + 1):
        print("Current Generation " + str(g))
        scores = []
        for ind in pop:
            score = game.run_train(ind)
            scores.append(score)
            ind.fitness.values = (score,)
            if score > best_score:
                best_ind = ind
                best_score = score

        max_out.write(str(max(scores)) + "\n")
        mean_out.write(str(np.mean(scores)) + "\n")
        min_out.write(str(min(scores)) + "\n")
        std_out.write(str(np.std(scores)) + "\n")
        var_out.write(str(np.var(scores)) + "\n")

        offspring = map(toolbox.clone, toolbox.select(pop, len(pop)))
        offspring = algorithms.varAnd(offspring, toolbox, prob_xover, prob_mut)
        pop[:] = offspring