Skip to main content

Genetist: optimization with genetic algorithms

Project description

alt text

Genetist: A genetic algorithm powered hyperparameter optimization framework

Python pypi License

Genetist is a high level framework that helps optimizing functions using the power of genetic algorithms.

1. Installation

Genetist is available at PyPI

$ pip install genetist

2. Quickstart

2.1. Define Search Space

2.1.1. Fixed Search Space

#defining a fixed set of Parameters for 4 variables
params = {
    'x': [35, -51, 0, 1, 2, 3, 4, 66, 11, 50, 90],
    'y': [-100, -51, 0, 7, 32, 31, 4, 51, 121, 50, 90, 1000, 231]
    'z': [-10, -51, 0, 12, 2, 43, 43, 5, 1231, 50, 90],
    'k': [-56, -51, 0, 1, 2, 13, 4, 5, 11, 50, 90]
}

2.1.2. Flexible Search Space

from genetist.parameters import Parameters

#defining a 4 variable search space of float values from -100.0 to 100.0
params = {
    'x': Parameters.suggest_float(-100, 100),
    'y': Parameters.suggest_float(-100, 100),
    'z': Parameters.suggest_float(-100, 100),
    'k': Parameters.suggest_float(-100, 100)
}

2.2. Define Single-Objective Function

#defining an objective function
def objective(individual):
    x = individual['x']
    y = individual['y']
    z = individual['z']
    k = individual['k']

    return (x**2 - 4*y**3 / z**4) * k**3

2.3. Define Multi-Objective Function

#defining an objective function
def objective(individual):
    x = individual['x']
    y = individual['y']
    z = individual['z']
    k = individual['k']

    objective_1 = ((x**2 - 4*y**3 / z**4) * k**3)
    objective_2 = (k**3 / x)

    return objective_1, objective_2

2.4. Start Optimization for Single-Objective Function

from genetist.environment import Environment

if __name__ == '__main__':
    #defining our Environment instance with a population of 100 individuals,
    #one-point crossover, a single gene mutation with a 25% probability of mutation
    #some verbose and a seed for reproducibility
    environment = Environment(
        params=params,
        num_population=100,
        crossover_type='one-point',
        mutation_type='single-gene',
        prob_mutation=0.25,
        verbose=1,
        random_state=42
    )
    #minimizing the objective function and adding 
    #3 stop criterias (num_generations, timeout, stop_score)
    results = environment.optimize(
        objective=objective,
        direction='minimize',
        num_generations=9999,
        timeout=60,
        stop_score=-np.inf
    )

2.5. Start Optimization for Multi-Objective Function

from genetist.environment import Environment

if __name__ == '__main__':
    #defining our Environment instance with a population of 100 individuals,
    #one-point crossover, a single gene mutation with a 25% probability of mutation
    #some verbose and a seed for reproducibility
    environment = Environment(
        params=params,
        num_population=100,
        crossover_type='one-point',
        mutation_type='single-gene',
        prob_mutation=0.25,
        verbose=1,
        random_state=42
    )
    #minimizing the first value and maximazing the second value of the objective function,
    #adding 1 stop criterias (timeout), adding 50% weight to each objective and
    #assigning a name to each objective score for the final dataframe
    results = environment.optimize(
        objective=objective,
        direction=['minimize', 'maximize'],
        weights=[0.5, 0.5],
        timeout=60,
        score_names=['complex_equation_score', 'simple_equation_score']
    )

2.6. Show Optimization Results

print(f'EXECUTION TIME={results.execution_time}')
print(f'BEST SCORE={results.best_score}')
print(f'BEST INDIVIDUAL={results.best_individual}')
print('BEST PER GENERATION:')
print(results.best_per_generation_dataframe)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

genetist-0.9.11.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genetist-0.9.11-py2.py3-none-any.whl (12.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file genetist-0.9.11.tar.gz.

File metadata

  • Download URL: genetist-0.9.11.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for genetist-0.9.11.tar.gz
Algorithm Hash digest
SHA256 67ac1dd305f5153da519cfd783f8da285ba424cc7b89be229217b8a6c2286b0e
MD5 86151f46c749cbe1e30442e43811c790
BLAKE2b-256 c63f7ee95175247221505ee606e4cfc1d28d9d4c1005b6fb17b05e7a3d35b630

See more details on using hashes here.

File details

Details for the file genetist-0.9.11-py2.py3-none-any.whl.

File metadata

  • Download URL: genetist-0.9.11-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for genetist-0.9.11-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 71aa73cc7c41195ca119c729cccf76912c85e173cf9a04ba4e363a177fc27575
MD5 46645a8fd348afb9944a18b7cbe5bcfa
BLAKE2b-256 02aa059f35c88e1e66ba196f816f63c312c98192227e01e8895dd15f1b38cd54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page