Exemplo n.º 1
0
def get_hv_for_algo(algo, max_fevals, pop_size, seed, problem, run):

    max_gen = math.ceil(max_fevals / pop_size)

    # same (random) starting population for both algos
    pop = pg.population(problem, pop_size, seed)

    f_list, x_list = pop.get_f(), pop.get_x()

    for i in range(max_gen):
        pop = algo.evolve(pop)

        # Get all the f and x values for this generation
        f_list = numpy.concatenate((f_list, pop.get_f()))
        x_list = numpy.concatenate((x_list, pop.get_x()))

    save_values(
        'storedvalues/' + algo.get_name().split(':')[0] + '_x_' +
        problem.get_name() + '_run' + str(run) + '.txt', x_list.tolist())
    save_values(
        'storedvalues/' + algo.get_name().split(':')[0] + '_f_' +
        problem.get_name() + '_run' + str(run) + '.txt', f_list.tolist())

    pop_empty = pg.population(prob=problem, seed=seed)

    return reconstruct_hv_per_feval(max_fevals, x_list, f_list, pop_empty)
Exemplo n.º 2
0
def calculate_mean_rbf(n,
                       max_fevals,
                       working_fevals,
                       seed,
                       problem,
                       cycle,
                       output_stream=None):
    return_array = []

    for i in range(n):
        # Create dictionary for algo settings
        dict_settings = {
            'max_evaluations': max_fevals,
            'rand_seed': seed,
        }

        var_types = ['R'] * problem.get_nx()

        algo_rbfmopt = rbfmopt.RbfmoptWrapper(dict_settings, problem,
                                              var_types, output_stream,
                                              'tchebycheff', cycle)

        # RBFMopt hypervolume calculations
        algo_rbfmopt.evolve()
        empty_pop = pg.population(prob=problem, seed=seed)

        x_list = np.array(algo_rbfmopt.get_x_list())
        f_list = np.array(algo_rbfmopt.get_f_list())

        save_values(
            'storedvalues/rbfmopt_x_cycle' + str(cycle) + '_' +
            problem.get_name() + '_run' + str(i + 1) + '.txt', x_list.tolist())
        save_values(
            'storedvalues/rbfmopt_f_cycle' + str(cycle) + '_' +
            problem.get_name() + '_run' + str(i + 1) + '.txt', f_list.tolist())

        return_array.append(
            reconstruct_hv_per_feval(working_fevals, algo_rbfmopt.get_x_list(),
                                     algo_rbfmopt.get_f_list(), empty_pop))

        # Make sure we change the seed each time the algo is being run
        seed += (i + 1)

    return numpy.mean(return_array, axis=0)
seed = 33

# For the each problem in the problem suite
for i in range(problem_number):
    problem_function = getattr(pg.problems, problem_name)
    if (problem_name == "dtlz"):
        problem = pg.problem(problem_function(i + 1, dim=dim, fdim=fdim))
    else:
        problem = pg.problem(problem_function(i + 1, param=dim))
    algo_moead = pg.algorithm(pg.moead(gen=1))
    algo_nsga2 = pg.algorithm(pg.nsga2(gen=1))

    # Hypervolume calculations, mean taken over n number of times
    hv_rbfmopt_plot = calculate_mean_rbf(n, max_fevals, working_fevals, seed,
                                         problem, cycle)
    hv_moead_plot = calculate_mean_pyg(n, algo_moead, working_fevals, pop_size,
                                       seed, problem)
    hv_nsga2_plot = calculate_mean_pyg(n, algo_nsga2, working_fevals, pop_size,
                                       seed, problem)
    fevals_plot = range(0, max_fevals)

    save_values(
        'storedvalues/rbfmopt_hv_' + problem.get_name() + '_fevals' +
        str(max_fevals) + '.txt', hv_rbfmopt_plot.tolist())
    save_values(
        'storedvalues/moead_hv_' + problem.get_name() + '_fevals' +
        str(max_fevals) + '.txt', hv_moead_plot.tolist())
    save_values(
        'storedvalues/nsga2_hv_' + problem.get_name() + '_fevals' +
        str(max_fevals) + '.txt', hv_nsga2_plot.tolist())
Exemplo n.º 4
0
def calculate_mean_rbf(n, max_fevals, working_fevals, seed, problem, cycle,
                       max_filter):
    return_array = []

    for i in range(n):
        # Create dictionary for algo settings
        dict_settings = {
            'max_evaluations': max_fevals,
            'rand_seed': seed,
        }

        var_types = ['R'] * problem.get_nx()

        file_string = 'log/rbfmopt_log_ncycle' + str(cycle) + '_filter' + str(
            max_filter) + '_fevals' + str(
                max_fevals) + problem.get_name() + '_run' + str(i + 1) + '.txt'

        stream = open(file_string, 'a')

        algo_rbfmopt = rbfmopt.RbfmoptWrapper(dict_settings, problem,
                                              var_types, stream, 'tchebycheff',
                                              cycle, max_filter)

        # RBFMopt hypervolume calculations

        start = time.time()
        # RBFMopt hypervolume calculations
        algo_rbfmopt.evolve()
        end = time.time()
        save_values(
            'timer/rbfmopt_timer_ncycle' + str(cycle) + '_filter' +
            str(max_filter) + '_fevals' + str(max_fevals) + '_' +
            problem.get_name() + '_run' + str(i + 1) + '.txt', (end - start))

        empty_pop = pg.population(prob=problem, seed=seed)

        x_list = np.array(algo_rbfmopt.get_x_list())
        f_list = np.array(algo_rbfmopt.get_f_list())

        save_values(
            'store_x/rbfmopt_x_ncycle' + str(cycle) + '_filter' +
            str(max_filter) + '_fevals' + str(max_fevals) + '_' +
            problem.get_name() + '_run' + str(i + 1) + '.txt', x_list.tolist())
        save_values(
            'store_f/rbfmopt_f_ncycle' + str(cycle) + '_filter' +
            str(max_filter) + '_fevals' + str(max_fevals) + '_' +
            problem.get_name() + '_run' + str(i + 1) + '.txt', f_list.tolist())

        hv_for_run = reconstruct_hv_per_feval(working_fevals,
                                              algo_rbfmopt.get_x_list(),
                                              algo_rbfmopt.get_f_list(),
                                              empty_pop)

        save_values(
            'store_hv/rbfmopt_hv_ncycle' + str(cycle) + '_filter' +
            str(max_filter) + '_fevals' + str(max_fevals) +
            problem.get_name() + '_run' + str(i + 1) + '.txt', hv_for_run)

        return_array.append(hv_for_run)

        # Make sure we change the seed each time the algo is being run
        seed += (i + 1)

    return numpy.mean(return_array, axis=0)
Exemplo n.º 5
0
problem_number = 6

# max_fevals = (dim+1) * 50
max_fevals = 30

# To account for the fact that the zero index array in util functions
# are actually the 1st feval
working_fevals = max_fevals-1
pop_size = 24
seed = 33

cycle = 3
max_filter = 3

# For the each problem in the problem suite
for i in range(problem_number):
    if i == 4:
        continue

    problem_function = getattr(pg.problems, problem_name)

    problem = pg.problem(problem_function(i+1, param=dim))

    hv_rbfmopt_plot = calculate_mean_rbf(n, max_fevals, working_fevals, seed, problem, cycle, None)
    save_values('store_hv/rbfmopt_hv_ncycle' + str(cycle) + '_filter' + 'None' + '_fevals' + str(max_fevals) + problem.get_name() + '.txt', hv_rbfmopt_plot.tolist())

    for j in range(3):
        hv_rbfmopt_plot = calculate_mean_rbf(n, max_fevals, working_fevals, seed, problem, cycle, (j+1)*max_filter*dim)
        save_values('store_hv/rbfmopt_hv_ncycle' + str(cycle) + '_filter' + str((j+1)*max_filter*dim) + '_fevals' + str(max_fevals) + problem.get_name() + '.txt', hv_rbfmopt_plot.tolist())

Exemplo n.º 6
0
problem_name = 'zdt'
problem_number = 6

max_fevals = (dim + 1) * 2

# To account for the fact that the zero index array in util functions
# are actually the 1st feval
working_fevals = max_fevals - 1
pop_size = 24
seed = 33

default_rf = 3

# For the each problem in the problem suite
for i in range(problem_number):
    problem_function = getattr(pg.problems, problem_name)

    problem = pg.problem(problem_function(i + 1, param=dim))

    for cycle_mult in range(3):

        cycle = (cycle_mult + 1) * default_rf

        hv_rbfmopt_plot = calculate_mean_rbf(n, max_fevals, working_fevals,
                                             seed, problem, cycle)

        save_values(
            'storedvalues/rbfmopt_hv_cycle' + str(cycle) + '_' +
            problem.get_name() + '_fevals' + str(max_fevals) + '.txt',
            hv_rbfmopt_plot.tolist())