Last active
April 25, 2020 10:18
-
-
Save mlubej/d9a95b85c495489b3b32898fce03cb71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mate(self, partner): | |
""" | |
Create offspring with a order-crossover method | |
:param partner: a second p1 which will participate in the mating | |
:return: two new offspring individuals | |
""" | |
c1, c2 = np.zeros((2, self.n_pieces), dtype=int) | |
start, end = np.sort(np.random.choice(range(self.n_pieces), 2, replace=False)) | |
p1 = self.chromosome | |
p2 = partner.chromosome | |
c1[start:end + 1] = p1[start:end + 1] | |
mask1 = ~np.in1d(p1, p1[start:end + 1]) | |
mask2 = ~np.in1d(p2, p1[start:end + 1]) | |
c1[mask1] = p2[mask2] | |
c2[start:end + 1] = p2[start:end + 1] | |
mask1 = ~np.in1d(p2, p2[start:end + 1]) | |
mask2 = ~np.in1d(p1, p2[start:end + 1]) | |
c2[mask1] = p1[mask2] | |
return Individual(self.polygons, self.board_size, c1), Individual(self.polygons, self.board_size, c2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment