GList* initializePopulation(individual* seed, gint number){ GList* population=NULL; gint i, j; gint random; individual* ind; ind=copyIndividual(seed); evaluateIndividual(ind, 0); memoryUsage* memoryU=stdMemoryAvg->data; memoryU->timeUsr=ind->time; memoryU->MEMORY_PROFILING_UNIT=ind->memory; population=g_list_append(population, ind); mutType mt; for(i=1; i<number; i++){ ind=copyIndividual(seed); //random=randomIntRange(0, numberOfGenes); for(j=0; j<numberOfGenes; j++){ if(default_mutation_type[j]==mutation_power2){ mt=mutation_power2_random; } else if(default_mutation_type[j]==mutation_power2_allow0){ mt=mutation_power2_allow0_random; } else mt=mutation_random; ind->chrom[j]=mutationGene(mt, ind->chrom[j], default_lower_bound[j], default_upper_bound[j]); } evaluateIndividual(ind, i); population=g_list_append(population, ind); } calculateFrontier(&population); return population; }
void CCGDE3::generateSolutions(){ int i,j,k; sizeSol=0; solutionCounter = 0; for(i=0;i<numSpecies;i++){ sizeSol += species[i].frontSize; } if((Solution = (Population *)calloc(1,sizeof(Population))) == NULL){ printf("ERROR!! --> calloc: no memory for population\n"); exit(1); } populationMemoryAllocation(Solution,sizeSol); for(i=0,k=0;i<numSpecies;i++){ for(j=0;j<NP;j++){ if(species[i].xG->individual[j].rank == 1){ copyIndividual(&species[i].xG->individual[j],&Solution->individual[k]); k++; } } } nonDominatedSorting(Solution,sizeSol,sizeSol); }
GList* initializePopulationFromFile(individual* seed, gint number, gchar* filename){ GList* population=NULL; gint i, j; individual* ind; gchar* line=g_malloc(512*sizeof(gchar)); FILE* fp=fopen(filename, "r"); if(fp==NULL){ g_printf("File %s does not exist.\n", filename); exit(0); } line=fgets(line, 512, fp); while(line!=NULL && line[0]=='m'){ ind=copyIndividual(seed); if(numberOfGenes==NUMBER_OF_SHALLOW_GENE){ sscanf(line, "%*s\t%*f\t%lf\t%*f\t%*d\t%*d\t%*f\t%*f\t%*f\t%d\t%d\t%d\t%d\t%d\t%d", &ind->memory, &ind->chrom[0], &ind->chrom[1], &ind->chrom[2], &ind->chrom[3], &ind->chrom[4], &ind->chrom[5]); } else{ sscanf(line, "%*s\t%*f\t%lf\t%*f\t%*d\t%*d\t%*f\t%*f\t%*f\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d", &ind->memory, &ind->chrom[0], &ind->chrom[1], &ind->chrom[2], &ind->chrom[3], &ind->chrom[4], &ind->chrom[5], &ind->chrom[6], &ind->chrom[7], &ind->chrom[8], &ind->chrom[9], &ind->chrom[10], &ind->chrom[11], &ind->chrom[12], &ind->chrom[13], &ind->chrom[14]); } population=g_list_append(population, ind); line=fgets(line, 512, fp); } g_free(line); fclose(fp); return population; }
void fillCrowdingDistance(int count, int frontSize, list *elite) { int *distance; list *temp; int i, j; int missing = NP - count; while(missing<frontSize) { assignCrowdingDistanceList(elite->child, frontSize); distance = (int *)calloc(frontSize,sizeof(int)); temp = elite->child; for(j=0; j<frontSize; j++) { distance[j] = temp->index; temp = temp->child; } quickSortDistance(distance, frontSize); deleteInd(elite,distance[0]); frontSize--; if(missing<frontSize) { free(distance); } } for(i=count, j=frontSize-1; i<NP; i++, j--) { copyIndividual(distance[j], i); } free(distance); return; }
gint crossover(GList** population){ gint num, length=g_list_length(*population); individual* parent1, *parent2, *child1, *child2; for(num=0; num<POPULATION_SIZE*CROSSOVER_RATE; num+=2){ g_printf("crossover %d, size=%d\n", num+1, g_list_length(*population)); parent1=tournamentSelect(*population); child1=copyIndividual(parent1); parent2=tournamentSelect(*population); while(parent1==parent2) parent2=tournamentSelect(*population); child2=copyIndividual(parent2); crossoverScattered(child1, child2); child1->fitness=evaluateIndividual(child1, length+num); child2->fitness=evaluateIndividual(child2, length+num+1); *population=g_list_append(*population, child1); *population=g_list_append(*population, child2); } return 0; }
GList* initializeRandPopulation(individual* seed, gint number){ GList* population=NULL; gint i, j; gint random; individual* ind; ind=copyIndividual(seed); evaluateIndividual(ind, 0); memoryUsage* memoryU=stdMemoryAvg->data; memoryU->timeUsr=ind->time; memoryU->MEMORY_PROFILING_UNIT=ind->memory; population=g_list_append(population, ind); //gint printGap=number/100==0?1:number/100; mutType mt; for(i=1; i<number; i++){ ind=copyIndividual(seed); if(i%50==0){ g_printf("\n%d-%d: ", i, i+50); fprintf(logfp, "\n%d-%d: ", i, i+50); } //random=randomIntRange(0, numberOfGenes); for(j=0; j<numberOfGenes; j++){ if(default_mutation_type[j]==mutation_power2){ mt=mutation_power2_random; } else if(default_mutation_type[j]==mutation_power2_allow0){ mt=mutation_power2_allow0_random; } else mt=mutation_random; ind->chrom[j]=mutationGene(mt, ind->chrom[j], default_lower_bound[j], default_upper_bound[j]); } evaluateIndividual(ind, i); population=g_list_append(population, ind); } g_printf("\n"); selection(&population); return population; }
Population *copyPopulation(Population* population) { int i, c; Population *newPopulation = malloc(sizeof(Population)); newPopulation->inds = malloc(population->size * sizeof(Individual*)); newPopulation->size = population->size; for (i = 0; i < newPopulation->size; i++) { newPopulation->inds[i] = copyIndividual(population->inds[i]); printf("%d:\t", i); for (c = 0; c < population->inds[i]->genotype->length; c++) printf("%d ", population->inds[i]->genotype->codons[c]); printf("\n"); printf("%d:\t", i); for (c = 0; c < newPopulation->inds[i]->genotype->length; c++) printf("%d ", newPopulation->inds[i]->genotype->codons[c]); printf("\n"); } return newPopulation; }
void selection() { int result; int i,j; int end; int frontSize; int popSize; int rank = 1; list *pool; list *elite; list *temp1, *temp2; pool = createList(-1); elite = createList(-1); frontSize = 0; popSize = 0; temp1 = pool; for (i=0; i<collection_size; i++){ insert(temp1,i); temp1 = temp1->child; } i=0; do{ temp1 = pool->child; insert(elite, temp1->index); frontSize = 1; temp2 = elite->child; temp1 = deleteNode(temp1); temp1 = temp1->child; do{ temp2 = elite->child; if (temp1==NULL){ break; } do{ end = 0; result = dominanceComparator(&(collection_fitness[(temp1->index)*Nobj]), &(collection_fitness[(temp2->index)*Nobj])); if (result == 1){ insert(pool, temp2->index); temp2 = deleteNode(temp2); frontSize--; temp2 = temp2->child; } if (result == 0){ temp2 = temp2->child; } if (result == -1){ end = 1; } }while ((end != 1) && (temp2 != NULL)); if(result == 0 || result == 1){ insert(elite, temp1->index); frontSize++; temp1 = deleteNode(temp1); } temp1 = temp1->child; }while(temp1 != NULL); if(rank == 1){ if(frontSize <= NP){ my_frontSize = frontSize; }else{ my_frontSize = NP; } } temp2 = elite->child; j=i; if((popSize + frontSize)<= NP){ do{ copyIndividual(temp2->index, i); x_rank[i] = rank; popSize+=1; temp2 = temp2->child; i+=1; }while(temp2 != NULL); assignCrowdingDistanceIndexes(j, i-1); rank+=1; }else{ fillCrowdingDistance(i, frontSize, elite); popSize = NP; for (j=i; j<NP; j++){ x_rank[j] = rank; } } temp2 = elite->child; do{ temp2 = deleteNode(temp2); temp2 = temp2->child; }while (elite->child !=NULL); }while(popSize < NP); deleteList(pool); deleteList(elite); return; }