-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Added an algorithm in how to use genetic algorithm for numbers #9577
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
Open
harshajv7981
wants to merge
17
commits into
TheAlgorithms:master
Choose a base branch
from
harshajv7981:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
67a5a24
Add files via upload
harshajv7981 91710b8
Delete genetic_algorithm/generic_optimisation.py
harshajv7981 dc3bf5c
Add files via upload
harshajv7981 0e34537
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] cb00f6b
Delete genetic_algorithm/generic_optimisation.py
harshajv7981 f223b99
Add files via upload
harshajv7981 ef50399
Delete genetic_algorithm/generic_optimisation.py
harshajv7981 3369e6e
Add files via upload
harshajv7981 ea6d61b
Update basic_number.py
harshajv7981 71601ed
Delete genetic_algorithm/basic_number.py
harshajv7981 0522023
Add files via upload
harshajv7981 be885f0
Delete genetic_algorithm/basic_number.py
harshajv7981 d0d13e9
Add files via upload
harshajv7981 251d154
Delete genetic_algorithm/basic_number.py
harshajv7981 784a279
Updated genetic on numbers
harshajv7981 9e26fb6
Update basic_number.py
harshajv7981 b2d2759
Update basic_number.py
harshajv7981 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
genetic_algorithm
. If the function does not return a value, please provide the type hint as:def function() -> None: