def cross_prob(ind_a, ind_b, mate_prob): new_ind_a = [] new_ind_b = [] for k in range(0,len(ind_a)): if rand() < mate_prob: new_ind_a.append(ind_b[k]) else: new_ind_a.append(ind_a[k]) if rand() < mate_prob: new_ind_b.append(ind_a[k]) else: new_ind_b.append(ind_b[k]) return (creator.individual(new_ind_a),creator.individual(new_ind_b))
def generate_individual(creator, number_endmembers, number_pixels, number_rows, number_columns): individual = numpy.random.choice(list(range(0, number_pixels)), number_endmembers) return creator.individual(individual)
def gaussian_mutation(individual, toolbox, number_rows, number_columns): gene_x = (np.array(individual) / number_rows).astype(int) gene_y = (np.array(individual) % number_rows) mut_x = abs(toolbox.gaussian_mutation_op(gene_x.copy())[0] % number_columns-1) mut_y = abs(toolbox.gaussian_mutation_op(gene_y.copy())[0] % number_rows-1) mutant = mut_x*number_rows+mut_y return creator.individual(mutant)
def cross_hadamard(ind_a, ind_b, mate_prob, virtual_prob): new_ind_a = [] new_ind_b = [] for k in range(0,len(ind_a)): if rand() < mate_prob: if rand() < virtual_prob: new_ind_a.append((ind_a[k][0],ind_b[k][1])) else: new_ind_a.append(ind_b[k]) else: new_ind_a.append(ind_a[k]) if rand() < mate_prob: if rand() < virtual_prob: new_ind_b.append((ind_b[k][0],ind_a[k][1])) else: new_ind_b.append(ind_a[k]) else: new_ind_b.append(ind_b[k]) return (creator.individual(new_ind_a),creator.individual(new_ind_b))
def GAEEII_IVFm(data, dimensions, number_endmembers): start_time = time.time() number_rows = int(dimensions[0]) number_columns = int(dimensions[1]) number_bands = int(dimensions[2]) number_pixels = number_rows * number_columns sigma_MAX = max(number_rows, number_generations) data_proj = numpy.asarray(affine_projection(data, number_endmembers)) creator.create("max_fitness", base.Fitness, weights=(-1.0, -1.0)) creator.create("individual", list, fitness=creator.max_fitness) toolbox = base.Toolbox() toolbox.register("create_individual", generate_individual, creator, number_endmembers, number_pixels, number_rows, number_columns) toolbox.register("initialize_population", tools.initRepeat, list, toolbox.create_individual) toolbox.register("evaluate_individual", multi_fitness, data=data, data_proj=data_proj, number_endmembers=number_endmembers) toolbox.register("cross_twopoints", tools.cxTwoPoint) toolbox.register("selNSGA2", tools.selNSGA2) toolbox.register("gaussian_mutation_op", tools.mutGaussian, mu=0, sigma=0, indpb=mutation_probability) toolbox.register("gaussian_mutation", gaussian_mutation, toolbox=toolbox, number_rows=number_rows, number_columns=number_columns) toolbox.register("random_mutation", random_mutation, number_pixels=number_pixels, number_endmembers=number_endmembers, mutation_probability=mutation_probability) population = toolbox.initialize_population(n=population_size) population_fitnesses = [ toolbox.evaluate_individual(individual) for individual in population ] for individual, fitness in zip(population, population_fitnesses): individual.fitness.values = fitness hof = tools.HallOfFame(3) hof.update(population) current_generation = 0 current_sigma = sigma_MAX generations_fitness_1 = [] generations_fitness_2 = [] generations_population = [] stop_criteria = deque(maxlen=5) stop_criteria.extend([1, 2, 3, 4, 5]) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", numpy.mean, axis=0) stats.register("std", numpy.std, axis=0) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = "gen", "evals", "std", "min", "avg", "max" record = stats.compile(population) logbook.record(gen=0, evals=len(population), **record) while current_generation < number_generations and numpy.var( numpy.array(stop_criteria)) > 0.000001: toolbox.unregister("gaussian_mutation_op") toolbox.register("gaussian_mutation_op", tools.mutGaussian, mu=0, sigma=current_sigma, indpb=mutation_probability) offspring = tools.selRandom(population, k=int(population_size / 2)) offspring = list(map(toolbox.clone, offspring)) # Crossing for child_1, child_2 in zip(offspring[::2], offspring[1::2]): if random.random() < crossing_probability: toolbox.cross_twopoints(child_1, child_2) del child_1.fitness.values del child_2.fitness.values # Mutation for mutant in offspring: if random.random() < mutation_probability: toolbox.gaussian_mutation(mutant) del mutant.fitness.values # Fitness offspring_fitnesses = [ toolbox.evaluate_individual(individual) for individual in offspring ] for individual, fitness in zip(offspring, offspring_fitnesses): individual.fitness.values = fitness # In Vitro Fertilization Module ivfoffspring = list(map(toolbox.clone, population)) ivffits = [ind.fitness.values[0] for ind in ivfoffspring] fatheridx = numpy.argmax(ivffits) # fatherfit = numpy.max(ivffits) father = creator.individual(ivfoffspring[fatheridx].copy()) for ind in ivfoffspring[::2]: toolbox.random_mutation(ind) del ind.fitness.values for child1 in ivfoffspring: child2 = creator.individual(father.copy()) toolbox.cross_twopoints(child1, child2) del child1.fitness.values del child2.fitness.values ivffitnesses = [ toolbox.evaluate_individual(ind) for ind in ivfoffspring ] for ind, fit in zip(ivfoffspring, ivffitnesses): ind.fitness.values = fit popmax = max(offspring_fitnesses) for ind in ivfoffspring: if (ind.fitness.values >= popmax): population.append(ind) new_population = population + offspring # Selection selected = toolbox.selNSGA2(new_population, population_size) selected = list(map(toolbox.clone, selected)) population[:] = selected hof.update(population) record = stats.compile(population) logbook.record(gen=current_generation, evals=len(population), **record) # print(logbook.stream) # Statistics fits_1 = [ind.fitness.values[0] for ind in population] fits_2 = [ind.fitness.values[1] for ind in population] mean_1_offspring = sum(fits_1) / len(population) mean_2_offspring = sum(fits_2) / len(population) generations_fitness_1.append(numpy.log10(mean_1_offspring)) generations_fitness_2.append(numpy.log(mean_2_offspring)) stop_criteria.append(numpy.log10(mean_1_offspring)) generations_population.append(population.copy()) current_generation += 1 current_sigma = sigma_MAX / ((current_generation + 1) / 1.5) best_individual = tools.selNSGA2(population, 1)[0] # print('Result:', multi_fitness(best_individual,data, data_proj, number_endmembers)) M = data[:, best_individual] duration = time.time() - start_time return M, duration, [ generations_fitness_1, generations_fitness_2, generations_population, current_generation ]
def GAEEII(data, dimensions, number_endmembers): start_time = time.time() population_size = 10 number_generations = 100 crossing_probability = 1 mutation_probability = 0.5 stop_criteria_MAX = 20 random.seed(64) number_endmembers = number_endmembers number_rows = int(dimensions[0]) number_columns = int(dimensions[1]) number_bands = int(dimensions[2]) number_pixels = number_rows * number_columns sigma_MAX = max(number_rows, number_generations) data_proj = numpy.asarray(affine_tranform(data, number_endmembers)) # data = numpy.asarray(data) # _coeff, score, _latent = princomp(data.T) # data_proj = numpy.squeeze(score[0:number_endmembers,:]) creator.create("min_fitness", base.Fitness, weights=(1.0, )) creator.create("individual", list, fitness=creator.min_fitness) toolbox = base.Toolbox() toolbox.register("create_individual", generate_individual, creator, number_endmembers, number_pixels, number_rows, number_columns) toolbox.register("initialize_population", tools.initRepeat, list, toolbox.create_individual) toolbox.register("evaluate_individual", multi_fitness, data=data, data_proj=data_proj, number_endmembers=number_endmembers) toolbox.register("cross_twopoints", tools.cxTwoPoint) toolbox.register("selNSGA2", tools.selNSGA2) toolbox.register("gaussian_mutation_op", tools.mutGaussian, mu=0, sigma=0, indpb=mutation_probability) toolbox.register("gaussian_mutation", gaussian_mutation, toolbox=toolbox, number_rows=number_rows, number_columns=number_columns) population = toolbox.initialize_population(n=population_size) ensemble_pop = [] for i in range(0, 5): [_, duration, other] = classical.VCA(data, dimensions, number_endmembers) # print(other[0]) # print('Time GAEE:',duration) ensemble_pop.append(creator.individual(other[0])) population[5:] = ensemble_pop population_fitnesses = [ toolbox.evaluate_individual(individual) for individual in population ] for individual, fitness in zip(population, population_fitnesses): individual.fitness.values = fitness hof = tools.HallOfFame(3) hof.update(population) current_generation = 0 current_sigma = sigma_MAX generations_fitness_1 = [] generations_fitness_2 = [] generations_population = [] stop_criteria = deque(maxlen=stop_criteria_MAX) stop_criteria.extend(list(range(1, stop_criteria_MAX))) while current_generation < number_generations and numpy.var( numpy.array(stop_criteria)) > 0.000001: toolbox.unregister("gaussian_mutation_op") toolbox.register("gaussian_mutation_op", tools.mutGaussian, mu=0, sigma=current_sigma, indpb=mutation_probability) offspring = tools.selRandom(population, k=int(population_size / 2)) offspring = list(map(toolbox.clone, offspring)) # Crossing for child_1, child_2 in zip(offspring[::2], offspring[1::2]): if random.random() < crossing_probability: toolbox.cross_twopoints(child_1, child_2) del child_1.fitness.values del child_2.fitness.values # Mutation for mutant in offspring: if random.random() < mutation_probability: toolbox.gaussian_mutation(mutant) del mutant.fitness.values # Fitness offspring_fitnesses = [ toolbox.evaluate_individual(individual) for individual in offspring ] for individual, fitness in zip(offspring, offspring_fitnesses): individual.fitness.values = fitness new_population = population + offspring # Selection selected = toolbox.selNSGA2(new_population, population_size) selected = list(map(toolbox.clone, selected)) population[:] = selected hof.update(population) # Statistics fits_1 = [ind.fitness.values[0] for ind in population] # fits_2 = [ind.fitness.values[1] for ind in population] mean_1_offspring = sum(fits_1) / len(population) # mean_2_offspring = sum(fits_2) / len(population) generations_fitness_1.append(numpy.log10(mean_1_offspring)) # generations_fitness_2.append(numpy.log(mean_2_offspring)) generations_population.append(population.copy()) stop_criteria.append(numpy.log10(mean_1_offspring)) current_generation += 1 current_sigma = sigma_MAX / ((current_generation + 1) / 4) best_individual = tools.selBest(population, 1)[0] # best_individual = hof[0] # print('Result:', multi_fitness(best_individual,data, data_proj,number_endmembers)) M = data[:, best_individual] duration = time.time() - start_time return M, duration, [ generations_fitness_1, generations_fitness_2, generations_population, current_generation ]
def GAEE_IVFm(data, dimensions, number_endmembers): start_time = time.time() number_rows = int(dimensions[0]) number_columns = int(dimensions[1]) number_bands = int(dimensions[2]) number_pixels = number_rows * number_columns tournsize = 3 data = numpy.asarray(data) _coeff, score, _latent = princomp(data.T) data_pca = numpy.squeeze(score[0:number_endmembers - 1, :]) creator.create("max_fitness", base.Fitness, weights=(1.0, )) creator.create("individual", list, fitness=creator.max_fitness) toolbox = base.Toolbox() toolbox.register("create_individual", generate_individual, creator, number_endmembers, number_pixels, number_rows, number_columns) toolbox.register("initialize_population", tools.initRepeat, list, toolbox.create_individual) toolbox.register("evaluate_simplex_volume", mono_fitness, data_pca=data_pca, number_endmembers=number_endmembers) toolbox.register("cross_twopoints", tools.cxTwoPoint) toolbox.register("tournament_select", tools.selTournament, tournsize=tournsize) toolbox.register("random_mutation", random_mutation, number_pixels=number_pixels, number_endmembers=number_endmembers, mutation_probability=mutation_probability) population = toolbox.initialize_population(n=population_size) population_fitnesses = [ toolbox.evaluate_simplex_volume(individual) for individual in population ] for individual, fitness in zip(population, population_fitnesses): individual.fitness.values = fitness current_generation = 0 generations_fitness = [] generations_population = [] stop_criteria = deque(maxlen=5) stop_criteria.extend([1, 2, 3, 4, 5]) while current_generation < number_generations and numpy.var( numpy.array(stop_criteria)) > 0.000001: offspring = list(map(toolbox.clone, population)) # Crossing for child_1, child_2 in zip(offspring[::2], offspring[1::2]): if random.random() < crossing_probability: toolbox.cross_twopoints(child_1, child_2) del child_1.fitness.values del child_2.fitness.values # Mutation for mutant in offspring: if random.random() < mutation_probability: toolbox.random_mutation(mutant) del mutant.fitness.values # Fitness offspring_fitnesses = [ toolbox.evaluate_simplex_volume(individual) for individual in offspring ] for individual, fitness in zip(offspring, offspring_fitnesses): individual.fitness.values = fitness # In Vitro Fertilization Module ivfoffspring = list(map(toolbox.clone, population)) ivffits = [ind.fitness.values[0] for ind in ivfoffspring] fatheridx = numpy.argmax(ivffits) fatherfit = numpy.max(ivffits) father = creator.individual(ivfoffspring[fatheridx].copy()) for ind in ivfoffspring[::2]: toolbox.random_mutation(ind) del ind.fitness.values for child1 in ivfoffspring: child2 = creator.individual(father.copy()) toolbox.cross_twopoints(child1, child2) del child1.fitness.values del child2.fitness.values ivffitnesses = [ toolbox.evaluate_simplex_volume(ind) for ind in ivfoffspring ] for ind, fit in zip(ivfoffspring, ivffitnesses): ind.fitness.values = fit popmax = max(offspring_fitnesses) for ind in ivfoffspring: if (ind.fitness.values >= popmax): population.append(ind) new_population = population + offspring # Selection selected = toolbox.tournament_select(new_population, population_size) population_selected = list(map(toolbox.clone, selected)) population[:] = population_selected # Statistics fits = [ind.fitness.values[0] for ind in population] mean_offspring = sum(fits) / len(population) generations_fitness.append(numpy.log10(mean_offspring)) generations_population.append(population.copy()) stop_criteria.append(numpy.log10(mean_offspring)) current_generation += 1 best_individual = tools.selBest(population, 1)[0] # print(best_individual) M = data[:, best_individual] duration = time.time() - start_time return M, duration, [ generations_fitness, generations_population, current_generation ]