class GENEALG: def __init__(self, play_back=False): if play_back: f = open('robot.p', 'rb') self.parent = pickle.load(f) f.close() else: self.parent = POPULATION(c.popSize) self.parent.Initialize() self.parent.Evaluate() def Evolve(self): for g in range(c.numGens): print(g, end=' ') self.parent.Print() child = POPULATION(c.popSize) child.Fill_From(self.parent) child.Evaluate() self.parent.ReplaceWith(child) def Show_Best(self): bestIndividual = self.parent.Best_Individual() self.parent.p[bestIndividual].Start_Evaluation(pb=False, pp=True) self.parent.p[bestIndividual].Compute_Fitness() print(self.parent.p[bestIndividual].fitness) def Save(self): f = open('robot.p', 'wb') pickle.dump(self.parent, f) f.close()
from population import POPULATION import copy parents = POPULATION(10) parents.Initialize() # 09 parents.Evaluate(pp=False, pb=True) parents.Print() for g in range(1, 200): children = copy.deepcopy(parents) children.Mutate() children.Evaluate(pp=False, pb=True) parents.ReplaceWith(children) #parents.Sort() print g, parents.Print() parents.Evaluate(pp=True, pb=False)
import copy from robot import ROBOT import pickle from population import POPULATION from individual import INDIVIDUAL import constants as c from environments import ENVIRONMENTS from environment import ENVIRONMENT envs = ENVIRONMENTS() parents = POPULATION(c.popSize) parents.Initialize() parents.Evaluate(envs, pp=False, pb=True) # parents.Print() for g in range(1, c.numGens): children = POPULATION(c.popSize) children.Fill_From(parents) children.Evaluate(envs, pp=False, pb=True) print(g), children.Print(), print(), parents.ReplaceWith(children) if (g == c.numGens - 1): print(parents.p[0].fitness) for e in range(0, c.numEnvs): parents.p[0].Start_Evaluation(envs.envs[e], pp=False, pb=False)
import pyrosim import matplotlib.pyplot as plt import random import numpy import copy import pickle from individual import INDIVIDUAL from robot import ROBOT from population import POPULATION parents = POPULATION( 10) # creates a empty population but stores the population size parents.Initialize() #start a population of individuals of size 'i' parents.Evaluate(True) # Evaluates the fitness of each individual #exit() # exit the program immediately parents.Print() #print ID and fitness of each individual for i in range(1, 200): children = POPULATION(5) #children.Print() children.Fill_From(parents) #children.Print() children.Evaluate(True) children.Print() parents = children parents.Evaluate(False) parents.Print()
from robot import ROBOT from individual import INDIVIDUAL from population import POPULATION import pyrosim import random import copy import pickle pop = POPULATION(5) pop.Evaluate() pop.Print() # parent = INDIVIDUAL() # parent.Evaluate(True) # print(parent.fitness) # for i in range (0, 500): # child = copy.deepcopy( parent ) # child.Mutate() # child.Evaluate(True) # print('[g:' ,i , '] [p:' ,parent.fitness , ']', '[c:' ,child.fitness , ']') # if ( child.fitness > parent.fitness ): # parent = child # parent.Evaluate(True) # f = open('robot.p','wb') # pickle.dump(parent , f ) # f.close()
fitnesses[0] = highest highest = -100 for g in range(1,c.numGens): children = POPULATION(c.popSize) #FOR RANDOM SEARCH ONLY, COMMENT OUT OTHERWISE #for i in parents.p: #parents.p[i].fitness = 0 children.Fill_From(parents) children.Evaluate(envs, pp=False, pb=True) for i in children.p: if (children.p[i].fitness > highest): highest = children.p[i].fitness fitnesses[g] = highest highest = -100 print(g) children.Print() parents.ReplaceWith(children) if(g == c.numGens-1): for e in range(0, c.numEnvs): children.p[0].Start_Evaluation(envs.envs[e],pp=True, pb=False) low = fitnesses[0] hi = fitnesses[c.numGens-2] print(fitnesses) f = plt.figure() panel = f.add_subplot(111) panel.set_xlim(0, 100) panel.set_ylim(0, 1.2) #panel.set_xlim(0, c.numGens) #panel.set_ylim(low, hi) plt.plot(fitnesses) plt.show()
file_2.close() file_3.close() # skip parent evaluation, child evolution, and saving in movie mode if (not c.movieMode): #---EVALUATE PARENTS---# # evaulate parents in respective environments types parents_1.Evaluate(envs_1, True, pp=False) parents_2.Evaluate(envs_2, True, pp=False) parents_3.Evaluate(envs_3, True, pp=False) # print results for each parent population print("\nfitness of parents 1 in envs 1:"), parents_1.Print() print("\nfitness of parents 2 in envs 2:"), parents_2.Print() print("\nfitness of parents 3 in envs 3:"), parents_3.Print() print("\n") ################### # EVOLVE CHILDREN # ################### startTime = time.time() g = 0 while (time.time() - startTime < c.runTime): # initialize children children_1 = POPULATION(c.popSize)
import copy import pickle from individual import INDIVIDUAL from population import POPULATION # import matplotlib.pyplot as plt parent = POPULATION(5) parent.Evaluate() for g in range(100): print(g, end=' ') parent.Print() child = copy.deepcopy(parent) child.Mutate() child.Evaluate() parent.ReplaceWith(child) # parent = INDIVIDUAL() # parent.Evaluate(False) # for g in range(0, 100): # child = copy.deepcopy(parent) # child.Mutate() # child.Evaluate(True) # print('[g: ', g, ' ] ', '[pw: ', parent.genome, ' ] ', # '[p: ', parent.fitness, ' ] ', '[c: ', child.fitness, ' ]') # if child.fitness > parent.fitness: # parent = child # parent.fitness = child.fitness # parent.Evaluate(False) # f = open('robot.p', 'wb') # pickle.dump(parent, f) # f.close()
from population import POPULATION import copy import constants as c from environments import ENVIRONMENTS import pickle envs = ENVIRONMENTS() parents = POPULATION(c.popSize) parents.Initialize() parents.Evaluate(envs, True, True, c.evalTime) parents.Print() parents2 = POPULATION(c.popSize) parents2.Initialize2() parents2.Evaluate(envs, True, True, c.evalTime) parents2.Print() for g in range(1, c.numGens): children = POPULATION(c.popSize) children.Fill_From(parents) # children.Fill_From2(parents) children.Evaluate(envs, False, True, c.evalTime) parents.ReplaceWith(children) # parents.ReplaceWith2(children) print('Whegged: '), print(g), children.Print() children2 = POPULATION(c.popSize) children2.Fill_From(parents2)