def create_random_individual(self): cities = [] cities += self.required_cities random.shuffle(self.not_required_possible_cities) for random_city in self.not_required_possible_cities: if random_city not in cities: backup = list(cities) cities.append(random_city) solution = Solution(self.origin_city, cities, self.required_cities, self.max_trip_time) if not solution.is_valid_knapsack_time(): cities = backup continue optimizer = TSPOptimizerClosestCityStrategy(self.origin_city, cities) cities = optimizer.optimize() solution.cities = cities if not solution.is_valid_total_trip_time(): cities = backup continue solution = Solution(self.origin_city, cities, self.required_cities, self.max_trip_time) optimizer = TSPOptimizerClosestCityStrategy(self.origin_city, cities) solution.cities = optimizer.optimize() return solution
def test_solution_valid_with_required_cities(self): required_cities = [self.possible_cities["Cordoba"]] 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_valid_with_just_origin_city(self): required_cities = [] max_trip_time = 999999 route = [] solution = Solution(origin_city=self.origin_city, cities=route, required_cities=required_cities, max_trip_time=max_trip_time) expected_total_stay_time = 0 self.assertEqual(expected_total_stay_time, solution.get_total_stay_time()) expected_total_travel_time = 0 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())
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
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