Ejemplo n.º 1
0
def calibrate_final_tuning(args, pool, hyperparameters: HyperParameters):
  sampling = MixedVariableSampling(mask, {
    # Integer numbers are sampled via Uniform Random Sampling
    'int': get_sampling('int_random')
  })

  crossover = MixedVariableCrossover(mask, {
    'int': get_crossover('real_sbx', prob=0.9, eta=3.0)
  })

  mutation = MixedVariableMutation(mask, {
    # Integer numbers are mutated via polynomial mutation
    'int': get_mutation('int_pm', eta=3.0)
  })
  
  problem = MetaProblem(args, hyperparameters=hyperparameters, parallelization=None)
  algorithm = NSGA2(
    pop_size=EXECUTIONS_FOR_ITERATION,
    sampling=sampling,
    crossover=crossover,
    mutation=mutation,
    eliminate_duplicates=True,
  )
  termination = utils.get_termination_for_final_tuning(time=ITERATIONS_TIME)

  res, \
    best_solution, \
    min_average, \
    min_stddev = utils.get_minimizer(problem, algorithm, termination)
  
  save_csv(args, best_solution, min_average, min_stddev)
Ejemplo n.º 2
0
def calibrate_variables(args, pool) -> HyperParameters:
  sampling = MixedVariableSampling(mask, {
    # Real numbers are sampled via Latin Hypercube Sampling
    'real': get_sampling('real_lhs'),

    # Integer numbers are sampled via Uniform Random Sampling
    'int': get_sampling('int_random')
  })

  crossover = MixedVariableCrossover(mask, {
    'real': get_crossover('real_sbx', prob=0.9, eta=3.0),
    'int': get_crossover('real_sbx', prob=0.9, eta=3.0)
  })

  mutation = MixedVariableMutation(mask, {
    # Real numbers are mutated via polynomial mutation
    'real': get_mutation('real_pm', eta=3.0),

    # Integer numbers are mutated via polynomial mutation
    'int': get_mutation('int_pm', eta=3.0)
  })

  problem = MetaProblem(args, parallelization=('starmap', pool.starmap))
  algorithm = NSGA2(
    pop_size=EXECUTIONS_FOR_ITERATION,
    sampling=sampling,
    crossover=crossover,
    mutation=mutation,
    eliminate_duplicates=True,
  )
  termination = utils.get_termination_for_variables(max_iterations=MAX_ITERATIONS)

  res, \
    best_solution, \
    min_average, \
    min_stddev = utils.get_minimizer(problem, algorithm, termination)

  best_solution = res.X[0]
  min_average = res.F[0][0]
  min_stddev = res.F[0][1]

  mutation_probability = get_var(best_solution, 'mutation_probability')
  crossover_rate = get_var(best_solution, 'crossover_rate')
  mu = get_var(best_solution, 'mu')
  lambda_ = get_var(best_solution, 'lambda')
  k = get_var(best_solution, 'k')

  hyperparameters = HyperParameters(
    mutation_probability=mutation_probability,
    crossover_rate=crossover_rate,
    mu=mu,
    lambda_=lambda_,
    k=k
  )
  
  save_csv(args, best_solution, min_average, min_stddev)

  return hyperparameters
    def get_mutation(self):
        mask = []
        for name in (self.space.numeric_names + self.space.enum_names):
            if self.space.paras[name].is_discrete_after_transform:
                mask.append('int')
            else:
                mask.append('real')

        mutation = MixedVariableMutation(mask, {
            'real' : get_mutation('real_pm', eta = 20), 
            'int'  : get_mutation('int_pm', eta = 20)
        })
        return mutation
Ejemplo n.º 4
0
def get_operators(config):
    if config.config == "DeepMindBigGAN":
        mask = ["real"]*config.dim_z + ["bool"]*config.num_classes
        
        real_sampling = None
        if config.config == "DeepMindBigGAN":
            real_sampling = TruncatedNormalRandomSampling()

        sampling = MixedVariableSampling(mask, {
            "real": real_sampling,
            "bool": BinaryRandomSampling(prob=5/1000)
        })

        crossover = MixedVariableCrossover(mask, {
            "real": get_crossover("real_sbx", prob=1.0, eta=3.0),
            "bool": get_crossover("bin_hux", prob=0.2)
        })

        mutation = MixedVariableMutation(mask, {
            "real": get_mutation("real_pm", prob=0.5, eta=3.0),
            "bool": get_mutation("bin_bitflip", prob=10/1000)
        })

        return dict(
            sampling=sampling,
            crossover=crossover,
            mutation=mutation
        )
    
    elif config.config.split("_")[0] == "StyleGAN2":
        return dict(
            sampling=NormalRandomSampling(),
            crossover=get_crossover("real_sbx", prob=1.0, eta=3.0),
            mutation=get_mutation("real_pm", prob=0.5, eta=3.0)
        )
    
    elif config.config == "GPT2":
        return dict(
            sampling=get_sampling("int_random"),
            crossover=get_crossover("int_sbx", prob=1.0, eta=3.0),
            mutation=get_mutation("int_pm", prob=0.5, eta=3.0)
        )

    else:
        raise Exception("Unknown config")
Ejemplo n.º 5
0
def run_multiJ(dict_t):
    # def run_multiJ():
    mask = [
        "real", "int", "real", "real", "int", "int", "int", "int", "int",
        "int", "int"
    ]
    sampling = MixedVariableSampling(mask, {
        "real": get_sampling("real_random"),
        "int": get_sampling("int_random")
    })
    crossover = MixedVariableCrossover(
        mask, {
            "real": get_crossover("real_sbx", prob=1.0, eta=3.0),
            "int": get_crossover("int_sbx", prob=1.0, eta=3.0)
        })
    mutation = MixedVariableMutation(
        mask, {
            "real": get_mutation("real_pm", eta=3.0),
            "int": get_mutation("int_pm", eta=3.0)
        })
    #[V_gBurn,ng,Tdig,debt_level,V_cng_p,e_priceS,farm1,farm2,farm3,farm4,farm5,farm6,farm7]
    problem = BiogasMultiJ(dict_t)
    # problem = BiogasMultiJ()
    algorithm = NSGA2(
        pop_size=dict_t['NSGA_pop'],
        sampling=sampling,
        crossover=crossover,
        n_offsprings=dict_t['NSGA_off'],
        mutation=mutation,
        eliminate_duplicates=True,
    )
    res = minimize(problem,
                   algorithm, ("n_gen", dict_t['NSGA_gen']),
                   verbose=True,
                   seed=1,
                   save_history=True)
    return res
Ejemplo n.º 6
0
def create_attack(
    initial_state,
    model,
    scaler,
    encoder,
    n_gen,
    pop_size,
    n_offsprings,
):

    problem = VenusProblem(initial_state, model, encoder, scaler)

    type_mask = [
        "real",
        "int",
        "real",
        "real",
        "real",
        "real",
        "real",
        "real",
        "real",
        "real",
        "real",
        "int",
        "real",
        "real",
        "int",
    ]

    sampling = MixedVariableSampling(
        type_mask,
        {
            "real": get_sampling("real_random"),
            "int": get_sampling("int_random")
        },
    )

    # Default parameters for crossover (prob=0.9, eta=30)
    crossover = MixedVariableCrossover(
        type_mask,
        {
            "real": get_crossover("real_sbx", prob=0.9, eta=30),
            "int": get_crossover("int_sbx", prob=0.9, eta=30),
        },
    )

    # Default parameters for mutation (eta=20)
    mutation = MixedVariableMutation(
        type_mask,
        {
            "real": get_mutation("real_pm", eta=20),
            "int": get_mutation("int_pm", eta=20),
        },
    )

    algorithm = NSGA2(
        pop_size=pop_size,
        n_offsprings=n_offsprings,
        sampling=sampling,
        crossover=crossover,
        mutation=mutation,
        eliminate_duplicates=True,
        return_least_infeasible=True,
    )

    termination = get_termination("n_gen", n_gen)

    return problem, algorithm, termination
mask = ["int", "int","int","int","int","int","int","int","int","int","int","int"]
from pymoo.factory import get_sampling, get_crossover, get_mutation
from pymoo.operators.mixed_variable_operator import MixedVariableSampling, MixedVariableMutation, MixedVariableCrossover

sampling = MixedVariableSampling(mask, {
    "real": get_sampling("real_random"),
    "int": get_sampling("int_random")
})

crossover = MixedVariableCrossover(mask, {
    "real": get_crossover("real_sbx", prob=1.0, eta=3.0),
    "int": get_crossover("int_sbx", prob=1.0, eta=3.0)
})

mutation = MixedVariableMutation(mask, {
    "real": get_mutation("real_pm", eta=3.0),
    "int": get_mutation("int_pm", eta=3.0)
})
ref_dirs = get_reference_directions("das-dennis", 3, n_partitions=12)
algorithm = NSGA3(ref_dirs)

termination = get_termination("f_tol", tol=0.001, n_last=20, n_max_gen=1000, nth_gen=10)
termination = ("n_gen", 1000)

from pymoo.factory import get_crossover, get_mutation, get_sampling
from pymoo.optimize import minimize
problem = MyProblem()
res = minimize(MyProblem(),
               algorithm,
               termination,
               seed=1,
               pf=problem.pareto_front(use_cache=False),