Git Product home page Git Product logo

easy_gene's Introduction

easy gene - easy to use genetic algorythm

Easy gene is a simple way to introduce genetic algorythms into your project. With a total line count of 37 lines. Insperation for this project was https://www.youtube.com/watch?v=RxTfc4JLYKs.

usage example:

Lets say we are trying to create a ideal animal. In the real world the score_animal(animal) function is unknown. We have two parameters, arm_length and torso_length. In this example we start with a population of 8 animals with distributed values. We use mutation rates of 0.2 for both parameters, save two parents every generation, and evolve 100 times.

from easy_gene import evolve


def score_animal(animal): # max score is when the value is 3, 6
    return 1 / (abs(animal['arm_length'] - 3) + abs(animal['torso_length'] - 6) + 1)

population = [
    {'arm_length': 2.0, 'torso_length': 2.0},
    {'arm_length': 6.0, 'torso_length': 7.0},
    {'arm_length': 4.0, 'torso_length': 2.0},
    {'arm_length': 9.0, 'torso_length': 5.0},
    {'arm_length': 4.0, 'torso_length': 2.0},
    {'arm_length': 9.0, 'torso_length': 5.0},
    {'arm_length': 4.0, 'torso_length': 2.0},
    {'arm_length': 9.0, 'torso_length': 5.0},
]

mutation_rates = {'arm_length': 0.2, 'torso_length': 0.2}
num_parents = 2

iterations = 100
max_scores = []
for _ in range(iterations):
    scores = [score_animal(animal) for animal in population]
    population = evolve(population, scores, num_parents, mutation_rates)
    max_scores.append(max(scores))
        
print(population[0])

result:

{'arm_length': 3.1127586555075375, 'torso_length': 6.533470856616043}

max scores: alt text

  • x axis = iteration
  • y axis = score

easy_gene's People

Contributors

thomsn avatar

Stargazers

Domenic Rosati avatar cagatay odabasi avatar Alex Shevchenko avatar  avatar Haipeng Wang avatar Rany Tith avatar floydwch avatar MtDersvan avatar Oleksandr Buzynnyi avatar Gary Wang avatar Paul English avatar Marc Romeyn avatar John Robenalt avatar Ilya avatar

Watchers

James Cloos avatar  avatar

Forkers

domenicrosati

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.