예제 #1
0
P = 40  # population of neural network pool
N = 10  # neurons number in a neural netowrk
S = 1  # stimuli pool size
G = 30 # generations of evolution
I = 10 * 10 ** 3   # iterations of stimuli on neural network
strengthen_rate = 0.001
#
print('#population: %s #neuron: %s #propagation: %s #generation: %s #iteration: %s lr: %s' % (
    P, N, S, G, I, strengthen_rate))
np.random.seed()
stimu_pool = StimuliPool(N, S)
print((' stimulation ').center(100, '-'))
stimu_pool.info()
env = Environment()
# nn_pool = NeuralNetwork.creations(P, N)
gene_pool = Gene.creations(P, N)

for g in range(G):
    # One Generation
    print((' generation %s ' % g).center(100, '-'))
    # continiously stimulate NNs with randomly picked stimuli for I iterations
    stats_l = env.evaluate_fitness(gene_pool, stimu_pool, NeuralNetwork, I, strengthen_rate)
    # env.output_stats(nn_pool)
    # env.store_stats(nn_pool)
    # print(stats_l[0]['accuracy'])
    new_gene_pool = env.build_new_gene_pool(P, gene_pool, stats_l, strength_threshhold=.15)
    if not new_gene_pool:
        break
    # print(mating_pool)
    gene_pool = new_gene_pool
# for nn in nn_pool: print nn.i
예제 #2
0
        gene_pool = []
        for _ in range(P):
            new_gene = Gene.crossover(random.sample(mating_pool, 2)) 
            gene_pool.append(new_gene)

        # mutation

        return gene_pool

    def output_stats(self, nn_pool):
        pass

    def store_stats(self, nn_pool):
        pass


if __name__ == "__main__":
    env = Environment()
    gene_pool = Gene.creations(1, 7)
    stats_l = []
    for gene in gene_pool:
        tmp_m = np.random.rand(7, 7)
        connection_strength_m = np.where(gene.connections == 1,tmp_m, -1)
        stat = {}
        stat['strength_matrix'] = connection_strength_m
        stat['accuracy'] = np.random.rand()
        stats_l.append(stat)
    mating_pool = env.select_mating_pool(gene_pool, stats_l, mating_pool_size=10, strength_threshhold=.1)
    print(mating_pool)