def _get_best_chromosome(chromosomes, data, count_clusters): """! @brief Returns the current best chromosome. @param[in] chromosomes (list): Chromosomes that are used for searching. @param[in] data (list): Input data that is used for clustering process. @param[in] count_clusters (uint): Amount of clusters that should be allocated. @return (list, float, list) The best chromosome, its fitness function value and fitness function values for all chromosomes. """ # Calc centers centres = ga_math.get_centres(chromosomes, data, count_clusters) # Calc Fitness functions fitness_functions = genetic_algorithm._calc_fitness_function( centres, data, chromosomes) # Index of the best chromosome best_chromosome_idx = fitness_functions.argmin() # Get chromosome with the best fitness function return chromosomes[best_chromosome_idx], fitness_functions[ best_chromosome_idx], fitness_functions
def _select(chromosomes, data, count_clusters, select_coeff): """! @brief Performs selection procedure where new chromosomes are calculated. @param[in] chromosomes (numpy.array): Chromosomes """ # Calc centers centres = ga_math.get_centres(chromosomes, data, count_clusters) # Calc fitness functions fitness = genetic_algorithm._calc_fitness_function( centres, data, chromosomes) for _idx in range(len(fitness)): fitness[_idx] = math.exp(1 + fitness[_idx] * select_coeff) # Calc probability vector probabilities = ga_math.calc_probability_vector(fitness) # Select P chromosomes with probabilities new_chromosomes = np.zeros(chromosomes.shape, dtype=np.int) # Selecting for _idx in range(len(chromosomes)): new_chromosomes[_idx] = chromosomes[ga_math.get_uniform( probabilities)] return new_chromosomes