Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] save&load (e.g., pickle) population snapshot easily based on certain criteria #91

Open
oni73 opened this issue Feb 12, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@oni73
Copy link

oni73 commented Feb 12, 2022

Hi,

i couldnt find any example to be able to save&load the population during evolving.

For example, something like this would be really useful:

pseudo code:

for i in range(n_iterations):
    pop =  evolve(pop)
    if pop.champion_f < best_f:
         save_snapshot(pop, i)

I wonder how / if this wold be easily possible with pygmo. Any example would be highly appreciated!

P.S: Would this way of evolving break any adaptive DE variants ?
Many Thanks

@oni73 oni73 added the enhancement New feature or request label Feb 12, 2022
@bluescarni
Copy link
Member

@oni73 apologies for the delay in the reply.

As long as you are using directly the algorithm API (as you show in your example), manually pickling after every evolve based on whatever criterion you like should "just work".

Are you asking about a higher-level API? I am not sure about the added value of a higher-level facility with respect to your code snippet, but I am open to suggestions.

@oni73
Copy link
Author

oni73 commented May 6, 2022

Hi @bluescarni ,

many thanks for your reply. Indeed, i can confirm that the proposed approach works and this way we have quite fine-grained control over the evolution process....

what i ended up using is the following:

prob=pygmo.problem()
n_iterations=100
popsize=40
algo = pygmo.algorithm(pygmo.de1220(gen=1))
pop = pygmo.population(prob, popsize)
for i in range(n_iterations):
    pop = algo.evolve(pop)
    if pop.champion_f < best_f:
        best_f = pop.champion_f
        save_population(pop, 'experiment-1', i, best_f)

where save_population and load_population are defined as:

    def save_population(pop, exp_name, iteration: int, bestf: float):
        pop_snapshot_fname = exp_name + '/' + 'population-' + str(iteration) + '-' + str(bestf)
        np.savez_compressed(pop_snapshot_fname, x=pop.get_x(), fitnesses=pop.get_f())

    def load_population(pop_snapshot_path: str, prob, popsize: int):
        print('loading population snapshot', pop_snapshot_path, '...')
        xfs = np.load(pop_snapshot_path)

        pop = pygmo.population(prob=prob)

        for i in range(popsize):
            pop.push_back(x=xfs['x'][i], f=xfs['fitnesses'][i])
        print('done.')

        return pop

IMO, it would be beneficial to provide library support for such functionality.

@EveCharbie
Copy link

Hi :)
Thank you @oni73 for sharing the code.
I have a large problem to run (weeks or months of computation).
I am concern that if something happens and the code gets interrupted (ex we loose power), I will loose all the progress made.
From the code above, I see that each evolution of the population can be saved.
So I can load the results form the last population evolution before interruption.
My question (@bluescarni) is : Is it possible to warm start a problem? I would provide the starting point and the optimization would resume from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants