示例#1
0
        //Runs the algoritm for number of iterations
        public override void Run(Configuration config)
        {
            log = new RandomLogSpecification(data.DataFileName, config.RandomSeed, config.NumberOfRuns, config.PenaltyCoefficient);
            solution = new SolutionSpecification();

            BitArray routerswitches = new BitArray((int)data.RouterCount);
            double highestfitness = 0.0;
            BitArray best = new BitArray((int)data.RouterCount);
            for(int i = 0; i < config.NumberOfRuns; i++)
            {
                routerswitches = new BitArray((int)data.RouterCount);
                int numbertoswitch = randomgenerator.Next((int)data.RouterCount);
                for(int j = 0; j < numbertoswitch; j++)
                {
                    int switching;
                    while(routerswitches.Get(switching = randomgenerator.Next((int)data.RouterCount)) == true);
                    routerswitches.Set(switching, true);
                }

                double fitness = FitnessEvaluation(data.NetworkPaths, routerswitches, numbertoswitch, config.PenaltyCoefficient);

                if(fitness > highestfitness)
                {
                    ((RandomLogSpecification)log).AddEvaluation(i, fitness);
                    highestfitness = fitness;
                    best = (BitArray)routerswitches.Clone();
                }
            }

            solution.RoutersTurnedOff = ExtensionMethods.ConvertBitArrayToOffRouterNumbers(best, data.HostCount);
        }
示例#2
0
        //Runs the algoritm for number of iterations
        public override void Run(int iterations, int randseed)
        {
            log = new RandomLogSpecification(data.DataFileName, randseed, iterations);
            solution = new SolutionSpecification();

            BitArray routerswitches = new BitArray((int)data.RouterCount);
            double highestfitness = 0.0;
            BitArray best = new BitArray((int)data.RouterCount);
            for(int i = 0; i < iterations; i++)
            {
              //  Console.WriteLine("Run #" + i);
                routerswitches = new BitArray((int)data.RouterCount);
              //  Console.WriteLine("Router count: " + routerswitches.Count);
                int numbertoswitch = randomgenerator.Next((int)data.RouterCount);
              //  Console.WriteLine("Switching amount: " + numbertoswitch);
                for(int j = 0; j < numbertoswitch; j++)
                {
                    int switching;
                    while(routerswitches.Get(switching = randomgenerator.Next((int)data.RouterCount)) == true);
                //    Console.WriteLine("Switching: " + switching);
                    routerswitches.Set(switching, true);
                }

                double fitness = FitnessEvaluation(data.NetworkPaths, routerswitches, numbertoswitch);

               // Console.WriteLine("Fitness: " + fitness);
                if(fitness > highestfitness)
                {
                    ((RandomLogSpecification)log).AddEvaluation(i, fitness);
                    highestfitness = fitness;
                    best = (BitArray)routerswitches.Clone();
                }
            }

            solution.RoutersTurnedOff = ExtensionMethods.ConvertBitArrayToOffRouterNumbers(best, data.HostCount);
        }
示例#3
0
 public EvolutionaryAlgorithm()
 {
     data = null;
     log = null;
     solution = null;
 }
示例#4
0
        //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);
        }