Git Product home page Git Product logo

genes's Introduction

codecov

Simple library for playing with genetic algorithms

Install

pip install genes

Use

from genes import *

To create initial population (randomly):

inital_population: Population = population_factory.create_population(POPULATION, LENGTH, PERCENTAGE)

where:

POPULATION - is a number of individuals

LENGTH - is a Genotype length

PERCENTAGE - each Genotype is randomly created, and consists a list of Gens (the length of which is determined by the LENGTH) - this factor allows to set percentage of chance to get "1" for a particular Gen.

Sample FitnessFunction:

class SimpleFitnessFunction(FitnessFunction):

    def fit(self, genotype: Genotype) -> float:
        n = 0
        for gen in genotype:
            if gen is True:
                n += 1
        return n

Having the initial population and fitness function defined, the Genetic Algorithm can be started:

genes_api = genes_api_from_params_factory(
    SimpleFitnessFunction(),
    GAFactoryParams(
        population=POPULATION,
        selection_limit=POPULATION // 2,
        children_per_pair=4,
        mutation_probability=0.2,
    )
)
gen = genes_api.calculate(inital_population, GENERATIONS)

where:

GENERATIONS - is how many generations we want to calculate

To print the last generated generation type (the first column is the fitness function result):

print(gen)

77	111011111011011110101110111111110001110111111101110111011111111111110101101010011111111000111111111
78	111111111011011110101110111111110001110111111101110111011111111111110101101010011111111000111111111
78	111111111011011110101110111111110001110111111101110111011111111111110101101010011111111000111111111
77	111111111011011110101110111111110001100111110101110111011111111111111101101010011111110100111111111
78	111111111011011110101110111111110001110111110101110111011111111111111101101010011111110100111111111
78	111111111011011110101110111111110001110111110101110111011111111111111101101010011111110100111111111
78	111111111011011110101110111111110001110111111101110111011111111111110101101010011111111000111111111
...

Dependencies required to play with plots:

import json
import pandas as pd
import matplotlib

Example:

p = pd.read_json(json.dumps(genes_api.stats.population_history_stats))
p.plot(figsize=(20,6))

genes's People

Contributors

lbacik avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.