Skip to content

Commit bbee49d

Browse files
committed
Refactor functions in function_optimization.py
1 parent 13d0e39 commit bbee49d

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

genetic_algorithm/function_optimization.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import random
8-
from typing import Callable, Tuple, List
8+
from collections.abc import Callable
99

1010
# Define the parameters for the genetic algorithm
1111
N_POPULATION = 100
@@ -14,7 +14,7 @@
1414
NUM_GENERATIONS = 50
1515

1616

17-
def evaluate(func: Callable, params: List[float]) -> float:
17+
def evaluate(func: Callable, params: list[float]) -> float:
1818
"""
1919
Evaluate the fitness of an individual based on the provided function.
2020
@@ -27,14 +27,14 @@ def evaluate(func: Callable, params: List[float]) -> float:
2727
return func(*params)
2828

2929

30-
def crossover(parent_1: List[float], parent_2: List[float]) -> List[float]:
30+
def crossover(parent_1: list[float], parent_2: list[float]) -> list[float]:
3131
"""Perform crossover between two parents."""
3232
crossover_point = random.randint(1, len(parent_1) - 1)
3333
child = parent_1[:crossover_point] + parent_2[crossover_point:]
3434
return child
3535

3636

37-
def mutate(individual: List[float], mutation_rate: float) -> List[float]:
37+
def mutate(individual: list[float], mutation_rate: float) -> list[float]:
3838
"""Mutate an individual with a certain probability."""
3939
mutated_individual = []
4040
for gene in individual:
@@ -48,8 +48,8 @@ def mutate(individual: List[float], mutation_rate: float) -> List[float]:
4848

4949

5050
def select(
51-
population: List[Tuple[List[float], float]], num_selected: int
52-
) -> List[List[float]]:
51+
population: list[tuple[list[float], float]], num_selected: int
52+
) -> list[list[float]]:
5353
"""Select individuals based on their fitness scores."""
5454
sorted_population = sorted(population, key=lambda x: x[1])
5555
selected_parents = [
@@ -60,10 +60,8 @@ def select(
6060

6161
def optimize(
6262
func: Callable,
63-
num_params: int,
64-
param_ranges: List[Tuple[float, float]],
65-
optimization_goal: str,
66-
) -> Tuple[List[float], float]:
63+
param_ranges: list[tuple[float, float]],
64+
) -> tuple[list[float], float]:
6765
"""Optimize the given function using a genetic algorithm."""
6866
# Initialize the population
6967
population = [
@@ -72,7 +70,7 @@ def optimize(
7270
]
7371

7472
# Main optimization loop
75-
for generation in range(NUM_GENERATIONS):
73+
for _generation in range(NUM_GENERATIONS):
7674
# Evaluate the fitness of each individual in the population
7775
population_fitness = [
7876
(individual, evaluate(func, individual)) for individual in population
@@ -83,7 +81,7 @@ def optimize(
8381

8482
# Generate offspring through crossover and mutation
8583
offspring = []
86-
for i in range(N_POPULATION):
84+
for _i in range(N_POPULATION):
8785
parent_1 = random.choice(selected_parents)
8886
parent_2 = random.choice(selected_parents)
8987
child = crossover(parent_1, parent_2)
@@ -109,8 +107,6 @@ def quadratic_function(x, y):
109107
return x**2 + y**2
110108

111109
param_ranges = [(-10, 10), (-10, 10)]
112-
best_params, best_fitness = optimize(
113-
quadratic_function, 2, param_ranges, "minimize"
114-
)
110+
best_params, best_fitness = optimize(quadratic_function, param_ranges)
115111
print("Best parameters:", best_params)
116112
print("Best fitness:", best_fitness)

0 commit comments

Comments
 (0)