EvolutionaryAlgorithm::EvolutionaryAlgorithm(vector<CoinType> & newCoinTypes, int newPopulationSize, int newTargetValue, int newNumberOfGenerations, double newElitismBias, int newNumberOfGenes, double newMutationRate, bool newStopAtGoal, int newAgeLimit,int newTournamentSize) { setStopAtGoal(newStopAtGoal); setMutationRate(newMutationRate); setNumberOfGenes(newNumberOfGenes); setEilitismBias(newElitismBias); setNumberOfGenerations(newNumberOfGenerations); setPopulationSize(newPopulationSize); setTargetValue(newTargetValue); setCoinTypes(newCoinTypes); setAgeLimit(newAgeLimit); setTournamentSize(newTournamentSize); setNumberOfChildren(); }
int main(void){ int i, gen; struct parameters *params = NULL; struct chromosome *population[POPULATIONSIZE]; struct chromosome *fittestChromosome = NULL; struct dataSet *trainingData = NULL; double targetFitness = 0; int maxGens = 10000; params = initialiseParameters(NUMINPUTS, NUMNODES, NUMOUTPUTS, ARITY); addNodeFunction(params, "or,nor,and,nand"); /*setTargetFitness(params, targetFitness);*/ setMutationType(params, "probabilistic"); setMutationRate(params, 0.08); trainingData = initialiseDataSetFromFile("./examples/parity3bit.data"); for(i=0; i<POPULATIONSIZE; i++){ population[i] = initialiseChromosome(params); } fittestChromosome = initialiseChromosome(params); /* for the number of allowed generations*/ for(gen=0; gen<maxGens; gen++){ /* set the fitnesses of the population of chromosomes*/ for(i=0; i<POPULATIONSIZE; i++){ setChromosomeFitness(params, population[i], trainingData); } /* copy over the last chromosome to fittestChromosome*/ copyChromosome(fittestChromosome, population[POPULATIONSIZE - 1]); /* for all chromosomes except the last*/ for(i=0; i<POPULATIONSIZE-1; i++){ /* copy ith chromosome to fittestChromosome if fitter*/ if(getChromosomeFitness(population[i]) < getChromosomeFitness(fittestChromosome)){ copyChromosome(fittestChromosome, population[i]); } } /* termination condition*/ if(getChromosomeFitness(fittestChromosome) <= targetFitness){ break; } /* set the first member of the population to be the fittest chromosome*/ copyChromosome(population[0], fittestChromosome); /* set remaining member of the population to be mutations of the fittest chromosome*/ for(i=1; i<POPULATIONSIZE; i++){ copyChromosome(population[i], fittestChromosome); mutateChromosome(params, population[i]); } } printf("gen\tfitness\n"); printf("%d\t%f\n", gen, getChromosomeFitness(fittestChromosome)); for(i=0; i<POPULATIONSIZE; i++){ freeChromosome(population[i]); } freeChromosome(fittestChromosome); freeDataSet(trainingData); freeParameters(params); return 0; }
// FIX - too long (separate into more than one function) // FIX - Make a function that prints out its usage (help function) void initParameters(int argc, char ** argv){ char * arch_filename = DEFAULT_ARCH_FILENAME; char * dfg_filename = DEFAULT_DFG_FILENAME; char * prr_filename = DEFAULT_PRR_FILENAME; int seed = randSeed(); int c; opterr = 0; while((c = getopt(argc, argv, "a:b:c:d:f:g:h:m:n:o:p:r:s:t:Vvw:")) != -1){ switch(c){ case 'a': arch_filename = optarg; break; case 'b': if(strncasecmp("one-point", optarg, strlen(optarg)) == 0 || strncasecmp("one_point", optarg, strlen(optarg)) == 0 || strncasecmp("one point", optarg, strlen(optarg)) == 0 ) crossover_type = 1; else if(strncasecmp("two-point", optarg, strlen(optarg)) != 0 && strncasecmp("two_point", optarg, strlen(optarg)) != 0 && strncasecmp("two point", optarg, strlen(optarg)) != 0 ){ fprintf(stderr, "Unknown crossover option : %s\n", optarg); exit(1); } break; case 'c': setCrossoverRate(atof(optarg)); break; case 'd': dfg_filename = optarg; break; case 'f': setFitnessFunction(optarg); break; case 'g': MAX_NUM_GENERATIONS = atoi(optarg); break; case 'h': setSetupIndex(atoi(optarg)); break; case 'm': setMutationRate(atof(optarg)); break; case 'n': if(strncasecmp("random", optarg, strlen(optarg)) == 0) mutation_type = 2; else if(strncasecmp("rotationally", optarg, strlen(optarg)) != 0){ fprintf(stderr, "Unknown mutation option : %s\n", optarg); exit(1); } break; case 'p': setPopSize(atoi(optarg)); break; case 'r': if(strncasecmp("keep-best", optarg, strlen(optarg)) == 0 || strncasecmp("keep_best", optarg, strlen(optarg)) == 0 || strncasecmp("keep best", optarg, strlen(optarg)) == 0) replacement_type = 2; else if(strncasecmp("all", optarg, strlen(optarg)) != 0){ fprintf(stderr, "Unknown replacement option : %s\n", optarg); exit(1); } break; case 's': if(strncasecmp("random", optarg, strlen(optarg)) == 0) selection_type = 2; else if(strncasecmp("tournament", optarg, strlen(optarg)) != 0){ fprintf(stderr, "Unknown selection option : %s\n", optarg); exit(1); } break; case 't': seed = atoi(optarg); break; case 'V': case 'v': fprintf(stdout, "Offline Scheduler version 2.0 (GA + Napoleon + rcSimulator)\n"); #ifdef DEBUG fprintf(stdout, "Using the DEBUG build\n\n"); #endif #ifdef VERBOSE fprintf(stdout, "Using the VERBOSE build\n\n"); #endif #ifdef STATS fprintf(stdout, "Using the STATS build\n\n"); #endif fprintf(stdout, "Please see https://github.com/Aalwattar/GA for more information\n"); exit(0); case 'w': setRuntimeWeight(atof(optarg)); break; case ':': fprintf(stderr, "Option -%c requires an operand\n", optopt); break; case '?': fprintf(stderr, "Unrecognized option: -%c\n", optopt); default: exit(1); } } if(optind < argc){ printf("Non-option argument %s\n", argv[optind]); exit(1); } // FIX - Check the return value seedRandGenerator(seed); if(initScheduler(arch_filename, dfg_filename, prr_filename) == EXIT_FAILURE) exit(1); if(POP_SIZE == 0){ POP_SIZE = getNumNodes(); } fprintf(stdout, "Parameters:\n"); fprintf(stdout, "\tFitness Function = %s\n\n", getFitnessFunction()); fprintf(stdout, "\tPopulation Size = %d\n", POP_SIZE); fprintf(stdout, "\tNumber of Generations = %d\n\n", MAX_NUM_GENERATIONS); fprintf(stdout, "\tMutation Rate = %.4lf\n", getMutationRate()); fprintf(stdout, "\tCrossover Rate = %.4lf\n", getCrossoverRate()); fprintf(stdout, "\tRuntime Weight = %.4lf\n", getRuntimeWeight()); fprintf(stdout, "\tPower Weight = %.4lf\n\n", 1.0 - getRuntimeWeight()); if(crossover_type == 1) fprintf(stdout, "\tCrossover Algorithm = One point Crossover\n"); else fprintf(stdout, "\tCrossover Algorithm = Two point Crossover\n"); if(mutation_type == 1) fprintf(stdout, "\tMutation Algorithm = Rotational Mutation\n"); else fprintf(stdout, "\tMutation Algorithm = Random Mutation\n"); if(selection_type == 1) fprintf(stdout, "\tSelection Algorithm = Tournament Selection\n"); else fprintf(stdout, "\tSelection Algorithm = Random Selection\n"); if(replacement_type == 1) fprintf(stdout, "\tReplacement Algorithm = Replace All\n\n"); else fprintf(stdout, "\tReplacement Algorithm = Keep Best\n\n"); // FIX - make this dynamic fprintf(stdout, "\tSeed = %d\n", seed); fprintf(stdout, "\tPercent seeding = %.4lf\n", (1.0 - PERCENT_POP_RANDOM)); }