我正在PyGMO
处理并行优化问题。不幸的是,文档不是很有帮助。根据{{3}},我正在讨论我的问题
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
这不起作用并产生以下错误:
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)
对__init__
指的是什么以及缺少哪些参数有任何想法?我怀疑这是我班级定义的问题。
答案 0 :(得分:0)
问题是由my_problem.__init__(...)
(即Child类)和base.__init__
(即Parent类)的输入参数之间的不匹配引起的。如果未提供这些参数的默认值,则__init__
从super(my_problem,self)
继承base
会产生冲突。在实践中,更正的工作版本是:
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
...
如果**kwargs
是硬编码的,则无法将base
传递给子类,因此应相应地更改为this post。