Random Search Optimization
Learn to implement the random search optimization algorithm.
We'll cover the following
What is random search optimization?
Random search optimization is a brute-force algorithm that finds the
Let’s consider an interview process to find a suitable candidate for a particular role without prescreening. Let’s say
The random search algorithm will first sample
The figure given below illustrates how random search works. Random search optimization tries to find the best solution for a problem by randomly sampling points within a range and evaluating the objective function over them.
Note: The time complexity of random search is
because the algorithm evaluates the objective function over all data points to decide the optimal solution.
Implementing random search
Let’s consider a scenario where a company wants to find the optimal salary raise for their employees so that their overall investment is minimized and, at the same time, employees are satisfied and productive.
Providing a high salary raise can overshoot the budget and overall investment. Providing a low raise can lower the productivity of individual workers, compelling the organization to hire more and spend more than expected. The company hypothesizes that the relationship between salary raise
One way to come up with the function above is to use some data points that represent the possible outcomes of different salary raises and their corresponding investments. For example, the company could conduct a survey or an experiment to estimate how the productivity, turnover, and demand of the employees change with different salary raises. Then, they could use these data points to fit a quadratic function that best describes the trend of the data.
Note: It is important to note that the values
are not unique or optimal for the equation . There are many other possible values that could produce a different quadratic function that also model the relationship between salary raise and overall investment. The choice of these values depends on the accuracy, simplicity, and validity of the function that the company wants to use.
The code snippet given below implements the random search algorithm to optimize the function
import numpy as npimport matplotlib.pyplot as plt# objective functiondef objective(x):return (x+2)*(x-5) + 15a, b = -10.0, 10.0 # range of random points to be sampledN = 1000datapoints = a + np.random.rand(N) * (b-a) # generate 100 random points between [a,b]outputs = objective(datapoints) # evaluate objective on random data pointsindex = np.argmin(outputs)# summarize best solutionprint('Best: f(%.5f) = %.5f' % (datapoints[index], outputs[index]))# plot the curve for graphic visualizationplt.figure(figsize=(10,10))plt.rcParams.update({'font.size': 22})x = np.arange(-10, 10, 0.1)plt.plot(x, objective(x), linewidth=2)# plot the random datapoints sampleplt.axvline(x=datapoints[index], ls='--', color='red', label='optimum')plt.axhline(y=0, ls='-', color='black')plt.axvline(x=0, ls='-', color='black')plt.legend()plt.savefig('./output/plot.png')
Lines 1–2: We import the NumPy and Matplotlib Python libraries.
Lines 5–6: We define our
objective()
objective function that takes the inputx
and returns the value of the objective at that point.Lines 8–9: We set the parameter
a
as, b
as, and N
as. Line 11: We sample
N
randomdatapoints
uniformly between[a, b]
.Lines 13–14: We evaluate the sampled
datapoints
to the objective function and pick the data point that returns the minimum value.Lines 20–23: We plot and visualize the objective function over the Matplotlib canvas.
Lines 26–30: We plot and visualize the optimal data point over the graph of the objective function.
As can be seen in the illustration, the algorithm returns a value close to the actual optimal value,
However, the algorithm can be inefficient and slow to converge to the optimal solution because it employs a brute-force approach. It can also be sensitive to the number of data points
For example, if we choose a range of