forked from gom-lewil/Genetic-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crossover.py
30 lines (27 loc) · 1 KB
/
crossover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import numpy as np
def cross_mean(parents):
"""
This function crosses multiple parents through their mean and returns a single offspring
Args:
parents (array):
(tuples): in each tuple is all data of parents
individual (array):
args* : sigma, z, ...
fitness: fitness
Returns:
(tuple):
(array): individual - mean of parents
args* : sigma, z, ... - mean of parents
fitness: mean fitness of parents - needs to be evaluated again
"""
to_cross = [[] for _ in parents[0]] # build structure to append each arg to the corresponding list
for parent in parents:
for i, arg in enumerate(parent):
to_cross[i].append(arg)
offspring = []
for arg in to_cross:
if type(arg[0]) == int: # case for sigmas
offspring.append(np.mean(arg))
else: # case of individuals and z
offspring.append(np.mean(arg, axis=0))
return offspring