source: framspy/evolalg/numerical_example/numerical_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: 609 bytes
Line 
1import numpy as np
2
3from ..base.experiment_abc import ExperimentABC
4
5
6class ExperimentNumerical(ExperimentABC):
7    def __init__(self, hof_size, popsize, save_only_best) -> None:
8        ExperimentABC.__init__(self,popsize=popsize,
9                               hof_size=hof_size,
10                               save_only_best=save_only_best)
11
12    def mutate(self, gen):
13        return gen + np.random.normal(0, 15, len(gen))
14
15    def cross_over(self, gen1, gen2):
16        a = np.random.uniform()
17        return a * gen1 + (1.0-a) * gen2
18
19    def evaluate(self, gen):
20        return -sum([x*x for x in gen])
Note: See TracBrowser for help on using the repository browser.