Ejemplo n.º 1
0
def run_experiment(differential_op,
                   max_gen,
                   pop_size,
                   exp_id=EXP_ID,
                   silent=False):

    # use `functool.partial` to create fix some arguments of the functions
    # and create functions with required signatures

    for fit_gen, fit_name in zip(fit_generators, fit_names):
        fit = fit_gen(DIMENSION)

        for run in range(REPEATS):
            pub_vars.fit_func = fit
            # initialize the log structure
            log = utils.Log(OUT_DIR,
                            exp_id + '.' + fit_name,
                            run,
                            write_immediately=True,
                            print_frequency=5,
                            silent=silent)
            # create population
            pop = create_pop(pop_size, cr_ind)
            pop = differential_evolution(pop,
                                         max_gen,
                                         differential_op,
                                         log=log)

        # write summary logs for the whole experiment
        utils.summarize_experiment(OUT_DIR, exp_id + '.' + fit_name)
Ejemplo n.º 2
0
def run_experiment(exp_id='default', data_input='inputs/tsp_std.in', repeats=10, mut_max_len=10, mut_prob=0.2, cx_prob=0.8, max_gen=500, pop_size=100, fitness=fitness,  create_ind=None, cross=order_cross, mutate=swap_mutate,  print_frequency=5, progress_callback=None):
    # read the locations from input
    locations = read_locations(data_input)

    # use `functool.partial` to create fix some arguments of the functions 
    # and create functions with required signatures
    cr_ind = functools.partial(create_ind, ind_len=len(locations))


    globals()['cities'] = locations
    fit = functools.partial(fitness, cities=locations)
    xover = functools.partial(crossover, cross=cross, cx_prob=cx_prob)
    mut = functools.partial(mutation, mut_prob=mut_prob, 
                            mutate=functools.partial(mutate, max_len=mut_max_len))


    # run the algorithm `REPEATS` times and remember the best solutions from 
    # last generations
    best_inds = []
    best_objective = 100000000
    for run in range(repeats):
        # initialize the log structure
        log = utils.Log(OUT_DIR, exp_id, run, 
                        write_immediately=True, print_frequency=print_frequency)
        # create population
        pop = create_pop(pop_size, cr_ind)
        # run evolution - notice we use the pool.map as the map_fn
        pop = evolutionary_algorithm(pop, pop_size, max_gen, fit, [xover, mut], tournament_selection, map_fn=map, log=log, progress_callback=progress_callback)
        # remember the best individual from last generation, save it to file
        bi = max(pop, key=fit)
        best_objective = min(best_objective, fit(bi).objective)
        best_inds.append(bi)

        best_template = '{individual}'
        with open('resources/kmltemplate.kml') as f:
            best_template = f.read()

        with open(f'{OUT_DIR}/{exp_id}_{run}.best', 'w') as f:
            f.write(str(bi))

        with open(f'{OUT_DIR}/{exp_id}_{run}.best.kml', 'w') as f:
            bi_kml = [f'{locations[i][1]},{locations[i][0]},5000' for i in bi]
            bi_kml.append(f'{locations[bi[0]][1]},{locations[bi[0]][0]},5000')
            f.write(best_template.format(individual='\n'.join(bi_kml)))
        
        # if we used write_immediately = False, we would need to save the 
        # files now
        # log.write_files()

    # print an overview of the best individuals from each run
    for i, bi in enumerate(best_inds):
        print(f'Run {i}: difference = {fit(bi).objective}')

    # write summary logs for the whole experiment
    utils.summarize_experiment(OUT_DIR, exp_id)

    return best_objective
Ejemplo n.º 3
0
def run_experiment(max_gen=MAX_GEN, mutation_op=Mutation(step_size=MUT_STEP), cross=one_pt_cross, cx_prob=CX_PROB, exp_id=EXP_ID, silent=True):

    # use `functool.partial` to create fix some arguments of the functions
    # and create functions with required signatures

    for fit_gen, fit_name in zip(fit_generators, fit_names):
        fit = fit_gen(DIMENSION)
        xover = functools.partial(crossover, cross=cross, cx_prob=cx_prob)
        mut = functools.partial(
            mutation, mut_prob=MUT_PROB, mutate=mutation_op)

        # run the algorithm `REPEATS` times and remember the best solutions from
        # last generations

        best_inds = []
        for run in range(REPEATS):
            # initialize the log structure
            log = utils.Log(OUT_DIR, exp_id + '.' + fit_name, run,
                            write_immediately=True, print_frequency=5, silent=silent)
            # create population
            pop = create_pop(POP_SIZE, cr_ind)
            # run evolution - notice we use the pool.map as the map_fn
            pop = evolutionary_algorithm(pop, max_gen, fit, [
                                         xover, mut], tournament_selection, mutation_op, map_fn=map, log=log)
            # remember the best individual from last generation, save it to file
            bi = max(pop, key=fit)
            best_inds.append(bi)

            # if we used write_immediately = False, we would need to save the
            # files now
            # log.write_files()

        # print an overview of the best individuals from each run
        for i, bi in enumerate(best_inds):
            if not silent:
                print(f'Run {i}: objective = {fit(bi).objective}')  

        # write summary logs for the whole experiment
        utils.summarize_experiment(OUT_DIR, EXP_ID + '.' + fit_name)
        with open(f'{OUT_DIR}/{EXP_ID}_{run}.best', 'w') as f:
            for w, b in zip(weights, bi):
                f.write(f'{w} {b}\n')

        # if we used write_immediately = False, we would need to save the
        # files now
        # log.write_files()

    # print an overview of the best individuals from each run
    for i, bi in enumerate(best_inds):
        print(
            f'Run {i}: difference = {fit(bi).objective}, bin weights = {bin_weights(weights, bi)}'
        )

    # write summary logs for the whole experiment
    utils.summarize_experiment(OUT_DIR, EXP_ID)

    # read the summary log and plot the experiment
    evals, lower, mean, upper = utils.get_plot_data(OUT_DIR, EXP_ID)
    plt.figure(figsize=(12, 8))
    utils.plot_experiment(evals,
                          lower,
                          mean,
                          upper,
                          legend_name='Default settings')
    plt.legend()
    plt.show()

    # you can also plot mutiple experiments at the same time using
    # utils.plot_experiments, e.g. if you have two experiments 'default' and
    # 'tuned' both in the 'partition' directory, you can call
Ejemplo n.º 5
0
            # initialize the log structure
            log = utils.Log(OUT_DIR,
                            EXP_ID + '.' + fit_name,
                            run,
                            write_immediately=True,
                            print_frequency=5)
            # create population
            pop = create_pop(POP_SIZE, cr_ind)
            # run evolution - notice we use the pool.map as the map_fn
            pop = evolutionary_algorithm(pop,
                                         MAX_GEN,
                                         fit,
                                         tournament_selection,
                                         mutate_ind,
                                         map_fn=map,
                                         log=log)
            # remember the best individual from last generation, save it to file
            bi = max(pop, key=fit)
            best_inds.append(bi)

            # if we used write_immediately = False, we would need to save the
            # files now
            # log.write_files()

        # print an overview of the best individuals from each run
        for i, bi in enumerate(best_inds):
            print(f'Run {i}: objective = {fit(bi).objective}')

        # write summary logs for the whole experiment
        utils.summarize_experiment(OUT_DIR, EXP_ID + '.' + fit_name)
Ejemplo n.º 6
0
def run_experiment(exp_id,
                   pop_size=250,
                   max_gen=500,
                   cx_prob=0.5,
                   mut_prob=1,
                   mut_flip_prob=0.05,
                   elitism=True,
                   repeats=10,
                   fitness=basic_fitness,
                   selection=roulette_wheel_selection,
                   input_weights='inputs/partition-easy.txt',
                   print_every=500):
    # read the weights from input
    weights = read_weights(input_weights)

    # use `functool.partial` to create fix some arguments of the functions
    # and create functions with required signatures
    cr_ind = functools.partial(create_ind, ind_len=len(weights))
    fit = functools.partial(fitness, weights=weights)
    xover = functools.partial(crossover, cross=one_pt_cross, cx_prob=cx_prob)
    mut = functools.partial(mutation,
                            mut_prob=mut_prob,
                            mutate=functools.partial(flip_mutate,
                                                     prob=mut_flip_prob,
                                                     upper=K))

    # we can use multiprocessing to evaluate fitness in parallel
    import multiprocessing
    pool = multiprocessing.Pool()

    # run the algorithm `REPEATS` times and remember the best solutions from
    # last generations
    best_inds = []
    for run in range(repeats):
        # initialize the log structure
        log = utils.Log(OUT_DIR,
                        exp_id,
                        run,
                        write_immediately=True,
                        print_frequency=print_every)
        # create population
        pop = create_pop(pop_size, cr_ind)
        # run evolution - notice we use the pool.map as the map_fn
        pop = evolutionary_algorithm(pop,
                                     max_gen,
                                     fit, [xover, mut],
                                     selection,
                                     map_fn=pool.map,
                                     log=log,
                                     elitism=elitism)

        # remember the best individual from last generation, save it to file
        best_ind = max(pop, key=fit)
        best_inds.append(best_ind)

        with open(f'{OUT_DIR}/{exp_id}_{run}.best', 'w') as f:
            for w, b in zip(weights, best_ind):
                f.write(f'{w} {b}\n')

        # if we used write_immediately = False, we would need to save the
        # files now
        # log.write_files

    # print an overview of the best individuals from each run
    for i, best_ind in enumerate(best_inds):
        print(
            f'Run {i}: difference = {fit(best_ind).objective}, bin weights = {bin_weights(weights, best_ind)}'
        )

    # write summary logs for the whole experiment
    utils.summarize_experiment(OUT_DIR, exp_id)
Ejemplo n.º 7
0
Archivo: rules.py Proyecto: kubic71/mff
def run_experiment(exp_id="default",
                   input_file="iris.csv",
                   repeats=10,
                   pop_size=100,
                   max_gen=50,
                   cx_prob=0.8,
                   max_rules=10,
                   mut_cls_prob=0.2,
                   mut_cls_prob_change=0.1,
                   mut_cond_prob=0.2,
                   mut_cond_sigma=0.3,
                   mut_prio_sigma=0.3,
                   elitism=1,
                   batch_size=10000,
                   cond_dist=[(LessThen, 0.25), (GreaterThen, 0.25),
                              (Any, 0.5)],
                   priority=False,
                   most_frequent_init=False,
                   print_frequency=1,
                   map_fn=None):

    train_data, test_data, num_attrs, num_classes, lb, ub = train_test_split(
        input_file)

    cr_ind = functools.partial(
        create_ind,
        max_rules=max_rules,
        num_attrs=num_attrs,
        num_classes=num_classes,
        lb=lb,
        ub=ub,
        cond_dist=cond_dist,
        priority=priority,
        most_frequent_cls=(-1 if most_frequent_init == False else np.argmax(
            np.bincount(train_data[1]))))
    xover = functools.partial(crossover, cross=cross, cx_prob=cx_prob)
    cls_mut_ind = functools.partial(cls_mutate,
                                    num_classes=num_classes,
                                    mut_cls_prob_change=mut_cls_prob_change)
    mut_cls = functools.partial(mutation,
                                mutate=cls_mut_ind,
                                mut_prob=mut_cls_prob)
    mut_cond = functools.partial(mutation,
                                 mutate=functools.partial(
                                     cond_mutate,
                                     mut_cond_sigma=mut_cond_sigma),
                                 mut_prob=mut_cond_prob)
    mut_priority = functools.partial(mutation,
                                     mutate=functools.partial(
                                         priority_mutate,
                                         mut_prio_sigma=mut_prio_sigma),
                                     mut_prob=mut_cond_prob if priority else 0)

    # run the algorithm `REPEATS` times and remember the best solutions from
    # last generations

    import multiprocessing

    if map_fn is None:
        pool = multiprocessing.Pool(8)
        map_fn = pool.map

    # fitness computed from the whole dataset
    full_fitness = functools.partial(fitness,
                                     train_data=train_data,
                                     test_data=test_data)

    best_inds = []
    for run in range(repeats):
        # initialize the log structure
        log = utils.Log(OUT_DIR,
                        exp_id,
                        run,
                        write_immediately=True,
                        print_frequency=print_frequency)
        # create population
        pop = create_pop(pop_size, cr_ind)
        # run evolution - notice we use the pool.map as the map_fn
        pop = evolutionary_algorithm(pop,
                                     max_gen,
                                     fitness,
                                     train_data,
                                     test_data,
                                     [xover, mut_cls, mut_cond, mut_priority],
                                     tournament_selection,
                                     map_fn=map_fn,
                                     log=log,
                                     elitism=elitism,
                                     batch_size=batch_size)
        # remember the best individual from last generation, save it to file
        bi = max(pop, key=full_fitness)
        best_inds.append(bi)

        # if we used write_immediately = False, we would need to save the
        # files now
        # log.write_files()

    # print an overview of the best individuals from each run
    for i, bi in enumerate(best_inds):
        print(f'Run {i}: objective = {full_fitness(bi).objective}')

    # write summary logs for the whole experiment
    utils.summarize_experiment(OUT_DIR, exp_id)