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

Changing population when using decorator meta-problem #144

Open
xand-stapleton opened this issue Sep 27, 2023 · 2 comments
Open

Changing population when using decorator meta-problem #144

xand-stapleton opened this issue Sep 27, 2023 · 2 comments

Comments

@xand-stapleton
Copy link

xand-stapleton commented Sep 27, 2023

(This should be under the feature-request/help labels, I'm just not sure how to add them)

I have written code that changes a problem using a decorator, but I'd also like to change the problem in an instantiated population. Is there currently a way to do this? For example, given some population with an associated problem at initialisation, is there a way to update the problem to the new decorated one without creating a new population? I know this is potentially very un-pygmonic (against the way PyGMO is designed), but I think it could be useful for some upcoming projects.

E.g.

import pygmo as pg

# Define a dummy decorated problem
def f_decor(orig_fitness_function):
    def new_fitness_function(self, dv):
        import time
        start = time.monotonic()
        fitness = orig_fitness_function(self, dv)
        print("Elapsed time: {} seconds".format(time.monotonic() - start))
        return fitness
    return new_fitness_function


# The original problem
prob = pg.problem(pg.rosenbrock(dim = 10))

# The initial population
pop = pg.population(prob, size = 20)

# The algorithm
algo = pg.algorithm(pg.sade(gen = 1000))

# Evolve the population
pop = algo.evolve(pop)

new_prob =  pg.problem(pg.decorator_problem(rb, fitness_decorator=f_decor))

#<what I want to do>
pop = pop.change_problem(new_prob)

Any help is much appreciated!

@bluescarni
Copy link
Member

Hi @xand-stapleton

You cannot replace a problem in a population, as it is supposed to be an "immutable" property of the population.

What you can do is to change the internal state of a problem. E.g., if the UDP stored in a population has a data member, you can alter that data member - that does not count as "changing the problem". I am not sure whether this helps in your situation though...

@xand-stapleton
Copy link
Author

xand-stapleton commented Sep 28, 2023

Hi @bluescarni, thanks for your reply!

I'm slightly confused by what you mean by the UDP's data member. So for example, say I have a population defined with some associated UDP, can I change attributes of the UDP between evolutions? For example, given some UDP that depends on a variable self.myvar, could I instantiate the population with the some seed in the fitness function, then update it manually after some number of evolutions (i.e. is this what you mean by changing its data members)? (Just to be clear I'm not talking about a multi-objective function here or any optimisation of self.myvar, I just want to be able to 'perturb' the problem after solving).

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

No branches or pull requests

2 participants