Introduction to Optimization in Python
Jasmin Ludolf
Content Developer
SciPy
from scipy.optimize import minimize_scalar
def objective_function(x): return x**2 - 12*x + 4
result = minimize_scalar(objective_function) print(result)
fun: -32.0
message: 'Optimization terminated successfully; The
returned value satisfies the termination criteria
(using xtol = 1.48e-08 )'
nfev: 10
nit: 4
success: True
x: 6.000000000000001
fun: -32.0
message: 'Optimization terminated successfully; The
returned value satisfies the termination criteria
(using xtol = 1.48e-08 )'
nfev: 10
nit: 4
success: True
x: 6.000000000000001
result.x
6.000000000000001
fun
: value of the function at the minimummessage
: statusnfev
: number of times the algorithm evaluated the functionnit
: number of iterations it took to reach the solutionsuccess
: boolean on whether an optimum was foundx
: the optimal valuedef objective_function(x):
return 40 * q - 0.5 * q**2
-1*(40 * q - 0.5 * q**2)
-40 * q + 0.5 * q**2
def negated_function(x):
return -40 * q + 0.5 * q**2
result = minimize_scalar(negated_function)
print(f"The maximum is {result.x:.2f} in two
decimals")
The maximum is 40.00 in two decimals
from scipy.optimize import minimize
def objective_function(a): return (a[0] - 2)**2 + (a[1] - 3)**2 + 1.5
x0 = [1, 2] result = minimize(objective_function, x0)
a[0]
represents x
and a[1]
represents y
print(result)
print(f"minimum is (x, y) = ({result.x[0]:.2f}, {result.x[1]:.2f}) in two decimals.")
fun: 1.5000000000000002
hess_inv: array([[ 0.75, -0.25], [-0.25, 0.75]])
jac: array([0., 0.])
message: 'Optimization terminated successfully.' nfev: 9 nit: 2 njev: 3
status: 0
success: True x: array([1.99999999, 2.99999999]) minimum is (x, y) = (2.00, 3.00) in two decimals.
Introduction to Optimization in Python