def test_breed(self):
     for i in range(1000):
         b1 = gh.generate_blueprint()
         b2 = gh.generate_blueprint()
         c1,c2 = gh.breed_bitstrings(b1,b2)
         self.assertEqual(len(b1) , len(c1))
         self.assertEqual(len(b2) , len(c2))
 def test_perplexity_func(self):
     #generate bitstrings
     for i in range(1000):
         bitstring = gh.generate_blueprint()
         perp = _bitstring_to_binary(bitstring[0:8])
         read_perp = gh._read_blueprint_perplexity(bitstring)
         self.assertEqual(perp, read_perp)
 def test_Perplexity_limits(self):
     for i in range(10000):
         x = gh.generate_blueprint()
         perp = x[0:8]
         self.assertEqual(8 , len(perp))
         intval = _bitstring_to_binary(perp)
         self.assertTrue(intval >= 5 and intval <= 50)
 def test_decode_blueprint(self):
     for i in range(1000):
         blueprint = gh.generate_blueprint()
         perp, layers = gh.decode_blueprint(blueprint)
         # types are correct
         self.assertTrue(type(perp) == int)
         self.assertTrue(type(layers) == list)
         lenlayers = len(layers)
         self.assertTrue(lenlayers >= 0 and lenlayers <=8)
         # each layer value is an int between zero and 32,768
         for i in layers:
             self.assertTrue(type(i) == int)
             self.assertTrue(i > 0 and i <= 32768)
Exemple #5
0
def test_breeding():
    # test a generation
    num_kids = 10
    num_eras = 5
    kidz = []
    gen = []  # this is a list of FFNN objects

    for i in range(num_kids):
        # get a random dna string
        dna = g.generate_blueprint()
        network = ffnn.FFNN(str(i) + 'th', 22, 3, dna)
        gen.append(network)

    for i in gen:
        i.show_RBM_layers()

    # randomly pick two fittest networks
    two_fittest = []
    two_fittest.append(rand.randint(0, num_kids - 1))
    next_val = rand.randint(0, num_kids - 1)
    while next_val in two_fittest:
        next_val = rand.randint(0, num_kids - 1)
    two_fittest.append(next_val)
    a, b = two_fittest  # the indicies of our two fittest for testing

    for i in range(num_kids // 2):
        try:
            kid1, kid2 = g.breed_bitstrings(gen[a].get_gamete(),
                                            gen[b].get_gamete())
        except IndexError:
            print("ERROR")
            print(a, ":", b)
        kidz.append(ffnn.FFNN(str(i) + 'st_offspring', 22, 3, kid1))
        kidz.append(ffnn.FFNN(str(i) + 'st_offspring', 22, 3, kid2))

    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')

    for i in kidz:
        i.show_RBM_layers()
def test_generations():
    generations = 50

    num_invalid_blueprints = 0

    list_of_layer_sizes = []
    layer_sizes_greater_than_4 = []

    for i in range(generations):
        blueprint = g.generate_blueprint()
        structure = g.read_blueprint(blueprint)
        if not g.is_valid_structure(structure):
            num_invalid_blueprints = num_invalid_blueprints + 1
        else:
            list_of_layer_sizes.append(len(structure))
            if (len(structure) > 4):
                layer_sizes_greater_than_4.append(len(structure))

    print("We generated ", str(num_invalid_blueprints), " invalid structures")
    print("We generated ", str(len(list_of_layer_sizes)), " valid structures")
    print("Of the valid structures, the average layer size was:",
          str(sum(list_of_layer_sizes) / len(list_of_layer_sizes)))
    print("Of the valid structures, there were ",
          str(len(layer_sizes_greater_than_4)), " with 5-7")
 def test_mutate(self):
     for i in range(1000):
         bitstring = gh.generate_blueprint()
         mutant = gh.mutate(bitstring , 0.1)
         self.assertEqual(len(bitstring) , len(mutant))
         self.assertNotEqual(bitstring, mutant)
 def test_bitstring_len(self):
     for i in range(1000):
         x = gh.generate_blueprint()
         len_bitstring = len(x)
         len_expected = 8 + 16 * 8
         self.assertTrue(len_bitstring == len_expected)
import network_gestation_aec as nga 
import genetic_helpers as gh 

blueprint = gh.generate_blueprint()

my_network = nga.generate_nn("test" , blueprint)

my_network.print_structure()