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.3.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.3-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ml_gen_alg-1.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 ea87d006b6a4e1a1729b7950f5a4f6bff7612c81f6b10b3ba779e8dc99bf65ea
MD5 830b1e9ea3646511df63c3a32d0511cc
BLAKE2b-256 8772c1dae7a7bff691ae89ea85220fddf108a67d31140db91894fab8bfd6383b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ml_gen_alg-1.1.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 432b09f340503847da29440dbfe8d875c94c8ac22e8d318b43a53174af143fa4
MD5 7ca42cbc2ec8b4d3b6306d0b9e5711b5
BLAKE2b-256 df45eb8caea69cd1d6e633443b1016a5d7f4b76082b1645d598cebe3f62379b6

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