def converging(ip, population, n_population, n_gene, search_idx, search_region): n_children = 10 children = np.empty((n_children, n_gene + 1)) for i in range(n_children): ip[2:] = np.random.choice(n_population, n_gene, replace=False) children[i, :] = xover(population[ip, :], n_gene) family = np.empty((n_children + 2, n_gene + 1)) family[:n_children, :] = children family[-2, :] = population[ip[0], :] family[-1, :] = population[ip[1], :] family = family[np.argsort(family[:, -1]), :] # Best, either of parents population[ip[0], :] = family[0, :] # Random population[ip[1], :] = \ family[np.random.randint(low=1, high=n_children+2, dtype=np.int), :] if np.isinf(population[ip[1], -1]): population[ip[1], -1] = objective(population[ip[1], :n_gene], search_idx, search_region) population = population[np.argsort(population[:, -1]), :] return ip, population
def get_new_child(parents, n_gene, search_idx, search_region): MAXITER = 100 for _ in range(MAXITER): child = undx(parents, n_gene) if 0. <= np.min(child[:n_gene]) and np.max(child[:n_gene]) <= 1.: break else: child[:n_gene] = np.clip(child[:n_gene], 0., 1.) child[-1] = objective(child[:n_gene], search_idx, search_region) return child
def get_initial_population(nth_paramset, n_population, n_gene, search_idx, search_region): population = np.full((n_population, n_gene + 1), np.inf) with open('./out/%d/initpop.log' % (nth_paramset), mode='w') as f: f.write('Generating the initial population. . .\n') for i in range(n_population): while np.isinf(population[i, -1]) or np.isnan(population[i, -1]): population[i, :n_gene] = np.random.rand(n_gene) population[i, -1] = objective(population[i, :n_gene], search_idx, search_region) with open('./out/%d/initpop.log' % (nth_paramset), mode='a') as f: f.write('%d/%d\n' % (i + 1, n_population)) population = population[np.argsort(population[:, -1]), :] return population
def get_initial_population_continue(nth_paramset, n_population, n_gene, search_idx, search_region, p0_bounds): best_generation = np.load('./out/%d/generation.npy' % (nth_paramset)) best_indiv = np.load('./out/%d/fit_param%d.npy' % (nth_paramset, int(best_generation))) population = np.full((n_population, n_gene + 1), np.inf) with open('./out/%d/initpop.log' % (nth_paramset), mode='w') as f: f.write('Generating the initial population. . .\n') for i in range(n_population): while np.isinf(population[i, -1]) or np.isnan(population[i, -1]): population[i, :n_gene] = encode_bestindiv2randgene( best_indiv, search_region, p0_bounds) population[i, :n_gene] = np.clip(population[i, :n_gene], 0., 1.) population[i, -1] = objective(population[i, :n_gene], search_idx, search_region) with open('./out/%d/initpop.log' % (nth_paramset), mode='a') as f: f.write('%d/%d\n' % (i + 1, n_population)) population = population[np.argsort(population[:, -1]), :] return population
def ga_v1_continue(nth_paramset, max_generation, n_population, n_children, n_gene, allowable_error, search_idx, search_region, p0_bounds): count_num = np.load('./out/%d/count_num.npy' % (nth_paramset)) best_generation = np.load('./out/%d/generation.npy' % (nth_paramset)) best_indiv = np.load('./out/%d/fit_param%d.npy' % (nth_paramset, int(best_generation))) best_fitness = objective((np.log10(best_indiv) - search_region[0, :]) / (search_region[1, :] - search_region[0, :]), search_idx, search_region) population = get_initial_population_continue(nth_paramset, n_population, n_gene, search_idx, search_region, p0_bounds) if best_fitness < population[0, -1]: population[0, :n_gene] = ( (np.log10(best_indiv) - search_region[0, :]) / (search_region[1, :] - search_region[0, :])) population[0, -1] = best_fitness else: best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] np.save( './out/%d/fit_param%d.npy' % (nth_paramset, int(count_num) + 1), best_indiv) with open('./out/%d/out.log' % (nth_paramset), mode='a') as f: f.write('\n----------------------------------------\n\n' + 'Generation%d: Best Fitness = %e\n' % (int(count_num) + 1, best_fitness)) if population[0, -1] <= allowable_error: best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] return best_indiv, best_fitness generation = 1 while generation < max_generation: population = mgg_alternation(population, n_population, n_children, n_gene, search_idx, search_region) best_indiv = decode_gene2variable(population[0, :n_gene], search_region) if population[0, -1] < best_fitness: np.save( './out/%d/fit_param%d.npy' % (nth_paramset, generation + int(count_num) + 1), best_indiv) np.save('./out/%d/generation.npy' % (nth_paramset), generation + int(count_num) + 1) np.save('./out/%d/best_fitness' % (nth_paramset), best_fitness) best_fitness = population[0, -1] np.save('./out/%d/count_num.npy' % (nth_paramset), generation + int(count_num) + 1) with open('./out/%d/out.log' % (nth_paramset), mode='a') as f: f.write('Generation%d: Best Fitness = %e\n' % (generation + int(count_num) + 1, best_fitness)) if population[0, -1] <= allowable_error: best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] return best_indiv, best_fitness generation += 1 best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] return best_indiv, best_fitness
def ga_v2_continue(nth_paramset, max_generation, n_population, n_children, n_gene, allowable_error, search_idx, search_region, p0_bounds): if n_population < n_gene + 2: raise ValueError('n_population must be larger than %d' % (n_gene + 2)) n_iter = 1 n0 = np.empty(2 * n_population) count_num = np.load('./out/%d/count_num.npy' % (nth_paramset)) best_generation = np.load('./out/%d/generation.npy' % (nth_paramset)) best_indiv = np.load('./out/%d/fit_param%d.npy' % (nth_paramset, int(best_generation))) best_fitness = objective((np.log10(best_indiv) - search_region[0, :]) / (search_region[1, :] - search_region[0, :]), search_idx, search_region) population = get_initial_population_continue(nth_paramset, n_population, n_gene, search_idx, search_region, p0_bounds) if best_fitness < population[0, -1]: population[0, :n_gene] = ( (np.log10(best_indiv) - search_region[0, :]) / (search_region[1, :] - search_region[0, :])) population[0, -1] = best_fitness else: best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] np.save( './out/%d/fit_param%d.npy' % (nth_paramset, int(count_num) + 1), best_indiv) with open('./out/%d/out.log' % (nth_paramset), mode='a') as f: f.write('\n----------------------------------------\n\n' + 'Generation%d: Best Fitness = %e\n' % (int(count_num) + 1, best_fitness)) n0[0] = population[0, -1] if population[0, -1] <= allowable_error: best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] return best_indiv, best_fitness generation = 1 while generation < max_generation: ip = np.random.choice(n_population, n_gene + 2, replace=False) ip, population = converging(ip, population, n_population, n_gene, search_idx, search_region) ip, population = local_search(ip, population, n_population, n_children, n_gene, search_idx, search_region) for _ in range(n_iter - 1): ip = np.random.choice(n_population, n_gene + 2, replace=False) ip, population = converging(ip, population, n_population, n_gene, search_idx, search_region) if generation % len(n0) == 0: n0 = np.empty_like(n0) n0[0] = population[0, -1] elif generation % len(n0) == len(n0) - 1: n0[-1] = population[0, -1] if n0[0] == n0[-1]: n_iter *= 2 else: n_iter = 1 else: n0[generation % len(n0)] = population[0, -1] best_indiv = decode_gene2variable(population[0, :n_gene], search_region) if population[0, -1] < best_fitness: np.save('./out/%d/generation.npy' % (nth_paramset), generation + int(count_num) + 1) np.save( './out/%d/fit_param%d.npy' % (nth_paramset, generation + int(count_num) + 1), best_indiv) np.save('./out/%d/best_fitness' % (nth_paramset), best_fitness) best_fitness = population[0, -1] np.save('./out/%d/count_num.npy' % (nth_paramset), generation + int(count_num) + 1) with open('./out/%d/out.log' % (nth_paramset), mode='a') as f: f.write('Generation%d: Best Fitness = %e\n' % (generation + int(count_num) + 1, best_fitness)) if population[0, -1] <= allowable_error: best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] return best_indiv, best_fitness generation += 1 best_indiv = decode_gene2variable(population[0, :n_gene], search_region) best_fitness = population[0, -1] return best_indiv, best_fitness