def find_target(act_position, stop): x_coord = act_position.x_coord y_coord = act_position.y_coord stop_x_coord = stop.x_coord stop_y_coord = stop.y_coord horizontal_length = 0 horizontal_direction = 0 vertical_length = 0 vertical_direction = 0 if y_coord != stop_y_coord: vertical_length = abs(stop_y_coord - y_coord) if y_coord < stop_y_coord: vertical_direction = DOWN else: vertical_direction = UP if x_coord != stop_x_coord: horizontal_length = abs(stop_x_coord - x_coord) if x_coord < stop_x_coord: horizontal_direction = RIGHT else: horizontal_direction = LEFT if horizontal_length != 0 and vertical_length != 0: return [st.Step(vertical_length, vertical_direction), st.Step(horizontal_length, horizontal_direction)] elif horizontal_length != 0: return [st.Step(horizontal_length, horizontal_direction)] elif vertical_length != 0: return [st.Step(vertical_length, vertical_direction)] else: return []
def test_concat_5(self): path = pa.Path() step1 = st.Step(4, RIGHT) step_list = [step1] path.step_list = step_list path.concat_same_direction_steps() self.assertEqual(path.step_list, [st.Step(4, RIGHT)])
def test_mutation_1(self): step_list = [ st.Step(5, LEFT), st.Step(4, UP), st.Step(3, LEFT), st.Step(1, DOWN) ] self.mutate(step_list, 2, UP)
def test_split_mutation_1(self): step_list = [ st.Step(5, LEFT), st.Step(4, UP), st.Step(5, LEFT), st.Step(1, DOWN) ] self.split_mutate(step_list, 2, UP, kind='split')
def test_crossover_2(self): child = op.crossover(self.solution1, self.solution2) child.path_list[0].step_list[0] = st.Step(99999, 1) child.path_list[1].step_list[0] = st.Step(99999, 1) print_paths(child, 'child') print_paths(self.solution1, 'parent 1') print_paths(self.solution2, 'parent 2')
def test_remove_zero_steps_2(self): path = pa.Path() step1 = st.Step(0, LEFT) step2 = st.Step(0, RIGHT) step_list = [step1, step2] path.step_list = step_list path.remove_zero_steps() self.assertEqual(path.step_list, [])
def split_step(self, step_index): step = self.step_list[step_index] if step.length > 1: random_index = ran.randint(1, step.length - 1) dire = step.direction len_1 = random_index len_2 = step.length - random_index self.step_list[step_index] = st.Step(len_1, dire) self.step_list.insert(step_index + 1, st.Step(len_2, dire))
def test_split_4(self): path = pa.Path() step1 = st.Step(10, LEFT) step2 = st.Step(5, UP) step3 = st.Step(0, RIGHT) step4 = st.Step(6, DOWN) step5 = st.Step(10, RIGHT) step_list = [step1, step2, step3, step4, step5] path.step_list = step_list path.split_step(2) print(path)
def create_random_path(start, stop, dimensions): step_list = [] # [Step(length, direction), Step(length, direction), Step(length, direction) ...] x, y = dimensions maximum_step_length = int((x + y) / m.sqrt(x + y)) maximum_steps_amount = int((x + y) / 2) steps_amount = ran.randint(1, maximum_steps_amount) actual_position = start previous_direction = -1 for i in range(steps_amount): direction = ran.choice([RIGHT, LEFT, UP, DOWN]) while is_step_back(previous_direction, direction): direction = ran.choice([RIGHT, LEFT, UP, DOWN]) length = ran.randint(1, maximum_step_length) length, actual_position = go_n_moves_in_direction(direction, length, actual_position, stop) step_list.append(st.Step(length, direction)) previous_direction = direction if actual_position == stop: return step_list step_list += find_target(actual_position, stop) return step_list
def test_convert_to_point_list_up(self): step = st.Step(3, UP) act_point, point_list = step.convert_to_point_list(self.start) self.assertEqual( point_list, [p.Point(0, 0), p.Point(0, -1), p.Point(0, -2)]) self.assertEqual(act_point, p.Point(0, -3))
def test_convert_to_point_list_down(self): step = st.Step(3, DOWN) act_point, point_list = step.convert_to_point_list(self.start) self.assertEqual( point_list, [p.Point(0, 0), p.Point(0, 1), p.Point(0, 2)]) self.assertEqual(act_point, p.Point(0, 3))
def test_convert_to_point_list_right(self): step = st.Step(3, RIGHT) act_point, point_list = step.convert_to_point_list(self.start) self.assertEqual( point_list, [p.Point(0, 0), p.Point(1, 0), p.Point(2, 0)]) self.assertEqual(act_point, p.Point(3, 0))
def test_convert_to_point_list_left(self): step = st.Step(3, LEFT) act_point, point_list = step.convert_to_point_list(self.start) self.assertEqual( point_list, [p.Point(0, 0), p.Point(-1, 0), p.Point(-2, 0)]) self.assertEqual(act_point, p.Point(-3, 0))
def test_concat_3(self): path = pa.Path() step1 = st.Step(4, RIGHT) step2 = st.Step(3, RIGHT) step3 = st.Step(3, RIGHT) step4 = st.Step(3, LEFT) step5 = st.Step(3, RIGHT) step6 = st.Step(5, RIGHT) step7 = st.Step(1, RIGHT) step_list = [step1, step2, step3, step4, step5, step6, step7] path.step_list = step_list path.concat_same_direction_steps() self.assertEqual( path.step_list, [st.Step(10, RIGHT), st.Step(3, LEFT), st.Step(9, RIGHT)])
def concat_same_direction_steps(self): prev_step = st.Step(0, -1) new_step_list = [] for step in self.step_list: if prev_step.direction == step.direction: prev_step.length += step.length else: new_step_list.append(step) prev_step = step self.step_list = new_step_list
def setup_neighbours(mutation_list, random_step_index, step_amount, mutation_direction, dir_1, dir_2): ngb_dir = dir_1 if mutation_direction == dir_1 else dir_2 if random_step_index == 0: # no left neighbour mutation_list.insert(0, st.Step(0, ngb_dir)) random_step_index += 1 step_amount += 1 if random_step_index == step_amount - 1: # no right neighbour mutation_list.append(st.Step(0, ngb_dir)) step_amount += 1 # in splitting mutation two duplicate direction steps may occur # duplicate direction step will always appear as right neighbour because of split_step method specification # insert right neighbour to eliminate this defect if mutation_list[ random_step_index + 1].direction == mutation_list[random_step_index].direction: mutation_list.insert(random_step_index + 1, st.Step(0, ngb_dir)) return mutation_list[random_step_index - 1], mutation_list[random_step_index + 1]
def test_concat_1(self): path = pa.Path() step1 = st.Step(4, LEFT) step2 = st.Step(4, RIGHT) step3 = st.Step(3, RIGHT) step4 = st.Step(6, RIGHT) step5 = st.Step(10, UP) step_list = [step1, step2, step3, step4, step5] path.step_list = step_list path.concat_same_direction_steps() self.assertEqual( path.step_list, [st.Step(4, LEFT), st.Step(13, RIGHT), st.Step(10, UP)])
def test_mutation_6(self): step_list = [ st.Step(2, LEFT), st.Step(2, UP), st.Step(1, LEFT), st.Step(1, UP), st.Step(1, RIGHT), st.Step(3, UP) ] self.mutate(step_list, 5, LEFT)
def test_split_mutation_6(self): step_list = [ st.Step(2, LEFT), st.Step(2, UP), st.Step(1, LEFT), st.Step(1, UP), st.Step(1, RIGHT), st.Step(6, UP) ] self.split_mutate(step_list, 5, LEFT, kind='split')
def test_remove_zero_steps_1(self): path = pa.Path() step1 = st.Step(0, LEFT) step2 = st.Step(0, RIGHT) step3 = st.Step(3, RIGHT) step4 = st.Step(6, RIGHT) step5 = st.Step(0, UP) step_list = [step1, step2, step3, step4, step5] path.step_list = step_list path.remove_zero_steps() self.assertEqual( path.step_list, [st.Step(3, RIGHT), st.Step(6, RIGHT)])
def test_mutation_7(self): sol = s.Solution() path1 = pa.Path(self.start) path1.step_list = [st.Step(5, LEFT), st.Step(4, UP), st.Step(1, LEFT)] path2 = pa.Path(pt.Point(2, 1)) path2.step_list = [ st.Step(2, RIGHT), st.Step(1, DOWN), st.Step(3, RIGHT) ] sol.path_list = [path1, path2] v.draw_plots(sol.parse_to_list_of_points(), (10, 10)) op.mutation(sol) v.draw_plots(sol.parse_to_list_of_points(), (10, 10))
def test_copy(self): step = st.Step(3, DOWN) step_copy = copy.deepcopy(step) self.assertEqual(step is step_copy, False) self.assertEqual(step == step_copy, True)
def test_mutation_4(self): step_list = [st.Step(5, LEFT), st.Step(4, UP), st.Step(1, LEFT)] self.mutate(step_list, 1, LEFT)
def test_set_oppose_direction_down(self): step = st.Step(3, DOWN) step.set_oppose_direction() self.assertEqual(step, st.Step(-3, UP))
def setUp(self): self.step1 = st.Step(5, LEFT) self.step2 = st.Step(4, UP) self.step3 = st.Step(1, RIGHT) self.step_list = [self.step1, self.step2, self.step3]
def test_split_mutation_3(self): step_list = [st.Step(5, LEFT), st.Step(4, UP)] self.split_mutate(step_list, 0, UP, kind='split')
def test_mutation_2(self): step_list = [st.Step(5, LEFT)] self.mutate(step_list, 0, DOWN)
def test_mutation_3(self): step_list = [st.Step(5, LEFT), st.Step(4, UP)] self.mutate(step_list, 1, RIGHT)
def test_split_mutation_4(self): step_list = [st.Step(5, LEFT), st.Step(4, UP), st.Step(1, LEFT)] self.split_mutate(step_list, 1, LEFT, kind='split')
def test_set_oppose_direction_left(self): step = st.Step(3, LEFT) step.set_oppose_direction() self.assertEqual(step, st.Step(-3, RIGHT))