Skip to content

Instantly share code, notes, and snippets.

@wwwslinger
Last active January 1, 2016 08:59
Show Gist options
  • Save wwwslinger/8121656 to your computer and use it in GitHub Desktop.
Save wwwslinger/8121656 to your computer and use it in GitHub Desktop.
Evolve yourself a Merry Christmas!
from pyevolve import G1DBinaryString, GSimpleGA
import binascii, numpy as np
targ = np.array(list(str(bin(int(binascii.hexlify("Merry Christmas!"), 16))[2:])))
genome = G1DBinaryString.G1DBinaryString(127)
genome.evaluator.set(lambda chromosome: sum(np.array(list(str(chromosome.getBinary()))) == targ))
ga = GSimpleGA.GSimpleGA(genome,42)
ga.setGenerations(100)
ga.evolve(freq_stats=10)
print ga.bestIndividual()
print "".join(targ)
print binascii.unhexlify('%x' % int("0b"+str(ga.bestIndividual().getBinary()),2))
@wwwslinger
Copy link
Author

$ python pyevolve_xmas.py
Gen. 1 (1.00%): Max/Min/Avg Fitness(Raw) [89.19(78.00)/64.92(72.00)/74.33(74.33)]
Gen. 10 (10.00%): Max/Min/Avg Fitness(Raw) [118.98(104.00)/82.18(95.00)/99.15(99.15)]
Gen. 20 (20.00%): Max/Min/Avg Fitness(Raw) [139.80(120.00)/113.17(116.00)/116.50(116.50)]
Gen. 30 (30.00%): Max/Min/Avg Fitness(Raw) [144.46(123.00)/116.82(120.00)/120.39(120.39)]
Gen. 40 (40.00%): Max/Min/Avg Fitness(Raw) [144.31(123.00)/117.96(120.00)/120.26(120.26)]
Gen. 50 (50.00%): Max/Min/Avg Fitness(Raw) [145.36(124.00)/119.97(121.00)/121.14(121.14)]
Gen. 60 (60.00%): Max/Min/Avg Fitness(Raw) [145.43(124.00)/119.57(121.00)/121.19(121.19)]
Gen. 70 (70.00%): Max/Min/Avg Fitness(Raw) [146.65(125.00)/120.35(122.00)/122.21(122.21)]
Gen. 80 (80.00%): Max/Min/Avg Fitness(Raw) [146.62(125.00)/120.56(122.00)/122.19(122.19)]
Gen. 90 (90.00%): Max/Min/Avg Fitness(Raw) [146.62(125.00)/120.56(122.00)/122.19(122.19)]
Gen. 100 (100.00%): Max/Min/Avg Fitness(Raw) [149.09(127.00)/122.10(124.00)/124.24(124.24)]
Total time elapsed: 2.875 seconds.

  • GenomeBase
    Score: 127.000000
    Fitness: 149.085000

    Slot [Evaluator](Count: 1)
    Name:
    Slot [Initializator](Count: 1)
    Name: G1DBinaryStringInitializator
    Doc: 1D Binary String initializator
    Slot [Mutator](Count: 1)
    Name: G1DBinaryStringMutatorFlip
    Doc: The classical flip mutator for binary strings
    Slot [Crossover](Count: 1)
    Name: G1DBinaryStringXSinglePoint
    Doc: The crossover of 1D Binary String, Single Point

    .. warning:: You can't use this crossover method for binary strings with length of 1.

  • G1DBinaryString
    String length: 127
    String: 1001101011001010111001001110010011110010010000001000011011010000111001001101001011100110111010001101101011000010111001100100001

1001101011001010111001001110010011110010010000001000011011010000111001001101001011100110111010001101101011000010111001100100001
Merry Christmas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment