Beispiel #1
0
 def island_run(self):
     algo = pg.algorithm(pg.pso(gen=self.generation))
     pop = pg.population(self.problem, self.pop_size)
     island = pg.island(algo=algo, pop=pop, udi=pg.mp_island())
     island.evolve()
     island.wait()
     pop = island.get_population()
     return pop.champion_f, pop.champion_x
Beispiel #2
0
def test_pygmo_single_generation(two_reservoir_wrapper):
    """ Simple pygmo wrapper test. """
    wrapper = two_reservoir_wrapper
    prob = pg.problem(wrapper)
    algo = pg.algorithm(pg.moead(gen=1))

    pg.mp_island.init_pool(2)
    isl = pg.island(algo=algo, prob=prob, size=50, udi=pg.mp_island())
    isl.evolve(1)
 def addIsland(arc, algo):
     isl = pg.island(
         udi=pg.mp_island(),
         algo=algo,
         prob=prob,
         #b=pg.mp_bfe(),
         b=pg.bfe(udbfe=multi_bfre2()),
         size=16)
     arc.push_back(isl)
Beispiel #4
0
def get_island(evaluate, params, hooks):
    # config
    # D = 8  decision space dimension
    # N0 = 100  initial population
    # Ng = 100  total generation
    D, Ng, N0 = itemgetter('D', 'Ng', 'N0')(params)

    algo = pg.algorithm(pg.de(gen=Ng))
    algo.set_verbosity(int(Ng / 10))
    prob = pg.problem(evaluate_wrapper(D, evaluate))
    island = pg.island(algo=algo, prob=prob, size=N0, udi=pg.mp_island())

    return island
Beispiel #5
0
def benchmark_differential_evolution_no_pool(generations):
    island = pg_island(
        algo=de(gen=generations),
        prob=B2_UDP(getLowerBound(),getUpperBound(),'../../../../../sbml/b2.xml'),
        size=10,
        udi=mp_island(use_pool=False))

    N = 50
    import arrow
    time_start = arrow.utcnow()
    print('Differential Evolution (no pool, pop. size {})'.format(island.get_population().get_f().size))
    for k in range(N):
        island.evolve()
        island.wait()
        delta_t = arrow.utcnow() - time_start
        print('DE {:2}/{}: best fitness {:9.2f}, fevals {}, duration {}'.format(
            k,N,float(island.get_population().champion_f[0]),
            island.get_population().problem.get_fevals(),
            delta_t))
Beispiel #6
0
        GA_based_optimize(pm,
                          cm,
                          prob_var.left_ind,
                          prob_var.mid_ind,
                          prob_var.right_ind,
                          gen=max_gen,
                          seed=rnd_seed,
                          log_lvl=log_lvl))
    # Generating isl_num initial populations
    pop_lst = [pg.population(prob) for _ in range(isl_num)]
    [[p.push_back(next(GenerateIndividual)) for p in pop_lst]
     for _ in range(pop_size)]
    # Generating pm_lst × cm_lst algorithms with different crossover and
    # mutation probability
    alg_lst = [
        pg.algorithm(
            GA_based_optimize(p,
                              c,
                              prob_var.left_ind,
                              prob_var.mid_ind,
                              prob_var.right_ind,
                              gen=max_gen,
                              seed=rnd_seed,
                              log_lvl=log_lvl)) for c in cm_lst for p in pm_lst
    ]
    # Creating islands
    islands = [
        pg.island(algo=a, pop=p, udi=pg.mp_island())
        for a, p in zip(alg_lst, pop_lst)
    ]
Beispiel #7
0
import pygmo as pg
"""
Okay, so the lesson learned here is: not sure :-D
"""
if __name__ == "__main__":
    prob = pg.problem(pg.rosenbrock(dim=5))
    pool_creator = pg.mp_island()
    # pool_creator.resize_pool(1)
    pool_creator.init_pool(1)
    island = pg.island(udi=pool_creator,
                       algo=pg.sga(gen=200),
                       pop=pg.population(prob, 200))
    island.evolve()
    island.wait()
    print("island: ***** \n", island)
Beispiel #8
0
def pygmo_main(harmonic=False):
    import pygmo as pg
    from pywr.optimisation.pygmo import PygmoWrapper

    def update_archive(pop, archive=None):
        if archive is None:
            combined_f = pop.get_f()
        else:
            combined_f = np.r_[archive, pop.get_f()]
        indices = pg.select_best_N_mo(combined_f, 50)
        new_archive = combined_f[indices, :]
        new_archive = np.unique(new_archive.round(4), axis=0)
        return new_archive

    wrapper = PygmoWrapper(get_model_data(harmonic=harmonic))
    prob = pg.problem(wrapper)
    print(prob)
    algo = pg.algorithm(pg.moead(gen=1))
    # algo = pg.algorithm(pg.nsga2(gen=1))

    pg.mp_island.init_pool(2)
    isl = pg.island(algo=algo, prob=prob, size=50, udi=pg.mp_island())

    ref_point = [216500, 4000]
    pop = isl.get_population()

    print("Evolving!")
    archive = update_archive(pop)
    hv = pg.hypervolume(archive)
    vol = hv.compute(ref_point)
    hvs = [
        vol,
    ]
    print(
        "Gen: {:03d}, Hypervolume: {:6.4g}, Archive size: {:d}".format(
            0, vol, len(archive)
        )
    )

    for gen in range(20):
        isl.evolve(1)
        isl.wait_check()

        pop = isl.get_population()
        archive = update_archive(pop, archive)

        hv = pg.hypervolume(archive)
        vol = hv.compute(ref_point)
        print(
            "Gen: {:03d}, Hypervolume: {:6.4g}, Archive size: {:d}".format(
                gen + 1, vol, len(archive)
            )
        )
        hvs.append(vol)

    hvs = pd.Series(hvs)

    print("Finished!")

    plt.scatter(archive[:, 0], archive[:, 1])

    objectives = wrapper.model_objectives
    plt.xlabel(objectives[0].name)
    plt.ylabel(objectives[1].name)
    plt.grid(True)
    title = "Harmonic Control Curve" if harmonic else "Monthly Control Curve"
    plt.savefig("{} Example ({}).pdf".format("pygmo", title), format="pdf")

    fig, ax = plt.subplots()
    ax.plot(hvs / 1e6, marker="o")
    ax.grid(True)
    ax.set_ylabel("Hypervolume")
    ax.set_xlabel("Generation")
    plt.savefig("{} Example Hypervolume ({}).pdf".format("pygmo", title), format="pdf")

    plt.show()
    def create_archipelago(
        unknowns: list,
        optimizers: list,
        optimizers_kwargs: list,
        pg_problem: pygmo.problem,
        rel_pop_size: float,
        archipelago_kwargs: dict,
        log_each_nth_gen: int,
        report_level: int,
    ) -> PyfoombArchipelago:
        """
        Helper method for parallelized estimation using the generalized island model.
        Creates the archipelago object for running several rounds of evolutions.

        Arguments
        ---------
            unknowns : list
                The unknowns, sorted alphabetically and case-insensitive. 
            optimizers : list
                A list of optimizers to be used on individual islands. 
            optimizers_kwargs : list
                A list of corresponding kwargs.
            pg_problem : pygmo.problem
                An pygmo problem instance.
            archipelago_kwargs : dict
                Additional kwargs for archipelago creation.
            log_each_nth_gen : int
                Specifies at which each n-th generation the algorithm stores logs. 
            report_level : int
                Prints information on the archipelago creation for values >= 1.

        Returns
        -------
            archipelago : PyfoombArchipelago
        """

        _cpus = joblib.cpu_count()

        # There is one optimizer with a set of kwargs
        if len(optimizers) == 1 and len(optimizers_kwargs) == 1:
            optimizers = optimizers * _cpus
            optimizers_kwargs = optimizers_kwargs * _cpus
        # Several optimizers with the same kwargs
        elif len(optimizers) > 1 and len(optimizers_kwargs) == 1:
            optimizers_kwargs = optimizers_kwargs * len(optimizers)
        # Several kwargs for the same optimizer
        elif len(optimizers) == 1 and len(optimizers_kwargs) > 1:
            optimizers = optimizers * len(optimizers_kwargs)
        elif len(optimizers) != len(optimizers_kwargs):
            raise ValueError(
                'Number of optimizers does not match number of corresponding kwarg dicts'
            )

        # Get the optimizer intances
        algos = [
            PygmoOptimizers.get_optimizer_algo_instance(
                name=_optimizers, kwargs=_optimizers_kwargs) for _optimizers,
            _optimizers_kwargs in zip(optimizers, optimizers_kwargs)
        ]

        # Update number of islands
        n_islands = len(algos)

        if report_level >= 1:
            print(
                f'Creating archipelago with {n_islands} islands. May take some time...'
            )

        pop_size = int(numpy.ceil(rel_pop_size * len(unknowns)))
        prop_create_args = ((pg_problem, pop_size,
                             seed * numpy.random.randint(0, 1e4))
                            for seed, pop_size in enumerate([pop_size] *
                                                            n_islands))
        try:
            parallel_verbose = 0 if report_level == 0 else 1
            with joblib.parallel_backend('loky', n_jobs=n_islands):
                pops = joblib.Parallel(verbose=parallel_verbose)(map(
                    joblib.delayed(
                        ArchipelagoHelpers.parallel_create_population),
                    prop_create_args))
        except Exception as ex:
            print(
                f'Parallelized archipelago creation failed, falling back to sequential\n{ex}'
            )
            pops = (
                ArchipelagoHelpers.parallel_create_population(prop_create_arg)
                for prop_create_arg in prop_create_args)

        # Now create the empyty archipelago
        if not 't' in archipelago_kwargs.keys():
            archipelago_kwargs['t'] = pygmo.fully_connected()
        archi = PyfoombArchipelago(**archipelago_kwargs)
        archi.set_migrant_handling(pygmo.migrant_handling.preserve)

        # Add the populations to the archipelago and wait for its construction
        with contextlib.redirect_stdout(io.StringIO()):
            for _pop, _algo in zip(pops, algos):
                if log_each_nth_gen is not None:
                    _algo.set_verbosity(int(log_each_nth_gen))
                _island = pygmo.island(algo=_algo,
                                       pop=_pop,
                                       udi=pygmo.mp_island())
                archi.push_back(_island)
        archi.wait_check()

        return archi