Line Plot with Samples
Finally, we might want to draw the samples of the input space selected by an optimization algorithm.
We will simulate these samples with random points drawn from the input domain.
…
simulate a sample made by an optimization algorithm
seed(1)
sample = r_min + rand(10) * (r_max - r_min)
evaluate the sample
sample_eval = objective(sample)
We can then plot this sample, in this case using small black circles.
…
plot the sample as black circles
pyplot.plot(sample, sample_eval, ‘o’, color=‘black’)
The complete example of creating a line plot of a function with the optima marked by a red line and an algorithm sample drawn with small black dots is listed below.
line plot of domain for a 1d function with optima and algorithm sample
from numpy import arange
from numpy.random import seed
from numpy.random import rand
from matplotlib import pyplot
objective function
def objective(x):
return x**2.0
define range for input
r_min, r_max = -5.0, 5.0
sample input range uniformly at 0.1 increments
inputs = arange(r_min, r_max, 0.1)
compute targets
results = objective(inputs)
simulate a sample made by an optimization algorithm
seed(1)
sample = r_min + rand(10) * (r_max - r_min)
evaluate the sample
sample_eval = objective(sample)
create a line plot of input vs result
pyplot.plot(inputs, results)
define the known function optima
optima_x = 0.0
draw a vertical line at the optimal input
pyplot.axvline(x=optima_x, ls=’–’, color=‘red’)
plot the sample as black circles
pyplot.plot(sample, sample_eval, ‘o’, color=‘black’)
show the plot
pyplot.show()
Running the example creates the line plot of the domain and marks the optima with a red line as before.
This time, the sample from the domain selected by an algorithm (really a random sample of points) is drawn with black dots.
We can imagine that a real optimization algorithm will show points narrowing in on the domain as it searches down-hill from a starting point.