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)
# print generation and time print("\n results of generation: ", g) print("\n evaluation time:", time.time() - startTime) print("\n remaining time:", c.runTime - (time.time() - startTime)) # print results for each parent population print("\n fitness of children 1 in envs 1:"), children_1.Print() print("\n fitness of children 2 in envs 2:"), children_2.Print() print("\n fitness of children 3 in envs 3:"), children_3.Print() print("\n") #---REPLACE---# parents_1.ReplaceWith(children_1) parents_2.ReplaceWith(children_2) parents_3.ReplaceWith(children_3) #---TABULATE FITNESS---# # add fitness of best individual at current generation to # tabulated fitness list for plotting parents_1.Tabulate_Fitness() parents_2.Tabulate_Fitness() parents_3.Tabulate_Fitness() #---INCREMENT GENS---# g += 1 ############### # PICKLE SAVE #
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()
parents.Evaluate(pb, 'doNotSave') # evaluate N unrelated robots in turn print('Fitness values of the 1st generation of parents:') parents.Print() print # end the current line of printing print('Fitness values of consequent generations of parents:') for g in range(0, G): # iterate over generations 0 to G # create a population of children children = copy.deepcopy(parents) # create children which are clones of the parents children.Mutate() # add mutations to the children population children.Evaluate(pb, 'doNotSave') # evaluate N unrelated robots in turn parents.ReplaceWith(children) # replace less fit parents with their more fit children; leave more fit parents in the parent population print(g), parents.Print() parents.Evaluate(False, 'save') # re-evaluate the last generation of parents and draw them to the screen # f = open('lastGen.p', 'w') # open a file called robot.p # pickle.dump(parents, f) # save the parents class instance into this file # f.close() # close the file #f = plt.figure() # create a figure #panel = f.add_subplot(111) # add a drawing panel inside that figure #plt.plot(sensorData) # plot the data in the vector to the panel ##panel.set_ylim(-1,2) # move the lower limit of the vertical limit to -1 and the upper limit to +2
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) children2.Evaluate(envs, False, True, c.evalTime) parents2.ReplaceWith(children2) print('Wheeled: '), print(g), children2.Print() if g == (c.numGens - 1):