public void CrowdingDistanceAssignment(Population_NSGA population) { int size = population.Population_size; if (size == 0) { return; } if (size == 1) { population.Get(0).CrowdingDistance = Double.MaxValue; return; } if (size == 2) { population.Get(0).CrowdingDistance = Double.MaxValue; population.Get(1).CrowdingDistance = Double.MaxValue; return; } Population_NSGA front = new Population_NSGA(size); for (int i = 0; i < size; i++) { front.Add(population.Get(i)); } for (int i = 0; i < size; i++) { front.Get(i).CrowdingDistance = 0.0; } double objetiveMaxn; double objetiveMinn; double distance; //2 hàm mục tiêu for (int i = 0; i < 2; i++) { // Sort the population by Obj n front.Sort(new ObjectiveComparator(i)); objetiveMinn = front.Get(0).Objective[i]; objetiveMaxn = front.Get(front.Size() - 1).Objective[i]; //Set de crowding distance front.Get(0).CrowdingDistance = double.PositiveInfinity; front.Get(size - 1).CrowdingDistance = double.PositiveInfinity; for (int j = 1; j < size - 1; j++) { distance = front.Get(j + 1).Objective[i] - front.Get(j - 1).Objective[i]; distance = distance / (objetiveMaxn - objetiveMinn); distance += front.Get(j).CrowdingDistance; front.Get(j).CrowdingDistance = distance; } } }
/// <summary> /// Runs the NSGA-II algorithm. /// </summary> /// <returns>a <code>SolutionSet</code> that is a set of non dominated solutions as a result of the algorithm execution</returns> public Population_NSGA Execute() { int evaluations; Population_NSGA population; Population_NSGA offspringPopulation; Population_NSGA union; //Initialize the variables evaluations = 0; // Bước 1: Khởi tạo quần thể // Create the initial solutionSet population = new Population_NSGA(m_Population_size); population = (Population_NSGA)Population.Clone(); // Danh gia ham muc tieu 1 cho quan the Pt for (int i = 0; i < m_Population_size; i++) { Individual_NSGA individual = population.IndividualList[i]; individual.Objective[0] = CalculateMSE(individual, inputData); } // Danh gia ham muc tieu 2 cho quan the Pt Diversity(population); // Vòng lặp tiến hóa while (evaluations < m_MaxEvaluations) { // Create the offSpring solutionSet offspringPopulation = new Population_NSGA(m_Population_size); // Tập cá thể con Individual_NSGA[] parents = new Individual_NSGA[2]; for (int i = 0; i < (m_Population_size / 2); i++) // N/2 { if (evaluations < m_MaxEvaluations) { //Lấy 2 Cha parents int sol1 = (int)(m_Random.NextDouble() * (m_Population_size - 1)); int sol2 = (int)(m_Random.NextDouble() * (m_Population_size - 1)); parents[0] = population.IndividualList[sol1]; parents[1] = population.IndividualList[sol2]; // while (sol1 == sol2) { sol2 = (int)(m_Random.NextDouble() * (m_Population_size - 1)); parents[1] = population.IndividualList[sol2]; } // Sử dụng SBX crossover tạo ra 2 thằng con Individual_NSGA[] offSpring = SBXCrossover(parents); //Dot bien DoMutation(offSpring[0]); DoMutation(offSpring[1]); //Danh gia ham muc tieu 1 cho quan the moi Qt offSpring[0].Objective[0] = CalculateMSE(offSpring[0], inputData); offSpring[1].Objective[0] = CalculateMSE(offSpring[1], inputData); // Đưa 2 con vào danh sách offspringPopulation.Add(offSpring[0]); offspringPopulation.Add(offSpring[1]); evaluations += 2; } } //Danh gia ham muc tieu 2 cho quan the moi Qt Diversity(offspringPopulation); // Create the solutionSet union of solutionSet and offSpring union = ((Population_NSGA)population).Union(offspringPopulation); // Ranking the union - Sxep ko troi - non-dominated sorting Ranking ranking = new Ranking(union); int remain = m_Population_size; int index = 0; Population_NSGA front = null; population.Clear(); // Obtain the next front front = ranking.GetSubfront(index); // Đưa các Front vào quần thể mới, đến thằng cuối cùng while ((remain > 0) && (remain >= front.Size())) { //Assign crowding distance to individuals // Gắn khoang cach CrowdingDistance cho ca the CrowdingDistanceAssignment(front); //Add the individuals of this front for (int k = 0; k < front.Size(); k++) { population.Add(front.Get(k)); } //Decrement remain remain = remain - front.Size(); //Obtain the next front index++; if (remain > 0) { front = ranking.GetSubfront(index); } } // Remain is less than front(index).size, insert only the best one if (remain > 0) { // front contains individuals to insert CrowdingDistanceAssignment(front); front.Sort(new CrowdingComparator()); for (int k = 0; k < remain; k++) { population.Add(front.Get(k)); } remain = 0; } } // Return the first non-dominated front Ranking rank = new Ranking(population); Population_NSGA result = rank.GetSubfront(0); return(result); }
/// <summary> /// Constructor. /// </summary> /// <param name="population">The <code>SolutionSet</code> to be ranked.</param> public Ranking(Population_NSGA population) { this.population = population; // dominateMe[i] contains the number of solutions dominating i int[] dominateMe = new int[this.population.Size()]; // iDominate[k] contains the list of solutions dominated by k List <int>[] iDominate = new List <int> [this.population.Size()]; // front[i] contains the list of individuals belonging to the front i List <int>[] front = new List <int> [this.population.Size() + 1]; // flagDominate is an auxiliar encodings.variable int flagDominate; // Initialize the fronts for (int i = 0; i < front.Length; i++) { front[i] = new List <int>(); } //-> Fast non dominated sorting algorithm // Contribution of Guillaume Jacquenot for (int p = 0; p < this.population.Size(); p++) { // Initialize the list of individuals that i dominate and the number // of individuals that dominate me iDominate[p] = new List <int>(); dominateMe[p] = 0; } for (int p = 0; p < (this.population.Size() - 1); p++) { // For all q individuals , calculate if p dominates q or vice versa for (int q = p + 1; q < this.population.Size(); q++) { flagDominate = constraint.Compare(population.Get(p), population.Get(q)); if (flagDominate == 0) { flagDominate = dominance.Compare(population.Get(p), population.Get(q)); } if (flagDominate == -1) { iDominate[p].Add(q); dominateMe[q]++; } else if (flagDominate == 1) { iDominate[q].Add(p); dominateMe[p]++; } } // If nobody dominates p, p belongs to the first front } for (int p = 0; p < this.population.Size(); p++) { if (dominateMe[p] == 0) { front[0].Add(p); population.Get(p).Rank = 0; } } //Obtain the rest of fronts int k = 0; while (front[k].Count != 0) { k++; foreach (var it1 in front[k - 1]) { foreach (var it2 in iDominate[it1]) { int index = it2; dominateMe[index]--; if (dominateMe[index] == 0) { front[k].Add(index); this.population.Get(index).Rank = k; } } } } //<- ranking = new Population_NSGA[k]; //0,1,2,....,i-1 are front, then i fronts for (int j = 0; j < k; j++) { ranking[j] = new Population_NSGA(front[j].Count); foreach (var it1 in front[j]) { ranking[j].Add(population.Get(it1)); } } }