예제 #1
0
 def __init__(self,
              distance_matrix,
              initial_solution_strategy='greedy',
              neighbor_selection='first',
              neighborhood='2-opt',
              seed=None):
     TSP.__init__(
         self,
         distance_matrix,
         initial_solution_strategy,
         neighbor_selection,
         neighborhood,
         seed)
예제 #2
0
    def test_initial_solution(self):
        tsp = TSP(distance_matrix=self.distance_matrix,
                  initial_solution_strategy='greedy')
        assert_equal(len(tsp.current_solution_), tsp.number_of_cities_)
        assert_equal(tsp.current_cost_, 7)

        tsp = TSP(distance_matrix=self.distance_matrix,
                  initial_solution_strategy='random')
        assert_equal(len(tsp.current_solution_), tsp.number_of_cities_)

        assert_raises(AttributeError,
                      TSP,
                      distance_matrix=self.distance_matrix,
                      initial_solution_strategy='error')
예제 #3
0
 def test_run(self):
     tsp = TSP(distance_matrix=self.distance_matrix)
     assert_raises(NotImplementedError, tsp.run)
예제 #4
0
 def test__select_neighbor(self):
     tsp = TSP(distance_matrix=self.distance_matrix)
     assert_raises(NotImplementedError, tsp._select_neighbor)
예제 #5
0
 def test__distance(self):
     tsp = TSP(distance_matrix=self.distance_matrix)
     assert_equal(tsp._distance(3, 4), 1)
     assert_equal(tsp._distance(2, 2), 0)
예제 #6
0
 def test__evaluate_solution(self):
     tsp = TSP(distance_matrix=self.distance_matrix)
     solution = [1, 3, 4, 2]
     assert_equal(tsp._evaluate_solution(solution), 9)
예제 #7
0
 def test__neighbor_swap(self):
     tsp = TSP(distance_matrix=self.distance_matrix)
     solution = [1, 3, 4, 2]
     assert_equal(tsp._neighbor_2_opt(solution, 2, 3), [1, 4, 3, 2])