# python - Is it possible to use SciPy Optimize with an external application call

2.5k views

### python - Is it possible to use SciPy Optimize with an external application call

I am trying to use scipy.optimize.basinhopping to find the best solution(s) to an engineering systems problem that has many potential minima.

The analytical equations for estimating the conditions and properties of a fluid system are built into a pre-existing and non-python application. These equations are ideal, based on physics and thermodynamics. These equations are then modified or "tuned" using coefficients to match actual fluid properties measured in the laboratory. I am trying to use scipy.optimize.basinhopping to find the best set of coefficients to minimize the prediction error or difference between predicted and measured values across a series of properties (ex: pressure, flow rate, turbulence, etc.). Error is measured by simple SSE across all of the properties. For example:

Pressure Error = (Pprediced - Pmeasured)^2

Basinhopping seems to be working to find solutions to the system but it is very time consuming. The main problem appears to be that only the step taking portion of the algorithm is working, while the minimization portion is unable to work with the external application call. I am trying to determine if the problem is with my implementation or if it is simply not possible to optimize with an external application.

Here is my implementation:

``````minimizer_kwargs = {"method": "BFGS", "options":{"maxiter":1, "disp": True, "return_all": True}}

basinhopping(tuningRun,[ 1.00,  1.05,  0.80,  0.09, -1.22, 0.45], niter = 500, minimizer_kwargs=minimizer_kwargs, disp =True))
``````

tuningRun is a function that calls the external application, inserts the coefficients, and returns the error/cost function.

Here is an example of the output I'm seeing:

From the solver iterations it seems that the cost/error function is not changing between the BFGS function evaluations. In this case it's staying constant at 152.08. It's particularly inefficient in this case, because the application call is quite costly (~70 seconds). Also looking at the hess and jac in the final output it doesn't appear that the algorithm is able to gather any derivatives. I have tried other minimization algorithms, for example COBYLA, and that doesn't seem to change minimization issue. My question is whether it's possible to improve my implementation so that the basinhopping algorithm can use differentials to take more efficient steps, or whether it's not possible in this case (because the function being optimized is not in python) and I should override/turn-off the minimization functionality. 