Skip to content

Instantly share code, notes, and snippets.

@mlubej
Last active April 25, 2020 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlubej/d9a95b85c495489b3b32898fce03cb71 to your computer and use it in GitHub Desktop.
Save mlubej/d9a95b85c495489b3b32898fce03cb71 to your computer and use it in GitHub Desktop.
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