def test_valid3(self): p1_dict = { 'INDI': 'I1', 'NAME': 'Person /One', 'SEX': 'M', 'BIRT': '24 Feb 1980', 'FAM': 'F0' } p2_dict = { 'INDI': 'I2', 'NAME': 'Person /Two', 'SEX': 'F', 'BIRT': '13 Feb 1980', 'FAM': 'F0' } person3_dict = {'INDI': 'I3', 'NAME': 'Person /3', 'SEX': 'M', 'BIRT': '15 Feb 2018', 'FAM': 'F0'} person4_dict = {'INDI': 'I4', 'NAME': 'Person /4', 'SEX': 'M', 'BIRT': '15 Feb 2018', 'FAM': 'F0'} person5_dict = {'INDI': 'I5', 'NAME': 'Person /5', 'SEX': 'M', 'BIRT': '15 Feb 2018', 'FAM': 'F0'} person6_dict = {'INDI': 'I6', 'NAME': 'Person /6', 'SEX': 'M', 'BIRT': '15 Feb 2018', 'FAM': 'F0'} person7_dict = {'INDI': 'I7', 'NAME': 'Person /7', 'SEX': 'M', 'BIRT': '15 Feb 2019', 'FAM': 'F0'} husband = Individual.instance_from_dict(p1_dict) wife = Individual.instance_from_dict(p2_dict) children = [person3_dict, person4_dict, person5_dict, person6_dict, person7_dict] for i in range(len(children)): children[i] = Individual.instance_from_dict(children[i]) fam_dict = {'FAM': 'F0', 'HUSB': husband, 'WIFE': wife, 'MARR': '14 FEB 2017', 'DIV': '13 FEB 2040', 'CHIL': children} self.assertTrue(children[4].age)
def run_for_start_point(self, start_point): solution = [] curr_city = start_point cities_left_to_visit = set(range(self.Num_of_cities)) dist_sum = 0 closest_city = 0 append = solution.append dist_method = self.dist_method for i in range(0, self.Num_of_cities - 1): append(curr_city) cities_left_to_visit.remove(curr_city) shortest_dist = np.inf # przeanalizuj dystans do kazdego miasta i wybierz najblizsze for considered_city in cities_left_to_visit: dist = dist_method(curr_city, considered_city) if dist < shortest_dist: shortest_dist = dist closest_city = considered_city curr_city = closest_city dist_sum += shortest_dist dist_sum += dist_method(curr_city, start_point) append(curr_city) best_solution = Individual(solution) best_solution.Fitness = dist_sum #print(best_solution.Genotype, best_solution.Fitness) return best_solution
def crossing_over(self): ''' Genes of the population undergo crossing over ''' for individual in range(0, self.pop_size - 1, 2): child_one = [] child_two = [] split_point = np.random.randint(0, self.rectangles * self.alleles) for i in range(0, split_point): child_one.append(self.population[individual].gray_encoding[i]) child_two.append(self.population[individual + 1].gray_encoding[i]) for i in range(split_point, self.rectangles * self.alleles): child_one.append(self.population[individual + 1].gray_encoding[i]) child_two.append(self.population[individual].gray_encoding[i]) child_one = "".join(child_one) child_two = "".join(child_two) brother = Individual(self.rectangles, child_one, self.pixel_x, self.pixel_y) sister = Individual(self.rectangles, child_two, self.pixel_x, self.pixel_y) self.population.append(brother) self.population.append(sister)
def crossover(self, ind1: Individual, ind2: Individual) -> Tuple[Individual, Individual]: point = random.randint(1, self.dna_length-1) str1 = self.mutate(ind1.dna[:point]+ind2.dna[point:]) str2 = self.mutate(ind2.dna[:point]+ind1.dna[point:]) ind3 = Individual(self.test, str1) ind4 = Individual(self.test, str2) return ind3, ind4
def test_both_invalid(self): p1_dict = { 'INDI': 'I0', 'NAME': 'Person /One', 'SEX': 'F', 'BIRT': '28 Feb 1980', 'FAM': 'F0' } p2_dict = { 'INDI': 'I1', 'NAME': 'Person /Two', 'SEX': 'M', 'BIRT': '13 Feb 1980', 'FAM': 'F0' } husband = Individual.instance_from_dict(p1_dict) wife = Individual.instance_from_dict(p2_dict) fam_dict = { 'FAM': 'F0', 'HUSB': husband, 'WIFE': wife, 'MARR': '15 Mar 2002', } self.assertTrue( len(Family.instance_from_dict(fam_dict).anomalies) == 2)
def __init__(self): self.ind = [] self.nextInd = [] for _ in range(Individual.POP_SIZE): self.ind.append(Individual()) self.nextInd.append(Individual()) self.evaluate()
def eval_func(chromosome): conf = LoadSystemConfiguration() if not initialPopulationSetted: setInitialPopulation(gaEngine) prop = DTIndividualPropertyVanillaEvolutive() if int(conf.getProperty("Concatenate walk cycles?")): embryo = DTIndividualGeneticMatrixWalk(chromosomeToLucyGeneticMatrix(chromosome)) else: embryo = DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(chromosome)) precycleFile = os.getcwd()+"/mocap/cmu_mocap/xml/util/walk_precycle.xml" preCycleEmbryo = DTIndividualGeneticTimeSerieFile(precycleFile) preCycleEmbryo.concatenate(embryo) newEmbryo = preCycleEmbryo #embryoLength = newEmbryo.getLength() individual = Individual(prop, newEmbryo) #individual.setLength(embryoLength) fitness = individual.execute() #return the fitness resulting from the simulator execution if int(conf.getProperty("re-evaluate fittest?"))==True: if fitness > max_score: #is really a better fitness ? candidateFitness = fitness fitness = individual.execute() print "candidateFitness: ", candidateFitness, "fitness: ", fitness while abs(candidateFitness-fitness) > 0.01: candidateFitness=fitness fitness = individual.execute() print "candidateFitness: ", candidateFitness, "fitness: ", fitness #the candidateFitness was validated! fitness = candidateFitness return fitness
def test_list_multipe_births_US32(self): from Individual import Individual list1 = { 'A1': Individual('A1', birthday='1960-03-12'), 'A2': Individual('A2', birthday='1960-03-12') } self.assertFalse(list_multipe_births_US32(list1, list1['A1']))
def startOfRun(self): if 'info-operator' in self.__OUTPUT_LIST: Individual.printOperatorList() if 'info-pygenalg' in self.__OUTPUT_LIST: print '***********************************************************************' print 'PYGENALG parameters:' self.printInformation()
def __onePoint_crossover__(self, individual1, individual2): #creates two new individuals #newIndiv=Individual(); TODO find out this weird bug.. for some reason the newIndiv wasn't reinitialized;;;This is the default behaviour of python when I use a muttable object for optional parameter # Solved look here for the answer http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument loop_code1 = [] loop_code2 = [] crossover_point = self.rand.choice(range(int(self.loopSize) - 1)) for i in range(int(self.loopSize)): if (i > crossover_point): #do crossover loop_code1.append(individual2.getInstruction(i).copy()) loop_code2.append(individual1.getInstruction(i).copy()) else: #keep instruction as it is loop_code1.append(individual1.getInstruction(i).copy()) loop_code2.append(individual2.getInstruction(i).copy()) children = [] children.append( Individual(sequence=loop_code1, generation=self.populationsExamined)) children.append( Individual(sequence=loop_code2, generation=self.populationsExamined)) children[0].setParents(individual1, individual2) ##the first parent is the code that remains the same.. the second parent is the code that came after crossover children[1].setParents(individual2, individual1) return children
def test_firstCousinsMarried_us19(self): from Individual import Individual from Family import Family list1 = { 'A1': Individual('A1', childFamily='B1'), 'A2': Individual('A1'), 'A3': Individual('A3', childFamily='B1', spouseFamily='B3'), 'A4': Individual('A4', childFamily='B2', spouseFamily='B4') } #Married to first cousin list2 = { 'B1': Family('B1'), 'B2': Family('B2', husband='A1', wife='A2'), 'B3': Family('B3', husband='A3'), 'B4': Family('B4', husband='A4', wife='A5') } children = ['A1', 'A3'] for child in children: list2['B1'].setChildren(child) list2['B2'].setChildren('A4') cousins = ['A5', 'A6'] for cousin in cousins: list2['B3'].setChildren(cousin) self.assertTrue(firstCousinsMarried_us19(list1['A4'], list1, list2)) #Not married to first cousin list3 = { 'B1': Family('B1'), 'B2': Family('B2', husband='A1', wife='A2'), 'B3': Family('B3'), 'B4': Family('B4', husband='A4', wife='A7') } self.assertFalse(firstCousinsMarried_us19(list1['A4'], list1, list3))
def __init__(self, cities): self.ind = [] self.nextInd = [] self.cities = cities for _ in range(Individual.POP_SIZE): self.ind.append(Individual(cities)) self.nextInd.append(Individual(cities)) self.evaluate()
def eval_func(chromosome): if not initialPopulationSetted: setInitialPopulation(gaEngine) #prop = DTIndividualPropertyVanilla() #TODO create a vanilla property as default argument in Individual constructor prop = DTIndividualPropertyVanillaEvolutive() individual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(chromosome))) fitness = individual.execute() #return the fitness resulting from the simulator execution return fitness
def crossover(parent1, parent2): n = parent1.n nb_of_weights = parent1.getNumberOfWeights() child = Individual(n) for i in range(nb_of_weights): avg = 0.5 * (parent1.getWeight(i) + parent2.getWeight(i)) child.setWeight(i, avg) return child
def initialize(self): ''' initialize the population ''' for i in range(0, self.sizepop): ind = Individual(self.vardim, self.bound) ind.generate() self.population.append(ind)
def initial(size, population, mylevel): Individual.level = mylevel Individual.CHROMOSOME_LENGTH = len(mylevel) for _ in range(size): chromosome = Individual.create_chromosome() population.append(Individual(chromosome))
def crossover(parent1, parent2): n = parent1.n nb_of_weights = parent1.getNumberOfWeights() child = Individual(n) for i in range(nb_of_weights): avg = 0.5*(parent1.getWeight(i) + parent2.getWeight(i)) child.setWeight(i, avg) return child
def crossover(self, mother, father): offspring = Individual(self.gvars) for i in range(offspring.segment_quantity): average_gene = (mother.genes[i] + father.genes[i]) / 2 offspring.genes[i] = average_gene offspring.get_fitness() return offspring
def crossover(individual1_passed, individual2_passed): new_sol = Individual() for i in range(individual1_passed.size()): if random() <= Algorithm.Uniform_rate: new_sol.set_gene(i, individual1_passed.get_gene(i)) else: new_sol.set_gene(i, individual2_passed.get_gene(i)) return new_sol
def generate(self): for pop in range(self.size): individual = Individual() while individual.weight == 0 or individual.weight >= 15: individual.create(self.blocks) self.population.append(individual)
def setInitialPopulation (ga_engine): prop = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() conf = LoadSystemConfiguration() CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") GAwalkDir = os.getcwd()+conf.getDirectory("GAWalk Files") UIBLHDir = os.getcwd()+conf.getDirectory("UIBLH mocap Files") BalieroDir = os.getcwd()+conf.getDirectory("Baliero transformed walk Files") ADHOCDir = os.getcwd()+conf.getDirectory("ADHOC Files") geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") population = ga_engine.getPopulation() popSize = len(population) individualCounter = 0 # if individualCounter < popSize: # for filename in glob.glob(os.path.join(GAwalkDir, '*.xml')): # print individualCounter, " individuals processed!" # print 'inserting individual: ' + filename + " into the initial population" # walk = Individual(propVanilla, DTIndividualGeneticTimeSerieFile(filename)) # geneticMatrix = walk.getGenomeMatrix() # if individualCounter < popSize: # adan = population[individualCounter] # for i in xrange(adan.getHeight()): # if i == len(geneticMatrix): # break # for j in xrange(adan.getWidth()): # adan.setItem(i,j,geneticMatrix[i][j]) # population[individualCounter]=adan # individualCounter = individualCounter + 1 # else: # break if individualCounter < popSize: for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): print individualCounter, " individuals processed!" print 'inserting individual: ' + filename + " into the initial population" walk = Individual(prop, DTIndividualGeneticTimeSerieFile(filename)) geneticMatrix = walk.getGenomeMatrix() if individualCounter < popSize: adan = population[individualCounter] for i in xrange(adan.getHeight()): if i == len(geneticMatrix): break for j in xrange(adan.getWidth()): adan.setItem(i,j,geneticMatrix[i][j]) population[individualCounter]=adan individualCounter = individualCounter + 1 else: break global initialPopulationSetted initialPopulationSetted = True
def __init__(self, size, initialize_population): self.size = size self.individuals = [None] * size if initialize_population: for i in range(0, size): individual = Individual() individual.generate_individual() self.save_individual(i, individual)
def __init__(self, size, sigma, f): self.sigma = sigma self.size = size self.pop = [] self.fitness = f for i in range(self.size): ind = Individual(sigma) ind.value_fitness = self.fitness(ind) self.pop.append(ind)
def mutate(individual: Individual, mutation_rate): no_of_genes_to_mutate = int(mutation_rate * individual.chromosome_length) for i in range(no_of_genes_to_mutate): gene_to_mutate = np.random.randint(0, individual.chromosome_length - 1) if individual.chromosome[gene_to_mutate] == 1: individual.chromosome[gene_to_mutate] = 0 else: individual.chromosome[gene_to_mutate] = 1 return individual
def test_recent_death_us36(self): from Individual import Individual self.assertEqual( recent_deaths_us36(Individual(self, 10, death='2017-11-11')), False) self.assertFalse( recent_deaths_us36(Individual(self, 10, death='1845-10-05'))) self.assertFalse( recent_deaths_us36(Individual(self, 10, death='2017-10-10')))
def test_List_upcoming_birthdsy_US38(self): from Individual import Individual from Family import Family list1 = { 'A1': Individual('A1', birthday='1960-12-10'), 'A2': Individual('A1', birthday='1960-12-11'), 'A3': Individual('A1', birthday='1993-12-09') } self.assertFalse(List_upcoming_birthdsy_US38(list1['A1']))
def MakeReport(self): print("===========Raport============") print("Pokolenie : {}".format(self.generation)) best = Individual() for individual in self.individuals: print(individual) if individual.GetFitness() > best.GetFitness(): best = individual print("Max Fit : {}".format(self.GetMaxFit())) print(best) print("Params : {}".format(best.GetVariables()))
def test_siblingSpacing_us13(self): from Individual import Individual from Family import Family list1 = {'B1': Family('B1')} children = ['A3', 'A4', 'A5'] for child in children: list1['B1'].setChildren(child) #Spacing violated by less than 8 months condition list2 = { 'A3': Individual('A1', birthday='1980-01-01'), 'A4': Individual('A2', birthday='1987-02-05'), 'A5': Individual('A3', birthday='1980-07-01') } self.assertTrue(siblingSpacing_us13(list1['B1'], list2)) #Spacing violated by more than 2 days condition list3 = { 'A3': Individual('A1', birthday='1980-01-01'), 'A4': Individual('A2', birthday='1987-02-05'), 'A5': Individual('A3', birthday='1980-01-03') } self.assertTrue(siblingSpacing_us13(list1['B1'], list2)) #Spacing not violated list4 = { 'A3': Individual('A1', birthday='1980-01-01'), 'A4': Individual('A2', birthday='1987-02-05'), 'A5': Individual('A3', birthday='1984-01-03') } self.assertFalse(siblingSpacing_us13(list1['B1'], list4))
def doSim(self, arr): a = arr for i in range(len(a)): self.population.getIndividuals()[i] = a[i] print("Population of " + str(self.population.getPopSize()) + " individual(s).") print("Generation: " + str(1) + " Fittest Score: " + str(self.population.getFittestScore())) GeneticALGO.showGeneticPool(self.population.getIndividuals()) # have the while loop condition be whatever you want fitness to attain. For example, while self.maxfit != 16. if you want the final score to be 16 while self.maxfit is not 16.41: self.selection() self.crossover() var = random.randrange(0, 8) if var == 1: self.mutation() counter = 0 self.updateAll() if self.inAll(): None else: self.addFittestOffspring() self.generationCount = self.generationCount + 1 self.findmax() ''' for individual1 in self.population.getIndividuals(): self.all.append(individual1) for individual in self.all: if individual is not None: if GeneticALGO.areSame(individual,self.place): counter = counter + 1 if counter == 0: self.generationCount = self.generationCount + 1 self.all.append(self.place) self.addFittestOffspring() ''' print("") print("----------This is a GeneticALGO test----------") print("Generation: " + str(self.generationCount) + " Fittest Score: " + str(self.population.getFittestScore())) GeneticALGO.showGeneticPool(self.population.getIndividuals()) self.place = Individual(0, 0, 0, 0, 0, 0, 0) self.population.setFittestScore() print("") print("Solution found in generation: " + str(self.generationCount)) print("Index of winner Individual: " + str(self.population.getFittestIndex())) print("Fitness: " + str(self.population.getFittestScore())) winGenes = numpy.empty(self.numberofGenes, dtype=Individual) print("Genes: ") for i in range(self.numberofGenes): print("Gene: " + str(i + 1) + " " + str(self.population.selectFittest().getGenes()[i])) return self.generationCount + 5
def test_List_large_age_difference_US34(self): from Individual import Individual from Family import Family list1 = { 'A1': Individual('A1', age='106'), 'A2': Individual('A1', age='39') } list2 = {'B1': Family('B1', husband='A1', wife='A2')} self.assertFalse(List_large_age_difference_US34(list1, list2))
def executeGeneticAlgorithm(self): for _ in range(0, self.numberPopulation): individual = Individual() individual.generateGenes(len(self.objetive), self.objetive) self.populations.append(individual) self.generation = 0 print("Buscando mejor individuo....") while True: self.evaluateMembersGeneration() self.selectMembersGeneration() self.reproductionMembersGeneration()
def setRandomPopulation(self, population_size, gen_number, allowed_gens=[0, 1], clean_function=ident, mutate_function=ident): self.individuals = [] for index in range(population_size): individual = Individual([], clean_function, mutate_function) individual.setRandomGens(gen_number, allowed_gens) self.individuals.append(individual)
def run_main(): initialPopulationSize = 51 generations = 1500 conf = LoadSystemConfiguration() #TODO make an object to encapsulate this kind of information # Genome instance genome = G2DList.G2DList(400, 18) genome.setParams(rangemin=0, rangemax=360) # The evaluator function (objective function) genome.evaluator.set(eval_func) genome.crossover.set(Crossovers.G2DListCrossoverSingleHPoint) #genome.mutator.set(Mutators.G2DListMutatorIntegerRange) genome.mutator.set(Mutators.G2DListMutatorIntegerGaussian) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(generations) #TODO class atribute ga.setPopulationSize(initialPopulationSize) #TODO class atribute ga.setMutationRate(0.2) ga.selector.set(Selectors.GRankSelector) #ga.selector.set(Selectors.GTournamentSelector) #ga.selector.set(Selectors.GRouletteWheel) ga.setElitism(True) ga.setElitismReplacement(initialPopulationSize/3) #ga.terminationCriteria.set(ConvergenceCriteria) # Create DB Adapter and set as adapter sqlite_adapter = DBAdapters.DBSQLite(identify="Lucy walk", resetDB=True) ga.setDBAdapter(sqlite_adapter) #callback to persist best individual of each generation ga.stepCallback.set(generationCallback) #keep a reference to the genetic algorithm engine global gaEngine gaEngine = ga # Do the evolution, with stats dump # frequency of 2 generations ga.evolve(freq_stats=1) # Best individual best = ga.bestIndividual() score = best.getRawScore() timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(generations) + ".xml" prop = DTIndividualPropertyVanilla() #TODO create a vanilla property as default argument in Individual constructor bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") bestIndividual.persist(geneticPoolDir + filename) #TODO store all the population, not only the fitest print ga.getStatistics()
def test_listIndividuals(standartinput): genIndividual = Individual("random individual", standartinput[0], standartinput[1], standartinput[2]) genfisher = Fisher("random fisher", standartinput[3], standartinput[5], standartinput[4], None, "0") genIsland = Island("Generic Island") assert genIsland.addIndividual(genIndividual) == 1 assert genIsland.addIndividual(genfisher) == 1 assert genIsland.listIndividuals().index(genIndividual.getDetail()) >= 0 assert genIsland.listIndividuals().index(genfisher.getDetail()) >= 0 assert genIsland.addIndividual(None) == 0 assert len(genIsland.listIndividuals()) == 2
def test_checkBirthBeforeMarriageOfParents_us08(self): from Family import Family from Individual import Individual self.assertEqual( checkBirthBeforeMarriageOfParents_us08( Family(self, 10, marriage='1945-10-05'), Individual(self, 10, birthday='1915-10-05')), False) self.assertTrue( checkBirthBeforeMarriageOfParents_us08( Family(self, 10, marriage='1945-10-05', divorce='1955-10-05'), Individual(self, 10, birthday='1947-10-05', death='1975-03-10')))
def getNeighbours(self, state): geno = state.getGeno() neighbors = [] for i in range(self.__size): for j in range(self.__size): for v in product(self.__setS, self.__setT): geno[i][j] = v[0] geno[i + self.__size][j] = v[1] indiv = Individual(self.__size, self.__setS, self.__setT) indiv.setGeno(geno) neighbors.append(indiv) return neighbors
def test_lessThan150Years_US07(self): #from Individual import birthBeforeMarriage from Individual import Individual self.assertEqual( lessThan150Years_US07(Individual(self, 10, birthday='1945-01-05')), True) self.assertFalse( lessThan150Years_US07(Individual(self, 10, birthday='1845-10-05'))) self.assertFalse( lessThan150Years_US07( Individual(self, 10, birthday='1845-10-05', death='2010-10-05')))
def generationCallback(ga_engine): # persist best individual at the moment conf = LoadSystemConfiguration() #TODO make an object to encapsulate this kind of information geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") gen = ga_engine.getCurrentGeneration() best = ga_engine.bestIndividual() score = best.getRawScore() timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(gen) + ".xml" prop = DTIndividualPropertyVanilla() #TODO create a vanilla property as default argument in Individual constructor bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) bestIndividual.persist(geneticPoolDir + filename) ga_engine.getDBAdapter().commit() ##population = ga_engine.getPopulation() ##averagePopulation = getPopulationAverage(population) #averageGeneration = getPopulationAverage(gen) ##print "current population raw score average: ", averagePopulation #print "current generation raw score average: ", averageGeneration return False
def generationCallback(ga_engine): # persist best individual at the moment conf = LoadSystemConfiguration() #TODO make an object to encapsulate this kind of information geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") gen = ga_engine.getCurrentGeneration() best = ga_engine.bestIndividual() score = best.getRawScore() #check if the convergence criteria has been reached global max_score, max_score_generation, convergenceCriteria if score > max_score: max_score = score max_score_generation = gen else: #if the score doesn't improve in NUMBER_GENERATIONS_CONVERGENCE_CRITERIA generations then there is no reason to continue and we have reach a convergence if gen - max_score_generation > NUMBER_GENERATIONS_CONVERGENCE_CRITERIA: convergenceCriteria = True timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(gen) + ".xml" #at generation 0 is the firs time to create de directory and store the GA parameters if gen == 0: global experimentDir experimentDir = geneticPoolDir + timestr global experimentTime experimentTime = timestr os.mkdir(experimentDir) storeExperimentGAparameters() prop = DTIndividualPropertyVanilla() bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) bestIndividual.persist(os.path.join(experimentDir, filename)) ga_engine.getDBAdapter().commit() #population = ga_engine.getPopulation() #popSize = len(population) print "generation executed!, best fit of generation: ", score, "fittest: ", max_score, "reached in generation: ", max_score_generation return False
def crossover( p, indexP1, indexP2): parent1 = p.getIndividual(indexP1) parent2 = p.getIndividual(indexP2) child1 = Individual() child2 = Individual() crossPoint1 = random.randrange(0,8) ##similitudes one-point crossover tempList = parent1.getAllSimilitudes()[:crossPoint1] tempList.extend( parent2.getAllSimilitudes()[crossPoint1:] ) child1.setAllSimilitudes(tempList) tempList = parent2.getAllSimilitudes()[:crossPoint1] tempList.extend( parent1.getAllSimilitudesCopy()[crossPoint1:] ) child2.setAllSimilitudes(tempList) ##indexList 2 point crossover crossPoint1 = random.randrange(0,64) crossPoint2 = random.randrange(0,64) if crossPoint1 > crossPoint2: crossPoint1, crossPoint2 = crossPoint2, crossPoint1 tempList = parent1.getIndexes()[:crossPoint1] tempList.extend( parent2.getIndexes()[crossPoint1:crossPoint2] ) tempList.extend( parent1.getIndexes()[crossPoint2:] ) child1.setAllIndexes(tempList) tempList = parent2.getIndexes()[:crossPoint1] tempList.extend( parent1.getIndexes()[crossPoint1:crossPoint2] ) tempList.extend( parent2.getIndexes()[crossPoint2:] ) child2.setAllIndexes(tempList) return (child1, child2)
def crossover(self, parent_1, parent_2): child = Individual() for i in range(0, child.get_size()): # Divide the genes according to the uniform rate if random.random() <= self.uniform_rate: child.set_gene(i, parent_1.get_gene(i)) else: child.set_gene(i, parent_2.get_gene(i)) return child
class Test(unittest.TestCase): def setUp(self): self.iDefault = Individual(); self.i10 = Individual(10); pass def tearDown(self): pass def testInit(self): self.assertEqual(self.iDefault.getNumberOfWeights(), 3*5+1, "iDefault.getNumberOfWeights() should be %d but is %d" % (3*5+1, self.iDefault.getNumberOfWeights())) self.assertEqual(self.i10.getNumberOfWeights(), 3*10+1, "i10.getNumberOfWeights() should be %d but is %d" % (3*10+1, self.i10.getNumberOfWeights())) self.assertEqual(self.iDefault.getNumberOfWeights(), len(self.iDefault.weights), "len(iDefault.weights) should be %d but is %d" % (self.iDefault.getNumberOfWeights(), len(self.iDefault.weights))) self.assertEqual(self.i10.getNumberOfWeights(), len(self.i10.weights), "len(i10.weights) should be %d but is %d" % (self.i10.getNumberOfWeights(), len(self.i10.weights))) self.assertEqual(self.i10.fitness, 0.0, "i10.fitness should be %f but is %f" % (0.0, self.i10.fitness)) self.i10.setFitness(4.5) self.assertEqual(self.i10.fitness, 4.5, "i10.fitness should be %f but is %f" % (4.5, self.i10.fitness)) self.i10.setWeight(0, 22) self.assertEqual(self.i10.getWeight(0), 22, "i10.weight[%d] should be %f but is %f" % (0, 22, self.i10.getWeight(0)))
def setUp(self): self.indi = Individual() self.indi.addID('@I2@') self.indi.addName('John Rivas') self.indi.addSex('M') self.indi.addBirt('9 MAY 1978') self.indi.addDeat('12 APR 2013') self.indi.addFams('@F2@') self.indi.addFamc('@F1@') self.fam1 = Family() self.fam1.addFamID('@F2@') self.fam1.addHusb('@I1@') self.fam1.addWife('@I2@') self.fam1.addChil('@I4@') self.fam1.addChil('@I5@') self.fam1.addMarr('5 OCT 1999') self.fam1.addDiv('12 JUN 2012') self.fam2 = Family() self.fam2.addFamID('@F3@') self.fam2.addHusb('@I3@') self.fam2.addWife('@I3@')
def createIndividual(filename): if int(conf.getProperty("Lucy simulated?"))==1: precycleFile = os.getcwd()+"/mocap/cmu_mocap/xml/util/walk_precycle.xml" preCycleEmbryo = DTIndividualGeneticTimeSerieFile(precycleFile) preCycleLength = preCycleEmbryo.getLength() if int(conf.getProperty("Concatenate walk cycles?")): walkEmbryo = DTIndividualGeneticTimeSerieFileWalk(os.getcwd()+"/"+filename) embryoCycleLength = (walkEmbryo.getLength() - preCycleLength) / int(conf.getProperty("Concatenate walk cycles?")) else: walkEmbryo = DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename) embryoCycleLength = walkEmbryo.getLength() - preCycleLength #walk = Individual(geneticVanillaPropNothingToAvoid, DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename)) #For Reda Al-Bahrani work compability if int(conf.getProperty("concatenate external cycle file?")): externalFirstCycleFile = os.getcwd() + conf.getFile("external cycle file") externalFirstCycle = DTIndividualGeneticTimeSerieFile(externalFirstCycleFile) preCycleEmbryo.concatenate(externalFirstCycle) preCycleLength = preCycleEmbryo.getLength() embryoCycleLength = embryoCycleLength - externalFirstCycle.getLength() preCycleEmbryo.concatenate(walkEmbryo) walkEmbryo = preCycleEmbryo walk = Individual(geneticVanillaProp, walkEmbryo) walk.setPrecycleLength(preCycleLength) walk.setCycleLength(embryoCycleLength) else: #TODO physical stage #TODO restructure the precycle for the case of physical and simulated walkEmbryo = DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename) precycleFile = os.getcwd()+"/mocap/cmu_mocap/xml/util/walk_precycle.xml" preCycleEmbryo = DTIndividualGeneticTimeSerieFile(precycleFile) preCycleEmbryo.concatenate(walkEmbryo) if int(conf.getProperty("concatenate external cycle file?")): precycleFile = os.getcwd() + conf.getFile("external cycle file") firstCycle = DTIndividualGeneticTimeSerieFile(precycleFile) preCycleEmbryo.concatenate(firstCycle) walkEmbryo = preCycleEmbryo walk = Individual(physicalProp, walkEmbryo) #TODO add support for walking cycle return walk
def setUp(self): self.iDefault = Individual(); self.i10 = Individual(10); pass
from parser.LoadPoses import LoadPoses from datatypes.DTIndividualProperty import DTIndividualProperty, DTIndividualPropertyCMUDaz, DTIndividualPropertyVanilla, DTIndividualPropertyBaliero, DTIndividualPropertyVanillaEvolutive, DTIndividualPropertyPhysicalBioloid from datatypes.DTIndividualGeneticMaterial import DTIndividualGeneticMaterial, DTIndividualGeneticTimeSerieFile, DTIndividualGeneticMatrix from Pose import Pose from configuration.LoadSystemConfiguration import LoadSystemConfiguration from simulator.LoadRobotConfiguration import LoadRobotConfiguration from Individual import Individual import os import glob import time import sys propCMUDaz = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() physicalProp = DTIndividualPropertyPhysicalBioloid() geneticVanillaProp = DTIndividualPropertyVanillaEvolutive() conf = LoadSystemConfiguration() CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): walk = Individual(propCMUDaz, DTIndividualGeneticTimeSerieFile(filename)) fitness = walk.execute() length = walk.getLength() walk.persist("/tmp/"+str(fitness)+".xml") print "individual: ", filename, "fitness: ", fitness, "length: ", length
class GedcomTest(unittest.TestCase): def setUp(self): self.indi = Individual() self.indi.addID('@I2@') self.indi.addName('John Rivas') self.indi.addSex('M') self.indi.addBirt('9 MAY 1978') self.indi.addDeat('12 APR 2013') self.indi.addFams('@F2@') self.indi.addFamc('@F1@') self.fam1 = Family() self.fam1.addFamID('@F2@') self.fam1.addHusb('@I1@') self.fam1.addWife('@I2@') self.fam1.addChil('@I4@') self.fam1.addChil('@I5@') self.fam1.addMarr('5 OCT 1999') self.fam1.addDiv('12 JUN 2012') self.fam2 = Family() self.fam2.addFamID('@F3@') self.fam2.addHusb('@I3@') self.fam2.addWife('@I3@') def test_getID(self): ID = '@I2@' self.assertEqual(self.indi.getID(), ID) def test_getName(self): NAME = 'John Rivas' self.assertEqual(self.indi.getName(), NAME) def test_getSex(self): SEX = 'M' self.assertEqual(self.indi.getSex(), SEX) def test_getBirt(self): BIRT = '9 MAY 1978' self.assertEqual(self.indi.getBirt(), BIRT) def test_getDeat(self): DEAT = '12 APR 2013' self.assertEqual(self.indi.getDeat(), DEAT) def test_getFams(self): FAMS = '@F2@' self.assertEqual(self.indi.getFams(), FAMS) def test_getFamc(self): FAMC = '@F1@' self.assertEqual(self.indi.getFamc(), FAMC) def test_getFamID(self): FAMID = '@F2@' self.assertEqual(self.fam1.getFamID(), FAMID) def test_getHusb(self): HUSB = "@I1@" self.assertEqual(self.fam1.getHusb(), HUSB) def test_getWife(self): WIFE = "@I2@" self.assertEqual(self.fam1.getWife(), WIFE) def test_getChil(self): CHIL = ['@I4@', '@I5@'] self.assertEqual(self.fam1.getChil(), CHIL) def test_getMarr(self): MARR = '5 OCT 1999' self.assertEqual(self.fam1.getMarr(), MARR) def test_getDiv(self): DIV = '12 JUN 2012' self.assertEqual(self.fam1.getDiv(), DIV) def test_checkSameHusbWife(self): self.assertTrue(checkSameHusbWife(self.fam2))
def __init__(self, envi): Individual.__init__(self, envi) envi.set_individual(self.x, self.y, ind='victim')
walk = Individual(geneticVanillaProp, walkEmbryo) else: #TODO restructure the precycle for the case of phyisical and simulated walkEmbryo = DTIndividualGeneticTimeSerieFile(os.getcwd()+"/"+filename) precycleFile = os.getcwd()+"/mocap/cmu_mocap/xml/util/walk_precycle.xml" preCycleEmbryo = DTIndividualGeneticTimeSerieFile(precycleFile) preCycleEmbryo.concatenate(walkEmbryo) walkEmbryo = preCycleEmbryo walk = Individual(physicalProp, walkEmbryo) #TODO add support for walking cycle return walk '''walk = Individual(geneticVanillaProp, DTIndividualGeneticMatrix()) #dummy individual to initialise the simulator and enable the time step configuration walk.execute() print "please set the proper time step in vrep" time.sleep(5)''' if arguments > 1: files = sys.argv[1:] for filename in files: print 'executing individual: ' + filename walk = createIndividual(filename) walk.execute() else: for filename in glob.glob(os.path.join(geneticPoolDir, '*.xml')): print 'executing individual: ' + filename walk = Individual(geneticVanillaProp, DTIndividualGeneticTimeSerieFile(filename)) walk.execute()
def setInitialPopulation(ga_engine): propCMUDaz = DTIndividualPropertyCMUDaz() propVanilla = DTIndividualPropertyVanilla() balieroProp = DTIndividualPropertyBaliero() conf = LoadSystemConfiguration() lucyCycles = os.getcwd()+conf.getDirectory("Lucy evolved walk cycles Files") CMUxmlDir = os.getcwd()+conf.getDirectory("Transformed CMU mocap Files") GAwalkDir = os.getcwd()+conf.getDirectory("GAWalk Files") UIBLHDir = os.getcwd()+conf.getDirectory("UIBLH mocap Files") BalieroDir = os.getcwd()+conf.getDirectory("Baliero transformed walk Files") ADHOCDir = os.getcwd()+conf.getDirectory("ADHOC Files") geneticPoolDir = os.getcwd()+conf.getDirectory("Genetic Pool") population = ga_engine.getPopulation() popSize = len(population) individualCounter = 0 walk = Individual(propVanilla, DTIndividualGeneticMatrix()) #dummy individual to initialise the simulator and enable the time step configuration walk.execute() print "please set the proper time step in vrep" dtgenoma = DTGenomeFunctions() #the random initia population created is replaced by the imitation motion capture database if individualCounter < popSize: for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): if individualCounter < popSize: print individualCounter, " individuals processed!" print 'inserting individual: ' + filename + " into the initial population" walk = Individual(propCMUDaz, DTIndividualGeneticTimeSerieFile(filename)) teacherGeneticMatrix = walk.getGenomeMatrix() adan = population[individualCounter] adanIndividualLength=dtgenoma.getIndividualLength(adan) adanJointLength=dtgenoma.getIndividualFrameLength(adan) for i in xrange(adanIndividualLength): for j in xrange(adanJointLength): if i < len(teacherGeneticMatrix): #if the fixed gnoma representation size is less than the teacher size adan.setItem(i,j,teacherGeneticMatrix[i][j]) else: #put a sentinel joint value to mark the end of the individual adan.setItem(i,j,sysConstants.JOINT_SENTINEL) population[individualCounter]=adan individualCounter = individualCounter + 1 else: break #uncomment this block and comment the one above to use individuals with vainilla format '''if individualCounter < popSize: for filename in glob.glob(os.path.join(CMUxmlDir, '*.xml')): if individualCounter < popSize: print individualCounter, " individuals processed!" print 'inserting individual: ' + filename + " into the initial population" walk = Individual(propVanilla, DTIndividualGeneticTimeSerieFile(filename)) teacherGeneticMatrix = walk.getGenomeMatrix() adan = population[individualCounter] adanIndividualLength=dtgenoma.getIndividualLength(adan) adanJointLength=dtgenoma.getIndividualFrameLength(adan) for i in xrange(adanIndividualLength): for j in xrange(adanJointLength): if i < len(teacherGeneticMatrix): #if the fixed gnoma representation size is less than the teacher size adan.setItem(i,j,teacherGeneticMatrix[i][j]) else: #put a sentinel joint value to mark the end of the individual adan.setItem(i,j,sysConstants.JOINT_SENTINEL) population[individualCounter]=adan individualCounter = individualCounter + 1 else: break ''' global initialPopulationSetted initialPopulationSetted = True
def run_main(): conf = LoadSystemConfiguration() initialPopulationSize = int(conf.getProperty("Population size")) generations = int(conf.getProperty("Number of generations")) # Genome instance framesQty = sysConstants.GENOMA_MAX_LENGTH genome = G2DList.G2DList(framesQty, 18) genome.setParams(rangemin=0, rangemax=360) genome.setParams(gauss_sigma=sysConstants.MUTATION_SIGMA, gauss_mu=0) # The evaluator function (objective function) genome.evaluator.set(eval_func) if conf.getProperty("Crossover operator") == "crossovers.G2DListCrossoverSingleNearHPoint": genome.crossover.set(crossovers.G2DListCrossoverSingleNearHPoint) elif conf.getProperty("Crossover operator") == "Crossovers.G2DListCrossoverSingleHPoint": genome.crossover.set(Crossovers.G2DListCrossoverSingleHPoint) #genome.crossover.set(crossovers.G2DListCrossoverSingleNearHPointImprove) #genome.crossover.set(crossovers.G2DListCrossoverSingleHPoint) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(generations) ga.setPopulationSize(initialPopulationSize) #genome.mutator.set(Mutators.G2DListMutatorIntegerRange) if conf.getProperty("Mutator operator") == "mutators.G2DListMutatorRealGaussianSpline": genome.mutator.set(mutators.G2DListMutatorRealGaussianSpline) elif conf.getProperty("Mutator operator") == "Mutators.G2DListMutatorRealGaussianGradient": genome.mutator.set(Mutators.G2DListMutatorRealGaussianGradient) ga.setMutationRate(float(conf.getProperty("MutationRate"))) if conf.getProperty("Selection operator") == "Selectors.GRankSelector" : ga.selector.set(Selectors.GRankSelector) elif conf.getProperty("Selection operator") == "Selectors.GTournamentSelector" : ga.selector.set(Selectors.GTournamentSelector) elif conf.getProperty("Selection operator") == "Selectors.GRouletteWheel" : ga.selector.set(Selectors.GRouletteWheel) elif conf.getProperty("Selection operator") == "Selectors.GUniformSelector" : ga.selector.set(Selectors.GUniformSelector) '''For crossover probability, maybe it is the ratio of next generation population born by crossover operation. While the rest of population...maybe by previous selection or you can define it as best fit survivors''' ga.setCrossoverRate(float(conf.getProperty("CrossoverRate"))) elitism = float(conf.getProperty("Elitism replacement percentage")) > 0 ga.setElitism(True) '''Set the number of best individuals to copy to the next generation on the elitism''' if elitism: numberIndividualsForNextGen = int(initialPopulationSize*float(conf.getProperty("Elitism replacement percentage"))) ga.setElitismReplacement(numberIndividualsForNextGen) if int(conf.getProperty("Convergence criteria enable?"))==True: ga.terminationCriteria.set(ConvergenceCriteria) # Create DB Adapter and set as adapter sqlite_adapter = DBAdapters.DBSQLite(identify="Lucy walk", resetDB=True) ga.setDBAdapter(sqlite_adapter) #callback to persist best individual of each generation ga.stepCallback.set(generationCallback) #keep a reference to the genetic algorithm engine global gaEngine gaEngine = ga # Do the evolution, with stats dump # frequency of every generation ga.evolve(freq_stats=0) # Best individual best = ga.bestIndividual() score = best.getRawScore() timestr = time.strftime("%Y%m%d-%H%M%S") filename = str(score) + "-" + timestr + "-" + str(generations) + ".xml" prop = DTIndividualPropertyVanilla() bestIndividual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(best))) bestIndividual.persist(os.path.join(experimentDir,filename)) #store all the final population, not only the fitest population = ga.getPopulation() popSize = len(population) for pos in range(popSize): individual = Individual(prop, DTIndividualGeneticMatrix(chromosomeToLucyGeneticMatrix(population[pos]))) timestr = time.strftime("%Y%m%d-%H%M%S") filename = "final-" + str(pos) + "-" + timestr + ".xml" individual.persist(os.path.join(experimentDir, filename)) #ga.getDBAdapter().commit() shutil.copy2('pyevolve.db', experimentDir) shutil.copy2(conf.getProperty("System Log"), experimentDir) os.system("pyevolve_graph.py -i \"Lucy walk\" -3 -o gene_pool/experiment_img/" + experimentTime + " -e png") #do the stats print ga.getStatistics()
def __init__(self, envi): Individual.__init__(self, envi) envi.set_individual(self.x, self.y, ind='predator')
def __init__(self, chromosome=None, num_states=NUM_INITIAL_STATES, memory=DEFAULT_MEMORY): Individual.__init__(self, chromosome, num_states)
def recalculate_attributes(self): Individual.recalculate_attributes(self) self.state_instruction_length = ACTION_PROB_LEN + self.bcd_length * 2**self.memory