public void Crossover() { List<GA_chromosome> newChromPool = new List<GA_chromosome>(); while (chromPool.Count >= 2) { GA_chromosome PA = chromPool[rand.Next(chromPool.Count)]; chromPool.Remove(PA); GA_chromosome PB = chromPool[rand.Next(chromPool.Count)]; chromPool.Remove(PB); //Decide crossover if (rand.Next(100) + 1 <= crossoverProbability * 100) { GA_chromosome CA = new GA_chromosome(phases, minGreen, maxGreen, roadInfos, reservationTimeEnable); GA_chromosome CB = new GA_chromosome(phases, minGreen, maxGreen, roadInfos, reservationTimeEnable); CA.SetFitnessWeight(Weight_IAWR, Weight_TDF, Weight_CLF); CB.SetFitnessWeight(Weight_IAWR, Weight_TDF, Weight_CLF); //Decide crossover point int crossoverPoint = 0; if (phases > 2) { crossoverPoint = rand.Next(phases - 1); } //Crossover start for (int i = 0; i < phases; i++) { if (i <= crossoverPoint) { CA.SetGreen(i, PA.GetGreen(i)); CB.SetGreen(i, PB.GetGreen(i)); } else { CA.SetGreen(i, PB.GetGreen(i)); CB.SetGreen(i, PA.GetGreen(i)); } } //Put child chromosome to new pool newChromPool.Add(CA); newChromPool.Add(CB); } else { //Put parent chromosome to new pool newChromPool.Add(PA); newChromPool.Add(PB); } } chromPool = newChromPool; }
public Dictionary<int, int> Optimize(Boolean cycleLengthFixed, int phases, int minGreen, int maxGreen, List<RoadInfo> roadInfos) { SetGAParametersByAIManager(); currentGeneration = 0; bestChromosome = null; optimizationRecords = new List<string>(); this.cycleLengthFixed = cycleLengthFixed; this.phases = phases; this.maxGreen = maxGreen; this.minGreen = minGreen; this.roadInfos = roadInfos; //GA process InitializePool(); do{ Reproduction_RestoreSeletedChro(); //Reproduction(); Crossover(); Mutation(); GenerateNewChromosome(); EvaluateFitness(); currentGeneration++; }while (currentGeneration < generationLimit); return bestChromosome.GetAllGreenTime(); }
public void GenerateNewChromosome() { int generatedQuantity = populationSize - chromPool.Count; //Generate lack of chromosome, until satisfied the population size. for (int i = 0; i < generatedQuantity; i++) { GA_chromosome newChrom = new GA_chromosome(phases, minGreen, maxGreen, roadInfos, reservationTimeEnable); newChrom.SetFitnessWeight(Weight_IAWR, Weight_TDF, Weight_CLF); chromPool.Add(newChrom); } }
public void InitializePool() { chromPool.Clear(); for (int i = 0; i < populationSize; i++) { GA_chromosome newChrom = new GA_chromosome(phases, minGreen, maxGreen, roadInfos, reservationTimeEnable); newChrom.SetFitnessWeight(Weight_IAWR, Weight_TDF, Weight_CLF); chromPool.Add(newChrom); } }
public void EvaluateFitness() { GA_chromosome bestChrom_currentPool = chromPool[0]; //Find best chromosome of this generation foreach (GA_chromosome chrom in chromPool) { if (chrom.GetFitness() < bestChrom_currentPool.GetFitness()) { bestChrom_currentPool = chrom; } } //Compare the best chromosome of this generation and best chromosome of all generation, //if this is first generation, set the best chromosome of this generation as the best chromesome of all generation. if (bestChromosome == null || bestChrom_currentPool.GetFitness() < bestChromosome.GetFitness()) { if (bestChromosome == null) { bestChromosome = new GA_chromosome(phases, minGreen, maxGreen, roadInfos, reservationTimeEnable); bestChromosome.SetFitnessWeight(Weight_IAWR, Weight_TDF, Weight_CLF); } for (int p = 0; p < phases; p++) { bestChromosome.SetGreen(p, bestChrom_currentPool.GetGreen(p)); } optimizationRecords.Add("Generation :" + currentGeneration + " Chromosome : " + bestChromosome.PrintChromosome() + " fitness : " + bestChromosome.GetFitness()); } }