Пример #1
0
    def _do(self):
        rnd = sample_on_unit_simplex(self.n_sample_points,
                                     self.n_dim,
                                     unit_simplex_mapping=self.sampling)

        def h(n):
            return get_partition_closest_to_points(n, self.n_dim)

        H = h(self.n_points)

        E = get_reference_directions("das-dennis", self.n_dim, n_partitions=H)
        E = E[np.any(E == 0, axis=1)]

        # add the edge coordinates
        X = np.row_stack([E, rnd])

        I = select_points_with_maximum_distance(X,
                                                self.n_points,
                                                selected=list(range((len(E)))))
        centroids = X[I].copy()

        if self.kmeans:
            #centroids = kmeans(X, centroids, self.kmeans_max_iter, self.kmeans_a_tol, 0)
            centroids = kmeans(X, centroids, self.kmeans_max_iter,
                               self.kmeans_a_tol, len(E))

        return centroids
Пример #2
0
def Main(algorithm, problem, pop_size, crossover_probability,
         mutation_probability, n_partitions, n_gen, seed):

    # Instancia el problema
    problem = Problems.get(problem)

    reference_directions = get_reference_directions("das-dennis",
                                                    problem.n_obj,
                                                    n_partitions=n_partitions)

    # Instancia el algoritmo
    algorithm = NSGA_II.Get_Algorithm_Instance(
        pop_size, crossover_probability, mutation_probability
    ) if (algorithm == Algorithms.NSGAII) else NSGA_III.Get_Algorithm_Instance(
        reference_directions, pop_size, crossover_probability,
        mutation_probability) if (algorithm == Algorithms.NSGAIII) else None

    # Instancia el optimizador
    optimizer = Optimizer(problem, algorithm)

    optimization_result = optimizer.Minimize(n_gen, seed)
    objective_spaces_values = optimization_result.F

    pareto_front = problem.pareto_front(reference_directions) if type(
        problem).__name__ == "DTLZ1" else problem.pareto_front()

    # Instancia los indicadores de rendimiento (Distancia Generacional Invertida (IGD) / Distancia Generacional Invertida Plus (IGD+))
    IGD = get_performance_indicator("igd", pareto_front)
    #IGD_plus = get_performance_indicator("igd+", pareto_front)

    # Imprime las métricas obtenidas por el conjunto de soluciones resultantes de la optimización multimodal/multiobjetivo
    print("IGD:", IGD.calc(objective_spaces_values))
Пример #3
0
 def ref_dirs(self):
     ref_dirs = get_reference_directions(
         "energy",
         self.optimization_problem.n_objectives,
         self.population_size,
         seed=1)
     return ref_dirs
Пример #4
0
 def _calc_pareto_front(self):
     ref_kwargs = dict(n_points=100) if self.n_obj == 2 else dict(
         n_partitions=15)
     ref_dirs = get_reference_directions('das-dennis',
                                         n_dim=self.n_obj,
                                         **ref_kwargs)
     return generic_sphere(ref_dirs)
Пример #5
0
    def generate_solution(self):

        if self.rescheduling:
            n_obj = 6
        else:
            n_obj = 5

        ref_dirs = get_reference_directions("das-dennis",
                                            n_obj,
                                            n_partitions=8)

        selection = get_selection('tournament',
                                  func_comp=feasability_tournament,
                                  pressure=self.tournament_size)

        algorithm = NSGA3(pop_size=self.population_size,
                          sampling=DockSampling(),
                          selection=selection,
                          crossover=DockCrossover(),
                          mutation=DockMutation(),
                          ref_dirs=ref_dirs,
                          eliminate_duplicates=func_is_duplicate)
        res = minimize(DockProblem(n_obj, self),
                       algorithm,
                       seed=1,
                       verbose=True,
                       save_history=True,
                       termination=('n_gen', self.max_generations))

        return res.X.flatten(), res.history
def build_problem(name,
                  n_var,
                  n_obj,
                  n_init_sample,
                  n_process=1,
                  extra_params=None,
                  mode=0):
    '''
    Build optimization problem from name, get initial samples
    Input:
        name: name of the problem (supports ZDT1-6, DTLZ1-7)
        n_var: number of design variables
        n_obj: number of objectives
        n_init_sample: number of initial samples
        n_process: number of parallel processes
    Output:
        problem: the optimization problem
        X_init, Y_init: initial samples
        pareto_front: the true pareto front of the problem (if defined, otherwise None)
    '''
    # build problem
    if name.startswith('zdt') or name == 'vlmop2':
        problem = get_problem(name, n_var=n_var)
        pareto_front = problem.pareto_front()
    elif name.startswith('dtlz'):
        problem = get_problem(name, n_var=n_var, n_obj=n_obj)
        if n_obj <= 3 and name in ['dtlz1', 'dtlz2', 'dtlz3', 'dtlz4']:
            ref_kwargs = dict(n_points=100) if n_obj == 2 else dict(
                n_partitions=15)
            ref_dirs = get_reference_directions('das-dennis',
                                                n_dim=n_obj,
                                                **ref_kwargs)
            pareto_front = problem.pareto_front(ref_dirs)
        elif n_obj == 3 and name in ['dtlz5', 'dtlz6']:
            pareto_front = problem.pareto_front()
        else:
            pareto_front = None
    else:
        if mode == 0:
            transformToInteger = False
        else:
            transformToInteger = True

        if name == "cdn_ram" or name == "cdn_placement":
            problem = get_problem(name,
                                  n_var=n_var,
                                  n_obj=n_obj,
                                  transformToInteger=transformToInteger)
            topo, fileSize, colorMode, colorList, runReqNums, warmUpReqNums, separatorRankIncrement, deleteCachePath, interval = extra_params
            problem.get_parameters(topo, fileSize, colorMode, colorList,
                                   runReqNums, warmUpReqNums,
                                   separatorRankIncrement, n_process,
                                   deleteCachePath, interval)
        else:
            problem = get_problem(name, n_var=n_var, n_obj=n_obj)
    # get initial samples
    X_init, Y_init = generate_initial_samples(problem, n_init_sample)
    return problem, X_init, Y_init  #  problem, pareto_front, X_init, Y_init
Пример #7
0
def get_setup(n_obj):
    if n_obj == 3:
        pop_size = 91
        ref_dirs = get_reference_directions("das-dennis",
                                            n_obj,
                                            n_points=pop_size)

    return {
        'ref_dirs': ref_dirs,
        'crossover': SimulatedBinaryCrossover(20, n_offsprings=1, prob=0.9),
        'mutation': PolynomialMutation(20)
    }
Пример #8
0
    def _nsga3(self) -> object:
        sampling = get_sampling("int_random")
        crossover = get_crossover("int_sbx")
        mutation = get_mutation("int_pm")
        # create the reference directions to be used for the optimization
        if self._nome_of_mono is None:
            ref_dirs = get_reference_directions("das-dennis",
                                                len(self._nomes_direcoes_of),
                                                n_partitions=len(
                                                    self._variaveis))
        else:
            ref_dirs = get_reference_directions("das-dennis",
                                                1,
                                                n_partitions=len(
                                                    self._variaveis))

        algorithm = NSGA3(pop_size=self._tamanho_populacao,
                          sampling=sampling,
                          crossover=crossover,
                          mutation=mutation,
                          ref_dirs=ref_dirs,
                          eliminate_duplicates=True)

        return algorithm
Пример #9
0
def set_algorithm(name, no_obj, setup):
    """
    Text.

    Args:
        name (str): Name of the optimization algorithm.
        n_obj (int): Number of objectives.
        setup (dict): Optimization setup parameters.

    Returns:
        algorithm (): Optization algorithm object.
    """
    if name == "default":
        if no_obj == 1:
            name = "ga"
        elif no_obj > 1:
            name = "nsga3"

    # Get settings
    setup_gen = load_json(
        os.path.join(settings["root"], "app", "config", "opticonf", "general"))
    setup_alg = load_json(
        os.path.join(settings["root"], "app", "config", "opticonf", name))
    algorithm_args = {}

    # Get optimization settings objects
    algorithm_args["sampling"] = get_sampling(setup_gen["sampling"])

    if "operators" in setup:
        for operator in setup["operators"]:
            algorithm_args[operator] = get_operator(operator, setup)

    # Get reference directions
    if name == "nsga3":
        algorithm_args["ref_dirs"] = get_reference_directions(
            "energy", no_obj, setup_alg["ref_dirs_coef"] * no_obj)

    # Modify population
    if "n_offsprings" in setup:
        algorithm_args["n_offsprings"] = setup["n_offsprings"]
    if "pop_size" in setup:
        algorithm_args["pop_size"] = setup["pop_size"]

    algorithm = get_algorithm(name,
                              eliminate_duplicates=True,
                              **algorithm_args)

    return algorithm
Пример #10
0
def test_equal_during_run(n_obj):
    # create the reference directions to be used for the optimization
    ref_dirs = get_reference_directions("energy", n_obj, n_points=100)

    # create the algorithm object
    algorithm = NSGA3(pop_size=92, ref_dirs=ref_dirs)

    print(f"NDS with {n_obj} objectives.")

    # execute the optimization
    minimize(DTLZ2(n_obj=n_obj),
             algorithm,
             callback=MyCallback(),
             seed=1,
             termination=('n_gen', 200),
             verbose=True)
Пример #11
0
 def generate_solution(self):
     ref_dirs = get_reference_directions("das-dennis", 3, n_partitions=8)
     selection = get_selection('random')
     #selection = get_selection('tournament',func_comp=feasability_tournament,pressure=self.tournament_size)
     algorithm = NSGA2(pop_size=self.population_size,
                       sampling=GA_wrapper.PrioSampling(),
                       selection=selection,
                       crossover=GA_wrapper.PrioCrossover(),
                       mutation=GA_wrapper.PrioMutation(),
                       eliminate_duplicates=False)
     res = minimize(GA_wrapper.PrioProblem(3, self),
                    algorithm,
                    seed=1,
                    verbose=True,
                    save_history=True,
                    termination=('n_gen', self.max_generations))
     return res.X.flatten(), res.history
Пример #12
0
def build_problem(name, n_var, n_obj, n_init_sample, n_process=1):
    '''
    Build optimization problem from name, get initial samples
    Input:
        name: name of the problem (supports ZDT1-6, DTLZ1-7)
        n_var: number of design variables
        n_obj: number of objectives
        n_init_sample: number of initial samples
        n_process: number of parallel processes
    Output:
        problem: the optimization problem
        X_init, Y_init: initial samples
        pareto_front: the true pareto front of the problem (if defined, otherwise None)
    '''
    # build problem
    if name.startswith('zdt') or name == 'vlmop2':
        problem = get_problem(name, n_var=n_var)
        pareto_front = problem.pareto_front()
    elif name.startswith('dtlz'):
        problem = get_problem(name, n_var=n_var, n_obj=n_obj)
        if n_obj <= 3 and name in ['dtlz1', 'dtlz2', 'dtlz3', 'dtlz4']:
            ref_kwargs = dict(n_points=100) if n_obj == 2 else dict(
                n_partitions=15)
            ref_dirs = get_reference_directions('das-dennis',
                                                n_dim=n_obj,
                                                **ref_kwargs)
            pareto_front = problem.pareto_front(ref_dirs)
        elif n_obj == 3 and name in ['dtlz5', 'dtlz6']:
            pareto_front = problem.pareto_front()
        else:
            pareto_front = None
    else:
        try:
            problem = get_problem(name)
        except:
            raise NotImplementedError('problem not supported yet!')
        try:
            pareto_front = problem.pareto_front()
        except:
            pareto_front = None

    # get initial samples
    X_init, Y_init = generate_initial_samples(problem, n_init_sample)

    return problem, pareto_front, X_init, Y_init
Пример #13
0
    def test_equal_during_run(self):
        class MyCallback(Callback):

            def notify(self, algorithm):
                F = algorithm.pop.get("F")

                python_fast_nds = load_function("fast_non_dominated_sort", _type="python")(F)
                cython_fast_nds = load_function("fast_non_dominated_sort", _type="cython")(F)
                assert_fronts_equal(python_fast_nds, cython_fast_nds)

                python_efficient_fast_nds = load_function("efficient_non_dominated_sort", _type="python")(F,
                                                                                                          strategy="binary")
                assert_fronts_equal(python_fast_nds, python_efficient_fast_nds)

                cython_efficient_fast_nds = load_function("efficient_non_dominated_sort", _type="cython")(F,
                                                                                                          strategy="binary")
                assert_fronts_equal(python_efficient_fast_nds, cython_efficient_fast_nds)

                python_efficient_fast_nds_bin = load_function("efficient_non_dominated_sort", _type="python")(F)
                assert_fronts_equal(python_fast_nds, python_efficient_fast_nds_bin)

                cython_efficient_fast_nds_bin = load_function("efficient_non_dominated_sort", _type="cython")(F)
                assert_fronts_equal(python_efficient_fast_nds_bin, cython_efficient_fast_nds_bin)

                python_tree_based_nds = load_function("tree_based_non_dominated_sort", _type="python")(F)
                assert_fronts_equal(python_fast_nds, python_tree_based_nds)

        for n_obj in [3, 5, 10]:
            # create the reference directions to be used for the optimization
            ref_dirs = get_reference_directions("energy", n_obj, n_points=100)

            # create the algorithm object
            algorithm = NSGA3(pop_size=92,
                              ref_dirs=ref_dirs)

            print(f"NDS with {n_obj} objectives.")

            # execute the optimization
            minimize(DTLZ2(n_obj=n_obj),
                     algorithm,
                     callback=MyCallback(),
                     seed=1,
                     termination=('n_gen', 600),
                     verbose=True)
Пример #14
0
def build_ensemble(x, y, num_splits, error_metric):
    K_folds = KFold(n_splits=num_splits)
    cv_error_measure = []
    weights = get_reference_directions("das-dennis", 5, n_partitions=10)
    count = 0
    for train_index, test_index in K_folds.split(x):
        count = count + 1
        print(
            '\x1b[1A\x1b[2K' + "Calculating optimal ensemble weights... [", (count / K_folds.get_n_splits()) * 100,
            "% ]")
        trained_models = train_models(x[train_index], y[train_index])
        pred = predict_models(trained_models, x[test_index])
        ensemble_error_measure = []
        for j in weights:
            y_pred = np.sum((j * np.array(pred).T), axis=1)
            ensemble_error_measure.append(error_metric(y_pred, y[test_index]))
        cv_error_measure.append(ensemble_error_measure)
    mean_cv_ensembles = np.mean(cv_error_measure, axis=0)
    best_weights = weights[np.argmin(mean_cv_ensembles)]
    return best_weights
Пример #15
0
def test_das_dennis_achievable_points():
    ref_dirs = get_reference_directions("das-dennis", 3, n_points=91)
    assert len(ref_dirs) == 91
Пример #16
0
def optimize(config):
    """ This is the core function."""
    #---------------------------------------------------------------------------
    # Config validation.
    #---------------------------------------------------------------------------
    if config.feasinfeas_2N:
        assert config.novelty is not None, 'Must set a novelty method for feasinfeas_2N'
    assert config.novelty is False or config.novelty in novelty.archives, 'Novelty method does not exist:' + config.novelty
    feasinfeas = bool(config.feasinfeas) or bool(config.feasinfeas_2N)
    if config.out is not None:
        os.makedirs(config.out, exist_ok=False)
    for k, v in vars(config).items():
        if k[0] != '_': print(f'{k}: {v}')
    original_state = State.fromFile(config.state)
    original_state = original_state.mergeIslands()[0]
    if config.seed is not None:
        random.seed(config.seed)
        np.random.seed(config.seed)
    # for metric_name, limit, in config.metrics:
    #     assert (type(limit) is float and 0 <= limit <= 1), F'{limit} is not a number.'
    #     assert hasattr(metrics, metric_name), F'{metric_name} is not a metric.'
    print('-' * 80)
    #---------------------------------------------------------------------------
    # The core of the code. First, contract the state graph.
    #---------------------------------------------------------------------------
    print('Subdividing State:')
    states = [original_state]
    mappings = [None]
    while states[-1].n_tiles > config.max_start_tiles:
        state, mapping = states[-1].contract()
        states.append(state)
        mappings.append(mapping)
    states = states[::-1]
    mappings = mappings[::-1]
    #---------------------------------------------------------------------------
    # Second, Create an initial population that has populaiton equality.
    #---------------------------------------------------------------------------
    state = states[0]
    print(f"{'-'*80}\nCreating Initial Population:")
    seeds = np.array(feasible_seeds(state, config))
    #---------------------------------------------------------------------------
    # Create metrics and hypervolume-masks. Masks determine which metrics are
    # used for hypervolume.
    #---------------------------------------------------------------------------
    used_metrics = []  # list of funtions that return floats.
    used_constraints = []  # list of funtions that return binary.
    if feasinfeas:
        feas_mask = []  # Binary array if metric_i is used in feas.
        infeas_mask = []  # Binary array if metric_i is used in infeas.

    metrics_limits = np.array([metrics.limits[m] for m in config.metrics])
    for mi, name in enumerate(config.metrics):
        limit = metrics.limits[name]
        used_metrics.append(getattr(metrics, name))

        if limit < 1.0:
            used_constraints.append(
                partial(value_constraint, index=mi, threshold=limit))
        if feasinfeas:
            feas_mask.append(True)
            infeas_mask.append(
                limit < 1.0)  # If it has a limit it is a constraint.

    if config.equality_constraint > 0:
        used_constraints.append(
            partial(equality_constraint, threshold=config.equality_constraint))
        if feasinfeas:  # FI uses contraints as objectives in infeas population.
            used_metrics.append(
                partial(metrics.equality
                        ))  # Equality objective does not have a threshold.
            feas_mask.append(False)  # Only infeas population considers this.
            infeas_mask.append(True)

    if feasinfeas and config.novelty:  # add the flag for novelty last.
        infeas_mask.append(True)  # Infeas always uses novelty.
        feas_mask.append(config.feasinfeas_2N
                         )  # Feas pop only uses novelty if this flag set.

    ALG = NSGA2
    if config.NSGA3:
        from pymoo.factory import get_reference_directions
        #TODO: grid search nsga3 params.
        ref_dirs = get_reference_directions("das-dennis",
                                            len(hv_mask),
                                            n_partitions=50)
        ALG = partial(NSGA3, ref_dirs=ref_dirs)

    if feasinfeas:
        run_fi = partial(run_fi_optimization,
                         feas_mask=feas_mask,
                         infeas_mask=infeas_mask)
        assert len(feas_mask) == len(infeas_mask) == (len(used_metrics) +
                                                      bool(config.novelty))

    #---------------------------------------------------------------------------
    # Run a optimization process for each resolution using the previous
    # outputs as the seeds for the next.
    #---------------------------------------------------------------------------
    print('-' * 80 + '\n' + 'Starting Optimization:')
    hv_histories = []
    # pf_histories = []
    n_gens = config.n_gens // len(states)

    for opt_i, (state, mapping) in enumerate(zip(states, mappings)):
        print('-' * 80)
        print(f'Optimizing {opt_i} / {len(states)}')
        print(f'\tNum tiles: {state.n_tiles}')
        last_phase = opt_i == len(states) - 1
        OPT = run_fi if feasinfeas else run_optimization
        result, hv_history = OPT(ALG, state, used_metrics, used_constraints,
                                 seeds, config, n_gens, opt_i)
        hv_histories.append(hv_history)
        # pf_histories.append(pf_history)
        if config.out is not None:
            save_results(config, state, result, opt_i,
                         hv_history)  #, pf_history)
            plot_results(config, state, result, opt_i,
                         hv_history)  #, pf_history)
        if not last_phase:
            # seeds = upscale(result.X, mapping)
            seeds = upscale(result.pop.get('X'), mapping)
        print('Final HV %f' % hv_history[-1])

    return result.X.tolist(), result.F.tolist(), hv_histories
Пример #17
0
# START load_data

from pymoo.factory import get_problem, get_reference_directions

ref_dirs = get_reference_directions("uniform", 6, n_partitions=5)
F = get_problem("dtlz1").pareto_front(ref_dirs)
# END load_data

# START radviz
from pymoo.visualization.radviz import Radviz
Radviz().add(F).show()
# END radviz

# START radviz_custom
plot = Radviz(title="Optimization",
              legend=(True, {'loc': "upper left", 'bbox_to_anchor': (-0.1, 1.08, 0, 0)}),
              labels=["profit", "cost", "sustainability", "environment", "satisfaction", "time"],
              endpoint_style={"s": 70, "color": "green"})
plot.set_axis_style(color="black", alpha=1.0)
plot.add(F, color="grey", s=20)
plot.add(F[65], color="red", s=70, label="Solution A")
plot.add(F[72], color="blue", s=70, label="Solution B")
plot.show()
# END radviz_custom
Пример #18
0
# file related constants      
GEN = 100
NP = 100

# important methods -> ref_point
problem = "zdt6"
ref_point = np.array([1, 9.735527117321219])
   
if problem.startswith("zdt"):
    pf = get_problem(problem).pareto_front()
# Dtlz-5 to dtlz-7 have their own files related to their pareto fronts
elif problem == "dtlz5" or problem == "dtlz6" or problem == "dtlz7":
   pf = get_problem(problem, n_var=N_VAR,n_obj=N_OBJ).pareto_front()
else:
   ref_dirs = get_reference_directions("das-dennis", 3, n_partitions=12)
   pf = get_problem(problem, n_var=N_VAR,n_obj=N_OBJ).pareto_front(ref_dirs=ref_dirs)


metric = Hypervolume(pf=pf, ref_point=ref_point, normalize=True)

# dtlz-1 -> [482.1632866685836,479.911835933397,471.9731868733398]
# dtlz-2 -> [2.651414902010465,2.5206368624614965,2.656093434231162]
# dtlz-3 -> [1784.9822112456513,1683.7871520696372,1679.1459524987113]
# dtlz-4 -> [2.7493608245409247,2.665459302333755,2.691506519652278]
# dtlz-5 -> [2.6184046195044153,2.3154562025982375,2.490037232873547]
# dtlz-6 -> [10.460414515081052,10.523716498291654,10.571261523682367]
# dtlz-7 -> [1,1,24.464595045398383]

# zdt-1 -> [1, 5.687041127771669]
# zdt-2 -> [1, 6.71194298397789]
Пример #19
0
def main():
    # Define search algorithms
    algorithms = list()
    # 1: GA
    algorithm = GA(
        pop_size=config.POPULATION_SIZE,
        sampling=SudoRandomInitialization(),
        # crossover=AdaptiveSinglePointCrossover(prob=0.8),
        crossover=get_crossover("real_k_point", n_points=2),
        mutation=BitStringMutation(),
        eliminate_duplicates=RefactoringSequenceDuplicateElimination()
    )
    algorithms.append(algorithm)

    # 2: NSGA II
    algorithm = NSGA2(pop_size=config.POPULATION_SIZE,
                      sampling=SudoRandomInitialization(),
                      # crossover=AdaptiveSinglePointCrossover(prob=0.8),
                      crossover=get_crossover("real_k_point", n_points=2),
                      mutation=BitStringMutation(),
                      eliminate_duplicates=RefactoringSequenceDuplicateElimination()
                      )
    algorithms.append(algorithm)

    # 3: NSGA III
    # Todo: Ask for best practices in determining ref_dirs
    ref_dirs = get_reference_directions("energy", 8, 90, seed=1)
    algorithm = NSGA3(ref_dirs=ref_dirs,
                      pop_size=config.POPULATION_SIZE,
                      sampling=SudoRandomInitialization(),
                      # crossover=AdaptiveSinglePointCrossover(prob=0.8),
                      crossover=get_crossover("real_k_point", n_points=2),
                      mutation=BitStringMutation(),
                      eliminate_duplicates=RefactoringSequenceDuplicateElimination()
                      )
    algorithms.append(algorithm)

    # Define problems
    problems = list()
    problems.append(
        ProblemSingleObjective(n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND)
    )
    problems.append(
        ProblemMultiObjective(n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND)
    )
    problems.append(
        ProblemManyObjective(n_refactorings_lowerbound=config.LOWER_BAND, n_refactorings_upperbound=config.UPPER_BAND)
    )

    # Do optimization for various problems with various algorithms
    res = minimize(problem=problems[2],
                   algorithm=algorithms[2],
                   termination=('n_gen', config.MAX_ITERATIONS),
                   seed=1,
                   verbose=True)
    logger.info("** FINISHED **")

    logger.info("Best Individual:")
    logger.info(res.X)
    logger.info("Objective Values:")
    logger.info(res.F)

    logger.info("==================")
    logger.info("Other Solutions:")
    for ind in res.opt:
        logger.info(ind.X)
        logger.info(ind.F)
        logger.info("==================")

    logger.info(f"Start Time: {res.start_time}")
    logger.info(f"End Time: {res.end_time}")
    logger.info(f"Execution Time in Seconds: {res.exec_time}")
Пример #20
0
from pymoo.factory import get_problem, get_reference_directions
from pymoo.visualization.pcp import PCP

ref_dirs = get_reference_directions("das-dennis", 6, n_partitions=5) * [2, 4, 8, 16, 32, 64]
F = get_problem("dtlz1").pareto_front(ref_dirs)

PCP().add(F).show()
plot = PCP()
plot.set_axis_style(color="grey", alpha=0.5)
plot.add(F, color="grey", alpha=0.3)
plot.add(F[50], linewidth=5, color="red")
plot.add(F[75], linewidth=5, color="blue")
plot.show()

plot.reset()
plot.normalize_each_axis = False
plot.bounds = [[1, 1, 1, 2, 2, 5], [32, 32, 32, 32, 32, 32]]
plot.show()
Пример #21
0
def test_das_dennis():
    ref_dirs = get_reference_directions("das-dennis", 3, n_partitions=12)
    assert len(ref_dirs) == 91
Пример #22
0
from pymoo.algorithms.nsga3 import NSGA3
from pymoo.factory import get_reference_directions
from pymoo.optimize import minimize
from pymoo.problems.multi.sympart import SYMPART, SYMPARTRotated
from pymoo.visualization.scatter import Scatter
import matplotlib.pyplot as plt

for problem, name in zip([SYMPART(), SYMPARTRotated()],
                         ["SYM-PART", "SYM-PART rotated"]):

    ref_dirs = get_reference_directions("das-dennis",
                                        problem.n_obj,
                                        n_partitions=20)
    PS = problem.pareto_set(500)
    PF = problem.pareto_front(500)

    algorithm = NSGA3(ref_dirs=ref_dirs)

    res = minimize(problem, algorithm, ('n_gen', 500), seed=1, verbose=False)

    fig_name = f"{algorithm.__class__.__name__} on {name}"
    # visualize decision space
    plot = Scatter(title="Decision Space")
    plot.add(PS, s=10, color='r', label="PS")
    plot.add(res.X, s=30, color='b', label="Obtained solutions")
    plot.do()
    plt.legend()

    # visualize objective space
    plot = Scatter(title="Objective Space")
    plot.add(PF, s=10, color='r', label="PF")
Пример #23
0
                sampling_func,
                crossover_func, crossover_func_args,
                mutation_func, mutation_func_args,
            )
        )


    elif alg_name == "MOEAD":
        alg_specific_args = MOO_CONFIG["alg_specific_args"]["MOEAD"]
        n_neighbors = alg_specific_args["n_neighbors"]
        prob_neighbor_mating = alg_specific_args["prob_neighbor_mating"]
        #################
        # set algorithm #
        #################
        algorithm = MOEAD(
            ref_dirs=get_reference_directions(ref_dir_func,
                                              **ref_dir_func_args),
            n_neighbors=n_neighbors,
            prob_neighbor_mating=prob_neighbor_mating,
            crossover=get_crossover(crossover_func, **crossover_func_args),
            mutation=get_mutation(mutation_func, **mutation_func_args),
        )
        #####################
        # algorithm logging #
        #####################
        MOO_log(
            msg="algorithm = {}(\n"
            "ref_dirs = get_reference_directions({},{}),\n"
            "n_neighbors = {}\n"
            "prob_neighbor_mating = {}\n"
            "crossover=get_crossover({},{}),\n"
            "mutation=get_mutation({},{}),\n"
Пример #24
0
            else:
                hv = 0
            results.append((j[0], res, res.F, hv))
            hypervolume_scores[ind, i] = hv
            ind = ind + 1
    mean_hv = np.mean(hypervolume_scores, axis=1)
    std = np.std(hypervolume_scores, axis=1)
    output = []
    for i in range(len(problems)):
        output.append([problems[i][0], mean_hv[i], std[i]])
    path = 'results/' + algorithm.__module__ + '.csv'
    pd.DataFrame(np.array(output)).to_csv(path,
                                          header=('function', 'mean HV',
                                                  'std'),
                                          index=False)
    print("all runs:", hypervolume_scores)
    return (hypervolume_scores, print("Output written to " + path + '\n'))


problems = (('bnh', np.array([140, 50])), ('tnk', np.array([2, 2])),
            ('ctp1', np.array([1, 2])), ('zdt4', np.array([1, 260])),
            ('kursawe', np.array([-10,
                                  2])), ('welded_beam', np.array([350, 0.1])),
            ('carside', np.array([42, 4.5, 13])))

ref_dir = get_reference_directions("das-dennis", 2, n_partitions=10)
algorithms = (NSGA2(pop_size=10), MOEAD(ref_dir,
                                        n_neighbors=10), CTAEA(ref_dir))
for algorithm in algorithms:
    print("Minimizing functions with " + algorithm.__module__)
    compute_mean_HV(algorithm, problems, 50)
Пример #25
0
from pymoo.algorithms.moead import MOEAD
from pymoo.factory import get_problem, get_visualization, get_reference_directions
from pymoo.optimize import minimize

problem = get_problem("dtlz2")

algorithm = MOEAD(get_reference_directions("das-dennis", 3, n_partitions=12),
                  n_neighbors=15,
                  decomposition="pbi",
                  prob_neighbor_mating=0.7)

res = minimize(problem, algorithm, termination=('n_gen', 200))

get_visualization("scatter").add(res.F).show()
def main():
    # get argument values
    args = get_args()
    # get reference point
    if args.ref_point is None:
        args.ref_point = get_ref_point(args.problem, args.n_var, args.n_obj,
                                       args.n_init_sample)

    t0 = time()

    # set seed
    np.random.seed(args.seed)

    # build problem, get initial samples
    jsonFile = "/home/picarib/Desktop/cdnet/config/json/sbd_custom.json"
    configDirPath = "/home/picarib/Desktop/cdnet/config/sbd_custom/"
    dataPath = "/home/picarib/Desktop/cdnet/data/"

    config = loadJSON(jsonFile)
    interval = 1 if "custom" not in config["RequestModels"] else config[
        "RequestModels"]["custom"]["interval"]
    isLoadRTable = config["isLoadRTable"]
    isLoadSeparatorRank = config["isLoadSeparatorRank"]
    mode = config["RoutingMode"]  # [no-cache, no-color, tag-color, full-color]
    fileSize = config["FileSize"]
    runReqNums = config["RunReqNums"] if "RunReqNums" in config else -1
    warmUpReqNums = config["WarmUpReqNums"] if "WarmUpReqNums" in config else -1
    colorNums = config["colorNums"]
    separatorRankIncrement = config["separatorRankIncrement"]

    colorList = [
        ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
        for i in range(colorNums)
    ]

    topo = NetTopology(config, configDirPath, mode, warmUpReqNums, fileSize,
                       colorList)
    topo.build()

    extra_params = (topo, fileSize, mode, colorList, runReqNums, warmUpReqNums,
                    separatorRankIncrement)
    problem, X_init, Y_init = build_problem(args.problem,
                                            args.n_var,
                                            args.n_obj,
                                            args.n_init_sample,
                                            args.n_process,
                                            extra_params=extra_params)
    args.n_var, args.n_obj, args.algo = problem.n_var, problem.n_obj, 'moeda'

    # save arguments and setup logger
    save_args(args)
    logger = setup_logger(args)
    print(problem)

    # initialize evolutionary algorithm
    ref_dir = get_reference_directions("das-dennis", 2, n_partitions=15)
    args.batch_size = min(len(ref_dir), args.batch_size)
    # initialize population
    if args.pop_init_method == 'lhs':
        sampling = LatinHypercubeSampling()
    elif args.pop_init_method == 'nds':
        sorted_indices = NonDominatedSorting().do(Y_init)
        sampling = X_init[np.concatenate(sorted_indices)][:args.batch_size]
        if len(sampling) < args.batch_size:
            rest_sampling = lhs(X_init.shape[1],
                                args.batch_size - len(sampling))
            sampling = np.vstack([sampling, rest_sampling])
    elif args.pop_init_method == 'random':
        sampling = FloatRandomSampling()
    else:
        raise NotImplementedError

    ea_algorithm = MOEAD(ref_dir,
                         n_neighbors=args.batch_size,
                         sampling=sampling,
                         crossover=get_crossover("int_one_point"),
                         mutation=get_mutation("int_pm", prob=1.0 / 13),
                         decomposition="pbi",
                         seed=1)

    # initialize data exporter
    exporter = DataExport(X_init, Y_init, args)

    # find Pareto front
    res = minimize(problem,
                   ea_algorithm, ('n_gen', args.n_iter),
                   save_history=True)
    X_history = np.array([algo.pop.get('X') for algo in res.history])
    Y_history = np.array([algo.pop.get('F') for algo in res.history])

    # update data exporter
    for X_next, Y_next in zip(X_history, Y_history):
        exporter.update(X_next, Y_next)

    # export all result to csv
    exporter.write_csvs()

    # statistics
    final_hv = calc_hypervolume(exporter.Y, exporter.ref_point)
    print('========== Result ==========')
    print('Total runtime: %.2fs' % (time() - t0))
    print('Total evaluations: %d, hypervolume: %.4f\n' %
          (args.batch_size * args.n_iter, final_hv))

    # close logger
    if logger is not None:
        logger.close()
Пример #27
0
            af = AirfoilFFD()

        # Setup problem
        class OptProblem(Problem):
            def __init__(self, n_var, xl, xu):
                super().__init__(n_var=n_var, n_obj=2, xl=xl, xu=xu)

            def _evaluate(self, X, out, *args, **kwargs):
                out["F"] = af.obj_func(X)

        problem = OptProblem(n_var=af.dim,
                             xl=af.bounds[:, 0],
                             xu=af.bounds[:, 1])

        algorithm = CTAEA(ref_dirs=get_reference_directions("das-dennis",
                                                            2,
                                                            n_partitions=14),
                          seed=None)

        termination = get_termination("n_gen", 11)

        print('Run EA ...')
        res = minimize(problem,
                       algorithm,
                       termination,
                       seed=None,
                       save_history=True,
                       verbose=True)

        run_time = time.time() - t0
        print('Wall time: {:.1f}s'.format(run_time))
Пример #28
0
# START ctaea
from pymoo.algorithms.ctaea import CTAEA
from pymoo.factory import get_problem, get_reference_directions
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter

problem = get_problem("DASCMOP1", 2)

# create the reference directions to be used for the optimization
ref_dirs = get_reference_directions("das-dennis", problem.n_obj, n_points=91)

# create the algorithm object
algorithm = CTAEA(ref_dirs=ref_dirs)

# execute the optimization
res = minimize(problem, algorithm, ('n_gen', 600), seed=1, verbose=True)

plot = Scatter()
plot.add(problem.pareto_front(), plot_type="line", color="black", alpha=0.7)
plot.add(res.F, color="red")
plot.show()
# END ctaea

# START carside
problem = get_problem("carside")
ref_dirs = get_reference_directions("das-dennis", problem.n_obj, n_points=91)
algorithm = CTAEA(ref_dirs=ref_dirs)

res = minimize(problem, algorithm, ('n_gen', 600), seed=1, verbose=True)

Scatter().add(res.F).show()
Пример #29
0
def test_das_dennis_not_achievable_points():
    get_reference_directions("das-dennis", 3, n_points=92)
Пример #30
0
def calc_pareto_front(problem, ref_dirs):
    n_pareto_points = 200
    np.random.seed(1)

    pf = problem.pareto_front(n_pareto_points=n_pareto_points, use_cache=False)
    # survival = ReferenceDirectionSurvival(ref_dirs)
    survival = RankAndCrowdingSurvival()

    for i in range(1000):
        _pf = problem.pareto_front(n_pareto_points=n_pareto_points,
                                   use_cache=False)
        F = np.row_stack([pf, _pf])

        pop = Population().new("F", F)
        pop = survival.do(problem, pop, n_pareto_points // 2)

        pf = pop.get("F")

    return pf


if __name__ == '__main__':

    ref_dirs = get_reference_directions("das-dennis", 3, n_points=91)
    F = calc_pareto_front(WFG3(6, 3), ref_dirs)

    Scatter().add(F).show()

    for problem in [WFG1, WFG2, WFG3, WFG4, WFG5, WFG6, WFG7, WFG8, WFG9]:
        print("")