"scipy.optimize" with XFunc or Proxy?

Is it possible to use Xfunc or Proxy to run scipy.optimize.minimize? (I am trying to combine Karamba results with an optimization algorithm in Python).

I gave the below code a try, and it gives a serialization error, no matter what opt_func I pick!

from compas.utilities import XFunc
sp_min = XFunc (‘spciy.optimize.minimize’, serializer=‘json’)
array = XFunc (‘numpy.array’, serializer=‘json’)

def opt_func(x):
“”“The Rosenbrock function”“”
return sum(100.0*(x[1:]-x[:-1]**2.0)**2.0 + (1-x[:-1])**2.0)

x0 = array([1.3, 0.7, 0.8, 1.9, 1.2])
res = sp_min(opt_func, x0, method=‘nelder-mead’, options={‘xatol’: 1e-8, ‘disp’: True})

Message: <function rosen at 0x0000000000000178> is not JSON serializable

Traceback:
line 442, in _iterencode, “C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib\json\encoder.py”
line 332, in _iterencode_list, “C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib\json\encoder.py”
line 408, in _iterencode_dict, “C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib\json\encoder.py”
line 434, in iterencode, “C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib\json\encoder.py”
line 184, in default, “C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib\json\encoder.py”
line 147, in default, “C:\Users\msalma\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts\compas\data\encoders.py”
line 189, in dump, "C:\Program Files\Rhino 7\Plug-ins\IronPython\Lib\json_init
.py"
line 346, in call, “C:\Users\msalma\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts\compas\utilities\xfunc.py”
line 25, in , “C:\Users\msalma\AppData\Local\Temp\TempScript.py”

first of all, i would not use XFunc for any of this. it is no longer actively supported and will be removed. please use Proxy

the problem is that you are trying to serialize a function to JSON to send it over the RPC connection, which is not supported.

to make this work you could try the procedure that is described here in the tutorial for remote procedure calls.