Optimization with Scipy

Jérémie Decock (www.jdhp.org)

Objective functions

1D sphere function

$f(x) = x^2$

In [2]:
def f1(x, *params):
    z = x**2
    return z

ND sphere function

$f(x) = \sum_i x_i^2$

In [4]:
def f2(x, *params):
    '''ND sphere function.
    
    $x = (x_1, x_2, ...)$
    $f(x) = \sum_i x_i^2 = x^T * x$'''
    
    z = np.dot(x, x)
    return z

Minimize using the "Brute force" algorithm

Uses the "brute force" method, i.e. computes the function's value at each point of a multidimensional grid of points, to find the global minimum of the function.

See https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.brute.html#scipy.optimize.brute

First example: the 1D sphere function

In [5]:
from scipy import optimize

search_ranges = (slice(-3., 3., 0.1),)

res = optimize.brute(f1,
                     search_ranges,
                     #args=params,
                     full_output=True,
                     finish=optimize.fmin)

print("x* =", res[0])
print("f(x*) =", res[1])
x* = [  2.66453526e-15]
f(x*) = 7.09974814699e-30

Second example: the 2D sphere function

In [8]:
search_ranges = (slice(-3., 3., 0.1), slice(-3., 3., 0.1))

res = optimize.brute(f2,
                     search_ranges,
                     #args=params,
                     full_output=True,
                     finish=optimize.fmin)

print("x* =", res[0])
print("f(x*) =", res[1])
x* = [ 0.  0.]
f(x*) = 0.0