Skip to content

Added an algorithm in how to use genetic algorithm for numbers - #9577

Open
harshajv7981 wants to merge 19 commits into
TheAlgorithms:masterfrom
harshajv7981:master
Open

Added an algorithm in how to use genetic algorithm for numbers#9577
harshajv7981 wants to merge 19 commits into
TheAlgorithms:masterfrom
harshajv7981:master

Conversation

@harshajv7981

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • 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".

@algorithms-keeper algorithms-keeper Bot added the require type hints https://docs.python.org/3/library/typing.html label Oct 3, 2023

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

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.

Comment thread genetic_algorithm/basic_number.py Outdated
return input_value**2 - 3 * input_value + 2


def genetic_algorithm():

Copy link
Copy Markdown

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: genetic_algorithm. If the function does not return a value, please provide the type hint as: def function() -> None:

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Oct 3, 2023
@harshajv7981 harshajv7981 changed the title Added an algorithm in how to use genetic for numbers Added an algorithm in how to use genetic algorithm for numbers Oct 3, 2023
@algorithms-keeper algorithms-keeper Bot removed the require type hints https://docs.python.org/3/library/typing.html label Oct 3, 2023
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Oct 3, 2023
@harshajv7981

Copy link
Copy Markdown
Author

Updated

@priya-sundaram-dev priya-sundaram-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @harshajv7981. The fitness_function part is fine (type hints + working doctests), but I think the core needs rethinking before this fits genetic_algorithm/:

  1. It isn't a genetic algorithm. genetic_algorithm() draws 100 random numbers once and returns the best by fitness — that's a single round of random search, with no population evolution: no selection, crossover, mutation, or generations. A GA contribution should show at least those steps iterating over multiple generations (see genetic_algorithm/basic_string.py in the repo for the expected shape).
  2. The doctests don't validate anything. Both assertions in genetic_algorithm() are written to evaluate to False and expect False:
    >>> abs(best_solution - (-1.45)) < 0.1
    False
    
    So the doctest "passes" without demonstrating a correct result. (For reference, x**2 - 3x + 2 is minimised at x = 1.5 on [-2, 2], giving -0.25, so -1.45/6.0 aren't the target anyway.) After reworking into a real GA, please seed RNG and assert a meaningful convergence property.

Happy to re-review once it evolves a population over generations — the fitness scaffolding is a good start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants