Minimize function with SciPy - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Minimize function with SciPy (/thread-41890.html) |
Minimize function with SciPy - PierreLCV - Apr-03-2024 Hello everyone, I'm currently trying to minimize a function using a Python algorithm. The function is a bit complicated and requires an FEA solver to be calculated. Anyway, I was able to define my function in the classical way according to the parameter I'm trying to optimize: def fonction_calcul(Parametre1): blabla function definition return function valueAs this last function definition works well, I can easily get the values of my calculation_function for the parameter values I want. Knowing this, I thought I could then "foolishly" use scipy.optimize to find the minimization value of my function, but alas, it doesn't work! Here's what I get starting with x=1 x_start=1.0 >>> result = spo.minimize(fonction_calcul, x_start) >>> result fun: 106.69134802297765 hess_inv: array([[1]]) jac: array([0.]) message: 'Optimization terminated successfully.' nfev: 2 nit: 0 njev: 1 status: 0 success: True x: array([1.])Overall, as you can see, minimize calculates a single value and stops at this value, considering that it has optimized well (knowing that in this case, the minimization value is around 0.03...). I've tried minimize with a "classic" function (polynomial) and it works well for finding the minimization value... Any idea how to make minimize function in my case? Thanks a lot! Pierre RE: Minimize function with SciPy - paul18fr - Apr-03-2024 I guess you're using python to:
Nontheless here I would suggest you to have a look to a powerfull optimizer which is Dakota inculing a lot of solvers (determinist & stochastic methods for instance); I wonder if now a python api exists (to be confirmed - i used it a long time ago). Paul RE: Minimize function with SciPy - PierreLCV - Apr-03-2024 Dear paul, Thank you for your reply ! I will take a look at Dakota :) However, i really would like to understand why minimize (which appears to be a basic function) is not working... Regards, Pierre RE: Minimize function with SciPy - paul18fr - Apr-05-2024 here is the well known Rosembrock test case: 2 variables, unconstraint problem, single (global) minimum. Keep in mind that:
Hope it helps. from scipy.optimize import minimize # https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html def Rosembrock(x): # f(x, y) = (1 - x)**2 + 100*(y - x**2)**2 # minimum in (x, y) = (1, 1) return (1 - x[0])**2 + 100*(x[1] - x[0]**2)**2 # Method = 'NM' # "Nelder-Mead" # Method = 'BFGS' # "Broyden-Fletcher-Goldfarb-Shanno" # Method = 'CG' # "Conjugate-gradient" Method = 'SLSQP' # "Sequential Least SQuares Programming" # Starting point x0 = [2., 3] if (Method == 'NM'): # order 0 method (Nelder-Mead) #x0 = starting point Results = minimize(Rosembrock, x0, method='nelder-mead', options={'xatol': 1e-8, 'disp': False} ) else: # order 1 gradient based method (BFGS / CG / SLSQP) # Gradient and Hessian are numerically calculated => several evaluations are needed Results = minimize(Rosembrock, x0, method = Method, jac = '3-point', options={'disp': False} ) # Optimization results: print(f"Optimized value: {Results.x}") print(f"Cost function value after optimization: {Results.fun}") print(f"Number of iterations: {Results.nit}") print(f"Number of function evalutaions: {Results.nfev}") print(f"Status: {Results.message}") |