def run(self): # Reset the GA self.generations = 0 self.start_time = time.time() population = [] for i in range(self.pop_size): population.append( Variation.generate_random_tree( self.functions, self.terminals, self.initialization_max_tree_height)) self.fitness_function.evaluate(population[i]) while not self.__should_terminate(): offspring = [] for i in range(self.pop_size): o = deepcopy(population[i]) if random() < self.crossover_rate: o = Variation.subtree_crossover( o, population[randint(self.pop_size)]) if random() < self.mutation_rate: o = Variation.subtree_mutation( o, self.functions, self.terminals, max_height=self.initialization_max_tree_height) if len(o.get_subtree()) > self.max_tree_size: del o o = deepcopy(population[i]) else: self.fitness_function.evaluate(o) offspring.append(o) population_and_offspring = self._tune_generation( population, offspring) population = Selection.tournament_select( population_and_offspring, self.pop_size, tournament_size=self.tournament_size) # Reset the weights of all individuals if in Baldwin mode if self.baldwin: for indv in population: indv.reset_weights() self.generations = self.generations + 1 print('GA ' f'{self.generations} ' f'{np.round(self.fitness_function.elite.fitness, 3)} ' f'{len(self.fitness_function.elite.get_subtree())}')
def Run(self): self.start_time = time.time() population = [] for i in range(self.pop_size): population.append( Variation.GenerateRandomTree( self.functions, self.terminals, self.initialization_max_tree_height)) self.fitness_function.Evaluate(population[i]) while not self.__ShouldTerminate(): O = [] for i in range(len(population)): o = deepcopy(population[i]) if (random() < self.crossover_rate): o = Variation.SubtreeCrossover( o, population[randint(len(population))]) if (random() < self.mutation_rate): o = Variation.SubtreeMutation( o, self.functions, self.terminals, max_height=self.initialization_max_tree_height) if len(o.GetSubtree()) > self.max_tree_size: del o o = deepcopy(population[i]) else: self.fitness_function.Evaluate(o) O.append(o) PO = population + O population = Selection.TournamentSelect( PO, len(population), tournament_size=self.tournament_size) self.generations = self.generations + 1 print('g:', self.generations, 'elite fitness:', np.round(self.fitness_function.elite.fitness, 3), ', size:', len(self.fitness_function.elite.GetSubtree()))
def Run(self): self.start_time = time.time() population = [] for i in range( self.pop_size ): population.append( Variation.GenerateRandomTree( self.functions, self.terminals, self.initialization_max_tree_height ) ) self.fitness_function.Evaluate( population[i] ) while not self.__ShouldTerminate(): if self.generations != 0 and self.generations % self.weight_tuning_generation_rate == 0 and (self.weight_tuning_max_generations == -1 or self.generations <= self.weight_tuning_max_generations): self.realEAflag = True print("Using real EA for generation: ", self.generations) O = [] for i in range( self.pop_size ): o = deepcopy(population[i]) if random() < self.crossover_rate: o = Variation.SubtreeCrossover( o, population[ randint( self.pop_size ) ] ) if random() < self.mutation_rate: o = Variation.SubtreeMutation( o, self.functions, self.terminals, max_height=self.initialization_max_tree_height ) if len(o.GetSubtree()) > self.max_tree_size: del o o = deepcopy( population[i] ) else: # Weight tuning here if self.realEAflag and random() < self.weight_tuning_individual_rate: rea = realEA.RealEA(o, self.fitness_function,pop_size = self.real_pop_size, crossover_rate = self.real_crossover_rate, mutation_rate = self.real_mutation_rate) weights = rea.main() o.set_weights(weights) self.fitness_function.Evaluate(o) O.append(o) PO = population+O population = Selection.TournamentSelect( PO, self.pop_size, tournament_size=self.tournament_size ) self.generations = self.generations + 1 self.realEAflag = False print ('g:',self.generations,'elite fitness:', self.fitness_function.elite.fitness, ', size:', len(self.fitness_function.elite.GetSubtree()))
def Run(self, applyBackProp=True, iterationNum=0, dirName="experiments"): self.dirName = dirName self.start_time = time.time() population = [] with open( self.dirName + "/" + self.getFilename(applyBackProp, iterationNum), "w+") as fp: for i in range(self.pop_size): population.append( Variation.GenerateRandomTree( self.functions, self.terminals, self.initialization_max_tree_height)) if self.initialBackprop: population[i] = self.backprop_function.Backprop( population[i]) if applyBackProp else population[i] self.fitness_function.Evaluate(population[i]) fp.write( "generations_elite-fitness_number-of-evaluations_time\r\n") # print ('g:',self.generations,'elite fitness:', np.round(self.fitness_function.elite.fitness,3), ', size:', len(self.fitness_function.elite.GetSubtree())) fp.write( str(self.generations) + "_" + str(np.round(self.fitness_function.elite.fitness, 3)) + "_" + str(self.fitness_function.evaluations) + "_" + str(time.time() - self.start_time) + "\r\n") while not self.__ShouldTerminate(): O = [] for i in range(len(population)): variated = False o = deepcopy(population[i]) if (random() < self.crossover_rate): o = Variation.SubtreeCrossover( o, population[numpy.random.randint(len(population))]) variated = True if (random() < self.mutation_rate): o = Variation.SubtreeMutation( o, self.functions, self.terminals, max_height=self.initialization_max_tree_height) variated = True if variated and self.reset_weights_on_variation: for node in o.GetSubtree(): node.weights = np.random.normal(size=node.arity * 2) if len(o.GetSubtree()) > self.max_tree_size: del o o = deepcopy(population[i]) if self.backprop_offspring_only: doBackprop = False if applyBackProp and self.generations % self.backprop_every_generations == 0 and self.generations < self.first_generations: if self.uniform_k == 1 or random( ) <= self.uniform_k: doBackprop = True individual = self.backprop_function.Backprop( individual) if doBackprop else individual self.fitness_function.Evaluate(individual) O.append(o) PO = population + O if not self.backprop_offspring_only: if self.backprop_selection_ratio != 1: if applyBackProp and self.generations % self.backprop_every_generations == 0 and self.generations < self.first_generations: po_fitness = np.array( [PO[curr].fitness for curr in range(len(PO))]) to_select = int( self.backprop_selection_ratio * len(PO)) # Get the top k% fitnessboys # Unsorted, lowest toSelect fitness individuals, in linear time :) top_k_percent = np.argpartition(po_fitness, 3)[:to_select] for curr_top_k in top_k_percent: PO[curr_top_k] = self.backprop_function.Backprop( PO[curr_top_k], override_iterations=True) self.fitness_function.Evaluate( PO[curr_top_k] ) # Re-evaluate fitness for coming tournament else: for individual in PO: ''' Apply backprop to all if uniform_k was not passed, otherwise apply to uniform_k percent.' Apply backprop every generation if backprop_every_generations is not passed, otherwise only do it every x gens ''' doBackprop = False if applyBackProp and self.generations % self.backprop_every_generations == 0 and self.generations < self.first_generations: if self.uniform_k == 1 or random( ) <= self.uniform_k: doBackprop = True individual = self.backprop_function.Backprop( individual) if doBackprop else individual self.fitness_function.Evaluate(individual) population = Selection.TournamentSelect( PO, len(population), tournament_size=self.tournament_size) self.generations = self.generations + 1 # print ('g:',self.generations,'elite fitness:', np.round(self.fitness_function.elite.fitness,3), ', size:', len(self.fitness_function.elite.GetSubtree())) fp.write( str(self.generations) + "_" + str(np.round(self.fitness_function.elite.fitness, 3)) + "_" + str(self.fitness_function.evaluations) + "_" + str(time.time() - self.start_time) + "\r\n") return self.generations, np.round( self.fitness_function.elite.fitness, 3), self.fitness_function.evaluations, str(time.time() - self.start_time)
def Run(self): self.start_time = time.time() # Initialization ramped half-n-half self.population = [] curr_max_depth = self.min_height init_depth_interval = self.pop_size / (self.initialization_max_tree_height - 1) / 2 next_depth_interval = init_depth_interval for i in range(int(self.pop_size/2)): if i >= next_depth_interval: next_depth_interval += init_depth_interval curr_max_depth += 1 while True: g = Variation.GenerateRandomTree(self.functions, self.terminals, curr_max_depth, curr_height=0, method='grow', min_height=self.min_height ) if not self.violates_heuristics(g): break self.fitness_function.Evaluate(g, self.generations, "long") self.population.append(g) while True: f = Variation.GenerateRandomTree(self.functions, self.terminals, curr_max_depth, curr_height=0, method='full', min_height=self.min_height ) if not self.violates_heuristics(f): break self.fitness_function.Evaluate(f, self.generations, "long") self.population.append(f) # Generational loop while not self.__ShouldTerminate(): # Selection parents = Selection.TournamentSelect(self.population, self.pop_size, tournament_size=self.tournament_size) O = [] # Variation for i in range(self.pop_size): invalid_offspring = True while invalid_offspring: o = deepcopy(parents[i]) variation_happened = False while not variation_happened: if (random() < self.crossover_rate): o = Variation.SubtreeCrossover(o, self.population[randint(self.pop_size)]) variation_happened = True if (random() < self.mutation_rate): o = Variation.SubtreeMutation(o, self.functions, self.terminals, max_height=self.initialization_max_tree_height, min_height=self.min_height) variation_happened = True if (random() < self.op_mutation_rate): o = Variation.OnePointMutation(o, self.functions, self.terminals) variation_happened = True # check offspring meets constraints invalid_offspring = False if (self.max_tree_size > -1 and len(o.GetSubtree()) > self.max_tree_size): invalid_offspring = True elif (o.GetHeight() < self.min_height): invalid_offspring = True if self.violates_heuristics(o): invalid_offspring = True self.fitness_function.Evaluate(o, self.generations, "long") O.append(o) # # Sort and evaluate the top 10% for longer duration. # O = sorted(O, key=lambda x: x.fitness, reverse=False) # for ind in range(ceil(0.1*len(O))): # o = O[ind] # self.fitness_function.Evaluate(o, "long") self.population = O self.generations = self.generations + 1 fitnesses = [individual.fitness for individual in self.population] if self.logging: gen_log = {'gen': self.generations, 'elite_fitness': np.round(self.fitness_function.elite.fitness, 5), 'evaluations': self.fitness_function.evaluations, 'epochs_ran': self.fitness_function.num_epochs_trained, 'pop_fitness_mean': np.mean(fitnesses), 'pop_fitness_var': np.std(fitnesses), 'elite_on_test': self.fitness_function.elite.test_perf, 'elite': self.fitness_function.elite.GetHumanExpression(), } with GPLogger('logs/generations/{}.jsonl'.format(self.runid)) as logger: logger.write(gen_log) # elif self.max_features > -1: # features = set() # for n in o.GetSubtree(): # if hasattr(n, 'id'): # features.add(n.id) # if len(features) > self.max_features: # invalid_offspring = True # if invalid_offspring: # del o # o = deepcopy(parents[i]) # else:
def Run(self): self.start_time = time.time() # ramped half-n-half initialization w/ rejection of duplicates self.population = [] attempts_duplicate_rejection = 0 max_attempts_duplicate_rejection = self.pop_size * 10 already_generated_trees = set() half_pop_size = int(self.pop_size / 2) for j in range(2): if j == 0: method = 'full' else: method = 'grow' curr_max_depth = 2 init_depth_interval = self.pop_size / ( self.initialization_max_tree_height - 1) / 2 next_depth_interval = init_depth_interval i = 0 while len(self.population) < (j + 1) * half_pop_size: if i >= next_depth_interval: next_depth_interval += init_depth_interval curr_max_depth += 1 t = Variation.GenerateRandomTree(self.functions, self.terminals, curr_max_depth, curr_height=0, method=method) t_as_str = str(t.GetSubtree()) if t_as_str in already_generated_trees and attempts_duplicate_rejection < max_attempts_duplicate_rejection: del t attempts_duplicate_rejection += 1 continue else: already_generated_trees.add(t_as_str) t.requires_reevaluation = True self.population.append(t) i += 1 # Sample a training set self.fitness_function.SampleTrainingSet() # Evaluate for t in self.population: self.fitness_function.Evaluate(t) # Run generational loop while not self.__ShouldTerminate(): ''' It looks like the paper uses (mu,lambda)-evolution ''' # Clearing method self.Clearing() # Evolve self.population = Selection.TournamentSelect( self.population, self.pop_size, tournament_size=self.tournament_size) O = [] for i in range(self.pop_size): o = deepcopy(self.population[i]) r = np.random.random() if (r < self.crossover_rate + self.mutation_rate): if (r < self.crossover_rate): o = Variation.SubtreeCrossover( o, self.population[randint(self.pop_size)]) else: o = Variation.SubtreeMutation( o, self.functions, self.terminals, max_height=self.initialization_max_tree_height) # check constraints if (self.max_tree_size > -1 and len(o.GetSubtree()) > self.max_tree_size): o = deepcopy(self.population[i]) O.append(o) # The offspring population replaces the parent population self.population = O # Sample new training set self.fitness_function.SampleTrainingSet() # Evaluate for t in self.population: self.fitness_function.Evaluate(t) self.generations = self.generations + 1 best_err = sorted(self.population, key=lambda x: x.fitness)[0].fitness if self.verbose: print('g:', self.generations, 'current best error:', np.round(best_err, 3)) # Create final ensemble self.ensemble = self.GreedyEnsemble()
def Run(self): self.start_time = time.time() # ramped half-n-half initialization population = [] curr_max_depth = 2 init_depth_interval = self.pop_size / ( self.initialization_max_tree_height - 1) / 2 next_depth_interval = init_depth_interval for i in range(int(self.pop_size / 2)): if i >= next_depth_interval: next_depth_interval += init_depth_interval curr_max_depth += 1 g = Variation.GenerateRandomTree(self.functions, self.terminals, curr_max_depth, curr_height=0, method='grow') self.fitness_function.Evaluate(g) population.append(g) f = Variation.GenerateRandomTree(self.functions, self.terminals, curr_max_depth, curr_height=0, method='full') self.fitness_function.Evaluate(f) population.append(f) while not self.__ShouldTerminate(): O = [] for i in range(self.pop_size): o = deepcopy(population[i]) if (random() < self.crossover_rate): o = Variation.SubtreeCrossover( o, population[randint(self.pop_size)]) if (random() < self.mutation_rate): o = Variation.SubtreeMutation( o, self.functions, self.terminals, max_height=self.initialization_max_tree_height) invalid_offspring = False if (self.max_tree_size > -1 and len(o.GetSubtree()) > self.max_tree_size): invalid_offspring = True elif self.max_features > -1: features = set() for n in o.GetSubtree(): if hasattr(n, 'id'): features.add(n.id) if len(features) > self.max_features: invalid_offspring = True if invalid_offspring: del o o = deepcopy(population[i]) else: self.fitness_function.Evaluate(o) O.append(o) PO = population + O population = Selection.TournamentSelect( PO, self.pop_size, tournament_size=self.tournament_size) self.generations = self.generations + 1 if self.verbose: print('g:', self.generations, 'elite fitness:', np.round(self.fitness_function.elite.fitness, 3), ', size:', len(self.fitness_function.elite.GetSubtree()))