Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1c37838

Browse files
committedOct 13, 2024·
Added type hints
1 parent c58431a commit 1c37838

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed
 

‎genetic_algorithm/traveling_salesman_problem.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
TOTAL_GENERATIONS = 100
2020

2121

22-
def readinp(filename):
22+
def readinp(filename: str) -> tsplib95.models.Problem:
2323
"""
2424
Loads the Traveling Salesman Problem (TSP) from the provided file using
2525
the tsplib95 package. This file should follow the .tsp format and contain
@@ -39,7 +39,7 @@ def readinp(filename):
3939
return problem
4040

4141

42-
def fitness(mem, dist_mat):
42+
def fitness(mem: list[int], dist_mat: np.ndarray) -> float:
4343
"""
4444
Calculates the fitness of a given solution (tour). The fitness is the
4545
inverse of the total distance of the tour. A shorter distance results in
@@ -66,7 +66,9 @@ def fitness(mem, dist_mat):
6666
return fitness
6767

6868

69-
def binary_tournament_selection(popln, dist_mat):
69+
def binary_tournament_selection(
70+
popln: list[list[int]], dist_mat: np.ndarray
71+
) -> tuple[list[int], list[int]]:
7072
"""
7173
Selects two parents from the population using binary tournament selection.
7274
Two individuals are randomly chosen, and the one with higher fitness is
@@ -86,7 +88,7 @@ def binary_tournament_selection(popln, dist_mat):
8688
>>> p1 in popln and p2 in popln
8789
True
8890
"""
89-
select = []
91+
select: list[list[int]] = []
9092
while len(select) != 2:
9193
i, j = random.sample(range(len(popln)), 2) # Randomly select two individuals
9294
if fitness(popln[i], dist_mat) > fitness(popln[j], dist_mat):
@@ -96,7 +98,9 @@ def binary_tournament_selection(popln, dist_mat):
9698
return select[0], select[1]
9799

98100

99-
def order_crossover(p1, p2, crossover_prob):
101+
def order_crossover(
102+
p1: list[int], p2: list[int], crossover_prob: float
103+
) -> tuple[list[int], list[int]]:
100104
"""
101105
Applies order crossover (OX) between two parent solutions with a certain
102106
probability. The OX method ensures that the relative order of the cities
@@ -122,8 +126,8 @@ def order_crossover(p1, p2, crossover_prob):
122126
[3, 1, 2, 0]
123127
"""
124128
if random.random() < crossover_prob: # Perform crossover with given probability
125-
c1 = [-1] * len(p1)
126-
c2 = [-1] * len(p2)
129+
c1: list[int] = [-1] * len(p1)
130+
c2: list[int] = [-1] * len(p2)
127131
start = random.randint(0, len(p1) - 1)
128132
end = random.randint(start + 1, len(p1))
129133
c1[start:end] = p1[start:end] # Copy a segment from parent 1 to child 1
@@ -144,7 +148,7 @@ def order_crossover(p1, p2, crossover_prob):
144148
return p1, p2 # No crossover, return the parents unchanged
145149

146150

147-
def swap_mutation(child, mutation_prob, num_cities):
151+
def swap_mutation(child: list[int], mutation_prob: float, num_cities: int) -> list[int]:
148152
"""
149153
Applies swap mutation to a child solution with a given probability.
150154
Two random cities in the tour are swapped to introduce variability.
@@ -169,7 +173,7 @@ def swap_mutation(child, mutation_prob, num_cities):
169173
return child
170174

171175

172-
def two_opt_local_search(child, dist_mat):
176+
def two_opt_local_search(child: list[int], dist_mat: np.ndarray) -> list[int]:
173177
"""
174178
Applies the 2-opt local search algorithm to improve a solution (tour).
175179
It repeatedly checks for pairs of edges in the tour and swaps them if
@@ -231,7 +235,7 @@ def two_opt_local_search(child, dist_mat):
231235
# Generate initial population (first city is taken to be depot by default):
232236
popln: list = []
233237
while len(popln) < TOTAL_POPULATION:
234-
sol_dist = 0
238+
sol_dist = 0.0
235239
individual = list(range(n_cities))
236240
subindiv = individual[1:]
237241
random.shuffle(subindiv)

0 commit comments

Comments
 (0)
Please sign in to comment.