int main(int argc, char *argv[]) { srand(time(NULL)); double populationSize = atoi(argv[1]); float target = (float)atoi(argv[2]); printf("Running with:\n Population Size = %.0f\n Bit Length = %d\n", populationSize, BITLENGTH); long int i = 0; int j = 0; struct subject *sheep = (struct subject*)malloc(sizeof(struct subject) * populationSize); gettimeofday(&start, NULL); //make the Adam&Eve generation for(i = 0; i < populationSize; i++) { if(debug == 0) { printf("subject %d bits = ", i); } for(j = 0; j < BITLENGTH; j++) { sheep[i].bits[j] = rand() %2; if(debug == 0) { printf("%d", sheep[i].bits[j]); } } sheep[i].fitness = 0.0f; if(debug == 0) { printf(" with fitness %f\n", sheep[i].fitness); } } int howLongThisShitTook = 0; int solutionFound = 0; //0 if false, 1 if true struct subject *fuckSpawn = (struct subject*)malloc(sizeof(struct subject) * populationSize); while(solutionFound != 1) { float totalFitness = 0.0f; for(i = 0; i < populationSize; i++) { sheep[i].fitness = AssignFitness(sheep[i].bits, target); if(debug == 0) { printf("\n subject %d with fitness %f\n", i, sheep[i].fitness); } totalFitness += sheep[i].fitness; if(debug == 0) { printf(" Total Fitness%f\n", totalFitness); } } for(i = 0; i < populationSize; i++) { if(sheep[i].fitness == 999.0f) { printf("It's been found. It took %d generations to find\n", howLongThisShitTook); solutionFound = 1; break; } } long fuckSpawnPopulationSize = 0; int fuckSpawn1Bits[BITLENGTH]; int fuckSpawn2Bits[BITLENGTH]; if(solutionFound != 1) { printf("Creating New Generation\n"); while(fuckSpawnPopulationSize < populationSize) { MakeMeABaby(totalFitness, populationSize, sheep, fuckSpawn1Bits); MakeMeABaby(totalFitness, populationSize, sheep, fuckSpawn2Bits); Crossover(fuckSpawn1Bits, fuckSpawn2Bits); Mutate(fuckSpawn1Bits); Mutate(fuckSpawn2Bits); for(i = 0; i < BITLENGTH; i++) { fuckSpawn[fuckSpawnPopulationSize].bits[i] = fuckSpawn1Bits[i]; fuckSpawn[fuckSpawnPopulationSize + 1].bits[i] = fuckSpawn2Bits[i]; } if(debug == 0) { printf("fuckspawn%d: ", fuckSpawnPopulationSize); for(i = 0; i < BITLENGTH; i++) { printf("%d", fuckSpawn[fuckSpawnPopulationSize].bits[i]); } printf("\nfuckspawn%d: ",fuckSpawnPopulationSize + 1); for(i = 0; i < BITLENGTH; i++) { printf("%d", fuckSpawn[fuckSpawnPopulationSize + 1].bits[i]); } printf("\n"); } fuckSpawn[fuckSpawnPopulationSize].fitness = 0.0f; fuckSpawn[fuckSpawnPopulationSize + 1].fitness = 0.0f; fuckSpawnPopulationSize += 2; } for(i = 0; i < populationSize; i++) { sheep[i] = fuckSpawn[i]; } howLongThisShitTook++; printf("New Generation done! Now on Generation %d\n", howLongThisShitTook); } if(howLongThisShitTook > MAXGENERATIONS) { printf("Did not find a solution in maximum allowable runs\n"); solutionFound = 1; } } gettimeofday(&end, NULL); int timeran = (((end.tv_sec - start.tv_sec) * 1000000) +(end.tv_usec - start.tv_usec)); printf("Completed in %d Nano Seconds\n", timeran); return 0; }
/** * C-routine to reach a floatvalue using a genetic model<br> * bugs: none found<br> * @param target floatvalue that serves as a target<br> * @param maxiter maximum number of itterations<br> */ int SolveNumber(float target, int maxiter){ while (maxiter > 0){ //storage for our population of chromosomes. bytechromosome Population[POP_SIZE]; //first create a random population, all with zero fitness. for (int i=0; i<POP_SIZE; i++){ Population[i].setbits(GetRandomBits(CHROMO_LENGTH)); Population[i].setfitness(0.0f); } int GenerationsRequiredToFindASolution = 0; //we will set this flag if a solution has been found bool bFound = false; //enter the main GA loop while(!bFound){ //this is used during roulette wheel sampling float TotalFitness = 0.0f; // test and update the fitness of every chromosome in the population for (int i=0; i<POP_SIZE; i++){ Population[i].setfitness(AssignFitness(Population[i].getbits(), (int)target)); TotalFitness += Population[i].getfitness(); } // check to see if we have found any solutions (fitness will be 999) for (int i=0; i<POP_SIZE; i++){ if (Population[i].getfitness() == 999.0f){ printf("Solution found in %d generations!\n",GenerationsRequiredToFindASolution); PrintChromo(Population[i].getbits()); bFound = true; break; } } // create a new population by selecting two parents at a time and creating offspring // by applying crossover and mutation. Do this until the desired number of offspring // have been created. //define some temporary storage for the new population we are about to create bytechromosome temp[POP_SIZE]; int cPop = 0; //loop until we have created POP_SIZE new chromosomes while (cPop < POP_SIZE){ // we are going to create the new population by grabbing members of the old population // two at a time via roulette wheel selection. string offspring1 = Roulette((int)TotalFitness, Population); string offspring2 = Roulette((int)TotalFitness, Population); //add crossover dependent on the crossover rate Crossover(offspring1, offspring2); //now mutate dependent on the mutation rate Mutate(offspring1); Mutate(offspring2); //add these offspring to the new population. (assigning zero as their fitness scores) temp[cPop++] = bytechromosome(offspring1, 0.0f); temp[cPop++] = bytechromosome(offspring2, 0.0f); }//end loop //copy temp population into main population array for (int i=0; i<POP_SIZE; i++){ Population[i] = temp[i]; } GenerationsRequiredToFindASolution++; if (GenerationsRequiredToFindASolution > MAX_ALLOWABLE_GENERATIONS){ printf("No solutions found this run!\n"); bFound = true; } } printf("\n"); maxiter--; }//end while return 0; }