def testOnePoint(self): a = MockPoint() actual = days_permutations.DaysPermutations([a], [True, True, True]) self.assertEqual(3, len(actual)) self.assertEqual({0: [a]}, dict(actual[0])) self.assertEqual({1: [a]}, dict(actual[1])) self.assertEqual({2: [a]}, dict(actual[2]))
def _PushPointsToDayVisitsImpl(depth, points, days_consider, day_visits, points_left, could_push, city_visit_heap, day_visit_parameterss, points_left_consistent): assert len(days_consider) == len(day_visits) assert len(day_visits) == len(day_visit_parameterss) for days_permutation in days_permutations.DaysPermutations( points, days_consider): # Initialize structure for next iteration. next_points_left = [] next_day_visits_consider = days_consider[:] next_day_visits = day_visits[:] # Try to fit to each day its points. for i, day_points_add in days_permutation.items(): day_points_all = day_visits[i].GetPoints() day_points_all.extend(day_points_add) day_visit_best, points_left_best = ( _day_visit_router.RouteDayVisit(day_points_all, day_visit_parameterss[i])) assert isinstance(day_visit_best, city_visit.DayVisitInterface) for point_left_best in points_left_best: assert isinstance(point_left_best, point_.PointInterface) next_points_left.extend(points_left_best) next_day_visits_consider[i] = False next_day_visits = (next_day_visits[:i] + [day_visit_best] + next_day_visits[i + 1:]) if len(next_points_left) > 1: print( 'More than one point left after adding to existing DayVisits!') # NOTE(igushev): The only option when next_points_left are the same as # input points, it than each corresponding day has not fit its points. The # recursive call will check other days, which should be covered by this # level of permutation. # NOTE(igushev): If maximum depth of recursion or no next_points_left, add # a potential result. if (set(next_points_left) == set(points) or depth == _max_depth or not next_points_left or all(not day_visit_consider for day_visit_consider in next_day_visits_consider)): city_visit_points_left = ( _city_visit_points_left_generator.Generate( next_day_visits, day_visit_parameterss, points_left + next_points_left)) city_visit_heap.PushCityVisit(city_visit_points_left) # If next_points_left are only the ones we consistently can't push, # consider this iteration successful and assign could_push True. if set(next_points_left) <= points_left_consistent: could_push.value = True continue # NOTE(igushev): Recursion call. _PushPointsToDayVisitsImpl(depth + 1, next_points_left, next_day_visits_consider, next_day_visits, points_left, could_push, city_visit_heap, day_visit_parameterss, points_left_consistent)
def testThreePoints(self): a, b, c = MockPoint(), MockPoint(), MockPoint() actual = days_permutations.DaysPermutations([a, b, c], [True, False, True]) self.assertEqual(8, len(actual)) self.assertEqual({0: [a, b, c]}, dict(actual[0])) self.assertEqual({0: [a, b], 2: [c]}, dict(actual[1])) self.assertEqual({0: [a, c], 2: [b]}, dict(actual[2])) self.assertEqual({0: [a], 2: [b, c]}, dict(actual[3])) self.assertEqual({0: [b, c], 2: [a]}, dict(actual[4])) self.assertEqual({0: [b], 2: [a, c]}, dict(actual[5])) self.assertEqual({0: [c], 2: [a, b]}, dict(actual[6])) self.assertEqual({2: [a, b, c]}, dict(actual[7]))
def testTwoPoint(self): a, b = MockPoint(), MockPoint() actual = days_permutations.DaysPermutations([a, b], [True, True, True]) self.assertEqual(9, len(actual)) self.assertEqual({0: [a, b]}, dict(actual[0])) self.assertEqual({0: [a], 1: [b]}, dict(actual[1])) self.assertEqual({0: [a], 2: [b]}, dict(actual[2])) self.assertEqual({1: [a], 0: [b]}, dict(actual[3])) self.assertEqual({1: [a, b]}, dict(actual[4])) self.assertEqual({1: [a], 2: [b]}, dict(actual[5])) self.assertEqual({2: [a], 0: [b]}, dict(actual[6])) self.assertEqual({2: [a], 1: [b]}, dict(actual[7])) self.assertEqual({2: [a, b]}, dict(actual[8]))
def testOneDay(self): a, b, c = MockPoint(), MockPoint(), MockPoint() actual = days_permutations.DaysPermutations([a, b, c], [True]) self.assertEqual(1, len(actual)) self.assertEqual({0: [a, b, c]}, dict(actual[0]))