Skip to main content

A simple implementation of Genetic Algorithm

Project description

simplega

simplega is a simple python implementation of genetic algorithm and it is available through PyPI.

Install

python3 -m pip install simplega

Usage

Import the package

from simplega import Chromosome, Population, GA, GAHelper
# or
from simplega import *

Create a fitness function that suits toyr problem

def maximize(chromosome):
    return sum( [ ord(gene) for gene in chromosome.dna ] )

Create a new instance of GA specifying the fitness function to be used

ga = GA(maximize)

Perform the steps of the genetic algorithm and retrieve the fittest chromosome

ga.run()
print(ga.get_fittest())

All the script - really simple:

from simplega import *

def maximize(chromosome):
    return sum( [ ord(gene) for gene in chromosome.dna ] )

ga = GA(maximize)
ga.run()
print(ga.get_fittest())

Advanced usage

You can customize your instance of GA, replacing any or all of its default values

ga = GA(fitness_function, 
  genes =  [ chr(n) for n in range(65,91) ], 
  chromosome_size =  10, 
  population_size =  100, 
  generations =  100, 
  crossover_points =  1, 
  elitism_rate =  0.05, 
  crossover_rate =  0.85, 
  mutation_rate =  0.01, 
  )

You can print the fittest chromosome of each generation with ga.run(True)

Contributing

Please submit bugfixes, enhancements, unit tests, usecases and examples with a pull request.

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

simplega-1.0.1.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

simplega-1.0.1-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

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