Example #1
0
int freeGA(struct ga * ga) {

  
  freePopulation(ga->population, ga->populationSize);
  free(ga->aConfig);
  free(ga);
  return 0;
}
Example #2
0
int main(int argc, char * argv[]){
    Population * pop, * selected;
    Individual * best_solution;
    int generation_num;
    
    initParameters(argc, argv);
    pop = genSeededPopulation(POP_SIZE);
    
#if (defined DIVERSITY)
    printGeneComposition(pop);
#endif

    determineFitness(pop);
    sortByFitness(pop);

    generation_num = 0;
    while(generation_num < MAX_NUM_GENERATIONS){
        
    #if  (defined VERBOSE || defined DIVERSITY)
        fprintf(stdout, "\n-----------------   GENERATION %d   -----------------\n", generation_num + 1);
        printPopulation(pop);
    #endif
        
        // FIX - use function pointers instead of flags + if statement
        if(selection_type == 1)
            selected = tournamentSelection(pop);
        else 
            selected = randomSelection(pop);
            
        // FIX - use function pointers instead of flags + if statement
        evolvePopulation(selected, crossover_type, mutation_type);
        determineFitness(selected);
        
        // FIX - use function pointers instead of flags + if statement
        if(replacement_type == 1)
            pop = replaceAll(pop, selected);
        else  
            pop = retainBest(pop, selected);

        generation_num++;
    }
    
    fprintf(stdout, "\nFINAL RESULT:\n");
    printPopulation(pop);
    
    fprintf(stdout, "\nBEST SOLUTION:\n");
    best_solution = findBest(pop);
    printIndividual(best_solution);
    
    freePopulation(pop);
    freeParameters();
    return EXIT_SUCCESS;
} 
Example #3
0
/*....................................................................*/
void
freeGrid(const unsigned int numPoints, const unsigned short numSpecies\
  , struct grid *gp){

  unsigned int i_u;

  if(gp != NULL){
    for(i_u=0;i_u<numPoints;i_u++){
      free(gp[i_u].v1);
      free(gp[i_u].v2);
      free(gp[i_u].v3);
      free(gp[i_u].dir);
      free(gp[i_u].neigh);
      free(gp[i_u].w);
      free(gp[i_u].dens);
      free(gp[i_u].ds);
      freePopulation(numSpecies, gp[i_u].mol);
    }
    free(gp);
  }
}
Example #4
0
int selection(ga * ga){
	machine elite = ga->population[ga->bestMachineIndex];
	machine * newPopulation = (machine*) malloc(POPULATION*sizeof(machine));
	newPopulation[0] = elite;
	newPopulation[0].id = 0;
	ga->bestMachineIndex = 0;
	int i, index1, index2;

	for (i = 1; i < POPULATION; i++) {
		index1 = rand() % POPULATION;
		index2 = rand() % POPULATION;
		//printf("Fitness1 %f\n", ga->population[index1].fitness);
		//printf("Fitness2 %f\n", ga->population[index2].fitness);
		if (ga->population[index1].fitness > ga->population[index2].fitness)
			newPopulation[i] = ga->population[index1];
		else
			newPopulation[i] = ga->population[index2];
		newPopulation[i].id = i;
	}

	freePopulation(ga->population, ga->populationSize);
	ga->population = newPopulation;
	return 0;
}
Example #5
0
int crossover(ga * ga){
	
	int i, indexP1, indexP2, pcoreId;
	int crossCounter = 0;
	int nrNewPop = -1;
	int satisfies = -1;
	machine * newPopulation = (machine*) malloc(POPULATION*sizeof(machine));
	machine * p1;
	machine * p2;
	machine * ch1;
	machine * ch2;

	while(nrNewPop < POPULATION - 1){

		indexP1 = rand() % ga->populationSize;
		indexP2 = rand() % ga->populationSize;

		// Parent pointers

		p1 = &ga->population[indexP1];
		p2 = &ga->population[indexP2];
               
		// Children pointers

	        ch1 = (machine *) malloc(sizeof(machine));
	        ch2 = (machine *) malloc(sizeof(machine));


		// Set the information of the physical cores
		// (It is constant for all machines)

		ch1->id = p1->id;		
		ch1->nrPcores = p1->nrPcores;
		ch1->fitness = 0.0;
		ch1->pcores = (pcore *) malloc(ch1->nrPcores * sizeof(pcore));
		ch1->vm = (vm *) malloc(sizeof(vm));
		ch1->vm->vcores = (vcore *) malloc(VCORES*sizeof(vcore));

		ch2->id = p2->id;
		ch2->nrPcores = p2->nrPcores;
		ch2->fitness = 0.0;
		ch2->pcores = (pcore *) malloc(ch2->nrPcores * sizeof(pcore));
		ch2->vm = (vm *) malloc(sizeof(vm));
		ch2->vm->vcores = (vcore *) malloc(VCORES*sizeof(vcore));
                
		for (i = 0; i < ch1->nrPcores; i++) {
		    ch1->pcores[i].id = i;
		    ch1->pcores[i].maxUtilization = p1->pcores[i].maxUtilization;
		    ch1->pcores[i].speedKhz = p1->pcores[i].speedKhz;
		    ch1->pcores[i].utilization = 0; // Should be recalculated later
		    ch1->pcores[i].nrVCores = 0;
		    
		    ch2->pcores[i].id = i;
		    ch2->pcores[i].maxUtilization = p2->pcores[i].maxUtilization;
		    ch2->pcores[i].speedKhz = p2->pcores[i].speedKhz;
		    ch2->pcores[i].utilization = 0; // Should be recalculated later
		    ch2->pcores[i].nrVCores = 0;
			

		}

			
		for(i=0;i<VCORES;i++){
			ch1->vm->vcores[i].pcore =  (pcore *) malloc( sizeof(pcore));
			ch2->vm->vcores[i].pcore =  (pcore *) malloc( sizeof(pcore));
		} // end for

		/*for(i=0;i<ch1->nrPcores;i++){
			ch1->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
			ch2->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
					
		} // end for*/


		while (satisfies < 0 && crossCounter <100 ) {

			//printf("Inside crossover while\n");
			for(i=0;i<ch1->nrPcores;i++){
				ch1->pcores[i].nrVCores = 0;
				ch2->pcores[i].nrVCores = 0;
				if(ch1->pcores[i].vcores)
					free(ch1->pcores[i].vcores);
				if(ch1->pcores[i].vcores)
					free(ch2->pcores[i].vcores);
				ch1->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
				ch2->pcores[i].vcores = (vcore *) malloc(VCORES*sizeof(vcore));
					
			} // end for

			int x = rand() % VCORES;// Crossover point

			if(rand()>CROSSRATE && x > 0){

				//printf("***** Doing crossover *****\n");

				ch1->vm->tdf = p1->vm->tdf;
				ch2->vm->tdf = p2->vm->tdf;
				for (i = 0; i < x; i++) {
					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
			
					vcoreCh1->id = p1->vm->vcores[i].id;
					vcoreCh1->period = p1->vm->vcores[i].period;
					vcoreCh1->slice = p1->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p1->vm->vcores[i].speedKhz;
			
					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	
                         	
					vcoreCh2->id = p2->vm->vcores[i].id;
					vcoreCh2->period = p2->vm->vcores[i].period;
					vcoreCh2->slice = p2->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p2->vm->vcores[i].speedKhz;

					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	
				
			
				} // end for
				for (i = x; i < VCORES; i++) {


					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
				
					vcoreCh1->id = p2->vm->vcores[i].id;
					vcoreCh1->period = p2->vm->vcores[i].period;
					vcoreCh1->slice = p2->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p2->vm->vcores[i].speedKhz;

					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	

					vcoreCh2->id = p1->vm->vcores[i].id;
					vcoreCh2->period = p1->vm->vcores[i].period;
					vcoreCh2->slice = p1->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p1->vm->vcores[i].speedKhz;

					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	

		
				} // end for
			} // end if
			else{
				//printf("***** No crossover just copying to next generation *****\n");
				
				for (i = 0; i < VCORES; i++) {

					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
			
					vcoreCh1->id = p1->vm->vcores[i].id;
					vcoreCh1->period = p1->vm->vcores[i].period;
					vcoreCh1->slice = p1->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p1->vm->vcores[i].speedKhz;
			
					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	
                         	
					vcoreCh2->id = p2->vm->vcores[i].id;
					vcoreCh2->period = p2->vm->vcores[i].period;
					vcoreCh2->slice = p2->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p2->vm->vcores[i].speedKhz;
				
					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	


				} // end for

				} // end else
			
			
			for (i = 0; i < VCORES; i++) {
				satisfies = 0;
			   	satisfies += checkConstraints(ch1->vm->vcores[i].pcore, ch1->vm);
			   	satisfies += checkConstraints(ch2->vm->vcores[i].pcore, ch2->vm);
			} // end for
		crossCounter++;
		
		}  // end while satisfies
		if (crossCounter >= 100) {
			for (i = 0; i < VCORES; i++) {

					vcore * vcoreCh1 = &ch1->vm->vcores[i];
					vcore * vcoreCh2 = &ch2->vm->vcores[i];
			
					vcoreCh1->id = p1->vm->vcores[i].id;
					vcoreCh1->period = p1->vm->vcores[i].period;
					vcoreCh1->slice = p1->vm->vcores[i].slice;
					vcoreCh1->speedKhz = p1->vm->vcores[i].speedKhz;
			
					pcoreId =  p1->vm->vcores[i].pcore->id;
					vcoreCh1->pcore = &ch1->pcores[pcoreId];
					vcoreCh1->pcore->vcores[vcoreCh1->pcore->nrVCores] = *vcoreCh1;
					vcoreCh1->pcore->nrVCores++;
					vcoreCh1->pcore->utilization = calculateUtilization(vcoreCh1->pcore);	
                         	
					vcoreCh2->id = p2->vm->vcores[i].id;
					vcoreCh2->period = p2->vm->vcores[i].period;
					vcoreCh2->slice = p2->vm->vcores[i].slice;
					vcoreCh2->speedKhz = p2->vm->vcores[i].speedKhz;

					pcoreId =  p2->vm->vcores[i].pcore->id;
					vcoreCh2->pcore = &ch2->pcores[pcoreId];
					vcoreCh2->pcore->vcores[vcoreCh2->pcore->nrVCores] = *vcoreCh2;
					vcoreCh2->pcore->nrVCores++;
					vcoreCh2->pcore->utilization = calculateUtilization(vcoreCh2->pcore);	


				}
		}

		newPopulation[++nrNewPop] = *ch1;
		newPopulation[++nrNewPop] = *ch2;
		satisfies = -1;
		crossCounter = 0;
 		
   
	} // end while population

        freePopulation(ga->population, ga->populationSize);
	ga->population = newPopulation;
	return 0;

} // end crossover function