示例#1
0
class TestACORoadSelector(unittest.TestCase):

    def setUp(self):
        """Set up road selector test."""
        self.prob_calc = aco_probability_calculator
        self.c1 = City(1, 538, 732)
        self.c2 = City(2, 349, 265)
        self.c3 = City(3, 446, 501)
        self.t = 0.5
        self.r1 = Road(self.c1, self.c2, self.t)
        self.r2 = Road(self.c1, self.c3, self.t)
        self.r3 = Road(self.c2, self.c3, self.t)
        self.roads = [self.r1, self.r2, self.r3]
        self.salesman = Salesman(self.c1, aco_road_selector)

        Constants = namedtuple('Constants', 'q p alpha beta')
        self.constants = Constants(1.0, 0.1, 0.5, 0.5)

        Params = namedtuple('Params', 'prob_calc roads constants')
        self.params = Params(self.prob_calc, self.roads, self.constants)

    def test_selector(self):
        """Test that aco_road_selector selects the correct next city"""
        self.salesman.next_city_selector(self.params)
        self.assertEqual(len(self.salesman.trip), 2)
        next_city = self.salesman.trip[-1]
        self.assertEqual(next_city, self.c3)
示例#2
0
class SalesmanTests(unittest.TestCase):

    def setUp(self):
        """Set up a new salesman instance for testing."""
        self.city = City(1, 538, 732)
        self.salesman = Salesman(self.city, mock_selector)

    def test_create_new_salesman(self):
        """Test that salesman object instantiated correctly.

        Should be an instance of Salesman class.
        Should have a list of cities visited named trip containing
        only initial city.
        Should have trip_length of 0
        """
        self.assertIsInstance(self.salesman, Salesman)
        self.assertEqual(1, len(self.salesman.trip))
        self.assertEqual(self.salesman.trip[0], self.city)
        self.assertEqual(self.salesman.trip_length, 0)

    def test_trip_length(self):
        """Test that trip_length property returns correct value."""
        city2 = City(2, 349, 265)

        self.assertEqual(self.salesman.trip_length, 0)

        length = self.city.distance(city2) * 2
        self.salesman.trip.append(city2)
        self.assertEqual(self.salesman.trip_length, length)

    def test_next_city(self):
        """Test next_city method.

        next_city method should append new city returned from
        mock_selector to trip
        trip length should now be equal to the length between the
        two cities (there and back)
        a salesman instance that is not passed a selector method
        should raise a NotImplementedError
        """
        roads = []
        prob_calc = None
        params = (1, 0.5, 0.5, 1)
        selector_params = (roads, prob_calc, params)
        self.salesman.next_city_selector(selector_params)
        new_city = self.salesman.trip[-1]

        self.assertEqual(len(self.salesman.trip), 2)
        self.assertEqual(new_city.id, 2)
        self.assertEqual(new_city.coordinates, (349, 265))

        new_salesman = Salesman(new_city)
        self.assertRaises(NotImplementedError, lambda:
                          new_salesman.next_city_selector(selector_params))
示例#3
0
    def test_next_city(self):
        """Test next_city method.

        next_city method should append new city returned from
        mock_selector to trip
        trip length should now be equal to the length between the
        two cities (there and back)
        a salesman instance that is not passed a selector method
        should raise a NotImplementedError
        """
        roads = []
        prob_calc = None
        params = (1, 0.5, 0.5, 1)
        selector_params = (roads, prob_calc, params)
        self.salesman.next_city_selector(selector_params)
        new_city = self.salesman.trip[-1]

        self.assertEqual(len(self.salesman.trip), 2)
        self.assertEqual(new_city.id, 2)
        self.assertEqual(new_city.coordinates, (349, 265))

        new_salesman = Salesman(new_city)
        self.assertRaises(NotImplementedError, lambda:
                          new_salesman.next_city_selector(selector_params))
示例#4
0
    def setUp(self):
        """Set up road selector test."""
        self.prob_calc = aco_probability_calculator
        self.c1 = City(1, 538, 732)
        self.c2 = City(2, 349, 265)
        self.c3 = City(3, 446, 501)
        self.t = 0.5
        self.r1 = Road(self.c1, self.c2, self.t)
        self.r2 = Road(self.c1, self.c3, self.t)
        self.r3 = Road(self.c2, self.c3, self.t)
        self.roads = [self.r1, self.r2, self.r3]
        self.salesman = Salesman(self.c1, aco_road_selector)

        Constants = namedtuple('Constants', 'q p alpha beta')
        self.constants = Constants(1.0, 0.1, 0.5, 0.5)

        Params = namedtuple('Params', 'prob_calc roads constants')
        self.params = Params(self.prob_calc, self.roads, self.constants)
示例#5
0
 def setUp(self):
     """Set up a new salesman instance for testing."""
     self.city = City(1, 538, 732)
     self.salesman = Salesman(self.city, mock_selector)