3
$\begingroup$

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?

$\endgroup$
10
  • 4
    $\begingroup$ I’m voting to close this question because this is a python question and should be asked in stackoverflow.com $\endgroup$
    – xzczd
    Commented Oct 16, 2022 at 3:51
  • 1
    $\begingroup$ Thanks. The question straddles Mathematica and Python. I need to understand whether I can use the Wolfram Client Library for Python without rewriting most of the code. It appears that WCL provides the interface to invoke Mathematica's functionality but it's not clear to me how I can pass complex data between the two without too much effort. BTW, what's the difference between StackOverflow and StackExchange? $\endgroup$
    – Neil Fazel
    Commented Oct 17, 2022 at 21:33
  • 1
    $\begingroup$ @xzczd, I decided to post this question on stackoverflow.com but came across a similar question that had been deemed off-topic and closed: [How to use Mathematica functions in Python programs? [closed]](stackoverflow.com/questions/10276834/…) This question straddles Mathematica and Python and for forums consider it off-topic. I'm not sure where to post it. $\endgroup$
    – Neil Fazel
    Commented Oct 26, 2022 at 2:56
  • 1
    $\begingroup$ This is a bit funny… according to the timeline, the moderator in stackoverflow is trying to push the question to this site and then it's rejected. I just started a discussion in meta: mathematica.meta.stackexchange.com/q/2705/1871 $\endgroup$
    – xzczd
    Commented Oct 26, 2022 at 4:07
  • 1
    $\begingroup$ I'm voting to reopen this question as an extraordinary measure until the topic is settled. Considering that: 1._ This question is currently homeless, (both rejected here and in in principle in stackoverflow). 2._ The discussion on meta of this edge cases are on or off topic is just starting. We should reopen this question as an extraordinary measure until the topic is settled, we can migrate it later if necessary. $\endgroup$
    – rhermans
    Commented Oct 26, 2022 at 8:38

0

Your Answer

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