Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

python - Passing args in scipy optimize.minimize objective function ( getting error on number of arguments)

I'm trying to use scipy's optimizer.minimize function but I'm unable to figure out exact way to pass args to objective function. I have following code which according to me should work fine but is giving me error on number of arguments.

result = minimize(compute_cost, x0, args=(parameter), method='COBYLA',constraints=cons, options={'maxiter':10000,'rhobeg':20})

Here is the function signature to objective function: def compute_cost(x,parameter)

parameter is a dict that has 51 key value pair.

This gives following error:

capi_return is NULL Call-back cb_calcfc_in__cobyla__user__routines failed. Traceback (most recent call last): File "C:.. esource_optimizer.py", line 138, in <module> result = minimize(compute_cost, x0, args=(parameter), method='COBYLA',constraints=cons, options={'maxiter':10000,'rhobeg':20}) File "C:Python27libsite-packagesscipyoptimize\_minimize.py", line 432, in minimize return _minimize_cobyla(fun, x0, args, constraints, **options) File "C:Python27libsite-packagesscipyoptimizecobyla.py", line 246, in _minimize_cobyla dinfo=info) File "C:Python27libsite-packagesscipyoptimizecobyla.py", line 238, in calcfc f = fun(x, *args) TypeError: compute_cost() takes exactly 2 arguments (52 given)

Can someone help me figure this out.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change args=(parameter) to args=(parameter,), so args is a tuple containing a single element.

args=(parameter) is equivalent to args=parameter. When you do that, each element of parameter is passed as a separate argument to your objective function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...