/* * Prune the dcache to remove unused children of the parent dentry. */ void shrink_dcache_parent(struct dentry * parent) { int found; while ((found = select_parent(parent)) != 0) prune_dcache(found); }
void shrink_dcache_parent(struct dentry * parent) { int found; while ((found = select_parent(parent)) != 0) { prune_dcache(found); conditional_schedule(); /* Typically sys_rmdir() */ } }
/* * evolution of the group, including the process of * select parent, crossover and mutation */ void evolve(Group* grp) { select_parent(grp); crossover(grp); mutate(grp); }
/** * The E-means algorithm, uses a genetic algorithm to optimize the parameters * for the K-means implemetation of clustering based Lloyds clustering algorithm. * * @return Status code, 0 for SUCCESS, 1 for ERROR */ int emeans(void) { gsl_matrix *data = NULL, *bounds = NULL, *parent1 = NULL, *parent2 = NULL, **population = NULL, **new_population = NULL, ***clusters = NULL; int status = SUCCESS; double fitness[size], probability[size]; // Initialize the PRNG pcg32_random_t rng; int rounds = 5; pcg32_srandom_r(&rng, time(NULL) ^ (intptr_t)&printf, (intptr_t)&rounds); // Allocate memory and load the data data = gsl_matrix_alloc(data_rows, data_cols); bounds = gsl_matrix_alloc(data_cols, 2); parent1 = gsl_matrix_alloc(n_clusters, data_cols); parent2 = gsl_matrix_alloc(n_clusters, data_cols); population = (gsl_matrix **)calloc(size, sizeof(gsl_matrix **)); new_population = (gsl_matrix **)calloc(size, sizeof(gsl_matrix **)); clusters = (gsl_matrix ***)calloc(size, sizeof(gsl_matrix ***)); for (int i = 0; i < (int)size; ++i) { clusters[i] = (gsl_matrix **)calloc(n_clusters, sizeof(gsl_matrix **)); population[i] = gsl_matrix_alloc(n_clusters, data_cols); new_population[i] = gsl_matrix_alloc(n_clusters, data_cols); } if ((status = load_data(data_file, data)) != SUCCESS) { fprintf(stderr, RED "Unable to load data!\n" RESET); status = ERROR; goto free; } // Calculate the bounds of the data calc_bounds(data, bounds); // Generate the initial population printf(CYAN "Generating initial population...\n" RESET); for (int i = 0; i < (int)size; ++i) { random_centroids(population[i], bounds, &rng); } // Perform the Genetic Algorithm for (int iter = 0; iter < max_iter; ++iter) { // Compute the fitness of each chromosome for (int i = 0; i < (int)size; ++i) { lloyd_defined(trials, population[i], data, n_clusters, clusters[i]); fitness[i] = dunn_index(population[i], n_clusters, clusters[i]); if (VERBOSE == 1) printf(CYAN "chromsome[%d], fitness: %10.6f\n" RESET, i, fitness[i]); } // Generate the probabilities for roulette wheel selection gen_probability(size, fitness, probability); // Save the results if there is a new best solution save_results(fitness_file, centroids_file, cluster_file, size, fitness, population, n_clusters, clusters); // Perform roulette wheel selection and GA operators if (VERBOSE == 1) printf(CYAN "Executing roulette wheel selection...\n" RESET); // Select parents from the population and perform genetic operators for (int i = 0; i < size; i += 2) { // Select parents for (int j = 0, idx = 0; j < 2; ++j) { idx = select_parent(size, probability, &rng); if (VERBOSE == 1) printf(CYAN "Parent %d selected as chromsome %d from population\n" RESET, j, idx); if (j == 0) gsl_matrix_memcpy(parent1, population[idx]); else gsl_matrix_memcpy(parent2, population[idx]); } // Perform crossover and mutation with specified probabilities if (VERBOSE == 1) printf(CYAN "Performing crossover...\n" RESET); if (pcg32_random_r(&rng) / (double)UINT32_MAX <= c_rate) { crossover(parent1, parent2, &rng); } if (VERBOSE == 1) printf(CYAN "Performing background mutation...\n" RESET); for (int j = 0; j < 2; ++j) { if (pcg32_random_r(&rng) / (double)UINT32_MAX <= m_rate) { if (j == 0) mutate(parent1, bounds, &rng); else mutate(parent2, bounds, &rng); } } // Copy each parent to the new population if (VERBOSE == 1) printf(CYAN "Copying parents to new population...\n" RESET); gsl_matrix_memcpy(new_population[i], parent1); gsl_matrix_memcpy(new_population[i+1], parent2); } // Copy the new population to the next population if (VERBOSE == 1) printf(CYAN "Copying current population as new population\n" RESET); for (int i = 0; i < size; ++i) { gsl_matrix_memcpy(population[i], new_population[i]); } // Check if stop signal, terminate if present if (access("./stop", F_OK) != -1) { printf(YELLOW "Stop signal received, shutting down!\n" RESET); remove("./stop"); break; } } printf(GREEN "Finished executing E-means, shutting down!\n" RESET); free: for (int i = 0; i < (int)size; ++i) { for (int j = 0; j < n_clusters; ++j) { gsl_matrix_free(clusters[i][j]); } free(clusters[i]); } free(clusters); for (int i = 0; i < (int)size; ++i) { gsl_matrix_free(population[i]); gsl_matrix_free(new_population[i]); } free(population); free(new_population); gsl_matrix_free(data); gsl_matrix_free(bounds); return status; }