Python icon indicating copy to clipboard operation
Python copied to clipboard

Implement genetic algorithm for optimizing continuous functions

Open UTSAVS26 opened this issue 1 year ago • 15 comments

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:

  • [x] I have read CONTRIBUTING.md.
  • [x] This pull request is all my own work -- I have not plagiarized.
  • [x] I know that pull requests will not be merged if they fail the automated tests.
  • [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • [x] All new Python files are placed inside an existing directory.
  • [x] All filenames are in all lowercase characters with no spaces or dashes.
  • [x] All functions and variable names follow Python naming conventions.
  • [x] All function parameters and return values are annotated with Python type hints.
  • [x] All functions have doctests that pass the automated testing.
  • [x] All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • [x] If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Fixes #11578

UTSAVS26 avatar Oct 02 '24 08:10 UTSAVS26

Hi can anyone guide me why it is failing everytime i run it?

UTSAVS26 avatar Oct 02 '24 09:10 UTSAVS26

remove extra spaces thats the problem

night-spring avatar Oct 02 '24 17:10 night-spring

remove extra spaces thats the problem

Thanks i will try to remove it and then rerun the jobs.

UTSAVS26 avatar Oct 03 '24 01:10 UTSAVS26

@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.

UTSAVS26 avatar Oct 03 '24 09:10 UTSAVS26

@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.

night-spring avatar Oct 04 '24 05:10 night-spring

@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? Any other changes I need to do?

Thank you for you advice I will change it and push the code today itself

UTSAVS26 avatar Oct 04 '24 05:10 UTSAVS26

@night-spring and @cclauss what this error means?

2024-10-04T06:39:26.5187712Z ##[group]Run ruff check --output-format=github .
ruff check --output-format=github .
shell: /usr/bin/bash -e {0}
genetic_algorithm/genetic_algorithm_optimization.py:1:1: I001 Import block is un-sorted or un-formatted
Process completed with exit code 1.

And also this one

genetic_algorithm/genetic_algorithm_optimization.py:1:1: I001 [*] Import block is un-sorted or un-formatted
  |
1 | / import numpy as np
2 | | import random
3 | | from concurrent.futures import ThreadPoolExecutor
4 | | 
5 | | 
6 | | # Parameters
  | |_^ I001
7 |   N_POPULATION = 100  # Population size
8 |   N_GENERATIONS = 500  # Maximum number of generations
  |
  = help: Organize imports

Found 1 error.
[*] 1 fixable with the `--fix` option.

UTSAVS26 avatar Oct 04 '24 06:10 UTSAVS26

@night-spring and @cclauss what this error means?

2024-10-04T06:39:26.5187712Z ##[group]Run ruff check --output-format=github .
ruff check --output-format=github .
shell: /usr/bin/bash -e {0}
genetic_algorithm/genetic_algorithm_optimization.py:1:1: I001 Import block is un-sorted or un-formatted
Process completed with exit code 1.
``

just fix format using copilot or any ai

night-spring avatar Oct 04 '24 06:10 night-spring

https://docs.astral.sh/ruff/rules/#isort-i % ruff check --select=I001 --fix genetic_algorithm/genetic_algorithm_optimization.py % ruff rule I001

unsorted-imports (I001)

Derived from the isort linter.

Fix is sometimes available.

What it does

De-duplicates, groups, and sorts imports based on the provided isort settings.

Why is this bad?

Consistency is good. Use a common convention for imports to make your code more readable and idiomatic.

Example

import pandas
import numpy as np

Use instead:

import numpy as np
import pandas

cclauss avatar Oct 04 '24 08:10 cclauss

@cclauss Thanks for the help, i will try to resolve the error. Thank you once again for giving your time.

UTSAVS26 avatar Oct 04 '24 08:10 UTSAVS26

@cclauss i have used these for the imports and use the same order

import numpy as np
import random
from concurrent.futures import ThreadPoolExecutor

UTSAVS26 avatar Oct 04 '24 08:10 UTSAVS26

PEP8 has been the Python style guide for a long time. https://peps.python.org/pep-0008/#imports

random comes from the standard libraries so it should be imported before third-party libraries.

cclauss avatar Oct 04 '24 09:10 cclauss

@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.

UTSAVS26 avatar Oct 06 '24 05:10 UTSAVS26

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.

genetic_algorithm/genetic_algorithm_optimization.py:1:1: I001 [*] Import block is un-sorted or un-formatted

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)

cclauss avatar Oct 06 '24 07:10 cclauss

Thank you @cclauss for your guidance, i will try to make necessary changes that you listed .

Thank you once again for your time.

UTSAVS26 avatar Oct 06 '24 07:10 UTSAVS26