In [1]:
%pylab inline
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]:
In [7]:
initial_surface = np.zeros_like(target)
population = {1:20}
definitions = {1:initial_surface}
fitness_function = pm.ModelDistanceFitnessFunction(target)
mutator = pm.PointMutator(0.001)
evolver = pm.WrightFisherEvolver(fitness_function, mutator)
pm.setup(777)
In [11]:
ans, series = pmain.modelEvolve(population, definitions, evolver, iterations=250000, return_time_series=True, seed=777)
In [12]:
plot(x,ans)
plot(x,target)
plt.xlim((-1,1))
plt.ylim((0,1))
Out[12]:
In [13]:
ani = pmain.create_video_from_time_series(series, target_surface=target, domain=x, filename='./model_parabola_hard.mp4', approximate_number_of_frames=50, record_every=5000)
display_animation(ani)
Out[13]:
Let's try with the gaussian mutation
In [31]:
domain = np.linspace(-1.0, 1.0, 1000)
target = target_function(domain)
initial_surface = np.zeros_like(target)
population = {1:20}
definitions = {1:initial_surface}
iterations= 50000
mutator = pm.GaussianMutator(0.01, domain, 0.05, 0)
fitness_function = pm.ModelDistanceFitnessFunction(target)
evolver = pm.WrightFisherEvolver(fitness_function, mutator)
pm.setup(777)
ans_soft, series_soft = pmain.modelEvolve(population, definitions, evolver, iterations=iterations, return_time_series=True, seed=777)
In [32]:
plot(domain,ans_soft)
plot(domain,target)
plt.xlim((-1,1))
plt.ylim((0,1))
Out[32]:
In [33]:
num_frames = 50
frame_every = iterations / num_frames
animation_soft = pmain.create_video_from_time_series(series_soft, target_surface=target, domain=domain, filename='./wfmodel_parabola_soft.mp4', approximate_number_of_frames=num_frames, record_every=frame_every)
display_animation(animation_soft)
Out[33]:
Understandably the gaussian mutation gets there faster and smoother!
In [34]:
domain = np.linspace(-1.0, 1.0, 1000)
target = sin(domain*domain*domain)
In [35]:
plot(domain, target)
Out[35]:
In [38]:
initial_surface = np.zeros_like(target)
population = {1:20}
definitions = {1:initial_surface}
iterations= 20000
mutator = pm.GaussianMutator(0.01, domain, 0.05)
fitness_function = pm.ModelDistanceFitnessFunction(target)
evolver = pm.WrightFisherEvolver(fitness_function, mutator)
pm.setup(777)
ans_sin, series_sin = pmain.modelEvolve(population, definitions, evolver, iterations=iterations, return_time_series=True, seed=777)
In [39]:
plot(domain, ans_sin)
Out[39]:
In [41]:
animation_sin = pmain.create_video_from_time_series(series_sin, target_surface=target, domain=domain, filename='./wfmodel_continuous.mp4', approximate_number_of_frames=50, record_every=200)
display_animation(animation_sin)
Out[41]:
In [42]:
def discontinous_target(x):
if (x < 0.0):
return -0.5
return 0.5
In [43]:
target = np.array([discontinous_target(x) for x in domain])
In [44]:
plot(domain, target)
Out[44]:
In [45]:
initial_surface = np.zeros_like(target)
population = {1:20}
definitions = {1:initial_surface}
iterations= 10000
mutator = pm.GaussianMutator(0.01, domain, 0.05)
fitness_function = pm.ModelDistanceFitnessFunction(target)
evolver = pm.WrightFisherEvolver(fitness_function, mutator)
pm.setup(777)
ans_dis, series_dis = pmain.modelEvolve(population, definitions, evolver, iterations=iterations, return_time_series=True, seed=777)
In [46]:
plot(domain, ans_dis)
Out[46]:
In [47]:
animation_dis = pmain.create_video_from_time_series(series_dis, target_surface=target, domain=domain, filename='./wfmodel_discontinuous.mp4', approximate_number_of_frames=50, record_every=100)
display_animation(animation_dis)
Out[47]:
In [ ]: