Skip to content

Commit 4c793a0

Browse files
committed
fixed linting issues
1 parent 419a9d8 commit 4c793a0

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

coin_change_using_ga.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
"""Implementation of Coin change problem using Genetic Algorithm."""
2+
13
import numpy as np
24
import pandas as pd
35

46

57
class Chromosome:
8+
"""Class to manage individual chromosomes in genetic algorithm.
9+
"""
610

711
def __init__(self, size, N, denominations) -> None:
812
self.N = N
@@ -14,14 +18,14 @@ def __init__(self, size, N, denominations) -> None:
1418
def fitness(self):
1519
return 1 / (1 + np.abs(np.sum(self.genes * self.denominations) - self.N))
1620

17-
def __lt__(self, __o: object) -> bool:
18-
return self.fitness > __o.fitness
21+
def __lt__(self, o: object) -> bool:
22+
return self.fitness > o.fitness
1923

20-
def __eq__(self, __o: object) -> bool:
21-
return self.fitness == __o.fitness
24+
def __eq__(self, o: object) -> bool:
25+
return self.fitness == o.fitness
2226

23-
def __gt__(self, __o: object) -> bool:
24-
return self.fitness < __o.fitness
27+
def __gt__(self, o: object) -> bool:
28+
return self.fitness < o.fitness
2529

2630
def single_point_crossover(self, chromosome):
2731
crossover_point = np.random.randint(1, self.size - 1)
@@ -40,6 +44,8 @@ def mutate(self, mutation_probability):
4044

4145

4246
class GeneticAlgorithm:
47+
"""Class to manage genetic algorithm for 0/1 Knapsack problem.
48+
"""
4349

4450
def __init__(self,
4551
population_size,
@@ -85,10 +91,9 @@ def evolve(self, log_freq=1000):
8591
generations = 0
8692
while self.fittest_chromosome().fitness < 1:
8793
ga.next_generation()
88-
if generations % 100 == 0:
89-
print(
90-
f'Generation {generations}: Max fitness = {self.fittest_chromosome().fitness}'
91-
)
94+
if generations % log_freq == 0:
95+
max_fitness = self.fittest_chromosome().fitness
96+
print(f'Generation {generations}: Max fitness = {max_fitness}')
9297
generations += 1
9398
return self.fittest_chromosome()
9499

@@ -105,4 +110,4 @@ def evolve(self, log_freq=1000):
105110
soln_table = pd.DataFrame(columns=['Denominations', 'Count'])
106111
soln_table['Denominations'] = denominations
107112
soln_table['Count'] = solution.genes
108-
print(soln_table.to_string(index=False))
113+
print(soln_table.to_string(index=False))

0 commit comments

Comments
 (0)