Python Forum
Minimize function with SciPy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Minimize function with SciPy
#4
here is the well known Rosembrock test case: 2 variables, unconstraint problem, single (global) minimum.

Keep in mind that:
  • several evaluations/iterations are necessary; in your case, 1 iteration = 1 FEA
  • gradient/hessian are numerically estimated (1 estimation = 1 simulation)
  • your cost function is essential to succeed
  • of course you can add constraints to bracket variables

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}")
Reply


Messages In This Thread
Minimize function with SciPy - by PierreLCV - Apr-03-2024, 09:29 AM
RE: Minimize function with SciPy - by paul18fr - Apr-03-2024, 10:03 AM
RE: Minimize function with SciPy - by PierreLCV - Apr-03-2024, 12:06 PM
RE: Minimize function with SciPy - by paul18fr - Apr-05-2024, 07:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to edit Tkinter Minimize, Maximize, and Close Buttons kucingkembar 7 641 Apr-26-2024, 11:36 AM
Last Post: kucingkembar
  How to Minimize ADB window OomKoos 0 454 Dec-29-2023, 12:41 PM
Last Post: OomKoos
  Can I minimize the code??? shantanu97 4 2,423 Mar-23-2021, 05:26 PM
Last Post: jefsummers
  How to use scipy.optimization.brute for multivariable function Shiladitya 9 6,519 Oct-28-2020, 10:40 PM
Last Post: scidam
  ModuleNotFoundError: No module named 'scipy.optimize'; 'scipy' is not a package AaronKR 1 10,499 Jul-09-2020, 02:36 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020