Skip to main content

Genopt: optimization with genetic algorithms

Project description

alt text

Genopt: A genetic algorithm powered hyperparameter optimization framework

Python pypi License

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

1. Installation

Genopt is available at PyPI

$ pip install genopt

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 genopt.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 and Default Parameters

from genopt.environment import Environment

if __name__ == '__main__':
    #defining our Environment instance with a population of 100 individuals,
    #roulette selection for 50% of the population, one-point crossover, 
    #a single gene mutation with 10% probability of mutation,
    #10% elite rate, some verbose and without a seed for reproducibility
    environment = Environment(
        params=params
    )
    #minimizing the objective function and adding a 60 seconds timeout
    #as an stop criteria and a single core execution (n_jobs=1)
    results = environment.optimize(
        objective=objective,
        direction='minimize',
        timeout=60
    )

2.5. Start Optimization for Single-Objective Function and Custom Parameters

from genopt.environment import Environment

if __name__ == '__main__':
    #defining our Environment instance with a population of 100 individuals,
    #tournament selection for 80% of the population, one-point crossover, 
    #a single gene mutation with 25% probability of mutation, some verbose and a seed for reproducibility
    environment = Environment(
        params=params,
        num_population=100,
        selection_type='tournament',
        selection_rate=0.8,
        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.6. Start Optimization for Multi-Objective Function and Custom Parameters

from genopt.environment import Environment

if __name__ == '__main__':
    #defining our Environment instance with a population of 100 individuals,
    #ranking selection for 70% of the population, one-point crossover,
    #a single gene mutation with 25% probability of mutation, some verbose and a seed for reproducibility
    environment = Environment(
        params=params,
        num_population=100,
        selection_type='ranking',
        selection_rate=0.7,
        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.7. 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)
print('LAST GENERATION INDIVIDUALS:')
print(results.last_generation_individuals_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

genopt-0.9.15.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

genopt-0.9.15-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file genopt-0.9.15.tar.gz.

File metadata

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

File hashes

Hashes for genopt-0.9.15.tar.gz
Algorithm Hash digest
SHA256 55a915bb27cd2950bee78defadd2bf0e4960808a9a2b7ff47a8f4bdbed8a12ea
MD5 e51a75eeb6f29e99eec84e1900c76b89
BLAKE2b-256 72bb9df7b10a1a275ac053c21aaa568d3da9dee9664ca096f3298a22e0844a94

See more details on using hashes here.

File details

Details for the file genopt-0.9.15-py2.py3-none-any.whl.

File metadata

  • Download URL: genopt-0.9.15-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.7 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 genopt-0.9.15-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8da92f7f5cba331d098ac17357d44c935a8ca46fc0753a1d76bd600d957160a2
MD5 8fcd86e7aa34bf022404b3edf116154e
BLAKE2b-256 85ea16e1ed8adbe2648c8b707d309ac47035b4ea1d9f417c6147ebd6408a3a80

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