Ejemplo n.º 1
0
def minimize(func_name):
    config = configparser.ConfigParser()
    config_name = os.path.join(project_dir,
                               'objective_function/config/noisy.ini')
    config.read(config_name, encoding='utf-8')
    print(config.sections())

    optimal_position_address = os.path.join(
        project_dir, config.get(obj_name, 'optimal_position_address'))
    dim_size = config.getint(obj_name, 'dim_size')
    dim_regs = eval(config.get(obj_name, 'dim_regs'))
    budget = config.getint(obj_name, 'budget')
    set_optimal_position(optimal_position_address)

    seed = 0
    random.seed(seed)
    np.random.seed(seed)
    repeat = 10
    for _ in range(repeat):
        x = fmin(function_dict[func_name],
                 space=[
                     hp.uniform(str(dim), dim_regs[0], dim_regs[1])
                     for dim in range(dim_size)
                 ],
                 algo=tpe.suggest,
                 max_evals=budget)
        epoch_first_items(budget)
        append_all_epoch()
    all_epoch = np.array(get_all_epoch())
    log_address = os.path.join(project_dir, 'hyperopt_exp/log/noisy/')
    file_name = os.path.join(log_address,
                             '{}_{}.txt'.format(obj_name, dim_size))
    os.makedirs(log_address, exist_ok=True)
    np.savetxt(file_name, all_epoch)
    print(all_epoch.shape)
Ejemplo n.º 2
0
def minimize(func_name):
    config = configparser.ConfigParser()
    config_name = os.path.join(project_dir,
                               'objective_function/config/low_dim.ini')
    config.read(config_name, encoding='utf-8')
    print(config.sections())

    optimal_position_address = os.path.join(
        project_dir, config.get(func_name, 'optimal_position_address'))
    dim_size = config.getint(func_name, 'dim_size')
    dim_regs = eval(config.get(func_name, 'dim_regs'))
    budget = config.getint(func_name, 'budget')

    repeat = 30
    set_optimal_position(optimal_position_address)
    seed = 0
    random.seed(seed)
    np.random.seed(seed)
    for i in range(repeat):
        init_pos = [
            np.random.uniform(dim_regs[0], dim_regs[1])
            for _ in range(dim_size)
        ]
        es = cma.CMAEvolutionStrategy(init_pos, 0.5)  # doctest: +ELLIPSIS
        while get_cnt() < budget:
            solutions = es.ask()
            es.tell(solutions,
                    [function_cmaes_dict[func_name](x) for x in solutions])
            es.logger.add()
        epoch_first_items(budget)
        append_all_epoch()
        sol = es.result_pretty()
        es.result_pretty()
    all_epoch = np.array(get_all_epoch())
    log_address = os.path.join(project_dir, 'pycma_exp/log/low_dim/')
    file_name = os.path.join(log_address,
                             '{}_{}.txt'.format(obj_name, dim_size))
    os.makedirs(log_address, exist_ok=True)
    np.savetxt(file_name, all_epoch)
    print(all_epoch.shape)
Ejemplo n.º 3
0
    num_genes = dim_size
    for i in range(repeat):
        gene_space = [{'low': dim_regs[0], 'high': dim_regs[1]}] * dim_size

        def fitness_func(solution, solution_idx):
            # Calculating the fitness value of each solution in the current population.
            output = -function_dict[obj_name](solution)
            return output

        fitness_function = fitness_func
        ga_instance = pygad.GA(num_generations=num_generations,
                               num_parents_mating=num_parents_mating,
                               fitness_func=fitness_function,
                               sol_per_pop=sol_per_pop,
                               gene_space=gene_space,
                               num_genes=num_genes)
        # Running the GA to optimize the parameters of the function.
        ga_instance.run()
        epoch_first_items(budget)
        append_all_epoch()

    all_epoch = np.array(get_all_epoch())
    print(all_epoch[:, 0])
    log_address = os.path.join(project_dir, 'pygad_exp/log/low_dim/')
    file_name = os.path.join(log_address,
                             '{}_{}.txt'.format(obj_name, dim_size))
    os.makedirs(log_address, exist_ok=True)
    np.savetxt(file_name, all_epoch)
    print(all_epoch.shape)
Ejemplo n.º 4
0
def minimize(func_name):
    config = configparser.ConfigParser()
    config_name = os.path.join(project_dir,
                               'objective_function/config/low_dim.ini')
    config.read(config_name, encoding='utf-8')
    print(config.sections())

    optimal_position_address = os.path.join(
        project_dir, config.get(obj_name, 'optimal_position_address'))
    dim_size = config.getint(obj_name, 'dim_size')
    dim_regs = eval(config.get(obj_name, 'dim_regs'))
    budget = config.getint(obj_name, 'budget')
    set_optimal_position(optimal_position_address)

    repeat = 30
    seed = 0
    random.seed(seed)
    np.random.seed(seed)
    speed_lim = dim_regs[1] / 5

    creator.create("FitnessMin", base.Fitness, weights=(-1.0, ))
    creator.create("Particle",
                   list,
                   fitness=creator.FitnessMin,
                   speed=list,
                   smin=None,
                   smax=None,
                   best=None)
    toolbox = base.Toolbox()
    toolbox.register("particle",
                     generate,
                     size=dim_size,
                     pmin=dim_regs[0],
                     pmax=dim_regs[1],
                     smin=-speed_lim,
                     smax=speed_lim)
    toolbox.register("population", tools.initRepeat, list, toolbox.particle)
    toolbox.register("update", updateParticle, phi1=2.0, phi2=2.0)
    toolbox.register("evaluate", lambda x: (function_dict[func_name](x), ))
    for i in range(repeat):
        fmin = []
        population = 10
        pop = toolbox.population(n=population)

        best = None
        i = 0
        while get_cnt() < budget:
            for part in pop:
                part.fitness.values = toolbox.evaluate(part)
                if not part.best or part.best.fitness < part.fitness:
                    part.best = creator.Particle(part)
                    part.best.fitness.values = part.fitness.values
                if not best or best.fitness < part.fitness:
                    best = creator.Particle(part)
                    best.fitness.values = part.fitness.values
            for part in pop:
                toolbox.update(part, best)
        epoch_first_items(budget)
        append_all_epoch()
    all_epoch = np.array(get_all_epoch())
    log_address = os.path.join(project_dir, 'DEAP_exp/log/low_dim/')
    file_name = os.path.join(log_address,
                             '{}_{}.txt'.format(obj_name, dim_size))
    os.makedirs(log_address, exist_ok=True)
    np.savetxt(file_name, all_epoch)
    print(all_epoch.shape)