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); }
public void Diversity(Population_NSGA population) { double objetiveMaxn; double objetiveMinn; double distance; int N = population.Size(); if (N == 1) { population.IndividualList[0].Objective[1] = 1; return; } //sắp xếp quần thể theo hàm mục tiêu từ bé đến lớn population.Sort(new ObjectiveComparator(0)); //nếu quần thể chỉ có 2 cá thể thì ta lấy cả 2 chứ ko cần tính f2 nữa //gán 1,2 để vẽ đồ thị. if (N == 2) { population.IndividualList[0].Objective[1] = 1; population.IndividualList[1].Objective[1] = 2; return; } //nếu quần thể có từ 3 cá thể trở lên objetiveMinn = population.IndividualList[0].Objective[0]; objetiveMaxn = population.IndividualList[N - 1].Objective[0]; population.IndividualList[0].Objective[1] = 0; population.IndividualList[N - 1].Objective[1] = Double.MaxValue; //gán thế này cái cuối cùng sẽ luôn bị loại vì nhỏ hơn thì được ưu tiên //if (indexF2 == 2)//cicle //{ //tìm bán kính lớn nhât double bkmax = 0; for (int i = 0; i < N - 1; i++) { double temp = (population.IndividualList[i + 1].Objective[0] - population.IndividualList[i].Objective[0]); if (bkmax < temp) { bkmax = temp; } } double r = bkmax; for (int i = 1; i < N - 1; i++) { distance = 0; int dem = 0; for (int j = 0; j < N; j++) { double dis = Math.Abs(population.IndividualList[i].Objective[0] - population.IndividualList[j].Objective[0]); if (dis <= r) { distance += dis; dem++; } } if (dem <= 1) { distance /= (dem - 1);//vì tính cả chính nó nên phải trừ đi 1 } population.IndividualList[i].Objective[1] = distance; } //} //if (indexF2 == 0)//crowding //{ // for (int j = 1; j < N - 1; j++) // { // distance = population.IndividualList[j + 1].Objective[0] - population.IndividualList[j - 1].Objective[0]; // distance = distance / (objetiveMaxn - objetiveMinn); // population.IndividualList[j].Objective[1] = distance; // } //} //if (indexF2 == 1)//distance //{ // for (int i = 1; i < N - 1; i++) // { // distance = 0; // for (int j = 0; j < N; j++) // { // distance += Math.Abs((population.IndividualList[i].Objective[0] - population.IndividualList[j].Objective[0])); // } // distance /= (N - 1); // population.IndividualList[i].Objective[1] = distance; // } //} }