Giter VIP home page Giter VIP logo

simplegeneticalgorithm's Introduction

SimpleGeneticAlgorithm

Really simple genetic algorithm in Python

Example

import random

import random_generator
from dna import DNA

TARGET = 'This is the target'
LENGTH_TARGET = len(TARGET)
POPULATION_SIZE = 100
AGENTS_INDEX = 0
FITNESS_SCORES_INDEX = 1

def fitness(agent):
    correct = 0
    for i in range(LENGTH_TARGET):
        if TARGET[i] == agent.dna[i]:
            correct += 1
    return correct / LENGTH_TARGET


last_population = []
current_population = [[], []]  # current_population[0] = agent
                               # current_population[1] = fitness

for _ in range(POPULATION_SIZE):
    agent = DNA(size=LENGTH_TARGET, random_generator=random_generator.RandomLetterGenerator())
    fitness_score = fitness(agent)
    current_population[AGENTS_INDEX].append(agent)
    current_population[FITNESS_SCORES_INDEX].append(fitness_score)

best_dna = ''
while best_dna != TARGET:
    last_population = current_population
    current_population = [[], []]
    for i in range(POPULATION_SIZE):
        random_agent, another_random_agent = random.choices(population=last_population[AGENTS_INDEX], weights=last_population[FITNESS_SCORES_INDEX], k=2)
        child_agent = random_agent.crossover(another_random_agent).mutated(mutation_rate=0.01)
        child_fitness_score = fitness(child_agent)

        current_population[AGENTS_INDEX].append(child_agent)
        current_population[FITNESS_SCORES_INDEX].append(child_fitness_score)
    best_fitness = max(current_population[FITNESS_SCORES_INDEX])
    best_agent = current_population[AGENTS_INDEX][current_population[FITNESS_SCORES_INDEX].index(best_fitness)]
    best_dna = ''.join(best_agent.dna)
print(best_dna)

simplegeneticalgorithm's People

Contributors

willguimont avatar

Watchers

James Cloos avatar  avatar

Forkers

yfortin

simplegeneticalgorithm's Issues

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.