• Stars
    star
    236
  • Rank 170,480 (Top 4 %)
  • Language
    Python
  • License
    GNU Lesser Genera...
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A python library for genetic algorithms

genetics

A python 3 library for genetic algorithms

Example

Finding Hello World

import string
import random

import genetics

letters = string.ascii_uppercase + string.ascii_lowercase + string.punctuation + ' '
solution = 'Hello World!'

class LetterComponent(genetics.DNAComponent):
    def mutate_value(self):
        return random.choice(letters)

class WordDNA(genetics.arrayed_segment(len(solution), LetterComponent)):
    def score(self):
        return sum(comp.value == letter for comp, letter in zip(self, solution))

    def __str__(self):
        return ''.join(comp.value for comp in self)

sim = genetics.DiscreteSimulation(
    population_size=100,
    mutation_mask=genetics.mutation_rate(0.05),  # Mutate at a 5% rate
    crossover_mask=genetics.two_point_crossover,
    selection_function=genetics.tournament(2),
    elite_size=2,
    initial_generator=WordDNA,
    fitness_function=WordDNA.score)


def dna_stats(population):
    '''Best DNA, best score, average score'''
    best_dna = max(population)
    best_score = best_dna.score
    average_score = sum(member.score for member in population) / len(population)

    return best_dna, best_score, average_score


population = sim.initial_population()

while True:
    best, best_score, average_score = dna_stats(population)

    print('{} | Average score: {}'.format(str(best), average_score))

    if str(best) == solution:
        break

    population = sim.step(population)

Sample Output:

&~$lo,{j'"wi | Average score: 0.148
&~$lo,{j'"wi | Average score: 0.292
H)Xl] ?lDf{@ | Average score: 0.506
H)Xlo {lZ!&@ | Average score: 0.816
H)Xlo {lZ!&@ | Average score: 1.154
HSlldpWjr`z> | Average score: 1.574
HeXyoKWqrl&K | Average score: 2.136
uello*c,rl"! | Average score: 2.722
uello*c,rl"! | Average score: 3.338
Heslo LKrlk! | Average score: 3.814
Heslo LKrlk! | Average score: 4.492
H(llooWIrld! | Average score: 5.258
Hello W,rl]! | Average score: 5.934
HeDlo World! | Average score: 6.628
HeDlo World! | Average score: 7.362
Hello World! | Average score: 8.004

TODO / Working on

Short term

  • Real documentation, not just examples
  • PyPI deployment

Medium term

  • Rebuilding to be better, faster, stronger, easier.
    • Taking advantage of the opportunities provided by the functional design

Long term

  • Comprehensive test coverage
  • New simulation types.
    • Fluid simulation removes discrete generations, allowing agents to combine and die randomly

More Repositories

1

nom-supreme

A collection of utilities for a superior nom experience
Rust
148
star
2

lazy_format

Lazy formatting utility macro for rust
Rust
109
star
3

kaydle

An alternative implementation of Kat's Document Language, including serde integration
Rust
72
star
4

joinery

A Rust library for generically joining iterables with a separator
Rust
72
star
5

autodefault

Autodefault automatically makes your struct literals fill themselves with `..default()`
Rust
71
star
6

ctreg

Compile-time regular expressions, the right way.
Rust
67
star
7

defer-drop

Defer dropping large structs to a background thread
Rust
61
star
8

Generator

A C++ implementation of python-style generators, powered by boost::context
C++
57
star
9

autocommand

Autocommand turns a python function into a CLI program
Python
50
star
10

iterate

Generate is a library for building iterators on the fly
Rust
39
star
11

twinsies

Twinsies is a specialized reference-counting pointer where the item is jointly owned in 2 places
Rust
26
star
12

cooked-waker

A trait-based system for creating async Wakers. Because RawWakers are healthier after they've been cooked.
Rust
21
star
13

stowaway

Stowaway is a library for packing data into a pointer, if it fits.
Rust
20
star
14

seredies

Seredies is a Redis Protocol implementation for serde.
Rust
19
star
15

standard-dist

An attribute macro for creating a `Standard` distribution for rust types
Rust
16
star
16

semester

A proc macro for creating compile-time checked CSS class sets, in the style of classNames
Rust
15
star
17

bobbin

Bobbin is a webapp for viewing and sharing twitter threads
Python
14
star
18

dislike-in-rust

A list of the few things I don't like about rust
13
star
19

LisPlusPlus

A lisp embedded in C++ syntax by overriding the comma operator
C++
12
star
20

brownstone

A library for constructing statically sized arrays
Rust
10
star
21

Dispatch

A python library for overloading functions on type and signature.
Python
9
star
22

cppregistry

A library for registering functions and types to persistent global registries in a boilerplate-free and flexible way.
C++
8
star
23

makepass-rs

A password generator written in Rust, inspired by https://xkcd.com/936/
Rust
7
star
24

handoff

Handoff is an unbuffered, single-producer / single-consumer, async channel
Rust
7
star
25

cool_asserts

Some useful assertions for Rust
Rust
5
star
26

LibCodeJam

Helper library for Google Code Jam, implemented in various languages.
Rust
5
star
27

serde-bufferless

Proof of concept implementation for deserialization without buffers
Rust
5
star
28

typeof-literal

A macro that gets the type of any literal or primitive expression
Rust
5
star
29

plumbing

plumbing is an abstraction for pipelining asynchronous requests through a single request/response channel
Rust
4
star
30

gridly-rs

A 2D grid library written for humans instead of linear algebraists.
Rust
4
star
31

advent2020

My solutions for Advent of Code 2020
Rust
3
star
32

EasyString

A safer string for C
C
3
star
33

generations

A simple utility for running generation-based simulations in Rust
Rust
3
star
34

batchloader

Batchloader is an async Dataloader implementation for Rust with an emphasis on type safety and minimal abstraction cost
Rust
3
star
35

in

`in` is a trivial utility that runs a command in a specific directory. It is for the working directory what `env` is for environment variables.
Rust
2
star
36

foreback

A future adapter for driving background work
Rust
2
star
37

serde-mobile

Pass-by-move versions of `SeqAccess` and `MapAccess` for `serde`
Rust
2
star
38

MakePass

A password generator inspired by https://xkcd.com/936/
Python
2
star
39

advent2019

My solutions for Advent of Code 2019
Rust
1
star
40

hot-takes

My hot takes about programming, because I keep forgetting them
1
star
41

tkinter-async

Helpers for integrating asyncio into a tkinter application (or vice versa)
Python
1
star
42

pyboxcar

A python reimplementation of the BoxCar2D genetic algorithm experiment - http://boxcar2d.com/
1
star
43

advent2023

My solutions for the Advent of Code 2023
Rust
1
star
44

sliver

A high precision, fixed-point Angle type
Rust
1
star
45

faucet

Faucet is an elegant, runtime-agnostic way to create async streams with an async function
Rust
1
star
46

advent2021

Our solutions for Advent of Code 2021
Rust
1
star
47

shargparse

pyparse brings the power of argparse to your shell scripts
Python
1
star
48

rand-float

An improved implementation of random float generation
Rust
1
star
49

Art2D

An increasingly elaborate basic graphics engine. Comes with physics and some software 2D plane projection.
C++
1
star
50

OrthodoxParse

Command line argument parsers in various language, all with the same functionality, interface, and design goals
1
star
51

loopify

Proc macro for "hygienic loops" that correctly bind `break` and `continue` in macro contexts
1
star
52

defibrillator

Defibrillator keeps a long-running server process alive
Rust
1
star
53

usefix

Utility to fix module import styles and automatically handle merge conflicts
Rust
1
star
54

butler

Butler is a minimalist, highly principled rust async runtime
Rust
1
star
55

indent-write

Nestable wrappers for `fmt::Write` and `io::Write` that perform indentation for you
Rust
1
star
56

async-forward

An async forwarder from `Read` to `Write` that operates with more concurrency
Rust
1
star
57

pamir-peace

A Rust implementation of Pax Pamir, a board game by Cole Wehrle
Rust
1
star