Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions genetic_algorithm/ga_optimisation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import numpy as np


class GeneticAlgorithm:
def __init__(
self,
func,
bounds,
pop_size=20,
generations=100,
mutation_rate=0.1,
crossover_rate=0.8,
selection_method="tournament",
):
self.func = func
self.bounds = bounds
self.pop_size = pop_size
self.generations = generations
self.mutation_rate = mutation_rate
self.crossover_rate = crossover_rate
self.selection_method = selection_method

def initialize_population(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: initialize_population. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function initialize_population

# Create random population within the bounds
dim = len(self.bounds)
return np.array(
[
np.random.uniform(self.bounds[d][0], self.bounds[d][1], self.pop_size)

Check failure on line 28 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:28:17: NPY002 Replace legacy `np.random.uniform` call with `np.random.Generator`
for d in range(dim)
]
).T

def fitness(self, individual):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: fitness. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function fitness

Please provide type hint for the parameter: individual

# Calculate fitness (for minimization, return function value)
return self.func(*individual)

def selection(self, population, fitness_values):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: selection. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function selection

Please provide type hint for the parameter: population

Please provide type hint for the parameter: fitness_values

if self.selection_method == "tournament":
return self.tournament_selection(population, fitness_values)
elif self.selection_method == "roulette":
return self.roulette_wheel_selection(population, fitness_values)

def tournament_selection(self, population, fitness_values):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: tournament_selection. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function tournament_selection

Please provide type hint for the parameter: population

Please provide type hint for the parameter: fitness_values

indices = np.random.choice(len(population), size=2, replace=False)

Check failure on line 44 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:44:19: NPY002 Replace legacy `np.random.choice` call with `np.random.Generator`
return (
population[indices[0]]
if fitness_values[indices[0]] < fitness_values[indices[1]]
else population[indices[1]]
)

def roulette_wheel_selection(self, population, fitness_values):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: roulette_wheel_selection. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function roulette_wheel_selection

Please provide type hint for the parameter: population

Please provide type hint for the parameter: fitness_values

fitness_sum = np.sum(1.0 / (1 + fitness_values)) # inverse for minimization
pick = np.random.uniform(0, fitness_sum)

Check failure on line 53 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:53:16: NPY002 Replace legacy `np.random.uniform` call with `np.random.Generator`
current = 0
for individual, fitness in zip(population, fitness_values):
current += 1.0 / (1 + fitness)
if current > pick:
return individual

def crossover(self, parent1, parent2):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: crossover. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function crossover

Please provide type hint for the parameter: parent1

Please provide type hint for the parameter: parent2

if np.random.rand() < self.crossover_rate:

Check failure on line 61 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:61:12: NPY002 Replace legacy `np.random.rand` call with `np.random.Generator`
# Uniform crossover
mask = np.random.rand(len(parent1)) < 0.5

Check failure on line 63 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:63:20: NPY002 Replace legacy `np.random.rand` call with `np.random.Generator`
child = np.where(mask, parent1, parent2)
return child
return parent1

def mutate(self, individual):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: mutate. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function mutate

Please provide type hint for the parameter: individual

for i in range(len(individual)):
if np.random.rand() < self.mutation_rate:

Check failure on line 70 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:70:16: NPY002 Replace legacy `np.random.rand` call with `np.random.Generator`
individual[i] += np.random.uniform(-1, 1)

Check failure on line 71 in genetic_algorithm/ga_optimisation.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (NPY002)

genetic_algorithm/ga_optimisation.py:71:34: NPY002 Replace legacy `np.random.uniform` call with `np.random.Generator`
individual[i] = np.clip(
individual[i], self.bounds[i][0], self.bounds[i][1]
)
return individual

def run(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: run. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function run

population = self.initialize_population()
best_individual = None
best_fitness = float("inf")

for generation in range(self.generations):
fitness_values = np.array([self.fitness(ind) for ind in population])

# Track the best solution
current_best_index = np.argmin(fitness_values)
if fitness_values[current_best_index] < best_fitness:
best_fitness = fitness_values[current_best_index]
best_individual = population[current_best_index]

# Create new generation
new_population = []
for _ in range(self.pop_size):
parent1 = self.selection(population, fitness_values)
parent2 = self.selection(population, fitness_values)
child = self.crossover(parent1, parent2)
child = self.mutate(child)
new_population.append(child)

population = np.array(new_population)

# Termination Condition: Minimal improvement
if (
generation > 1
and abs(best_fitness - self.fitness(best_individual)) < 1e-6
):
break

return best_individual, best_fitness


# Example Usage:
def my_function(x, y):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: my_function. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file genetic_algorithm/ga_optimisation.py, please provide doctest for the function my_function

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

return x**2 + y**2


bounds = [(-10, 10), (-10, 10)]
ga = GeneticAlgorithm(func=my_function, bounds=bounds, pop_size=30, generations=100)
best_solution, best_value = ga.run()
print(f"Optimized Solution: {best_solution}, Function Value: {best_value}")
51 changes: 51 additions & 0 deletions graphs/kruskal_algo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class DisjointSet:
def __init__(self, n):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: __init__. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: n

Please provide descriptive name for the parameter: n

self.parent = list(range(n))
self.rank = [0] * n

def find(self, u):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: find. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file graphs/kruskal_algo.py, please provide doctest for the function find

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

if self.parent[u] != u:
self.parent[u] = self.find(self.parent[u])
return self.parent[u]

def union(self, u, v):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: union. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file graphs/kruskal_algo.py, please provide doctest for the function union

Please provide type hint for the parameter: u

Please provide descriptive name for the parameter: u

Please provide type hint for the parameter: v

Please provide descriptive name for the parameter: v

root_u = self.find(u)
if root_u != (root_v := self.find(v)):
# Union by rank
if self.rank[root_u] > self.rank[root_v]:
self.parent[root_v] = root_u
elif self.rank[root_u] < self.rank[root_v]:
self.parent[root_u] = root_v
else:
self.parent[root_v] = root_u
self.rank[root_u] += 1


def kruskal_algorithm(vertices, edges):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: kruskal_algorithm. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file graphs/kruskal_algo.py, please provide doctest for the function kruskal_algorithm

Please provide type hint for the parameter: vertices

Please provide type hint for the parameter: edges

# Step 1: Sort edges based on weight
edges.sort(key=lambda x: x[2])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: x


# Step 2: Initialize Disjoint Set
ds = DisjointSet(vertices)

mst = []
mst_weight = 0

# Step 3: Iterate through sorted edges
for u, v, weight in edges:
# Check if adding this edge creates a cycle
if ds.find(u) != ds.find(v):
ds.union(u, v)
mst.append((u, v, weight))
mst_weight += weight

return mst, mst_weight


# Example usage:
vertices = 4
edges = [(0, 1, 10), (0, 2, 6), (0, 3, 5), (1, 3, 15), (2, 3, 4)]

mst, total_weight = kruskal_algorithm(vertices, edges)
print("Edges in the MST:", mst)
print("Total weight of the MST:", total_weight)
Loading