In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
from JSAnimation.IPython_display import display_animation

In [3]:
import pyximport; pyximport.install(setup_args={"include_dirs":numpy.get_include()})
import paramless as pmain
import paramless_cython as pm

In [4]:
def target_function(x):
    return x**2.0

In [5]:
x = np.linspace(-1.0, 1.0, 25)  
target = target_function(x)

In [6]:
plot(x, target_function(x))


Out[6]:
[<matplotlib.lines.Line2D at 0x7f35eeff7450>]

In [7]:
initial_surface = np.zeros_like(target)
fitness_function = pm.DistanceFitnessFunction(target)
mutator = pm.PointMutator(0.001)
evolver = pm.StandardEvolver(fitness_function, mutator, 1e-12)

In [8]:
ans, series = pmain.evolve(initial_surface, evolver, iterations=100000, return_time_series=True, seed=777)

In [9]:
plot(x,ans)
plot(x,target)
plt.xlim((-1,1))
plt.ylim((0,1))


Out[9]:
(0, 1)

In [10]:
ani = pmain.create_video_from_time_series(series, target_surface=target, domain=x, filename='./parabola_hard.mp4', approximate_number_of_frames=100, record_every=1000)
display_animation(ani)


Out[10]:


Once Loop Reflect

A softer world mutation

Let's try with the gaussian mutation


In [11]:
domain = np.linspace(-1.0, 1.0, 1000)  
target = target_function(domain)
initial_surface = np.zeros_like(target)
iterations= 10000
mutator = pm.GaussianMutator(0.01, domain, 0.05)
fitness_function = pm.DistanceFitnessFunction(target)
evolver = pm.StandardEvolver(fitness_function, mutator, 1e-12)
ans_soft, series_soft = pmain.evolve(initial_surface, evolver, iterations=iterations, return_time_series=True, seed=777)

In [12]:
plot(domain,ans_soft)
plot(domain,target)
plt.xlim((-1,1))
plt.ylim((0,1))


Out[12]:
(0, 1)

In [13]:
animation_soft = pmain.create_video_from_time_series(series_soft, target_surface=target, domain=domain, filename='./parabola_soft.mp4', approximate_number_of_frames=50, record_every=200)
display_animation(animation_soft)


Out[13]:


Once Loop Reflect

Understandably the gaussian mutation gets there faster and smoother!


In [14]:
domain = np.linspace(-1.0, 1.0, 1000)  
target = sin(domain*domain*domain)

In [15]:
plot(domain, target)


Out[15]:
[<matplotlib.lines.Line2D at 0x7f35eea2d0d0>]

In [16]:
initial_surface = np.zeros_like(target)
iterations= 10000
mutator = pm.GaussianMutator(0.01, domain, 0.05)
fitness_function = pm.DistanceFitnessFunction(target)
evolver = pm.StandardEvolver(fitness_function, mutator, 1e-12)
ans_sin, series_sin = pmain.evolve(initial_surface, evolver, iterations=iterations, return_time_series=True, seed=777)

In [17]:
plot(domain, ans_sin)


Out[17]:
[<matplotlib.lines.Line2D at 0x7f35ee431590>]

In [18]:
animation_sin = pmain.create_video_from_time_series(series_sin, target_surface=target, domain=domain, filename='./continuous.mp4', approximate_number_of_frames=50, record_every=200)
display_animation(animation_sin)


Out[18]:


Once Loop Reflect

Discountinuous target


In [19]:
def discontinous_target(x):
    if (x < 0.0):
        return -0.5
    return 0.5

In [20]:
target = np.array([discontinous_target(x) for x in domain])

In [21]:
plot(domain, target)


Out[21]:
[<matplotlib.lines.Line2D at 0x7f35eeee27d0>]

In [22]:
domain = np.linspace(-1.0, 1.0, 1000)  
initial_surface = np.zeros_like(target)
iterations= 10000
mutator = pm.GaussianMutator(0.01, domain, 0.05)
fitness_function = pm.DistanceFitnessFunction(target)
evolver = pm.StandardEvolver(fitness_function, mutator, 1e-12)
ans_dis, series_dis = pmain.evolve(initial_surface, evolver, iterations=iterations, return_time_series=True, seed=777)

In [23]:
plot(domain, ans_dis)


Out[23]:
[<matplotlib.lines.Line2D at 0x7f35ef594d10>]

In [24]:
animation_dis = pmain.create_video_from_time_series(series_dis, target_surface=target, domain=domain, filename='./discontinuous.mp4', approximate_number_of_frames=50, record_every=100)
display_animation(animation_dis)


Out[24]:


Once Loop Reflect

In [ ]: