From 67a5a24a62b2a5b1d19be48a927154953bc5adfc Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:03:18 +0530 Subject: [PATCH 01/17] Add files via upload --- genetic_algorithm/generic_optimisation.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 genetic_algorithm/generic_optimisation.py diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py new file mode 100644 index 000000000000..b8cede56af5c --- /dev/null +++ b/genetic_algorithm/generic_optimisation.py @@ -0,0 +1,24 @@ +def fitness_function(input_value: float) -> float: + """ + Calculate the fitness (objective) function value for a given input. + + Args: + input_value (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Raises: + ValueError: If the input is not a valid floating-point number. + + Example: + >>> fitness_function(2.5) + 12.25 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(input_value, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return input_value**2 + 3 * input_value + 2 From 91710b85220e849ea6f732d94ecca3396a6fe5a4 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:27:45 +0530 Subject: [PATCH 02/17] Delete genetic_algorithm/generic_optimisation.py --- genetic_algorithm/generic_optimisation.py | 24 ----------------------- 1 file changed, 24 deletions(-) delete mode 100644 genetic_algorithm/generic_optimisation.py diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py deleted file mode 100644 index b8cede56af5c..000000000000 --- a/genetic_algorithm/generic_optimisation.py +++ /dev/null @@ -1,24 +0,0 @@ -def fitness_function(input_value: float) -> float: - """ - Calculate the fitness (objective) function value for a given input. - - Args: - input_value (float): The input value for which the fitness is calculated. - - Returns: - float: The fitness value calculated for the input. - - Raises: - ValueError: If the input is not a valid floating-point number. - - Example: - >>> fitness_function(2.5) - 12.25 - >>> fitness_function(-1.0) - 6.0 - """ - if not isinstance(input_value, (int, float)): - raise ValueError("Input must be a valid number.") - - # Define your fitness function here (e.g., x^2, or any other function) - return input_value**2 + 3 * input_value + 2 From dc3bf5ca90ad53cd987f7c5263203032a4e5f93c Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:28:06 +0530 Subject: [PATCH 03/17] Add files via upload --- genetic_algorithm/generic_optimisation.py | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 genetic_algorithm/generic_optimisation.py diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py new file mode 100644 index 000000000000..1bd8db533de9 --- /dev/null +++ b/genetic_algorithm/generic_optimisation.py @@ -0,0 +1,34 @@ +# Remove the unused import statement +# import random + +# Your code here +def fitness_function(x: float) -> float: + """ + Calculate the fitness (objective) function value for a given input. + + Args: + x (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Raises: + ValueError: If the input is not a valid floating-point number. + + Example: + >>> fitness_function(2.5) + 12.25 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(x, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return x**2 + 3*x + 2 + +if __name__ == "__main__": + # Example usage + x_value = float(input("Enter the value of x: ").strip()) + fitness = fitness_function(x_value) + print(f"The fitness for x = {x_value} is {fitness}.") From 0e34537ee2f5ac53513b6e539770213a6bbf6f89 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 06:59:17 +0000 Subject: [PATCH 04/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- genetic_algorithm/generic_optimisation.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py index 1bd8db533de9..bcdddf8382fe 100644 --- a/genetic_algorithm/generic_optimisation.py +++ b/genetic_algorithm/generic_optimisation.py @@ -1,11 +1,12 @@ # Remove the unused import statement # import random + # Your code here def fitness_function(x: float) -> float: """ Calculate the fitness (objective) function value for a given input. - + Args: x (float): The input value for which the fitness is calculated. @@ -23,9 +24,10 @@ def fitness_function(x: float) -> float: """ if not isinstance(x, (int, float)): raise ValueError("Input must be a valid number.") - + # Define your fitness function here (e.g., x^2, or any other function) - return x**2 + 3*x + 2 + return x**2 + 3 * x + 2 + if __name__ == "__main__": # Example usage From cb00f6b54bb7581e84fe3babc9709c121e4e12ab Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:31:18 +0530 Subject: [PATCH 05/17] Delete genetic_algorithm/generic_optimisation.py --- genetic_algorithm/generic_optimisation.py | 36 ----------------------- 1 file changed, 36 deletions(-) delete mode 100644 genetic_algorithm/generic_optimisation.py diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py deleted file mode 100644 index bcdddf8382fe..000000000000 --- a/genetic_algorithm/generic_optimisation.py +++ /dev/null @@ -1,36 +0,0 @@ -# Remove the unused import statement -# import random - - -# Your code here -def fitness_function(x: float) -> float: - """ - Calculate the fitness (objective) function value for a given input. - - Args: - x (float): The input value for which the fitness is calculated. - - Returns: - float: The fitness value calculated for the input. - - Raises: - ValueError: If the input is not a valid floating-point number. - - Example: - >>> fitness_function(2.5) - 12.25 - >>> fitness_function(-1.0) - 6.0 - """ - if not isinstance(x, (int, float)): - raise ValueError("Input must be a valid number.") - - # Define your fitness function here (e.g., x^2, or any other function) - return x**2 + 3 * x + 2 - - -if __name__ == "__main__": - # Example usage - x_value = float(input("Enter the value of x: ").strip()) - fitness = fitness_function(x_value) - print(f"The fitness for x = {x_value} is {fitness}.") From f223b99d05e3f3d096e0a98dba813188164d8601 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:31:36 +0530 Subject: [PATCH 06/17] Add files via upload --- genetic_algorithm/generic_optimisation.py | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 genetic_algorithm/generic_optimisation.py diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py new file mode 100644 index 000000000000..3cb7f937c103 --- /dev/null +++ b/genetic_algorithm/generic_optimisation.py @@ -0,0 +1,31 @@ +def fitness_function(input_value: float) -> float: + """ + Calculate the fitness (objective) function value for a given input. + + Args: + input_value (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Raises: + ValueError: If the input is not a valid floating-point number. + + Example: + >>> fitness_function(2.5) + 12.25 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(input_value, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return input_value**2 + 3 * input_value + 2 + + +if __name__ == "__main__": + # Example usage + x_value = float(input("Enter the value of input_value: ").strip()) + fitness = fitness_function(x_value) + print(f"The fitness for = {x_value} is {fitness}.") From ef5039920a391831c9a2256ac5301266cd5c39d5 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:09:05 +0530 Subject: [PATCH 07/17] Delete genetic_algorithm/generic_optimisation.py --- genetic_algorithm/generic_optimisation.py | 31 ----------------------- 1 file changed, 31 deletions(-) delete mode 100644 genetic_algorithm/generic_optimisation.py diff --git a/genetic_algorithm/generic_optimisation.py b/genetic_algorithm/generic_optimisation.py deleted file mode 100644 index 3cb7f937c103..000000000000 --- a/genetic_algorithm/generic_optimisation.py +++ /dev/null @@ -1,31 +0,0 @@ -def fitness_function(input_value: float) -> float: - """ - Calculate the fitness (objective) function value for a given input. - - Args: - input_value (float): The input value for which the fitness is calculated. - - Returns: - float: The fitness value calculated for the input. - - Raises: - ValueError: If the input is not a valid floating-point number. - - Example: - >>> fitness_function(2.5) - 12.25 - >>> fitness_function(-1.0) - 6.0 - """ - if not isinstance(input_value, (int, float)): - raise ValueError("Input must be a valid number.") - - # Define your fitness function here (e.g., x^2, or any other function) - return input_value**2 + 3 * input_value + 2 - - -if __name__ == "__main__": - # Example usage - x_value = float(input("Enter the value of input_value: ").strip()) - fitness = fitness_function(x_value) - print(f"The fitness for = {x_value} is {fitness}.") From 3369e6e275f2747f6cd6cb628d63a49b80c5560e Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:09:53 +0530 Subject: [PATCH 08/17] Add files via upload --- genetic_algorithm/basic_number.py | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py new file mode 100644 index 000000000000..cb69b20493ef --- /dev/null +++ b/genetic_algorithm/basic_number.py @@ -0,0 +1,62 @@ +import doctest +import random + + +def fitness_function(input_value: float) -> float: + """ + Calculate the fitness (objective) function value for a given input. + + Args: + input_value (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Raises: + ValueError: If the input is not a valid floating-point number. + + Example: + >>> fitness_function(2.5) + 0.75 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(input_value, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return input_value**2 - 3 * input_value + 2 + + +def genetic_algorithm(): + """ + >>> random.seed(42) + >>> num_runs = 10 + >>> best_solutions = [] + >>> best_fitnesses = [] + >>> for _ in range(num_runs): + ... best_solution, best_fitness = genetic_algorithm() + ... best_solutions.append(best_solution) + ... best_fitnesses.append(best_fitness) + >>> average_best_solution = sum(best_solutions) / num_runs + >>> average_best_fitness = sum(best_fitnesses) / num_runs + >>> abs(average_best_solution - (-1.45)) < 0.5 + False + >>> abs(average_best_fitness - 6.0) < 0.5 + False + """ + + population = [random.uniform(-2, 2) for _ in range(100)] + best_solution = min(population, key=fitness_function) + best_fitness = fitness_function(best_solution) + return best_solution, best_fitness + + +if __name__ == "__main__": + # Example usage + input_value = float(input("Enter the value of input_value: ").strip()) + fitness = fitness_function(input_value) + print(f"The fitness for input_value = {input_value} is {fitness}.") + + # Run the doctests + doctest.testmod() From ea6d61b98248b45d8a3581fff5e3a19642c6fd8d Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:25:37 +0530 Subject: [PATCH 09/17] Update basic_number.py --- genetic_algorithm/basic_number.py | 64 +++++++++++++++++++------------ 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py index cb69b20493ef..3975eeb54110 100644 --- a/genetic_algorithm/basic_number.py +++ b/genetic_algorithm/basic_number.py @@ -1,8 +1,8 @@ -import doctest import random +import doctest -def fitness_function(input_value: float) -> float: +def fitness_function(input_value): """ Calculate the fitness (objective) function value for a given input. @@ -12,9 +12,6 @@ def fitness_function(input_value: float) -> float: Returns: float: The fitness value calculated for the input. - Raises: - ValueError: If the input is not a valid floating-point number. - Example: >>> fitness_function(2.5) 0.75 @@ -28,35 +25,54 @@ def fitness_function(input_value: float) -> float: return input_value**2 - 3 * input_value + 2 -def genetic_algorithm(): +def genetic_algorithm() -> tuple[float, float]: """ - >>> random.seed(42) - >>> num_runs = 10 - >>> best_solutions = [] - >>> best_fitnesses = [] - >>> for _ in range(num_runs): - ... best_solution, best_fitness = genetic_algorithm() - ... best_solutions.append(best_solution) - ... best_fitnesses.append(best_fitness) - >>> average_best_solution = sum(best_solutions) / num_runs - >>> average_best_fitness = sum(best_fitnesses) / num_runs - >>> abs(average_best_solution - (-1.45)) < 0.5 + Optimize a numeric value using a simple genetic algorithm. + + Returns: + tuple[float, float]: A tuple containing the best solution and its fitness value. + + Example: + >>> best_solution, best_fitness = genetic_algorithm() + >>> -1.5 < best_solution < -1.4 False - >>> abs(average_best_fitness - 6.0) < 0.5 + >>> 6 < best_fitness < 6.1 False """ + population_size = 50 + mutation_rate = 0.1 + num_generations = 100 + + population = [random.uniform(-10, 10) for _ in range(population_size)] + + for generation in range(num_generations): + fitness_scores = [fitness_function(individual) for individual in population] - population = [random.uniform(-2, 2) for _ in range(100)] - best_solution = min(population, key=fitness_function) + selected_population = [] + for _ in range(population_size): + tournament_size = 5 + tournament = random.sample(range(population_size), tournament_size) + tournament_fitness = [fitness_scores[i] for i in tournament] + selected_individual = population[ + tournament[tournament_fitness.index(max(tournament_fitness))] + ] + selected_population.append(selected_individual) + + for i in range(population_size): + if random.random() < mutation_rate: + selected_population[i] += random.uniform(-0.5, 0.5) + + population = selected_population + + best_solution = max(population, key=fitness_function) best_fitness = fitness_function(best_solution) + return best_solution, best_fitness if __name__ == "__main__": - # Example usage + # Run the doctests + doctest.testmod() input_value = float(input("Enter the value of input_value: ").strip()) fitness = fitness_function(input_value) print(f"The fitness for input_value = {input_value} is {fitness}.") - - # Run the doctests - doctest.testmod() From 71601ed1be7fca369595319d77ba4293df40b659 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:30:06 +0530 Subject: [PATCH 10/17] Delete genetic_algorithm/basic_number.py --- genetic_algorithm/basic_number.py | 78 ------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py deleted file mode 100644 index 3975eeb54110..000000000000 --- a/genetic_algorithm/basic_number.py +++ /dev/null @@ -1,78 +0,0 @@ -import random -import doctest - - -def fitness_function(input_value): - """ - Calculate the fitness (objective) function value for a given input. - - Args: - input_value (float): The input value for which the fitness is calculated. - - Returns: - float: The fitness value calculated for the input. - - Example: - >>> fitness_function(2.5) - 0.75 - >>> fitness_function(-1.0) - 6.0 - """ - if not isinstance(input_value, (int, float)): - raise ValueError("Input must be a valid number.") - - # Define your fitness function here (e.g., x^2, or any other function) - return input_value**2 - 3 * input_value + 2 - - -def genetic_algorithm() -> tuple[float, float]: - """ - Optimize a numeric value using a simple genetic algorithm. - - Returns: - tuple[float, float]: A tuple containing the best solution and its fitness value. - - Example: - >>> best_solution, best_fitness = genetic_algorithm() - >>> -1.5 < best_solution < -1.4 - False - >>> 6 < best_fitness < 6.1 - False - """ - population_size = 50 - mutation_rate = 0.1 - num_generations = 100 - - population = [random.uniform(-10, 10) for _ in range(population_size)] - - for generation in range(num_generations): - fitness_scores = [fitness_function(individual) for individual in population] - - selected_population = [] - for _ in range(population_size): - tournament_size = 5 - tournament = random.sample(range(population_size), tournament_size) - tournament_fitness = [fitness_scores[i] for i in tournament] - selected_individual = population[ - tournament[tournament_fitness.index(max(tournament_fitness))] - ] - selected_population.append(selected_individual) - - for i in range(population_size): - if random.random() < mutation_rate: - selected_population[i] += random.uniform(-0.5, 0.5) - - population = selected_population - - best_solution = max(population, key=fitness_function) - best_fitness = fitness_function(best_solution) - - return best_solution, best_fitness - - -if __name__ == "__main__": - # Run the doctests - doctest.testmod() - input_value = float(input("Enter the value of input_value: ").strip()) - fitness = fitness_function(input_value) - print(f"The fitness for input_value = {input_value} is {fitness}.") From 0522023574668e493e06171ba022d35470cd2d77 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:30:31 +0530 Subject: [PATCH 11/17] Add files via upload --- genetic_algorithm/basic_number.py | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py new file mode 100644 index 000000000000..3975eeb54110 --- /dev/null +++ b/genetic_algorithm/basic_number.py @@ -0,0 +1,78 @@ +import random +import doctest + + +def fitness_function(input_value): + """ + Calculate the fitness (objective) function value for a given input. + + Args: + input_value (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Example: + >>> fitness_function(2.5) + 0.75 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(input_value, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return input_value**2 - 3 * input_value + 2 + + +def genetic_algorithm() -> tuple[float, float]: + """ + Optimize a numeric value using a simple genetic algorithm. + + Returns: + tuple[float, float]: A tuple containing the best solution and its fitness value. + + Example: + >>> best_solution, best_fitness = genetic_algorithm() + >>> -1.5 < best_solution < -1.4 + False + >>> 6 < best_fitness < 6.1 + False + """ + population_size = 50 + mutation_rate = 0.1 + num_generations = 100 + + population = [random.uniform(-10, 10) for _ in range(population_size)] + + for generation in range(num_generations): + fitness_scores = [fitness_function(individual) for individual in population] + + selected_population = [] + for _ in range(population_size): + tournament_size = 5 + tournament = random.sample(range(population_size), tournament_size) + tournament_fitness = [fitness_scores[i] for i in tournament] + selected_individual = population[ + tournament[tournament_fitness.index(max(tournament_fitness))] + ] + selected_population.append(selected_individual) + + for i in range(population_size): + if random.random() < mutation_rate: + selected_population[i] += random.uniform(-0.5, 0.5) + + population = selected_population + + best_solution = max(population, key=fitness_function) + best_fitness = fitness_function(best_solution) + + return best_solution, best_fitness + + +if __name__ == "__main__": + # Run the doctests + doctest.testmod() + input_value = float(input("Enter the value of input_value: ").strip()) + fitness = fitness_function(input_value) + print(f"The fitness for input_value = {input_value} is {fitness}.") From be885f052bffa27665cb119f906138e51f5997da Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:35:42 +0530 Subject: [PATCH 12/17] Delete genetic_algorithm/basic_number.py --- genetic_algorithm/basic_number.py | 78 ------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py deleted file mode 100644 index 3975eeb54110..000000000000 --- a/genetic_algorithm/basic_number.py +++ /dev/null @@ -1,78 +0,0 @@ -import random -import doctest - - -def fitness_function(input_value): - """ - Calculate the fitness (objective) function value for a given input. - - Args: - input_value (float): The input value for which the fitness is calculated. - - Returns: - float: The fitness value calculated for the input. - - Example: - >>> fitness_function(2.5) - 0.75 - >>> fitness_function(-1.0) - 6.0 - """ - if not isinstance(input_value, (int, float)): - raise ValueError("Input must be a valid number.") - - # Define your fitness function here (e.g., x^2, or any other function) - return input_value**2 - 3 * input_value + 2 - - -def genetic_algorithm() -> tuple[float, float]: - """ - Optimize a numeric value using a simple genetic algorithm. - - Returns: - tuple[float, float]: A tuple containing the best solution and its fitness value. - - Example: - >>> best_solution, best_fitness = genetic_algorithm() - >>> -1.5 < best_solution < -1.4 - False - >>> 6 < best_fitness < 6.1 - False - """ - population_size = 50 - mutation_rate = 0.1 - num_generations = 100 - - population = [random.uniform(-10, 10) for _ in range(population_size)] - - for generation in range(num_generations): - fitness_scores = [fitness_function(individual) for individual in population] - - selected_population = [] - for _ in range(population_size): - tournament_size = 5 - tournament = random.sample(range(population_size), tournament_size) - tournament_fitness = [fitness_scores[i] for i in tournament] - selected_individual = population[ - tournament[tournament_fitness.index(max(tournament_fitness))] - ] - selected_population.append(selected_individual) - - for i in range(population_size): - if random.random() < mutation_rate: - selected_population[i] += random.uniform(-0.5, 0.5) - - population = selected_population - - best_solution = max(population, key=fitness_function) - best_fitness = fitness_function(best_solution) - - return best_solution, best_fitness - - -if __name__ == "__main__": - # Run the doctests - doctest.testmod() - input_value = float(input("Enter the value of input_value: ").strip()) - fitness = fitness_function(input_value) - print(f"The fitness for input_value = {input_value} is {fitness}.") From d0d13e9b4b6a644a8b8d34ba7fab3bdc5611800b Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:36:02 +0530 Subject: [PATCH 13/17] Add files via upload --- genetic_algorithm/basic_number.py | 78 +++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py new file mode 100644 index 000000000000..d1a08e1e3b59 --- /dev/null +++ b/genetic_algorithm/basic_number.py @@ -0,0 +1,78 @@ +import random +import doctest + + +def fitness_function(input_value: float) -> float: + """ + Calculate the fitness (objective) function value for a given input. + + Args: + input_value (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Example: + >>> fitness_function(2.5) + 0.75 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(input_value, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return input_value**2 - 3 * input_value + 2 + + +def genetic_algorithm() -> tuple[float, float]: + """ + Optimize a numeric value using a simple genetic algorithm. + + Returns: + tuple[float, float]: A tuple containing the best solution and its fitness value. + + Example: + >>> best_solution, best_fitness = genetic_algorithm() + >>> -1.5 < best_solution < -1.4 + False + >>> 6 < best_fitness < 6.1 + False + """ + population_size = 50 + mutation_rate = 0.1 + num_generations = 100 + + population = [random.uniform(-10, 10) for _ in range(population_size)] + + for generation in range(num_generations): + fitness_scores = [fitness_function(individual) for individual in population] + + selected_population = [] + for _ in range(population_size): + tournament_size = 5 + tournament = random.sample(range(population_size), tournament_size) + tournament_fitness = [fitness_scores[i] for i in tournament] + selected_individual = population[ + tournament[tournament_fitness.index(max(tournament_fitness))] + ] + selected_population.append(selected_individual) + + for i in range(population_size): + if random.random() < mutation_rate: + selected_population[i] += random.uniform(-0.5, 0.5) + + population = selected_population + + best_solution = max(population, key=fitness_function) + best_fitness = fitness_function(best_solution) + + return best_solution, best_fitness + + +if __name__ == "__main__": + # Run the doctests + doctest.testmod() + input_value = float(input("Enter the value of input_value: ").strip()) + fitness = fitness_function(input_value) + print(f"The fitness for input_value = {input_value} is {fitness}.") From 251d154f267cef3026987680225a5c7262a688ae Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:42:37 +0530 Subject: [PATCH 14/17] Delete genetic_algorithm/basic_number.py --- genetic_algorithm/basic_number.py | 78 ------------------------------- 1 file changed, 78 deletions(-) delete mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py deleted file mode 100644 index d1a08e1e3b59..000000000000 --- a/genetic_algorithm/basic_number.py +++ /dev/null @@ -1,78 +0,0 @@ -import random -import doctest - - -def fitness_function(input_value: float) -> float: - """ - Calculate the fitness (objective) function value for a given input. - - Args: - input_value (float): The input value for which the fitness is calculated. - - Returns: - float: The fitness value calculated for the input. - - Example: - >>> fitness_function(2.5) - 0.75 - >>> fitness_function(-1.0) - 6.0 - """ - if not isinstance(input_value, (int, float)): - raise ValueError("Input must be a valid number.") - - # Define your fitness function here (e.g., x^2, or any other function) - return input_value**2 - 3 * input_value + 2 - - -def genetic_algorithm() -> tuple[float, float]: - """ - Optimize a numeric value using a simple genetic algorithm. - - Returns: - tuple[float, float]: A tuple containing the best solution and its fitness value. - - Example: - >>> best_solution, best_fitness = genetic_algorithm() - >>> -1.5 < best_solution < -1.4 - False - >>> 6 < best_fitness < 6.1 - False - """ - population_size = 50 - mutation_rate = 0.1 - num_generations = 100 - - population = [random.uniform(-10, 10) for _ in range(population_size)] - - for generation in range(num_generations): - fitness_scores = [fitness_function(individual) for individual in population] - - selected_population = [] - for _ in range(population_size): - tournament_size = 5 - tournament = random.sample(range(population_size), tournament_size) - tournament_fitness = [fitness_scores[i] for i in tournament] - selected_individual = population[ - tournament[tournament_fitness.index(max(tournament_fitness))] - ] - selected_population.append(selected_individual) - - for i in range(population_size): - if random.random() < mutation_rate: - selected_population[i] += random.uniform(-0.5, 0.5) - - population = selected_population - - best_solution = max(population, key=fitness_function) - best_fitness = fitness_function(best_solution) - - return best_solution, best_fitness - - -if __name__ == "__main__": - # Run the doctests - doctest.testmod() - input_value = float(input("Enter the value of input_value: ").strip()) - fitness = fitness_function(input_value) - print(f"The fitness for input_value = {input_value} is {fitness}.") From 784a279840067e1c6cebcc155cae2596f15788a4 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:43:19 +0530 Subject: [PATCH 15/17] Updated genetic on numbers --- genetic_algorithm/basic_number.py | 56 +++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 genetic_algorithm/basic_number.py diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py new file mode 100644 index 000000000000..9affeb44a1eb --- /dev/null +++ b/genetic_algorithm/basic_number.py @@ -0,0 +1,56 @@ +import doctest +import random + + +def fitness_function(input_value: float) -> float: + """ + Calculate the fitness (objective) function value for a given input. + + Args: + input_value (float): The input value for which the fitness is calculated. + + Returns: + float: The fitness value calculated for the input. + + Raises: + ValueError: If the input is not a valid floating-point number. + + Example: + >>> fitness_function(2.5) + 0.75 + >>> fitness_function(-1.0) + 6.0 + """ + if not isinstance(input_value, (int, float)): + raise ValueError("Input must be a valid number.") + + # Define your fitness function here (e.g., x^2, or any other function) + return input_value**2 - 3 * input_value + 2 + + +def genetic_algorithm(): + """ + A simplified genetic algorithm example. + + Example: + >>> random.seed(42) + >>> best_solution, best_fitness = genetic_algorithm() + >>> abs(best_solution - (-1.45)) < 0.1 + False + >>> abs(best_fitness - 6.0) < 0.1 # Check if the best fitness is within a tolerance + False + """ + population = [random.uniform(-2, 2) for _ in range(100)] + best_solution = min(population, key=fitness_function) + best_fitness = fitness_function(best_solution) + return best_solution, best_fitness + + +if __name__ == "__main__": + # Example usage + input_value = float(input("Enter the value of input_value: ").strip()) + fitness = fitness_function(input_value) + print(f"The fitness for input_value = {input_value} is {fitness}.") + + # Run the doctests + doctest.testmod() From 9e26fb6bf73b6949601320258b8b4b7c55cd74a5 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:55:56 +0530 Subject: [PATCH 16/17] Update basic_number.py --- genetic_algorithm/basic_number.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py index 9affeb44a1eb..f6903d0f0587 100644 --- a/genetic_algorithm/basic_number.py +++ b/genetic_algorithm/basic_number.py @@ -1,5 +1,6 @@ import doctest import random +from typing import Tuple def fitness_function(input_value: float) -> float: @@ -28,7 +29,7 @@ def fitness_function(input_value: float) -> float: return input_value**2 - 3 * input_value + 2 -def genetic_algorithm(): +def genetic_algorithm() -> Tuple[float, float]: """ A simplified genetic algorithm example. From b2d2759f67688a2fbe811d91f0d7f51956017b88 Mon Sep 17 00:00:00 2001 From: Jallepalli Harsha Vardhan <87539405+harshajv7981@users.noreply.github.com> Date: Tue, 3 Oct 2023 13:59:06 +0530 Subject: [PATCH 17/17] Update basic_number.py --- genetic_algorithm/basic_number.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/genetic_algorithm/basic_number.py b/genetic_algorithm/basic_number.py index f6903d0f0587..5ef18d99c6ff 100644 --- a/genetic_algorithm/basic_number.py +++ b/genetic_algorithm/basic_number.py @@ -1,6 +1,5 @@ import doctest import random -from typing import Tuple def fitness_function(input_value: float) -> float: @@ -29,7 +28,7 @@ def fitness_function(input_value: float) -> float: return input_value**2 - 3 * input_value + 2 -def genetic_algorithm() -> Tuple[float, float]: +def genetic_algorithm() -> tuple[float, float]: """ A simplified genetic algorithm example.