First, we can define a function that implements our function.
In this case, we will use a simple offset version of the x^2 function e.g. a simple parabola (u-shape) function. It is a minimization objective function with an optima at -5.0.
objective function
def objective(x):
return (5.0 + x)**2.0
We can plot a coarse grid of this function with input values from -10 to 10 to get an idea of the shape of the target function.
The complete example is listed below.
plot a convex target function
from numpy import arange
from matplotlib import pyplot
objective function
def objective(x):
return (5.0 + x)**2.0
define range
r_min, r_max = -10.0, 10.0
prepare inputs
inputs = arange(r_min, r_max, 0.1)
compute targets
targets = [objective(x) for x in inputs]
plot inputs vs target
pyplot.plot(inputs, targets, ‘–’)
pyplot.show()
Running the example evaluates input values in our specified range using our target function and creates a plot of the function inputs to function outputs.
Note: in a real optimization problem, we would not be able to perform so many evaluations of the objective function so easily. This simple function is used for demonstration purposes so we can learn how to use the optimization algorithm.
Next, we can use the optimization algorithm to find the optima.
…
minimize the function
result = minimize_scalar(objective, method=‘brent’)
Once optimized, we can summarize the result, including the input and evaluation of the optima and the number of function evaluations required to locate the optima.
summarize the result
opt_x, opt_y = result[‘x’], result[‘fun’]
print(‘Optimal Input x: %.6f’ % opt_x)
print(‘Optimal Output f(x): %.6f’ % opt_y)
print(‘Total Evaluations n: %d’ % result[‘nfev’])
Finally, we can plot the function again and mark the optima to confirm it was located in the place we expected for this function.
…
define the range
r_min, r_max = -10.0, 10.0
prepare inputs
inputs = arange(r_min, r_max, 0.1)
compute targets
targets = [objective(x) for x in inputs]
plot inputs vs target
pyplot.plot(inputs, targets, ‘–’)
plot the optima
pyplot.plot([opt_x], [opt_y], ‘s’, color=‘r’)
show the plot
pyplot.show()
The complete example of optimizing an unconstrained convex univariate function is listed below.
optimize convex objective function
from numpy import arange
from scipy.optimize import minimize_scalar
from matplotlib import pyplot
objective function
def objective(x):
return (5.0 + x)**2.0
minimize the function
result = minimize_scalar(objective, method=‘brent’)
summarize the result
opt_x, opt_y = result[‘x’], result[‘fun’]
print(‘Optimal Input x: %.6f’ % opt_x)
print(‘Optimal Output f(x): %.6f’ % opt_y)
print(‘Total Evaluations n: %d’ % result[‘nfev’])
define the range
r_min, r_max = -10.0, 10.0
prepare inputs
inputs = arange(r_min, r_max, 0.1)
compute targets
targets = [objective(x) for x in inputs]
plot inputs vs target
pyplot.plot(inputs, targets, ‘–’)
plot the optima
pyplot.plot([opt_x], [opt_y], ‘s’, color=‘r’)
show the plot
pyplot.show()
Running the example first solves the optimization problem and reports the result.