def NAS_EA_FA(): cur_time_budget = 0 history2acc = {} best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0] x_train, y_train = [], [] total_population = [Individual() for _ in range(POPULATION_SIZE)] while cur_time_budget <= MAX_TIME_BUDGET: population = sorted(total_population, key=lambda x: x.fitness, reverse=True) new_population = [] num = 0 for individual in population: valid_acc, test_acc, time = get_model_data_from_history( individual, history2acc) individual.fitness = valid_acc if time == 0.0: continue new_population.append(individual) x_train.append(get_model_sequences(individual)) y_train.append(valid_acc) if valid_acc > best_valid_acc[-1]: best_valid_acc.append(valid_acc) best_test_acc.append(test_acc) else: best_valid_acc.append(best_valid_acc[-1]) best_test_acc.append(best_test_acc[-1]) times.append(time) cur_time_budget += time num += 1 if cur_time_budget > MAX_TIME_BUDGET or num >= 50: break if len(new_population) != 0: population = new_population gbm = XGBRegressor(eta=0.1) gbm.fit(np.array(x_train), np.array(y_train)) total_population = [] for epoch in range(10): new_population = [] for i in range(POPULATION_SIZE): individual = cp.deepcopy(tournament_selection(population)) bitwise_mutation(individual) individual.fitness = gbm.predict( np.array([get_model_sequences(individual)]))[0] new_population.append(individual) total_population.append(individual) population = new_population return best_valid_acc, best_test_acc, times
def regularized_evolution_algorithm(dataset: str): cur_time_budget = 0 history2acc = {} best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0] population = [Individual() for _ in range(POPULATION_SIZE)] for individual in population: valid_acc, test_acc, time = get_individual_data_from_history( individual, history2acc, dataset) individual.fitness = valid_acc if valid_acc > best_valid_acc[-1]: best_valid_acc.append(valid_acc) best_test_acc.append(test_acc) else: best_valid_acc.append(best_valid_acc[-1]) best_test_acc.append(best_test_acc[-1]) times.append(time) cur_time_budget += time while cur_time_budget <= MAX_TIME_BUDGET: individual = cp.deepcopy(tournament_selection(population)) bitwise_mutation(individual) valid_acc, test_acc, time = get_individual_data_from_history( individual, history2acc, dataset) individual.fitness = valid_acc population.append(individual) population.pop(0) if time == 0.0: continue if valid_acc > best_valid_acc[-1]: best_valid_acc.append(valid_acc) best_test_acc.append(test_acc) else: best_valid_acc.append(best_valid_acc[-1]) best_test_acc.append(best_test_acc[-1]) times.append(time) cur_time_budget += time if cur_time_budget > MAX_TIME_BUDGET: break return best_valid_acc, best_test_acc, times
def main(): population = [] generation = 1 for i in range(INITIAL_POPULATION): population.append(generate_individual(len(MASTER_SOLUTION))) while generation < MAX_ITERATIONS: print("Generation: {}, Population: {}".format(generation, len(population))) new_population = tournament_selection(population) population = [] for x, y in new_population: population.append(crossover(x, y)) population.append(crossover(y, x)) population = mutate_population(population) generation += 1
def NAS_EA_FA_V2(): cur_time_budget = 0 history2acc = {} best_valid_acc, best_test_acc, times = [0.0], [0.0], [0.0] x_train, y_train = [], [] total_population = [Individual() for _ in range(POPULATION_SIZE)] while cur_time_budget <= MAX_TIME_BUDGET: population = sorted(total_population, key=lambda x: x.fitness, reverse=True) new_population = [] num = start_index = 0 # top n individuals for index in range(len(population)): individual = population[index] valid_acc, test_acc, time = get_model_data_from_history( individual, history2acc) individual.fitness = valid_acc if time == 0.0: continue new_population.append(individual) temp_sequences = get_all_isomorphic_sequences(individual) x_train.extend(temp_sequences) for _ in range(len(temp_sequences)): y_train.append(valid_acc) if valid_acc > best_valid_acc[-1]: best_valid_acc.append(valid_acc) best_test_acc.append(test_acc) else: best_valid_acc.append(best_valid_acc[-1]) best_test_acc.append(best_test_acc[-1]) times.append(time) cur_time_budget += time num += 1 if cur_time_budget > MAX_TIME_BUDGET or num >= 30: start_index = index break # diversity individual max_dis_list = [ get_min_distance(x_train, get_model_sequences(indiv)) for indiv in population[start_index + 1:] ] while num < 50 and cur_time_budget <= MAX_TIME_BUDGET: max_dis, temp_index = 0, start_index for index in range(len(max_dis_list)): if max_dis_list[index] > max_dis: max_dis = max_dis_list[index] temp_index = index individual = population[temp_index + start_index + 1] max_dis_list[temp_index] = 0 valid_acc, test_acc, time = get_model_data_from_history( individual, history2acc) individual.fitness = valid_acc if time == 0.0: continue new_population.append(individual) temp_sequences = get_all_isomorphic_sequences(individual) x_train.extend(temp_sequences) for _ in range(len(temp_sequences)): y_train.append(valid_acc) if valid_acc > best_valid_acc[-1]: best_valid_acc.append(valid_acc) best_test_acc.append(test_acc) else: best_valid_acc.append(best_valid_acc[-1]) best_test_acc.append(best_test_acc[-1]) times.append(time) cur_time_budget += time num += 1 if len(new_population) != 0: population = new_population gbm = XGBRegressor(eta=0.1) gbm.fit(np.array(x_train), np.array(y_train)) total_population = [] for epoch in range(10): new_population = [] for i in range(POPULATION_SIZE): individual = cp.deepcopy(tournament_selection(population)) bitwise_mutation(individual) individual.fitness = gbm.predict( np.array([get_model_sequences(individual)]))[0] new_population.append(individual) total_population.append(individual) population = new_population return best_valid_acc, best_test_acc, times
sets.build_terminal_set(accept, reject) sets.build_function_set() population = gen_population(1000, accept, reject) bestest = gen_individual(accept, reject) for i in range(100): best = ['fjkghdfkghdfkghsdkfhgksdfjhgksjdfhgkjsdhfgkjhsdfkghsdfkjghksdfhgkj', -9999] print("\nGeneration:", i) new_pop = [] for _ in range(800): while True: try: m1 = utils.tournament_selection(population, 50) m2 = utils.tournament_selection(population, 50) child = utils.crossover(m1[0], m2[0]) child = [child, utils.calc_score(child, accept, reject)] new_pop.append(child) break except: pass for _ in range(100): new_pop.append(gen_individual(accept, reject)) for _ in range(100): while True: try: a = random.choice(population)