Skip to main content

Una librería de algoritmos genéticos en Python

Project description

📦 ml-gen-alg

ml-gen-alg es una librería de Python para aplicar algoritmos genéticos personalizados sobre funciones objetivo, permitiendo seleccionar distintos métodos de selección, cruce y mutación.


🚀 Instalación

Puedes instalar la librería directamente desde PyPI con:

pip install ml-gen-alg

⚙️ Estructura del objeto GA

La librería expone un objeto principal GA, el cual ejecuta el algoritmo genético sobre una función objetivo personalizada.

📌 Constructor GA()

GA(
    num_generations=1000,
    num_parents_mating=20,
    sol_per_pop=50,
    initial_population=None,
    num_genes=None,
    init_range_low=0,
    init_range_high=255,
    mutation_percent_genes=[0.3, 1.5],
    parent_selection_type=None,
    K_tournament=3,
    crossover_type=None,
    mutation_type=None,
    keep_parents=5,
    on_generation=None,
    fitness_func=None,
    stop_criteria=[],
    optimization_mode="maximize"  # O "minimize"
)

⚠️ Parámetros obligatorios

  • fitness_func: función de evaluación de cada individuo.
  • num_genes y/o initial_population.

🧮 Función de Fitness

La función de fitness debe tener una o dos entradas:

def fitness_func(solution):
    return sum(solution)

# o bien:
def fitness_func(solution, solution_idx):
    return sum(solution)

📊 Métodos de Selección Soportados

Desde ml_gen_alg.functions.operations:

steady_state_selection
rank_selection
random_selection
tournament_selection
roulette_wheel_selection
stochastic_universal_selection
nsga2_selection
tournament_selection_nsga2

🔗 Métodos de Cruce Soportados

single_point_crossover
two_points_crossover
uniform_crossover
scattered_crossover

🧬 Métodos de Mutación Soportados

random_mutation
swap_mutation
inversion_mutation
scramble_mutation
adaptive_mutation

✅ Ejemplo Básico (Maximización)

from ml_gen_alg import GA
from ml_gen_alg.functions.operations import (
    roulette_wheel_selection,
    two_points_crossover,
    inversion_mutation
)

def fitness_func(solution):
    return sum(solution)

def on_generation(generation, best, score):
    print(f"Gen {generation}: best={best}, score={score}")

initial_population = [
    [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
    [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
    [1, 1, 0, 0, 1, 1, 0, 0, 1, 1],
    [0, 0, 1, 1, 0, 0, 1, 1, 0, 0]
]

ga = GA(
    num_generations=50,
    num_parents_mating=2,
    sol_per_pop=4,
    initial_population=initial_population,
    num_genes=10,
    parent_selection_type=roulette_wheel_selection,
    crossover_type=two_points_crossover,
    mutation_type=inversion_mutation,
    fitness_func=fitness_func,
    on_generation=on_generation,
    stop_criteria=["saturate_10"],
    optimization_mode="maximize"
)

best_solution, best_score = ga.genetic_algorithm()

🔻 Ejemplo (Minimización)

def fitness_func(solution):
    return sum([gene ** 2 for gene in solution])

ga = GA(
    num_generations=100,
    num_parents_mating=3,
    sol_per_pop=6,
    initial_population=[[1,2,3,4], [2,2,2,2], [3,1,4,1], [4,4,4,4], [0,0,0,0], [1,1,1,1]],
    num_genes=4,
    parent_selection_type=roulette_wheel_selection,
    crossover_type=two_points_crossover,
    mutation_type=inversion_mutation,
    fitness_func=fitness_func,
    optimization_mode="minimize"
)

best_solution, best_score = ga.genetic_algorithm()

📤 Publicación

Para más información sobre cómo publicar tu propia versión:

  • Actualiza el archivo setup.py
  • Cambia la versión (version='1.0.1', por ejemplo)
  • Luego ejecuta:
python setup.py sdist bdist_wheel

Y publica:

twine upload dist/*

📬 Autor

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

ml_gen_alg-1.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

ml_gen_alg-1.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file ml_gen_alg-1.1.0.tar.gz.

File metadata

  • Download URL: ml_gen_alg-1.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for ml_gen_alg-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3eee9f8831d68839865a597b53ee71df81024f03e710b3d2ad6e68026514902a
MD5 92e34d91abd30ab9d74bffb2ff9ba5a4
BLAKE2b-256 9e28f1efdcd1e39e6db3f19f543e7f0dc3250396269e90f663458545c55a2987

See more details on using hashes here.

File details

Details for the file ml_gen_alg-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ml_gen_alg-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for ml_gen_alg-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 57768a62fd0c9b2e6be89f1d9ae0607d937e8a0b4c74cc963d4ea4e527161156
MD5 b59a6c9754b6a1117612d02c4638d4e2
BLAKE2b-256 34bb6e521d352a14de63d5558b3c6dd907e9646c3a2cacc17ec19102213f9c50

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