Mark As Completed Discussion

Crossover

The crossover operator in this section will generate a predefined number of offspring from parents selected in the previous step by using the single-point crossover. In this example, each kth offspring will have the first half from the parent at index (k mod mattingPoolSize) in parents array and second from the parent at index ((k+1) mod mattingPoolSize). It should be noted that there are many other ways to select parents while one of them is implemented here.

PYTHON
1# Following statement will create an empty two dimensional array to store offspring
2offspring = numpy.empty((offspringSize, population.shape[1]))
3
4for k in range(offspringSize):
5         #Determining the crossover point
6         crossoverPoint = numpy.random.randint(0,genes) 
7
8         # Index of the first parent.
9         parent1Index = k%parents.shape[0]
10
11         # Index of the second.
12         parent2Index = (k+1)%parents.shape[0]
13
14         # Extracting first half of the offspring
15         offspring[k, 0: crossoverPoint] = parents[parent1Index, 0: crossoverPoint]
16
17         # Extracting second half of the offspring
18         offspring[k, crossoverPoint:] = parents[parent2Index, crossoverPoint:]