def crossover_weights(ind1, ind2, force): #ind1.check() #ind2.check() child1, child2 = tools.cxUniform(ind1.weights, ind2.weights, indpb=0.5) ind1.weights[:] = fix_weights(child1, force) ind2.weights[:] = fix_weights(child2, force) #ind1.check() #ind2.check() return ind1, ind2
def crossover_weights(ind1, ind2, force): #ind1.check() #ind2.check() child1, child2 = tools.cxUniform(ind1.weights.flatten(), ind2.weights.flatten(), indpb=0.5) ind1.weights[:, :] = child1.reshape((ind1.NC, ind1.ND)) ind2.weights[:, :] = child2.reshape((ind2.NC, ind2.ND)) NC, ND = ind1.weights.shape for i in range(NC): ind1.weights[i, :] = fix_weights(ind1.weights[i, :], force) ind2.weights[i, :] = fix_weights(ind2.weights[i, :], force) #ind1.check() #ind2.check() return ind1, ind2
def mutate_weights(ind, force): new_solution = ind.weights.copy() ND = len(new_solution) #select at random a dimension to modify sel_dim = np.random.randint(ND) new_solution[sel_dim] += np.random.normal(loc=0.0, scale=0.1) new_solution[sel_dim] = np.clip(new_solution[sel_dim], 0.0000001, 1.0) new_solution[:] = fix_weights(new_solution[:], force) ind.weights[:] = new_solution return ind,
def mutate_weights(ind, force): new_solution = ind.weights.copy() NC, ND = new_solution.shape #select at random a cluster to modify sel_cluster = np.random.randint(NC) #select at random a dimension to modify sel_dim = np.random.randint(ND) new_solution[sel_cluster, sel_dim] += np.random.normal(loc=0.0, scale=0.1) new_solution[sel_cluster, sel_dim] = np.clip(new_solution[sel_cluster, sel_dim], 0.0000001, 1.0) new_solution[sel_cluster, :] = fix_weights(new_solution[sel_cluster, :], force) ind.weights[:, :] = new_solution return ind,
cl.distances.set_categorical(1, 3, distances_cat) #cl.distances.set_targeted(23,targets,False) #force = (22,0.15) force = None #initial centroids at random indices = np.random.choice(N, size=NC, replace=False) current_centroids = np.asfortranarray(np.empty((NC, ND))) current_centroids[:, :] = data[indices, :] #initial weights are uniform weights = np.asfortranarray(np.ones((NC, ND), dtype=np.float32) / ND) for c in range(NC): weights[c, :] = fix_weights(weights[c, :], force=force) for k in range(100): best_centroids, best_u, best_energy_centroids, best_jm, current_temperature, evals = clustering_ga.optimize_centroids( data, current_centroids, weights, m, lambda_value, var_types, {}, ngen=ngen, npop=npop, cxpb=cxpb, mutpb=mutpb, stop_after=stop_after, min_values=min_values,
def evolve_weights(toolbox, centroids, initial_weights, values, ngen, npop, stop_after, cxpb, mutpb, force, verbose=False): NC, ND = centroids.shape N = len(values) pop = [WeightIndividual(NC, ND, None) for _ in range(npop)] for i, ind in enumerate(pop): if i == 0: ind.weights[:, :] = initial_weights else: #random for k in range(NC): ind.weights[k, :] = np.random.random(ND) ind.weights[k, :] /= np.sum(ind.weights[k, :]) ind.weights[k, :] = fix_weights(ind.weights[k, :], force=force) ind.check() hof = tools.HallOfFame(1, similar=similar_op_weights) stats = tools.Statistics(key=lambda ind: ind.fitness.values) stats.register("avg", np.mean) stats.register("min", np.min) stats.register("max", np.max) logbook = tools.Logbook() logbook.header = "gen", "min", "avg", "max", "best" #logger.info("Evaluating initial population") # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit hof.update(pop) record = stats.compile(pop) if stats else {} logbook.record(gen=0, best=hof[0].fitness.values[0], **record) if verbose: print(logbook.stream) evaluations = 0 no_improvements = 0 #logger.info("Starting evolution!") for gen in range(1, ngen + 1): prev_fitness = hof[0].fitness.values[0] # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Vary the pool of individuals offspring = algorithms.varAnd(offspring, toolbox, cxpb, mutpb) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit evals_gen = len(invalid_ind) # Update the hall of fame with the generated individuals if hof is not None: hof.update(offspring) # Replace the current population by the offspring pop[:] = offspring # Append the current generation statistics to the logbook record = stats.compile(pop) if stats else {} logbook.record(gen=gen, best=hof[0].fitness.values[0], **record) #logger.info(logbook.stream) if verbose: print(logbook.stream) evaluations += evals_gen current_fitness = hof[0].fitness.values[0] if current_fitness >= prev_fitness: no_improvements += 1 else: no_improvements = 0 if no_improvements > stop_after: break return pop, stats, hof, logbook, gen, evaluations