source: framspy/evolalg/mutation_cross/frams_cross.py @ 1113

Last change on this file since 1113 was 1113, checked in by Maciej Komosinski, 4 years ago

Added a framework for evolutionary algorithms cooperating with FramsticksLib?.py

File size: 782 bytes
Line 
1import random
2
3from evolalg.base.frams_step import FramsStep
4from evolalg.base.individual import Individual
5
6
7class FramsCross(FramsStep):
8    def __init__(self, frams_lib, commands, cross_prob):
9        super().__init__(frams_lib, commands)
10        self.cross_prob = cross_prob
11
12    def call(self, population):
13        for i in range(1, len(population), 2):
14            if random.random() < self.cross_prob:
15                geno1 = population[i - 1].genotype
16                geno2 = population[i].genotype
17
18                cross_geno1 = self.frams.crossOver(geno1, geno2)
19                cross_geno2 = self.frams.crossOver(geno1, geno2)
20
21                population[i - 1] = Individual(cross_geno1)
22                population[i] = Individual(cross_geno2)
23
24        return population
Note: See TracBrowser for help on using the repository browser.