8000 DEA on custom model with cost parametrized function · Issue #192 · esa/pagmo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.
This repository was archived by the owner on Jul 16, 2024. It is now read-only.
DEA on custom model with cost parametrized function #192
Open
@mdepitta

Description

I am trying to use differential evolution for a problem where the function to minimize is of the form f(x,p) where x is the vector of constrained variables to change during evolution to optimize f, and p is a vector of parameters.

I need to perform optimization of "f" for different parameter vectors p1,..., pN.

There is a problem however in passing every time different parameter vectors.
the class definition of my problem specify some defaults for "p". Say p=p1.
If I run a for-loop over different parameter vectors, it turns out that p_i -- the parameter vector at the i-th step of the loop -- is passed only initially to the problem, but then at successive (internal) calls of my problem class by island.evolve, p returns to be p1, that is the default.

Take for example:

import PyGMO as pygmo

class my_problem(pygmo.problem.base):
    def __init__(self, par1 = 10.):
        self.__dim = 10
        self.p1 = par1
        print self.p1
        super(my_problem,self).__init__(self.__dim)
        self.set_bounds(-5.12,5.12)

    def _objfun_impl(self,x):
        f = 0
        for i in range(self.__dim):
            f = f + (x[i]/self.p1)*(x[i])
        return (f,)
if __name__=="__main__":
    for i in xrange(2):
        print i
        prob = my_problem(i)
        algo = pygmo.algorithm.bee_colony(gen=500)
        isl = pygmo.island(algo,prob,20)
        isl.evolve(1)
        isl.join()
        print isl.population.champion.f
        print isl.population.champion.x

You will see that every time "i" is passed to the problem correctly, but then internally to "my_problem" p1 is restored to 10.

The only (ugly) work around that I found so far, is to renew class definition and its defaults within the for-loop.

if __name__=="__main__":
    for i in xrange(2):
        class my_problem(pygmo.problem.base):
            def __init__(self, par1 = i):
                    self.__dim = 10
                    self.p1 = par1
                    print self.p1
                    super(my_problem,self).__init__(self.__dim)
                    self.set_bounds(-5.12,5.12)

            def _objfun_impl(self,x):
                f = 0
                for i in range(self.__dim):
                    f = f + (x[i]/(1+self.p1))*(x[i])
                return (f,)

        print i
        prob = my_problem(i)
        algo = pygmo.algorithm.bee_colony(gen=500)
        isl = pygmo.island(algo,prob,20)
        isl.evolve(1)
        isl.join()
        print isl.population.champion.f
        print isl.population.champion.x

Is it really not possibly to pass time-to-time parameters to my function?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0