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:\..\resource_optimizer.py", line 138, in <module>
result = minimize(compute_cost, x0, args=(parameter), method='COBYLA',constraints=cons, options={'maxiter':10000,'rhobeg':20})
File "C:\Python27\lib\site-packages\scipy\optimize\_minimize.py", line 432, in minimize
return _minimize_cobyla(fun, x0, args, constraints, **options)
File "C:\Python27\lib\site-packages\scipy\optimize\cobyla.py", line 246, in _minimize_cobyla
dinfo=info)
File "C:\Python27\lib\site-packages\scipy\optimize\cobyla.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.
The answer is yes.
NumPy/SciPy's functions are usually optimized for multithreading. Did you look at your CPU utilization to confirm that only one core is being used while the simulation is being ran? Otherwise you have nothing to gain from running multiple instances.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With