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; } } }
public Population_NSGA Union(Population_NSGA population) { int newSize = this.Size() + population.Population_size; if (newSize < population_size) { newSize = population_size; } Population_NSGA union = new Population_NSGA(newSize); for (int i = 0; i < this.Size(); i++) { union.Add(this.individualList[i]); } for (int i = 0; i < population.Size(); i++) { union.Add(population.individualList[i]); } return(union); }
/// <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); }