Skip to content
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

feat: add genetic algorithm library #222

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Cyntwikip
Copy link

@Cyntwikip Cyntwikip commented Oct 31, 2021

Description

My implementation of genetic algorithm. This is a search heuristic for identifying the parameters in a system that will yield good results through the process of natural selection. This file is a modification of the library made by me as well. Here is the link of the original implementation: https://github.com/Cyntwikip/libraries/blob/master/genetic_algo/ga.py

Sample Usage:

from ga import GeneticAlgo as ga
import numpy as np

# Pool of genes
pool_scaler = ["MinMax (-1,1)", "MinMax (0,1)", "Std"]
pool_batch_size = [64, 72, 128, 256]
pool_lstm_layers = [[128, 128, 128],
                    [64, 64, 64],
                    [32, 32, 32],
                    [128, 64, 64],
                    [64, 32, 32],
                    [128, 64, 32]]
pool_dropout = [0., 0.1, 0.2]
pool_activation = ['tanh', 'sigmoid']
pool_loss = ['mean_squared_error', 'mae']
pool_epochs = [5]

genes_dict = dict( scaler = pool_scaler,
                batch_size = pool_batch_size,
                lstm_layers = pool_lstm_layers,
                dropout = pool_dropout,
                activation = pool_activation,
                loss = pool_loss,
                epochs = pool_epochs
            )

def survival(n):
    codes = ga.get_initial_population(n)
    np.random.seed(42)
    scores = np.random.randint(0,40,n)
    fitness = dict(zip(codes, scores))
    print(fitness)
    best_parents = ga.get_best_parents(fitness)
    print(best_parents)

ga = ga(genes_dict, debug=True)
survival(10)

More sample test cases will pushed in a follow-up commit.

Checklist:

  • Code contain Problem statement, examples test cases.
  • My code follows the style guidelines(Clean Code) of this project
  • I have performed a self-review of my own code
  • Explained Detailed approach of Solution
  • I have commented on my code, particularly in hard-to-understand areas
  • Performed Time complexity analysis of the algorithm used in code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant