Summary:
I would like to use the Wolfram Client Library for Python to invoke Mathematica's Minimize function directly from within Python, passing to it Python data structures.
Details:
I have a Python program that reads data files, processes them, constructs a function to minimize and finally performs global multi-variable minimization using Python's scipy.optimize.basinhopping algorithm. I would like to compare the optimization results with what Mathematica's Minimize function would return but I don't want to duplicate the (very involved) data processing steps in Mathematica.
I would like to replace the last statement below (the one invoking scipy.optimize.basinhopping):
from scipy import optimize
# x is the variable array
# Z_f(x) is the function to minimize
# dZ_f is its Jacobian (used by scipy.optimize.basinhopping() function)
# x0 is the minimization starting point
#%% Z_f(): construct -Z, the objective function to minimize (Z is the potential function to maximize)
# (this function is called by scipy.optimize.basinhopping())
def Z_f(self, x, contour, default_spectrum):
...
#%% dZ_f(): construct -dZ/dx[idx], the Jacobian of the objective function to minimize (Z is the potential function to maximize)
# (this function is called by scipy.optimize.basinhopping())
def dZ_f(self, x, contour, default_spectrum):
...
#%% optimize_func(): used by optimize.basinhopping()
def optimize_func(self, x, contour, default_spectrum):
return self.Z_f(x, contour, default_spectrum), self.dZ_f(x, contour, default_spectrum)
x0 = [0] * self.num_detectors[contour]
parameters = (contour, self.default_spectrum)
minimizer_args = {"args": parameters, "method": "L-BFGS-B", "jac": True, "options": {'disp': False}}
OptimizeResult = optimize.basinhopping(self.optimize_func, x0, minimizer_kwargs=minimizer_args, disp=True)
with something like
OptimizeResult = Minimize(Z_f, x)
or
OptimizeResult = Minimize(Z_f, {x[0],x[1],...,x[N]})
where x is the N-dimensional variable array.
Is there an easy way to do this without rewriting everything in Python?