Skip to content
Closed
Prev Previous commit
Next Next commit
Update genetic_algorithm_optimization.py
  • Loading branch information
UTSAVS26 authored Oct 2, 2024
commit 7b85f11cd25b5eb68b1746d769cee65243b25761
67 changes: 33 additions & 34 deletions genetic_algorithm/genetic_algorithm_optimization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import random
import numpy as np
import random
from concurrent.futures import ThreadPoolExecutor

# Parameters

Check failure on line 5 in genetic_algorithm/genetic_algorithm_optimization.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

genetic_algorithm/genetic_algorithm_optimization.py:1:1: I001 Import block is un-sorted or un-formatted
N_POPULATION = 100 # Population size
N_GENERATIONS = 500 # Maximum number of generations
N_SELECTED = 50 # Number of parents selected for the next generation
Expand All @@ -9,8 +10,9 @@
CROSSOVER_RATE = 0.8 # Probability of crossover
SEARCH_SPACE = (-10, 10) # Search space for the variables

# Random number generator
rng = np.random.default_rng()

# Genetic Algorithm for Function Optimization
class GeneticAlgorithm:
def __init__(

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:

self,
Expand All @@ -35,11 +37,9 @@
self.population = self.initialize_population()

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/genetic_algorithm_optimization.py, please provide doctest for the function initialize_population

# Generate random initial population within the search space
# Generate random initial population within the search space using the generator
return [
np.random.uniform(
low=self.bounds[i][0], high=self.bounds[i][1], size=self.dim
)
rng.uniform(low=self.bounds[i][0], high=self.bounds[i][1], size=self.dim)
for i in range(self.population_size)
]

Expand All @@ -48,14 +48,10 @@
value = self.function(*individual)
return value if self.maximize else -value # If minimizing, invert the fitness

def select_parents(self):
# Rank individuals based on fitness and select top individuals for mating
scores = [
(individual, self.fitness(individual)) for individual in self.population
]
scores.sort(key=lambda x: x[1], reverse=True)
selected = [ind for ind, _ in scores[:N_SELECTED]]
return selected
def select_parents(self, population_score):

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: select_parents. 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/genetic_algorithm_optimization.py, please provide doctest for the function select_parents

Please provide type hint for the parameter: population_score

# Select top N_SELECTED parents based on fitness
population_score.sort(key=lambda x: x[1], reverse=True)

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

return [ind for ind, _ in population_score[:N_SELECTED]]

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/genetic_algorithm_optimization.py, please provide doctest for the function crossover

Please provide type hint for the parameter: parent1

Please provide type hint for the parameter: parent2

# Perform uniform crossover
Expand All @@ -67,16 +63,28 @@
return parent1, parent2

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/genetic_algorithm_optimization.py, please provide doctest for the function mutate

Please provide type hint for the parameter: individual

# Apply mutation to an individual with some probability
# Apply mutation to an individual using the new random generator
for i in range(self.dim):
if random.random() < self.mutation_prob:
individual[i] = np.random.uniform(self.bounds[i][0], self.bounds[i][1])
individual[i] = rng.uniform(self.bounds[i][0], self.bounds[i][1])
return individual

def evaluate_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: evaluate_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/genetic_algorithm_optimization.py, please provide doctest for the function evaluate_population

# Multithreaded evaluation of population fitness
with ThreadPoolExecutor() as executor:
return list(executor.map(lambda ind: (ind, self.fitness(ind)), self.population))

Check failure on line 75 in genetic_algorithm/genetic_algorithm_optimization.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

genetic_algorithm/genetic_algorithm_optimization.py:75:89: E501 Line too long (92 > 88)

def evolve(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: evolve. 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/genetic_algorithm_optimization.py, please provide doctest for the function evolve

for generation in range(self.generations):
# Select parents based on fitness
parents = self.select_parents()
# Evaluate population fitness (multithreaded)
population_score = self.evaluate_population()

# Check the best individual
best_individual = max(population_score, key=lambda x: x[1])[0]

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

best_fitness = self.fitness(best_individual)

# Select parents for next generation
parents = self.select_parents(population_score)
next_generation = []

# Generate offspring using crossover and mutation
Expand All @@ -87,42 +95,33 @@
next_generation.append(self.mutate(child2))

# Ensure population size remains the same
self.population = next_generation[: self.population_size]

# Track the best solution so far
best_individual = max(self.population, key=self.fitness)
best_fitness = self.fitness(best_individual)
self.population = next_generation[:self.population_size]

if generation % 10 == 0:
print(
f"Generation {generation}: Best Fitness = {best_fitness}, Best Individual = {best_individual}"
)
print(f"Generation {generation}: Best Fitness = {best_fitness}")

# Return the best individual found
return max(self.population, key=self.fitness)
return best_individual


# Define a sample function to optimize (e.g., minimize the sum of squares)
# Example target function for optimization
def target_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: target_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/genetic_algorithm_optimization.py, please provide doctest for the function target_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 # Example: simple parabolic surface (minimization)
return x**2 + y**2 # Simple parabolic surface (minimization)


# Set bounds for the variables (x, y)
bounds = [(-10, 10), (-10, 10)] # Both x and y range from -10 to 10

# Instantiate the genetic algorithm
# Instantiate and run the genetic algorithm
ga = GeneticAlgorithm(
function=target_function,
bounds=bounds,
population_size=N_POPULATION,
generations=N_GENERATIONS,
mutation_prob=MUTATION_PROBABILITY,
crossover_rate=CROSSOVER_RATE,
maximize=False, # Set to False for minimization
maximize=False # Minimize the function
)

# Run the genetic algorithm and find the optimal solution
best_solution = ga.evolve()

print(f"Best solution found: {best_solution}")
print(f"Best fitness (minimum value of function): {target_function(*best_solution)}")
Loading