//Runs the algoritm for number of iterations public override void Run(Configuration config) { Stopwatch totaltimer = Stopwatch.StartNew(); //Local population average and best fitness double localaveragefitness = 0.0; double localbestfitness = 0.0; //Best solution per run Individual bestsolution = null; //Best solution overall Individual bestbestsolution = null; log = new MuLambdaLogSpecification(config); solution = new SolutionSpecification(); this.config = config; for(int run = 0; run < config.NumberOfRuns; run++) { Stopwatch runtimer = Stopwatch.StartNew(); Console.WriteLine("Run: " + run); bestsolution = null; fitnessevaluations = 0; int samefitnessiterations = 0; //Initialize our population Stopwatch inittimer = Stopwatch.StartNew(); List<Individual> population = InitUniformRandomPopulation(config.PopulationSize, (int)data.RouterCount); inittimer.Stop(); Console.WriteLine("Init time(ms): " + inittimer.Elapsed.TotalMilliseconds); List<Individual> children = new List<Individual>(); //Forever, until broken out of for(;;) { //Generation of children (lambda) Stopwatch generationtimer = Stopwatch.StartNew(); for(int k = 0; k < config.NumberOfChildren; k++) { Individual parenta; Individual parentb; if(config.UseKTournamentForParent) { //Choose parents by K Tournament selection with replacement. parenta = KTournamentSelection(config.ParentTournamentSize, population, true); parentb = KTournamentSelection(config.ParentTournamentSize, population, true); } else // if(config.UseFitnessProportionForParent) { //Choose parents by fitness proportional selection parenta = FitnessProportionalSelection(population); parentb = FitnessProportionalSelection(population); } if(config.UseNCrossover) { //Generate child by using n point crossover Individual child = GenerateChildNPoint(new Individual[2] { parenta, parentb }, config.NumberOfCrossoverPoints); children.Add(child); } else // if(config.UseUniformCrossover) { //Generate child by using uniform crossover Individual child = GenerateChildUniform(parenta, parentb); children.Add(child); } } //Mutation of children by bit flip //This also adds the childs to the population for(int a = 0; a < children.Count; a++) { children[a] = MutateByBitFlip(children[a], config.BitsToFlip); population.Add(children[a]); } List<Individual> newpopulation = new List<Individual>(); localaveragefitness = 0.0; localbestfitness = 0.0; if(config.UseKTournamentForSurvivor) { //Finding the survivors for the new population for(int i = 0; i < config.PopulationSize; i++) { //Use K Tournament without replacement Individual survivor = KTournamentSelection(config.SurvivorTournamentSize, population, false); newpopulation.Add(survivor); //Adjust average and best fitnesses localaveragefitness += survivor.Fitness; if(survivor.Fitness > localbestfitness) localbestfitness = survivor.Fitness; } localaveragefitness /= (double)config.PopulationSize; } else // if(config.UseTruncationForSurvivor) { //Find survivors using truncation. This also adjusts best and average fitnesses. newpopulation = Truncate(population, config.PopulationSize, out localbestfitness, out localaveragefitness); } population = newpopulation; //If it is the first iteration, or we found a new best solution. if(bestsolution == null || newpopulation[0].Fitness > bestsolution.Fitness) { samefitnessiterations = 0; bestsolution = newpopulation[0]; } //Otherwise no new best found. else { samefitnessiterations++; } generationtimer.Stop(); Console.WriteLine("Generation time(s): " + generationtimer.Elapsed.TotalMilliseconds / 1000); Console.WriteLine(fitnessevaluations + " " + localaveragefitness + " " + localbestfitness); //Add generation to log ((MuLambdaLogSpecification)log).AddEvalListing(run, fitnessevaluations, localaveragefitness, localbestfitness); //If we hit the termination number of same best fitness (t) or the number of fitness evals is >= the max. if(samefitnessiterations >= config.TerminationConvergence || fitnessevaluations >= config.NumberOfEvals ) break; } //Grab the possible global best solution if(bestbestsolution == null || bestsolution.Fitness > bestbestsolution.Fitness) { bestbestsolution = bestsolution; } runtimer.Stop(); Console.WriteLine("Run time(s): " + runtimer.Elapsed.TotalMilliseconds / 1000); } totaltimer.Stop(); Console.WriteLine("Total time(s): " + totaltimer.Elapsed.TotalMilliseconds / 1000); //Give the router numbers that were turned off to the solution. solution.RoutersTurnedOff = ExtensionMethods.ConvertBitArrayToOffRouterNumbers(bestbestsolution.Chromosome, data.HostCount); }
//Runs the algoritm for number of iterations public override void Run(Configuration config) { this.config = config; //Prepare solution and log files log = new MuLambdaLogSpecification(config); solution = new NsgaIISolutionSpecification(); //For finding global best pareto front double bestaverage = 0.0; List<Individual> bestpopulation = new List<Individual>(); for(int run = 0; run < config.NumberOfRuns; run++) { //Reset fitness evals every run fitnessevaluations = 0; Console.WriteLine("Run: " + (run + 1)); //Randomly initialize List<Individual> population = InitUniformRandomPopulation(config.PopulationSize, (int)data.RouterCount); //Sort that population into pareto fronts population = FastNonDominatedSort(population); //Forever (until broken out of) for(;;) { List<Individual> childrens = new List<Individual>(); //Create children by selecting parents with binary tournament selection //And recombinating with N Point crossover for(int i = 0; i < config.NumberOfChildren; i++) { Individual parenta = BinaryTournamentSelection(population); Individual parentb = BinaryTournamentSelection(population); childrens.Add(GenerateChildNPoint(new Individual[] { parenta, parentb }, config.NumberOfCrossoverPoints)); } //Mutate the childrens for(int j = 0; j < childrens.Count; j++) { population.Add(MutateByBitFlip(childrens[j], config.BitsToFlip)); } //For holding the local best and local average double localbestfitness = 0.0; double localaveragefitness = 0.0; //Sort the population population = FastNonDominatedSort(population); //Get rid of the worst pareto fronts population = Truncate(population, config.PopulationSize, out localbestfitness, out localaveragefitness); //Output to log file ((MuLambdaLogSpecification)log).AddEvalListing(run, fitnessevaluations, localaveragefitness, localbestfitness); Console.WriteLine(fitnessevaluations + " " + localaveragefitness + " " + localbestfitness); //If we have run over the max of fitness evaluations if(fitnessevaluations > config.NumberOfEvals) { //See if the new average is the best average. if(localaveragefitness > bestaverage) { bestaverage = localaveragefitness; bestpopulation = population; } break; } } } //Get the best pareto front ((NsgaIISolutionSpecification)solution).BestParetoFront = bestpopulation.GetRange(0, bestpopulation.FindIndex(delegate(Individual i) { return i.ParetoFrontRank == 2; }) - 1); ((NsgaIISolutionSpecification)solution).HostOffset = (uint)data.HostCount; }