def test_many_moves(self): route = [0, 1, 6, 3, 4, 5, 2, 7, 0] required_moves = 2 for i in range(required_moves): opt_route, opt_f = do_relocate_move(route, self.D, LSOPT.BEST_ACCEPT) if opt_route is None: return route else: route = opt_route self.assertEqual(i + 1, required_moves) self.assertEqual(opt_route, [0, 1, 2, 3, 4, 5, 6, 7, 0])
def test_one_move(self): route = [0, 1, 3, 4, 2, 0] initial_f = objf(route, self.D) sol, delta_f = do_relocate_move(route, self.D, LSOPT.BEST_ACCEPT) do_1pm_f = objf(sol, self.D) self.assertEqual( sol, [0, 1, 2, 3, 4, 0], "chose invalid move, initial %f, optimized %f" % (initial_f, do_1pm_f)) self.assertEqual( initial_f + delta_f, do_1pm_f, "The delta based and recalculated objective functions differ")
def single_heuristic(routes, D, C, d, L): # TODO: this does not exactly correspond to the Wren and Holliday # as the intra and inter route operations are different local # search moves, and, therefore, all options for a node are # not considered in the same order. # Therefore, it is a possible source of replication problems. improvement_found = False for rd1_idx, rd1 in enumerate(routes): if rd1.is_empty(): continue route_improvement_found = True while route_improvement_found: route_improvement_found = False # move on same route new_route, delta = do_relocate_move(rd1.route, D, FAS) if delta is not None: rd1.route = new_route rd1.cost += delta route_improvement_found = True improvement_found = True # move between routes for rd2_idx, rd2 in enumerate(routes): if rd1_idx == rd2_idx: continue new_rd1, new_rd2, delta =\ do_1point_move(rd1, rd2, D, d, C, L, FAS) if delta is not None: routes[rd1_idx] = new_rd1 routes[rd2_idx] = new_rd2 rd1 = new_rd1 route_improvement_found = True improvement_found = True return improvement_found
def test_no_improvements(self): route = [0, 1, 2, 3, 0] sol, delta_f = do_relocate_move(route, self.D) self.assertEqual( sol, None, "Route was already optimal, improvements are not possible")
def test_empty_route(self): self.assertEqual(do_relocate_move([], self.D), (None, None)) self.assertEqual(do_relocate_move([0, 0], self.D), (None, None))