public static double GetLoadRate(this PlantSet plantSet, double demandPower) { var totalPower = plantSet.GetTotalPower(); if (demandPower > totalPower) { return(0); } return(demandPower / totalPower); }
public static double Fitness(this PlantSet plantSet, double demandPower) { if (plantSet.GetTotalPower() < demandPower) { return(0); } var loadRate = plantSet.GetLoadRate(demandPower); var averageCost = plantSet.GetAverageCost(demandPower); var averageCo2 = plantSet.GetAverageCO2(demandPower); var fitness = loadRate + 1 / averageCost + 1 / averageCo2; return(fitness); }
public static double GetAverageCO2(this PlantSet plantSet, double demandPower) { var totalPower = plantSet.GetTotalPower(); if (demandPower > totalPower) { return(double.PositiveInfinity); } var totalCo2 = 0.0; foreach (var gene in plantSet) { totalCo2 += gene.CO2 * gene.Power * gene.Count; } return(totalCo2 / totalPower); }
static void Main(string[] args) { var stopWatch = new Stopwatch(); stopWatch.Start(); var maxGeneration = 2000; var generation = 0; var bestFitness = 0.0; PlantSet bestPlantSet = null; var populationSize = 1000; var demandPower = 10000; //Init population var population = PopulationGenerator.Generate(populationSize).ToList(); while (generation < maxGeneration) { //For each partical update pbest and gbest foreach (var p in population) { var fitness = p.Fitness(demandPower); p.UpdatePBest(fitness); p.UpdateGBest(fitness); p.UpdateBestFitness(fitness); } //Get the best one population = population.OrderBy(x => x.FitnessLast).ToList(); var lastP = population.Last(); //Save the best one if (lastP.GetTotalPower() >= demandPower) { var fitness = lastP.FitnessLast; if (fitness > bestFitness) { bestFitness = fitness; bestPlantSet = lastP.Copy(); } } Console.WriteLine(bestPlantSet); Console.WriteLine($"Best fitness: {bestPlantSet.Fitness(demandPower)}"); Console.WriteLine($"Best Total Power: {bestPlantSet.GetTotalPower()}"); Console.WriteLine($"Best LoadRate: {bestPlantSet.GetLoadRate(demandPower)}"); Console.WriteLine($"Best Cost: {bestPlantSet.GetAverageCost(demandPower)}"); Console.WriteLine($"Best CO2: {bestPlantSet.GetAverageCO2(demandPower)}"); Console.WriteLine($"Gen: {generation}"); Console.WriteLine($"========================="); //This the known best solution. if (Math.Abs(bestFitness - 1.1054208443867) < 0.0001) { break; } //Move partical var w = WCalculator.CalcW(generation, maxGeneration); foreach (var p in population) { p.NextPosition(w); } generation++; } stopWatch.Stop(); Console.WriteLine($"Execution Time:{stopWatch.Elapsed.TotalSeconds}"); Console.ReadKey(); }