Ejemplo n.º 1
0
# In[61]:

pop = toolbox.population(n=n_pop)  #
hof = tools.HallOfFame(
    champs
)  # only record the best three individuals ever found in all generations

startDT = datetime.datetime.now()
print(str(startDT))

# start evolution
pop, log = gep.gep_simple(pop,
                          toolbox,
                          n_generations=n_gen,
                          n_elites=1,
                          stats=stats,
                          hall_of_fame=hof,
                          verbose=True)

print("Evolution times were:\n\nStarted:\t", startDT, "\nEnded:   \t",
      str(datetime.datetime.now()))

print(hof[0])

best_ind = hof[0]

print(best_ind)

symplified_best = gep.simplify(best_ind)
print(symplified_best)
Ejemplo n.º 2
0
def search_model():
    # define fitness and individual type
    creator.create('FitnessMax', base.Fitness, weights=(1,))
    creator.create('Individual', Chromosome, fitness=creator.FitnessMax)

    # create individuals (genotype space)
    toolbox = Toolbox()
    toolbox.register('gene', Gene, pset, args.head_length)
    toolbox.register('individual', creator.Individual, toolbox.gene, args.num_genes)
    toolbox.register('population', tools.initRepeat, list, toolbox.individual)

    # translate individuals into computation graph (phenotype space)
    toolbox.register('comp_graph', cell_graph.generate_comp_graph)

    # evaluation function
    def evaluate(indv):
        _, comp_graph = toolbox.comp_graph(indv)
        net = build_model(comp_graph)
        acc = train_model(net)
        #size = count_parameters(net)
        fit = acc[1].item() #+ 1 / expit(size)
        return fit,

    # evaluate and select
    toolbox.register('evaluate', evaluate)
    toolbox.register('select', tools.selRoulette)

    # recombine and mutate
    #toolbox.register('cx_1p', cross_one_point, pb=args.cx_pb)
    toolbox.register('cx_gene', cross_gene, pb=args.cx_pb[0])
    toolbox.register('cx_2p', cross_two_point, pb=args.cx_pb[1])
    toolbox.register('mut_uniform', mutate_uniform, pset=pset, pb=args.mu_pb)
    toolbox.register('mut_invert_program', invert_program, pb=args.invert_pb)
    #toolbox.register('mut_invert_cell', invert_cell, pb=args.invert_pb)
    toolbox.register('mut_transpose_program', transpose_program, pb=args.transpose_pb)
    toolbox.register('mut_transpose_cell', transpose_cell, pb=args.transpose_pb)

    # evolution statistics
    stats = tools.Statistics(key=lambda ind: ind.fitness.values[0])
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    # population size and best individual (hall of fame)
    pop = toolbox.population(n=args.pop_size)
    hof = tools.HallOfFame(args.hof)

    # call gep_simple evolutionary algorithm
    pop, log = gep_simple(pop,
                          toolbox,
                          n_generations=args.num_gen,
                          n_elites=args.elites,
                          stats=stats,
                          hall_of_fame=hof,
                          verbose=True)
    print('\n', log)

    # save and draw best individuals graphs
    for i, best in enumerate(hof):
        graph, _ = cell_graph.generate_comp_graph(best)
        cell_graph.save_graph(graph, args.path+'/best/indv_{}'.format(i))
        cell_graph.draw_graph(graph, args.path+'/best/indv_{}'.format(i))

    # save population graphs
    for i, p in enumerate(pop):
        graph, _ = cell_graph.generate_comp_graph(p)
        cell_graph.save_graph(graph, args.path+'/pop/indv_{}'.format(i))

    # save stats record
    with open(args.path+'/best/stats.pkl', 'wb') as f:
        pickle.dump(log, f,)