0

I am working on a parallelized optimization problem in PyGMO. Unfortunately the docs are not very helpful. According to these guidelines, I am riting my problem as

import PyGMO as pygmo
class my_problem(pygmo.base):
    def __init__(self,model,config,pars,**kwargs):
        # Does some parameter definition according to input arguments model, config etc...
        ...

        # Invoke base class as required by PyGMO
        super(my_problem,self).__init__(self.__dim)

    def _objfun_impl(self,x):
        ...
        f = ...  # Cost function to optimize
        return (f,)

# Main
model = 'ei'
config = 'x1'
args = (...)
prob = my_problem(model,config,args)
algo = pygmo.algorithm.de(gen=20)
isl = pygmo.island(algo,prob,20)
print isl.population.champion.f
isl.evolve(10)
print isl.population.champion.f

This does not work and produce the following error:

File     "/home/maurizio/Dropbox/Stability_Analysis_network/mymain.py", line 643, in main_routine
isl = pygmo.island(algo,prob,20)
File "/usr/lib/python2.7/site-packages/PyGMO/core/__init__.py", line 239, in island
return _generic_island_ctor(None, *args, **kwargs)
File "/usr/lib/python2.7/site-packages/PyGMO/core/__init__.py", line 132, in _generic_island_ctor
return py_island(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/PyGMO/core/__init__.py", line 119, in _generic_island_ctor
super(type(self), self).__init__(*ctor_args)
File "/usr/lib/python2.7/site-packages/PyGMO/core/__init__.py", line 48, in __init__
_core._base_island.__init__(self, *args)
File "/usr/lib/python2.7/site-packages/PyGMO/problem/_base.py", line 36, in __get_deepcopy__
return deepcopy(self)
File "/usr/lib64/python2.7/copy.py", line 190, in deepcopy
y = _reconstruct(x, rv, 1, memo)
File "/usr/lib64/python2.7/copy.py", line 329, in _reconstruct
y = callable(*args)
TypeError: __init__() takes exactly 4 arguments (1 given)

Any idea to what __init__ is referring to and what parameters are missing? I suspect that it is a problem with my class definition.

maurizio
  • 745
  • 1
  • 7
  • 25
  • I don't see you calling your class in the code... – alec_djinn Jan 16 '17 at 10:07
  • Sorry, I am editing that... The post was a generalization. – maurizio Jan 16 '17 at 10:12
  • @alec_djinn I suspect it is a problem with `super` call and the base class `__init__` ... – maurizio Jan 16 '17 at 10:34
  • I think you need to pass `my_problem` to super `super(my_problem,self).__init__(dim)` – alec_djinn Jan 16 '17 at 10:41
  • @alec_djinn Sorry that was another typo. The code does so... The problem seems rather that I am trying to use `__init__` from `base` which is different in call from the one defined by `my_problem`... so the question is rather how to inherit `__init__` from `base` but doing some other stuff meanwhile... – maurizio Jan 16 '17 at 10:44
  • still, you are passing self.dim to init, instead, try passing just dim as in my previous example, of course, you have to add dim to your __init__ definition. – alec_djinn Jan 16 '17 at 10:55
  • @alec_djinn Nope. It does not solve the issue. In fact the error is now `TypeError: __init__() takes exactly 5 arguments (1 given)` – maurizio Jan 16 '17 at 11:10
  • Now you are sure that that is the init you are misscalling... what about passing also model,config,pars,**kwargs to it? – alec_djinn Jan 16 '17 at 11:12
  • @alec_djinn I don't think I can: If I understood the `super(my_problem.self).__init__(...)` correctly, I am making my `my_problem` class use the `__init__` method from `pygmo.problem.base` class, which has its own fixed parameters... which are not mine... – maurizio Jan 16 '17 at 11:17
  • but you have refactored that init in your class... check here https://esa.github.io/pygmo/tutorials/adding_a_new_algorithm.html, basically the same example you have, he did not passed iter because in his __init__ there is a default value of 10 – alec_djinn Jan 16 '17 at 12:37
  • @alec_djinn so you are saying that if I set some defaults I avoid the problem? The error actually changes in this way and it gives a stream error. I can't follow up right now as I'm off my pc... but please let's touch base in the next hours or tomorrow as soon as I'm back on my code. – maurizio Jan 16 '17 at 14:23

1 Answers1

0

The issue is caused by the mismatch between input arguments of my_problem.__init__(...) (i.e. the Child class) and the base.__init__ (i.e. the Parent class). If defaults for these parameters are not provided then a conflict results by the inheritance of __init__ by super(my_problem,self) from base. In practice, a corrected working version is:

import PyGMO as pygmo
class my_problem(pygmo.base):
    def __init__(self,model='ei',config='conf1',pars=[1]*20):
        # Does some parameter definition according to input arguments model, config etc...
    ...

    self.__dim = 3
    ...

    # Invoke base class as required by PyGMO
    super(my_problem,self).__init__(self.__dim)

    def _objfun_impl(self,x):
        ...
        f = ...  # Cost function to optimize
        return (f,)

# Main
...

It is not possible instead to pass **kwargs to the child class insofar as base is hard coded and should be changed accordingly to this post.

Community
  • 1
  • 1
maurizio
  • 745
  • 1
  • 7
  • 25