How to use the deap.tools.selBest function in deap

To help you get started, we’ve selected a few deap examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DEAP / deap / examples / ga / onemax_island_scoop.py View on Github external
random.seed(64)
    NISLES = 5
    islands = [toolbox.population(n=300) for i in range(NISLES)]

    # Unregister unpicklable methods before sending the toolbox.
    toolbox.unregister("attr_bool")
    toolbox.unregister("individual")
    toolbox.unregister("population")

    NGEN, FREQ = 40, 5
    toolbox.register("algorithm", algorithms.eaSimple, toolbox=toolbox, 
                     cxpb=0.5, mutpb=0.2, ngen=FREQ, verbose=False)
    for i in range(0, NGEN, FREQ):
        results = toolbox.map(toolbox.algorithm, islands)
        islands = [pop for pop, logbook in results]
        tools.migRing(islands, 15, tools.selBest)

    return islands
github Ambrosys / glyph / examples / symbolic_regression / observer.py View on Github external
for gen in range(20):
        pop = algorithm.evolve(pop)
        pop = update_fitness(pop)

        records = stats.compile(pop)
        logbook.record(gen=gen, **records)
        front = deap.tools.ParetoFront()
        front.update(pop)

        app = Mock()
        app.gp_runner = Mock()
        app.gp_runner.pareto_front = front
        app.logbook = logbook
        observer(app)

        best = deap.tools.selBest(pop, 1)[0]

        if best.fitness.values[0] <= 1e-3:
            break
github DEAP / deap / examples / ga / onemax.py View on Github external
# Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        
        print("  Min %s" % min(fits))
        print("  Max %s" % max(fits))
        print("  Avg %s" % mean)
        print("  Std %s" % std)
    
    print("-- End of (successful) evolution --")
    
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))
github EricssonResearch / monad / LookAheadVersion2 / main.py View on Github external
def main():
    # Generate the population
    pop = toolBox.toolbox.population(n=POPULATION_SIZE)
    hof = tools.HallOfFame(1)
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean)
    stats.register("std", numpy.std)
    stats.register("min", numpy.min)
    stats.register("max", numpy.max)
    pop, log = algorithms.eaSimple(pop, toolBox.toolbox, cxpb=CROSS_OVER_PROB,
                                   mutpb=MUTATION_PROB, ngen=NO_OF_GENERATION, stats=stats,
                                   halloffame=hof, verbose=True)

    # The Best Individual found
    best_ind = tools.selBest(pop, 1)[0]
    individual = sorted(best_ind, key=itemgetter(3))
    individual = sorted(individual, key=itemgetter(0))
    #print("Best individual is %s, %s" % (individual, best_ind.fitness.values))
    timetable = fitnessClass.genTimetable(individual)
    databaseClass.insertBusTrip(timetable)
github darden1 / tradingrrl / 06_ga_optimizer / main.py View on Github external
stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    # Get elapsed time
    import time
    tic = time.clock()
    def get_elapsedtime(data):
        return time.clock() - tic
    stats.register("elapsed time",get_elapsedtime)

    pop_last, logbook = algorithms.eaSimple(pop, toolbox, cxpb=0.5, mutpb=0.2, ngen=ngen, stats=stats, halloffame=hof)
    best_ind = tools.selBest(pop_last, 1)[0]

    return best_ind, logbook
github DEAP / deap / examples / ga / ga_onemax.py View on Github external
# Gather all the fitnesses in one list and print the stats
        fits = [ind.fitness.values[0] for ind in pop]
        
        length = len(pop)
        mean = sum(fits) / length
        sum2 = sum(x*x for x in fits)
        std = abs(sum2 / length - mean**2)**0.5
        
        print "  Min %s" % min(fits)
        print "  Max %s" % max(fits)
        print "  Avg %s" % mean
        print "  Std %s" % std
    
    print "-- End of (successful) evolution --"
    
    best_ind = tools.selBest(pop, 1)[0]
    print "Best individual is %s, %s" % (best_ind, best_ind.fitness.values)
github SmokinCaterpillar / pypet / examples / example_20_using_deap_manual_runs.py View on Github external
del child2.fitness.values

            for mutant in offspring:
                if random.random() < MUTPB:
                    toolbox.mutate(mutant)
                    del mutant.fitness.values

            # The population is entirely replaced by the offspring
            pop[:] = offspring

    # Stop the multiprocessing pool
    pool.close()
    pool.join()

    print("-- End of (successful) evolution --")
    best_ind = tools.selBest(pop, 1)[0]
    print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values))

    traj.f_store()  # And store all the rest of the data
github Ambrosys / glyph / examples / symbolic_regression / simplification.py View on Github external
mate = deap.gp.cxOnePoint
    expr_mut = partial(deap.gp.genFull, min_=0, max_=2)
    mutate = partial(deap.gp.mutUniform, expr=expr_mut, pset=Individual.pset)
    simplify = gp.individual.simplify_constant

    algorithm = gp.algorithms.AgeFitness(mate, mutate, deap.tools.selNSGA2, Individual.create_population)

    pop = update_fitness(Individual.create_population(pop_size))

    for gen in range(20):
        pop = algorithm.evolve(pop)
        pop = [Individual(simplify(ind)) for ind in pop]

        pop = update_fitness(pop)
        best = deap.tools.selBest(pop, 1)[0]
        print(gp.individual.simplify_this(best), best.fitness.values)

        if best.fitness.values[0] <= 1e-3:
            break
github aqibsaeed / Genetic-CNN / GeneticCNN.py View on Github external
toolbox = base.Toolbox()
toolbox.register("binary", bernoulli.rvs, 0.5)
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.binary, n=L)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

toolbox.register("mate", tools.cxOrdered)
toolbox.register("mutate", tools.mutShuffleIndexes, indpb=0.8)
toolbox.register("select", tools.selRoulette)
toolbox.register("evaluate", evaluateModel)

popl = toolbox.population(n=population_size)
result = algorithms.eaSimple(popl, toolbox, cxpb=0.4, mutpb=0.05, ngen=num_generations, verbose=True)
print(result)

# print top-3 optimal solutions
best_individuals = tools.selBest(popl, k=3)
for bi in best_individuals:
    print(bi)