Azzera filtri
Azzera filtri

Python call from Matlab returning Incorrect Results for Basinhopping in SciPy

6 visualizzazioni (ultimi 30 giorni)
I am attempting to call a Python function from MATLAB, but the MATLAB result is incorrect compared to running the same line directly in my Python IDE. Below is my Python code which calls the basinhopping Scipy function, and is supposed to stop when the stop criteria is hit to reduce run time. Python stops after about 30 iterations, but MATLAB runs through the entire maximum 1000 iterations and returns the last value, which is both time consuming and incorrect. Python 3.8 is used, with MATLAB at 2022a. Is there a fix or issue with this setup?
x0 = ([0.015,70,0.015,4,2])
xmin = ([0.01,5,0.01,1,1])
xmax = ([0.1,200,0.5,6,6])
bounds = [(low, high) for low, high in zip(xmin, xmax)]
def callback_opt_threshold(x,f,accept,stop_criteria=1):
if f<stop_criteria:
return True
args = fluid_states[0,:]
minimizer_kwargs = {"args":args,"bounds":bounds,"method":"L-BFGS-B"}
res = basinhopping(test_function,x0, niter=1000, minimizer_kwargs=minimizer_kwargs,callback=callback_opt_threshold)

Risposte (1)

Sachin
Sachin il 24 Mar 2023
Hi
I understand that you are getting the wrong output for python code. This may be because MATLAB
Some workarounds that might be helpful :
  1. Make sure that the version of Python being used in MATLAB is the same as the one you are using in the Python IDE.
  2. Try running a complete python file using ‘pyrunfile’ instead of the python function.
pyrunfile(your file name.py)
3. Consider adding print statements or other debugging tools to the python code.
For more information about ‘pyrunfile’ refer to the following page :

Prodotti


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by