1

I'm trying to use the scipy.optimize.differential_evolution (python) from MATLAB environment.

So far, I am able to call the differential_evolution function. The only problem is that it apprently cannot receive MATLAB function handles as an argument.

I get the following error:

Error using py.scipy.optimize.differential_evolution Handle to MATLAB function '@(x)x(1).^2.*x(2).^2' is not supported. Use a handle to a Python function.

Is there some neat way or function to "convert" a MATLAB function handle into a python function, so that I could use the neat optimization function from scipy?

1 Answer 1

1

I suspect that what you want can't directly be done.

Firstly, anything in the python interface will give you that error if you pass it a MATLAB anonymous function:

>> py.print(@(x) x)     
Error using py.print
Handle to MATLAB function '@(x)x' is not supported. Use a handle to a Python function.

This very strongly suggests that once you have a MATLAB anonymous function you can't pass it to python.

Also note that while the lack of MATLAB function handle support is not explicitly mentioned among the limitations, the section of the documentation detailing supported data types makes this remark:

MATLAB Input Argument Type — Scalar Values Only
function handle @py.module.function, to Python functions only

The distinction for Python functions is in line with the fact that even the simplest Python functions refuse to accept MATLAB function handles.

We could try converting your MATLAB anonymous function to a python one, but I have to note upfront that it's messy and I'd avoid doing this if I were you. Since lambdas aren't directly exposed by MATLAB's Python API:

>> py.lambda
Unable to resolve the name py.lambda.

we have to resort to calling Python's eval using a python lambda (because exec also doesn't seem to be exposed):

>> py_f = py.eval('lambda x: x**2', py.dict());
>> py_f(3)

ans =

     9

(Kudos to @yuyichao for fixing this snippet of mine by pointing out the missing globals dict that needs to be passed.)


However, a straightforward question is: do you really need a MATLAB anonymous function? You could just as well use a proper python function (or lambda) and pass possible other arguments to the underlying scipy.optimize function as args. You could define your custom function in a python file and import that from MATLAB and use the corresponding function handle. This would probably be the straightforward way.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! The easiest way for me will be to make my own differential_evolution (DE) method in matlab. The initial goal was to optimize operating points on an optoelectronic device. The measurement being noisy, I think DE is better than regular gradient methods. The idea here would have been to pass in argument the matlab function that calls the instruments and measures the device. The main limitation is that all the instrument drivers are already coded in matlab and re-doing them in python would take a while.
Note that the reason your py.eval doesn't work is actually because you didn't pass in the required global scope argument. py.eval('lambda x: x', py.dict()) works just fine.
@yuyichao good catch, I had no idea about that. Not sure how I'd feel about doing that, to be honest. You could post a separate answer with that, as it's a different workaround to what I'm suggesting.
@AndrasDeak I don't think there's need for a new answer since the gist of this answer still hold (I'm still not aware of a way for the python side to call the matlab side code without the matlab side polling).
@yuyichao 1. I've finally gotten around to updating my answer, thanks again for fixing the snippet. 2. I just learned that you contributed to my system monitor of choice as well a while back; thanks for that too!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.