最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Solution to syntax problem in using "differential_evolution()" in scipy - Stack Overflow

programmeradmin3浏览0评论

I am trying to minimise a complex problem (non-linear) with differential_evolution method in scipy. Key syntaxes are given below

a) I am creating an object assigned to differential_evolution

res = differential_evolution(self.loss_function,x0,bounds,args=(input_series, ilt,bhpgrad,q_obs),  streategy='belst1bin',  maxiter=1000,   popsize=15, disp =True,  tol=1e-6  )

b) bounds are provided as tuples in the following form, the len(bounds) == len(x)

bounds = [ (0,1000), (0,1000),........ , (-1000,1000)]

depending on the nature of corresponding elements in x.

c) loss-function is part of a class, the definition of the self is accomplished through def __init__().

def loss_function(self, x,        input_series,ilt,bhpgrad, q_obs):

Note that there are 4 constants which shall not be optimized by differential_evolutions, but needs to execute them. These constants are being passed through args = (.......) method.

I get the following error:

res = differential_evolution(self.loss_function,x0,bounds,args=(input_series, ilt,bhpgrad,q_obs),  \
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: differential_evolution() got multiple values for argument 'args'

I don't understand why this is happening or how to fix it.

I am trying to minimise a complex problem (non-linear) with differential_evolution method in scipy. Key syntaxes are given below

a) I am creating an object assigned to differential_evolution

res = differential_evolution(self.loss_function,x0,bounds,args=(input_series, ilt,bhpgrad,q_obs),  streategy='belst1bin',  maxiter=1000,   popsize=15, disp =True,  tol=1e-6  )

b) bounds are provided as tuples in the following form, the len(bounds) == len(x)

bounds = [ (0,1000), (0,1000),........ , (-1000,1000)]

depending on the nature of corresponding elements in x.

c) loss-function is part of a class, the definition of the self is accomplished through def __init__().

def loss_function(self, x,        input_series,ilt,bhpgrad, q_obs):

Note that there are 4 constants which shall not be optimized by differential_evolutions, but needs to execute them. These constants are being passed through args = (.......) method.

I get the following error:

res = differential_evolution(self.loss_function,x0,bounds,args=(input_series, ilt,bhpgrad,q_obs),  \
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: differential_evolution() got multiple values for argument 'args'

I don't understand why this is happening or how to fix it.

Share Improve this question edited Mar 23 at 14:20 Nick ODell 25.8k7 gold badges46 silver badges88 bronze badges asked Mar 23 at 7:47 MuraliMurali 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Per the documentation, the argument list is as follows:

differential_evolution(func, bounds, args=(), strategy='best1bin', maxiter=1000, popsize=15, tol=0.01, mutation=(0.5, 1), recombination=0.7, rng=None, callback=None, disp=False, polish=True, init='latinhypercube', atol=0, updating='immediate', workers=1, constraints=(), x0=None, *, integrality=None, vectorized=False)

So, if you pass positional arguments, the first one will be func, the second bounds, the third args, etc. You pass 3 positional arguments before switching to keyword arguments, so scipy is interpreting x0 as bounds and bounds as your args, and then you have a keyword argument for args so scipy thinks you're passing it twice. The simple fix is to move x0 later in your argument list and pass it as a keyword argument. Or, just use keyword arguments for everything.

发布评论

评论列表(0)

  1. 暂无评论