Last change
on this file since 1312 was
1290,
checked in by Maciej Komosinski, 10 months ago
|
Better mutation, crossover, and evaluation function for a simple minimalistic numerical optimization example
|
File size:
609 bytes
|
Rev | Line | |
---|
[1190] | 1 | import numpy as np |
---|
| 2 | |
---|
| 3 | from ..base.experiment_abc import ExperimentABC |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | class 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 | |
---|
[1290] | 12 | def mutate(self, gen): |
---|
| 13 | return gen + np.random.normal(0, 15, len(gen)) |
---|
[1190] | 14 | |
---|
| 15 | def cross_over(self, gen1, gen2): |
---|
[1290] | 16 | a = np.random.uniform() |
---|
| 17 | return a * gen1 + (1.0-a) * gen2 |
---|
[1190] | 18 | |
---|
[1290] | 19 | def evaluate(self, gen): |
---|
| 20 | return -sum([x*x for x in gen]) |
---|
Note: See
TracBrowser
for help on using the repository browser.