Skip to content

Commit

Permalink
Update salesman_traveling.py
Browse files Browse the repository at this point in the history
  • Loading branch information
alesac12 authored Dec 10, 2023
1 parent b412655 commit 3f662ed
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions travelpy/salesman_traveling.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
"""
Module for solving the Traveling Salesman Problem (TSP) using brute force.
"""

from itertools import permutations

def city_permutation(cities):
"""Function that generates all the permutations of the cities to be traveled"""
all_permutations = permutations(range(1, cities))
all_permutations = permutations(range(1, cities))
for permutation in all_permutations:
yield (0,) + permutation + (0,)
yield (0,) + permutation + (0,)

def cost(matrix_distance, route):
"""Gives you the prices of all the routes that we have generated in city permutations."""
Expand All @@ -15,9 +19,8 @@ def cost(matrix_distance, route):

def salesman_traveling(cities, matrix_distance):
"""Solves the Traveling Salesman Problem (TSP) using brute force"""
opt_c = float('inf') # Inicializar con un valor alto para comparación
opt_c = float('inf')
opt_x = []

for route in city_permutation(cities):
c = cost(matrix_distance, route)
if c < opt_c:
Expand Down

0 comments on commit 3f662ed

Please sign in to comment.