def test_solution_valid_with_all_required_cities(self):
        required_cities = [
            self.possible_cities["Cordoba"], self.possible_cities["Rosario"],
            self.possible_cities["Ushuaia"]
        ]
        max_trip_time = 14
        route = [
            self.possible_cities["Cordoba"], self.possible_cities["Rosario"],
            self.possible_cities["Ushuaia"]
        ]
        solution = Solution(origin_city=self.origin_city,
                            cities=route,
                            required_cities=required_cities,
                            max_trip_time=max_trip_time)

        expected_total_stay_time = 6
        self.assertEqual(expected_total_stay_time,
                         solution.get_total_stay_time())

        expected_total_travel_time = 1 + 1 + 3 + 3
        self.assertEqual(expected_total_travel_time,
                         solution.get_total_travel_time())

        expected_total_trip_time = expected_total_stay_time + expected_total_travel_time
        self.assertEqual(expected_total_trip_time,
                         solution.get_total_trip_time())

        self.assertTrue(solution.is_valid_required_city())

        self.assertTrue(solution.is_valid_knapsack_time())

        self.assertTrue(solution.is_valid_total_trip_time())

        self.assertTrue(solution.is_valid())
    def test_solution_invalid_with_two_required_city_both_not_present(self):
        required_cities = [
            self.possible_cities["Cordoba"], self.possible_cities["Rosario"]
        ]
        max_trip_time = 999999
        route = [self.possible_cities["Ushuaia"]]
        solution = Solution(origin_city=self.origin_city,
                            cities=route,
                            required_cities=required_cities,
                            max_trip_time=max_trip_time)

        self.assertFalse(solution.is_valid_required_city())

        self.assertTrue(solution.is_valid_knapsack_time())

        self.assertTrue(solution.is_valid_total_trip_time())

        self.assertFalse(solution.is_valid())
Ejemplo n.º 3
0
    def get_valid_solution(self):
        while True:
            cities = random.sample(
                self.possible_trip_cities,
                random.randint(1, len(self.possible_trip_cities)))
            random.shuffle(cities)
            solution = Solution(self.origin_city, cities, self.required_cities,
                                self.max_trip_time)

            if not solution.is_valid_knapsack_time(
            ) or not solution.is_valid_required_city():
                continue

            optimizer = TSPOptimizerClosestCityStrategy(
                self.origin_city, solution.cities)
            solution.cities = optimizer.optimize()

            if solution.is_valid_total_trip_time():
                return solution
    def solve(self):
        best_solution_fitness = 0
        best_solution_trip_time = 0
        if self.should_print_stats():
            self.print_stats()
        for x in range(1, len(self.possible_trip_cities) + 1):
            for cities in combinations(self.possible_trip_cities, x):
                self.current_iteration += 1
                if self.should_print_stats():
                    self.print_stats()

                solution = Solution(self.origin_city, cities, self.required_cities,
                                    self.max_trip_time)
                if not solution.is_valid_knapsack_time() or not solution.is_valid_required_city():
                    continue

                # Apply construction heuristic
                optimizer = TSPOptimizerClosestCityStrategy(self.origin_city, solution.cities)
                solution.cities = optimizer.optimize()

                if not solution.is_valid_total_trip_time():
                    continue

                if solution.fitness > best_solution_fitness:
                    # Update best_solution as first objective fitness is better
                    best_solution = solution
                    self.set_best_solution(best_solution)
                    best_solution_fitness = solution.fitness
                    best_solution_trip_time = solution.get_total_travel_time()
                elif solution.fitness == best_solution_fitness:
                    if solution.get_total_travel_time() < best_solution_trip_time:
                        # Update best_solution as first objective fitness is same but
                        # second objective travel time is better
                        best_solution = solution
                        self.set_best_solution(best_solution)
                        best_solution_fitness = solution.fitness
                        best_solution_trip_time = solution.get_total_travel_time()
                # self.add_good_solution(solution)
        self.improve_solutions()
        # return self.best_solution, self.good_solutions
        return self.best_solution
Ejemplo n.º 5
0
    def create_random_solution(self):
        while True:
            cities = []
            cities += self.required_cities
            sample_size = random.randint(1, len(self.possible_cities))
            random_sample_cities = random.sample(self.possible_cities,
                                                 sample_size)
            for random_city in random_sample_cities:
                if random_city not in cities:
                    cities.append(random_city)
            random.shuffle(cities)
            solution = Solution(self.origin_city, cities, self.required_cities,
                                self.max_trip_time)

            if not solution.is_valid_knapsack_time(
            ) or not solution.is_valid_required_city():
                continue

            optimizer = TSPOptimizerClosestCityStrategy(
                self.origin_city, solution.cities)
            solution.cities = optimizer.optimize()

            if solution.is_valid_total_trip_time():
                return solution