Skip to main content

No project description provided

Project description

GeneOpt - Genetic Algorithm Python Library

GeneOpt is a Python library designed for creating and optimizing genetic algorithms, including support for distributed evaluation and detailed plotting tools. It provides a robust framework for solving optimization problems by simulating the principles of natural selection.

Features

  • Custom Objective Function: Easily integrate your specific optimization goals.
  • Flexible Optimization Goals: Support for both maximization and minimization.
  • Distributed Computation: Enables scaling across multiple servers for resource-intensive tasks.
  • Advanced Configurations: Control mutation rates, crossover rates, and population dynamics.
  • Comprehensive Caching: Choose between Ram or HardDisk for efficient handling of large datasets.
  • Detailed Plotting: Visualize best scores, parameter values, diversity, and more across generations.

Repository

Find the source code and contribute on GitHub: GeneOpt Repository

Installation

Install GeneOpt via pip:

pip install GeneOpt

Getting Started

Here's a simplified example to get started with GeneOpt:

Local Optimization

This example demonstrates using GeneOpt to optimize a function locally:

import numpy as np
from GeneOpt import GeneticAlgorithm, GeneticAlgorithmComparisonType, GeneticAlgorithmCacheType, Logger


def objective_function(a, b, c):
    return a + b + c


if __name__ == "__main__":
    # Define the search space
    environment = {
        "a": np.linspace(0, 100, 1024),
        "b": np.linspace(0, 100, 1024),
        "c": np.linspace(0, 100, 1024)
    }

    # Initialize the genetic algorithm
    genetic_algorithm = GeneticAlgorithm(
        objective_func=objective_function,
        comparison_type=GeneticAlgorithmComparisonType.maximize,
        optimizer_name="test_optimizer",
        environment=environment,
        seed=42,
        mutation_rate=0.06,
        number_of_generations=100,
        number_of_population=100,
        r_cross=0.63,
        start_number_of_population=100,
        tournament_selection_number=4,
        cache_type=GeneticAlgorithmCacheType.Ram,
        verbose=1
    )

    # Run the genetic algorithm
    best_solution, best_score = genetic_algorithm.start()

    # Log results
    Logger.log_m("Optimization Complete!")
    Logger.log_m(f"Best Solution: {best_solution}, Best Score: {best_score}")

    # Plot results
    genetic_algorithm.plot_best_score()
    genetic_algorithm.plot_parameter_values_for_the_best_scores()
    genetic_algorithm.plot_population_diversity()
    genetic_algorithm.plot_gene_frequencies()
    genetic_algorithm.feature_plots()

Outputs

  1. Best Solution: Prints the optimal parameters and corresponding fitness score.
  2. Plots: Saves detailed graphs to the ./cache/ directory.

Plotting Functions

GeneOpt provides a variety of detailed plots to understand the algorithm's performance:

1. Best Score Over Generations

Plots the best score achieved in each generation. Best Score Over Generations Image

2. Best Parameter Values Over Generations

Shows the evolution of parameters contributing to the best scores. Best Parameter a Value Over Generations Image

3. Population Diversity

Illustrates the diversity within the population for each generation. Population Diversity Image

4. Gene Frequencies

Displays the frequency of genes across generations using a heatmap. Gene Frequencies Image

5. Feature Analysis

Includes:

  • Pairwise correlation with score. Pairwise correlation with score Image
  • Pairwise correlation of features. Pairwise correlation of features Image
  • Predicted vs Actual values using regression. Predicted vs Actual values using regression Image
  • Feature importance analysis. Feature importance analysis Image
  • Histograms plot of features. Histograms plot of features Image
  • scatter plot of features. scatter plot of features Image
  • KDE plot of features. KDE plot of features
  • Parameter and Parameter and score scatter plots Parameter and Parameter and score scatter plots Image

Distributed Optimization with GeneOpt

GeneOpt can also be used for distributed optimization by leveraging multiple servers. This setup evaluates the objective function across different ports, enabling efficient parallel processing for computationally intensive tasks.

Components

  1. Server: A Flask-based API to evaluate the objective function(or any other programing language and framework).
  2. Server Runner: A script to start multiple instances of the server.
  3. Main: The main genetic algorithm execution script using asynchronous requests.

Server Code (server.py)

The server evaluates the objective function via HTTP GET requests.

import sys
from flask import Flask, jsonify, request

app = Flask(__name__)

def objective_func(a, b, c):
    return (a - 5) ** 2 + (b - 20) ** 2 + (c - 33.33) ** 4

@app.route('/run_objective', methods=['GET'])
def run_objective():
    input_parameters = request.args.to_dict()
    try:
        parameters = {key: float(value) for key, value in input_parameters.items()}
    except ValueError:
        res = jsonify({"data": "Bad request: All parameters must be numeric"})
        res.status_code = 400
        return res

    return jsonify({"data": objective_func(**parameters)})

if __name__ == '__main__':
    app.run(
        debug=True,
        port=int(sys.argv[1]),
        threaded=False
    )

Server Runner (RunNServer.py)

This script launches multiple server instances on different ports.

import subprocess
import sys

processes = []

for index in range(int(sys.argv[1])):
    process = subprocess.Popen(["python", "server.py", str(5000 + index)])
    processes.append(process)

# Wait for all processes to finish
for process in processes:
    process.wait()

Genetic Algorithm Execution (main.py)

The main script initializes the genetic algorithm and evaluates the objective function asynchronously.

import asyncio
import sys
import aiohttp
import numpy as np
from GeneOpt import GeneticAlgorithm, GeneticAlgorithmComparisonType, GeneticAlgorithmCacheType, Logger

timeout = 60 * 30


async def objective_func(port, **parameters):
    """Objective function to evaluate parameters."""
    url = f"http://127.0.0.1:{port}/run_objective"
    while True:
        try:
            async with aiohttp.ClientSession() as session:
                async with session.get(url, params=parameters, timeout=timeout) as response:
                    response.raise_for_status()
                    res_json = await response.json()
                    if res_json and res_json.get("data"):
                        return float(res_json["data"])
        except KeyboardInterrupt as e:
            raise e
        except Exception as e:
            print(f"Error occurred: {e}, retrying...")
            await asyncio.sleep(5)


def run_async_objective_func(port, **parameters):
    """Wrapper for running async objective function."""
    return asyncio.run(objective_func(port=port, **parameters))


if __name__ == "__main__":
    environment = {
        "a": np.linspace(0, 100, 1024),
        "b": np.linspace(0, 100, 1024),
        "c": np.linspace(0, 100, 1024)
    }
    genetic_algorithm = GeneticAlgorithm(
        objective_func=run_async_objective_func,
        comparison_type=GeneticAlgorithmComparisonType.minimize,
        optimizer_name="test_op2",
        environment=environment,
        seed=42,
        mutation_rate=0.06,
        number_of_generations=100,
        number_of_population=200,
        r_cross=0.8,
        start_number_of_population=200,
        tournament_selection_number=4,
        cache_type=GeneticAlgorithmCacheType.Ram,
        verbose=1,
        number_of_workers=int(sys.argv[1])
    )
    best, score = genetic_algorithm.start()
    Logger.log_m("Done!")
    Logger.log_m(f"{best} = {score}")
    genetic_algorithm.plot()

Usage

  1. Start Servers:

    python RunNServer.py <number_of_servers>
    
  2. Run Genetic Algorithm:

    python main.py <number_of_servers>
    

Example Workflow

  • Start 4 servers:

    python RunNServer.py 4
    
  • Run the genetic algorithm using these servers:

    python main.py 4
    

Outputs

  1. Optimized Solution: Logs the best solution and its fitness score.
  2. Plots: Visualizations saved to the ./cache/ directory.

This distributed setup allows GeneOpt to scale across multiple servers, making it ideal for resource-intensive optimization tasks.

Configuration Parameters

Parameter Description
objective_func The fitness function to optimize.
comparison_type Either maximize or minimize to specify optimization goal.
optimizer_name Name for the optimizer instance.
environment Dictionary defining the search space for variables.
seed Random seed for reproducibility.
mutation_rate Probability of mutation in the population.
number_of_generations Number of generations to evolve.
number_of_population Total size of the population.
r_cross Probability of crossover between individuals.
start_number_of_population Initial population size.
tournament_selection_number Number of candidates in tournament selection.
cache_type Choose Ram or HardDisk for caching.
verbose Verbosity level for logging (-1 for no logging, 0 for minimal, up to 3 for maximum detail).
number_of_workers Number of worker servers (default is -1, meaning no workers are used).

Requirements

  • Python 3.6 or higher
  • NumPy
  • Matplotlib
  • Seaborn
  • CatBoost (optional for feature analysis)

License

This project is licensed under the MIT License. Feel free to use, modify, and contribute.


Happy optimizing with GeneOpt! 🚀

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

geneopt-1.0.0.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

GeneOpt-1.0.0-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file geneopt-1.0.0.tar.gz.

File metadata

  • Download URL: geneopt-1.0.0.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for geneopt-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9397fedf58319fdfb6170c9d8df7f891c28ad5b65bf4bab1a2b9e9f6e626fdda
MD5 9106acd8181248e524fdfb506cd6e620
BLAKE2b-256 0d4c87ffd3c62dc0068ec90b0a1ef7b13edfb3fbbb8ab48b43241a265dfe6ee6

See more details on using hashes here.

File details

Details for the file GeneOpt-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: GeneOpt-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for GeneOpt-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 461843fca9ac11ca0bcd573aac2fa6944110a37dceb9002935ef226b1f58a5da
MD5 1d0f6780f39efece87c82c4e2d1d523a
BLAKE2b-256 760f8decd07dc9980b0c0bd60aa5753baf553fda1234f34a10fef61e46ae9278

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