Add crossover probability

This commit is contained in:
coolneng 2021-06-21 02:25:54 +02:00
parent 35ca73ba74
commit f4dd4700c7
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 7 additions and 2 deletions

View File

@ -146,13 +146,18 @@ def group_parents(parents):
return parent_pairs return parent_pairs
def crossover(mode, parents, m): def crossover(mode, parents, m, probability=0.7):
parent_groups = group_parents(parents) parent_groups = group_parents(parents)
offspring = [] offspring = []
if mode == "uniform": if mode == "uniform":
for element in parent_groups: expected_crossovers = int(len(parents) * probability)
cutoff = expected_crossovers // 2
for element in parent_groups[:cutoff]:
offspring.append(uniform_crossover(element, m)) offspring.append(uniform_crossover(element, m))
offspring.append(uniform_crossover(element, m)) offspring.append(uniform_crossover(element, m))
for element in parent_groups[cutoff:]:
offspring.append(element[0])
offspring.append(element[1])
else: else:
for element in parent_groups: for element in parent_groups:
first_offspring, second_offspring = position_crossover(element) first_offspring, second_offspring = position_crossover(element)