for t in time_LW: nn.step( np.concatenate((np.zeros(3), np.zeros(4), body.state()))) body.step(stepsize_LW, nn.output() + np.random.normal(0.0, noisestd)) #body.step(stepsize_LW, nn.output()) #body.step(stepsize_LW, np.concatenate(((nn.output() + np.random.normal(0.0,noisestd)),np.zeros(2)))) fit += body.cx / duration_LW fitness3 = (fit / total_trials_LW) / MaxFit np.save('Fitness_legged', fitness3) #np.load('Fitness_legged.npy') return fitness1 * fitness2 * fitness3 # Evolve and visualize fitness over generations ga = mga.Microbial(fitnessFunction, popsize, genesize, recombProb, mutatProb, demeSize, generations, boundaries) ga.run(tournaments) ga.showFitness() # Get best evolved network and show its activity af, bf, bi = ga.fitStats() #print(ga.avgHistory) #print(ga.bestHistory) ah = ga.avgHistory bh = ga.bestHistory np.save('average_history', ah) np.load('average_history.npy') np.save('best_history', bh) np.load('best_history.npy')
# -*- coding: utf-8 -*- """ Created on Tue Oct 1 16:13:40 2019 @author: benso """ import mga import numpy as np population_size = 20 genotype = 10 #total_pop = np.zeros((population_size, genotype)) transfectprob = 0.5 mutationprob = 0.5 generations = 5 num_tournaments = generations * population_size def fitness_func(genotype): fitness_scores = np.sum(genotype) return fitness_scores ga = mga.Microbial(fitness_func, population_size, genotype, transfectprob, mutationprob) ga.run(num_tournaments)
ip_count += 1 return ip_count # EA Params popsize = 10 genesize = nnsize * nnsize + 2 * nnsize recombProb = 0.5 mutatProb = 0.1 generations = 50 tournaments = generations * popsize # Evolve and visualize fitness over generations ga = mga.Microbial(fitnessFunction, popsize, genesize, recombProb, mutatProb) ga.run(tournaments) ga.showFitness() # Get best evolved network and show its activity af, bf, bi = ga.fitStats() time = np.arange(0.0, duration, stepsize) nn = ctrnn.CTRNN(nnsize) nn.setParameters( ga.pop[bi], WeightRange, BiasRange, TimeConstMin, TimeConstMax ) #edited to give genotype of bi rather than just bi, which is an address in ga.pop nn.initializeState(np.zeros(nnsize)) outputs = np.zeros((len(time), nnsize)) step = 0 for t in time: nn.step(stepsize)
t1 = (t1 + 1) % n t2 = (t2 + 1) % n avg_dist = cum_dist / (n * numpy.sqrt(dim)) return 1 - avg_dist # EA Params popsize = 500 demesize = 5 genesize = nnsize * nnsize + 2 * nnsize recombProb = 0.5 mutatProb = 1 / genesize generations = 1500 # Evolve and visualize fitness over generations ga = mga.Microbial(large_n_fitness, popsize, genesize, recombProb, mutatProb, demesize, generations, 1) ga.run() #ga.showFitness() af, bf, bi = ga.fitStats() #id = int(sys.argv[1]) numpy.save("avg_fitness_{0}_{1}.npy".format(targ, num), ga.avgHistory) #"avg_fitness_"+str(id)+".npy" numpy.save("best_fitness_{0}_{1}.npy".format(targ, num), ga.bestHistory) #"best_fitness_"+str(id)+".npy" numpy.save("best_individual_{0}_{1}.npy".format(targ, num), bi) #"best_individual_"+str(id)+".npy" #avg_fit = open("avg_fitness_{0}.npy".format(num), 'rb') #numpy.save(avg_fit, ga.avgHistory) #avg_fit.flush()