In [1]:
%pylab inline
In [2]:
from JSAnimation.IPython_display import display_animation
In [3]:
%run ../paramless.py
In [4]:
def target_function(x):
return x**2.0
In [5]:
x = np.linspace(-1.0, 1.0, 100)
target = target_function(x)
In [6]:
plot(x, target_function(x))
Out[6]:
In [7]:
initial_surface = np.zeros_like(target)
ans, series = evolve(initial_surface=initial_surface, fitness_function=distance_fitness_function, mutation_function=point_mutation, iterations=1000000, return_time_series=True, seed=777, target_surface=target, mutation_epsilon=0.001, atol=1e-12)
In [8]:
plot(x,ans)
plot(x,target)
plt.xlim((-1,1))
plt.ylim((0,1))
Out[8]:
In [9]:
ani = create_video_from_time_series(series, target_surface=target, domain=x, filename='/Users/garcia/Desktop/x_cuadrado.mp4', approximate_number_of_frames=200, record_every=1000)
display_animation(ani)
Out[9]:
Let's stry with the gaussian mutation
In [10]:
domain = np.linspace(-1.0, 1.0, 1000)
target = target_function(domain)
initial_surface = np.zeros_like(target)
iterations= 10000
mutation_epsilon=0.01
width=0.05
ans_soft, series_soft = evolve(initial_surface=initial_surface, fitness_function=distance_fitness_function, mutation_function=gaussian_mutation, iterations=iterations, return_time_series=True, seed=777, target_surface=target, mutation_epsilon=mutation_epsilon, domain=domain, width=width, atol=1e-12)
In [11]:
plot(domain,ans_soft)
plot(domain,target)
plt.xlim((-1,1))
plt.ylim((0,1))
Out[11]:
In [13]:
animation_soft = create_video_from_time_series(series_soft, target_surface=target, domain=domain, filename='/Users/garcia/Desktop/soft.mp4', approximate_number_of_frames=200, record_every=200)
display_animation(animation_soft)
Out[13]:
Understandably the gaussian mutations gets there faster and smoother!
In [15]:
domain = np.linspace(-1.0, 1.0, 1000)
target = sin(domain*domain*domain)
In [16]:
plot(domain, target)
Out[16]:
In [17]:
domain = np.linspace(-1.0, 1.0, 1000)
target = sin(domain*domain*domain)
initial_surface = np.zeros_like(target)
iterations= 10000
mutation_epsilon=0.01
width=0.05
ans_sin, series_sin = evolve(initial_surface=initial_surface, fitness_function=distance_fitness_function, mutation_function=gaussian_mutation, iterations=iterations, return_time_series=True, seed=777, target_surface=target, mutation_epsilon=mutation_epsilon, domain=domain, width=width, atol=1e-12)
In [18]:
plot(domain, ans_sin)
Out[18]:
In [20]:
animation_sin = create_video_from_time_series(series_sin, target_surface=target, domain=domain, filename='/Users/garcia/Desktop/soft_sin.mp4', approximate_number_of_frames=200, record_every=200)
display_animation(animation_sin)
Out[20]:
In [21]:
def discontinous_target(x):
if (x < 0.0):
return -0.5
return 0.5
In [22]:
target = np.array([discontinous_target(x) for x in domain])
In [23]:
plot(domain, target)
Out[23]:
In [24]:
domain = np.linspace(-1.0, 1.0, 1000)
initial_surface = np.zeros_like(target)
iterations= 10000
mutation_epsilon=0.01
width=0.05
ans_dis, series_dis = evolve(initial_surface=initial_surface, fitness_function=distance_fitness_function, mutation_function=gaussian_mutation, iterations=iterations, return_time_series=True, seed=777, target_surface=target, mutation_epsilon=mutation_epsilon, domain=domain, width=width, atol=1e-12)
In [25]:
plot(domain, ans_dis)
Out[25]:
In [26]:
animation_dis = create_video_from_time_series(series_dis, target_surface=target, domain=domain, filename='/Users/garcia/Desktop/soft_sin.mp4', approximate_number_of_frames=200, record_every=100)
display_animation(animation_dis)
Out[26]: