Skip to content

Commit 2cfc1bc

Browse files
Fix variable names
1 parent be3e084 commit 2cfc1bc

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

genetic_algorithm/knapsack.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def evaluate(genome: genome_t, items: list[Item], capacity: int) -> tuple[int, i
126126
return total_value, total_weight
127127

128128

129-
def random_genome(n: int) -> genome_t:
129+
def random_genome(length: int) -> genome_t:
130130
"""
131131
Generates a random genome (list of 0/1) of length n.
132132
@@ -139,10 +139,10 @@ def random_genome(n: int) -> genome_t:
139139
>>> len(g), set(g).issubset({0, 1})
140140
(5, True)
141141
"""
142-
return [random.randint(0, 1) for _ in range(n)]
142+
return [random.randint(0, 1) for _ in range(length)]
143143

144144

145-
def selection(population: list[genome_t], fitnesses: list[int], k: int) -> genome_t:
145+
def selection(population: list[genome_t], fitnesses: list[int], tournament_k: int) -> genome_t:
146146
"""
147147
Performs tournament selection to choose a genome from the population.
148148
@@ -159,13 +159,13 @@ def selection(population: list[genome_t], fitnesses: list[int], k: int) -> genom
159159
>>> parent in pop
160160
True
161161
"""
162-
contenders = random.sample(list(zip(population, fitnesses)), k)
163-
get_fitness = lambda x: x[1]
162+
contenders = random.sample(list(zip(population, fitnesses)), tournament_k)
163+
get_fitness = lambda contender: contender[1]
164164
return max(contenders, key=get_fitness)[0][:]
165165

166166

167167
def crossover(
168-
a: genome_t, b: genome_t, p_crossover: float
168+
genome_1: genome_t, genome_2: genome_t, p_crossover: float
169169
) -> tuple[genome_t, genome_t]:
170170
"""
171171
Performs single-point crossover between two genomes.
@@ -192,14 +192,14 @@ def crossover(
192192
>>> c1, c2
193193
([0, 0, 0], [1, 1, 1])
194194
"""
195-
min_length = min(len(a), len(b))
195+
min_length = min(len(genome_1), len(genome_2))
196196
if random.random() > p_crossover or min_length < 2:
197-
return a[:], b[:]
197+
return genome_1[:], genome_2[:]
198198
cutoff_point = random.randint(1, min_length - 1)
199-
return a[:cutoff_point] + b[cutoff_point:], b[:cutoff_point] + a[cutoff_point:]
199+
return genome_1[:cutoff_point] + genome_2[cutoff_point:], genome_2[:cutoff_point] + genome_1[cutoff_point:]
200200

201201

202-
def mutation(g: genome_t, p_mutation: float) -> genome_t:
202+
def mutation(genome: genome_t, p_mutation: float) -> genome_t:
203203
"""
204204
Performs bit-flip mutation on a genome. Each bit flips with probability p_mutation.
205205
@@ -217,7 +217,7 @@ def mutation(g: genome_t, p_mutation: float) -> genome_t:
217217
>>> mutation([0, 1, 1, 0], p_mutation=0.0)
218218
[0, 1, 1, 0]
219219
"""
220-
return [(1 - gene) if random.random() < p_mutation else gene for gene in g]
220+
return [(1 - gene) if random.random() < p_mutation else gene for gene in genome]
221221

222222

223223
def run_ga(
@@ -290,8 +290,8 @@ def run_ga(
290290
# New generation
291291
new_pop = elites[:]
292292
while len(new_pop) < pop_size:
293-
parent1 = selection(population, fitnesses, k=tournament_k)
294-
parent2 = selection(population, fitnesses, k=tournament_k)
293+
parent1 = selection(population, fitnesses, tournament_k=tournament_k)
294+
parent2 = selection(population, fitnesses, tournament_k=tournament_k)
295295
child1, child2 = crossover(parent1, parent2, p_crossover)
296296
child1 = mutation(child1, p_mutation)
297297
child2 = mutation(child2, p_mutation)

0 commit comments

Comments
 (0)