GithubHelp home page GithubHelp logo

inspiaaa / evo Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 57 KB

Lightweight Tool for Genetic Algorithms in Python

License: MIT License

Python 100.00%
evo genetic-algorithms evolution genetic maximization python optimization

evo's Introduction

Evo

A lightweight library for creating genetic algorithms with ease in Python.

  • 100% pure python

  • Lightweight: No external dependencies / packages

  • Quick start template for multi-parameter optimisation problems

Getting started

  1. Copy the evo2.py file into your own project

  2. Import the file and create a class which will represent an individual of the population

from evo2 import Individual, Evolution, Selection
import random


class Optimisation (Individual):
   # __slots__ makes the individual class use less memory
   __slots__ = ("x")

   def __init__(self):
       super().__init__()
       self.x = 0

   # You can pass in your own data for initialisation
   # Although a dictionary is handy for that, any data type can be used
   def create(self, init_params):
       # Here you randomly initialise the individual
       self.x = random.uniform(init_params["lower"], init_params["upper"])

   # Mutate is used for introducing some randomness after pairing
   def mutate(self, mutate_params):
       self.x += random.random() * mutate_params["intensity"]
       # Clamp the x value into the desired range, if it goes over
       self.x = min(mutate_params["upper"], min(mutate_params["lower"], self.x))

   # Create a new offspring (Also known as the crossover operator)
   def pair(self, other, pair_params):
       offspring = Optimisation()
       offspring.x = (self.x + other.x) / 2
  1. Create a fitness function
def curve(x):
    return -x*(x-1)*(x-2)*(x-3)*(x-4)
  1. Initialise a population
evo = Evolution(
    Optimisation,
    size=20,
    n_offsprings=10,
    selection_method=Selection.tournament
    init_params={"lower": 0, "upper": 4},
    mutate_params={"lower": 0, "upper": 4},
    fitness_func=lambda obj: curve(obj.x))
  1. Run the algorithm
# Run 100 generations
evo.evolve(100)
  1. Get the best individual
best = evo.get_best_n(1)[0]
best_fitness = best.fitness

Other features

  • Computing the diversity: Evo uses the standard deviation of the fitness values to describe the diversity of the population
diversity = evo.population.compute_diversity()
  • Stopping after a certain number of generations without improvement
while True:
    evo.evolve()

    # Stop after 50 generations of no improvement
    if evo.stall_gens > 50:
        break
  • Preserving diversity by using "social disasters"
from evo2 import SocialDisasters

for i in range(100):
    diversity = evo.population.compute_diversity()

    if diversity < 50:
        # Randomly re-initialise individuals that are too similar
        SocialDisasters.packing(evo.population, 10)

        # OR only keep the best individual and randomly re-initialise all others
        SocialDisasters.judgement_day(evo.population)
  • Getting the generation number

    print(f"Generation #{ evo.gen_number }")

Using the optimisation template

The Evo library has a builtin template for multi parameter optimisation problems

  1. Copy the evo_templates.py file in to your workspace (as well as the main Evo file itself)

  2. Define the problem

    # 3 parameter function, taking 3 floats
    def cost(a, b, c):
        return (a+1)*(b+2)*(c+3)*(a-b-c)*(c-b-a)
  3. Let the library optimise for you

    from evo_templates import maximise_multi_param
    
    a, b, c = maximise_multi_param(cost, lower_bounds=[-2, -2, -2], upper_bounds=[2, 2, 2]))
    print(a, b, c)

Different selection methods

The selection method tells Evo, which individuals to pair

  • Selection.random Randomly chooses individuals

  • Selection.fittest Chooses the best individuals

  • Selection.roullette_wheel Gives the best individuals a better chance of being chosen

  • Selection.tournament Takes two random individuals and chooses the better one

evo's People

Contributors

inspiaaa avatar

Stargazers

 avatar

Watchers

 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.