//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(); BetterBitArray routerswitches = new BetterBitArray((int)data.RouterCount); double highestfitness = 0.0; BetterBitArray best = new BetterBitArray((int)data.RouterCount); for(int i = 0; i < config.NumberOfRuns; i++) { routerswitches = new BetterBitArray((int)data.RouterCount); int numbertoswitch = randomgenerator.Next((int)data.RouterCount); for(int j = 0; j < numbertoswitch; j++) { int switching; while(routerswitches.IsSet(switching = randomgenerator.Next((int)data.RouterCount)) == true); routerswitches.Set(switching); } int pathscut = 0; double fitness = FitnessEvaluation(data.NetworkPaths, routerswitches, config.PenaltyCoefficient, out pathscut); if(fitness > highestfitness) { ((RandomLogSpecification)log).AddEvaluation(i, fitness); highestfitness = fitness; best = routerswitches; } } solution.RoutersTurnedOff = ExtensionMethods.ConvertBitArrayToOffRouterNumbers(best, data.HostCount); }
public static List<int> ConvertBitArrayToOffRouterNumbers(BetterBitArray ba, uint hostoffset) { List<int> routers = new List<int>(); for(int i = 0; i < ba.Length; i++) { if(ba.IsSet(i)) routers.Add((int)(i + hostoffset)); } return routers; }
//Uniform random initialization of population private List<Individual> InitUniformRandomPopulation(int number, int numberrouters) { double timedoingfitness = 0.0; //double timegenerating = 0.0; List<Individual> population = new List<Individual>(); for(int i = 0; i < number; i++) { BetterBitArray individual = new BetterBitArray(numberrouters); int numbertoswitch = randomgenerator.Next(numberrouters); for(int j = 0; j < numbertoswitch; j++) { int switching; while(individual.IsSet(switching = randomgenerator.Next(numberrouters)) == true); individual.Set(switching); } Stopwatch fitnesstime = Stopwatch.StartNew(); int pathscut = 0; double fitness = FitnessEvaluation(data.NetworkPaths, individual, config.PenaltyCoefficient, out pathscut); fitnesstime.Stop(); Console.WriteLine("Fitness time: " + fitnesstime.Elapsed.TotalMilliseconds); timedoingfitness += fitnesstime.Elapsed.TotalMilliseconds; population.Add(new Individual(fitness, pathscut, individual)); } Console.WriteLine("Fitness time(ms): " + timedoingfitness / 1000.0); return population; }
//Uniform random initialization of population private List<Individual> InitUniformRandomPopulation(int number, int numberrouters) { List<Individual> population = new List<Individual>(); for(int i = 0; i < number; i++) { BetterBitArray individual = new BetterBitArray(numberrouters); int numbertoswitch = randomgenerator.Next(numberrouters); for(int j = 0; j < numbertoswitch; j++) { int switching; while(individual.IsSet(switching = randomgenerator.Next(numberrouters)) == true); individual.Set(switching); } int pathscut = 0; double fitness = FitnessEvaluation(data.NetworkPaths, individual, config.PenaltyCoefficient, out pathscut); population.Add(new Individual(fitness, pathscut, individual)); } return population; }