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 main(): w = stdio.readInt() h = stdio.readInt() tour = Tour() while not stdio.isEmpty(): x = stdio.readFloat() y = stdio.readFloat() p = Point(x, y) tour.insertSmallest(p) tour.show() stdio.writef('Tour distance = %f\n', tour.distance()) stdio.writef('Number of points = %d\n', tour.size())
def main(): w = stdio.readInt() h = stdio.readInt() stddraw.setCanvasSize(w, h) stddraw.setXscale(0, w) stddraw.setYscale(0, h) stddraw.setPenRadius(.005) tour = Tour() while not stdio.isEmpty(): x = stdio.readFloat() y = stdio.readFloat() p = Point(x, y) tour.insertSmallest(p) stdio.writef('Tour distance = %f\n', tour.distance()) stdio.writef('Number of points = %d\n', tour.size()) tour.draw() stddraw.show()