source: framspy/evolalg/numerical_example/numerical_islands_example.py @ 1290

Last change on this file since 1290 was 1290, checked in by Maciej Komosinski, 6 weeks ago

Better mutation, crossover, and evaluation function for a simple minimalistic numerical optimization example

File size: 884 bytes
Line 
1import numpy as np
2
3from ..base.experiment_islands_model_abc import ExperimentIslands
4from ..structures.hall_of_fame import HallOfFame
5
6
7class ExperimentNumericalIslands(ExperimentIslands):
8    def __init__(self, hof_size, popsize, number_of_populations, migration_interval, save_only_best) -> None:
9        ExperimentIslands.__init__(self,popsize=popsize,
10                                hof_size=hof_size,
11                                number_of_populations=number_of_populations,
12                                migration_interval=migration_interval,
13                                save_only_best=save_only_best)
14
15    def mutate(self, gen):
16        return gen + np.random.normal(0, 15, len(gen))
17
18    def cross_over(self, gen1, gen2):
19        a = np.random.uniform()
20        return a * gen1 + (1.0-a) * gen2
21
22    def evaluate(self, gen):
23        return -sum([x*x for x in gen])
Note: See TracBrowser for help on using the repository browser.