-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Implement genetic algorithm for optimizing continuous functions #11670
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement genetic algorithm for optimizing continuous functions #11670
Conversation
- Added a flexible genetic algorithm that allows users to define their own target functions for optimization. - Included features for population initialization, fitness evaluation, selection, crossover, and mutation. - Example function provided for minimizing f(x, y) = x^2 + y^2. - Configurable parameters for population size, mutation probability, and generations.
for more information, see https://pre-commit.ci
Hi can anyone guide me why it is failing everytime i run it? |
remove extra spaces thats the problem |
Thanks i will try to remove it and then rerun the jobs. |
@night-spring can you guide me what extra spaces I need to remove as i am checking my code and based on that I can't able to find any extra whitespace. If possible for you can you help me for resolving the error and for merging this pr. |
after end of every para of your code you may give two newlines. remove one. |
Para means functions right? Thank you for you advice I will change it and push the code today itself |
@night-spring and @cclauss what this error means?
And also this one
|
just fix format using copilot or any ai |
https://docs.astral.sh/ruff/rules/#isort-i unsorted-imports (I001)Derived from the isort linter. Fix is sometimes available. What it doesDe-duplicates, groups, and sorts imports based on the provided Why is this bad?Consistency is good. Use a common convention for imports to make your code Exampleimport pandas
import numpy as np Use instead: import numpy as np
import pandas |
@cclauss Thanks for the help, i will try to resolve the error. Thank you once again for giving your time. |
@cclauss i have used these for the imports and use the same order import numpy as np
import random
from concurrent.futures import ThreadPoolExecutor |
PEP8 has been the Python style guide for a long time. https://peps.python.org/pep-0008/#imports
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
|
||
|
||
class GeneticAlgorithm: | ||
def __init__( |
There was a problem hiding this comment.
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:
class GeneticAlgorithm: | ||
def __init__( | ||
self, | ||
function, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide type hint for the parameter: function
def __init__( | ||
self, | ||
function, | ||
bounds, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide type hint for the parameter: bounds
self, | ||
function, | ||
bounds, | ||
population_size, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide type hint for the parameter: population_size
function, | ||
bounds, | ||
population_size, | ||
generations, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide type hint for the parameter: generations
return child1, child2 | ||
return parent1, parent2 | ||
|
||
def mutate(self, individual): |
There was a problem hiding this comment.
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
individual[i] = rng.uniform(self.bounds[i][0], self.bounds[i][1]) | ||
return individual | ||
|
||
def evaluate_population(self): |
There was a problem hiding this comment.
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
executor.map(lambda ind: (ind, self.fitness(ind)), self.population) | ||
) | ||
|
||
def evolve(self): |
There was a problem hiding this comment.
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
population_score = self.evaluate_population() | ||
|
||
# Check the best individual | ||
best_individual = max(population_score, key=lambda x: x[1])[0] |
There was a problem hiding this comment.
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
|
||
|
||
# Example target function for optimization | ||
def target_function(x, y): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
for more information, see https://pre-commit.ci
@cclauss can you please check my code as it is failing the test cases and don't know how to resolve, if possible for you. Thank you. |
There are three Details buttons to the right of the three failing tests below. Click on each, read the error messages, and try to fix the code so those errors disappear.
import random
from concurrent.futures import ThreadPoolExecutor
import numpy as np genetic_algorithm/genetic_algorithm_optimization.py:50:89: E501 Line too long (109 > 88) >>> ga = GeneticAlgorithm(lambda x, y: x**2 + y**2,
... [(-10, 10), (-10, 10)], 10, 100, 0.1, 0.8, False) |
Thank you @cclauss for your guidance, i will try to make necessary changes that you listed . Thank you once again for your time. |
Describe your change:
Added a flexible genetic algorithm that allows users to define their own target functions for optimization.
Included features for population initialization, fitness evaluation, selection, crossover, and mutation.
Example function provided for minimizing f(x, y) = x^2 + y^2.
Configurable parameters for population size, mutation probability, and generations.
Add an algorithm? ✅ Yes
Fix a bug or typo in an existing algorithm? ❌ No
Add or change doctests? ❌ No
Documentation change? ❌ No
Checklist:
Fixes #11578