def crossover(parent_1, parent_2): """ crossover subsections of parent 1 and left over cities from parent2 """ from random import random child = Tour(parent_1.tour_manager) start_pos = (int) (random() * parent_1.size()) end_pos = (int) (random() * parent_2.size()) if start_pos > end_pos: temp_pos = start_pos start_pos = end_pos end_pos = temp_pos for i in range(0, child.size()): if i > start_pos and i < end_pos: child.set_city(i, parent_1.get_city(i)) for i in range(0, parent_2.size()): if not child.contains_city(parent_2.get_city(i)): for j in range(0, child.size()): if child.get_city(j) is None: child.set_city(j, parent_2.get_city(i)) break return child
def crossover(parent_1, parent_2): """ crossover subsections of parent 1 and left over cities from parent2 """ from random import random child = Tour(parent_1.tour_manager) start_pos = (int)(random() * parent_1.size()) end_pos = (int)(random() * parent_2.size()) if start_pos > end_pos: temp_pos = start_pos start_pos = end_pos end_pos = temp_pos for i in range(0, child.size()): if i > start_pos and i < end_pos: child.set_city(i, parent_1.get_city(i)) for i in range(0, parent_2.size()): if not child.contains_city(parent_2.get_city(i)): for j in range(0, child.size()): if child.get_city(j) is None: child.set_city(j, parent_2.get_city(i)) break return child
def crossover(self, parent1, parent2): child = Tour(len(self._cities_list)) start_pos = int(random.random() * parent1.tour_size()) end_pos = int(random.random() * parent1.tour_size()) for i in xrange(child.tour_size()): if start_pos < end_pos and i > start_pos and i < end_pos: child.set_city(i, parent1.get_city(i)) elif start_pos > end_pos: if not (i < start_pos and i > end_pos): child.set_city(i, parent1.get_city(i)) for i in xrange(parent2.tour_size()): if not child.contains_city(parent2.get_city(i)): for j in xrange(child.tour_size()): if not child.get_city(j): child.set_city(j, parent2.get_city(i)) break return child