public bool Add(Individual_NSGA individual) { if (individualList.Count == population_size) { return(false); } individualList.Add(individual); return(true); }
//tinh mse cho moi ca the //private double CalculateMSE(Individual_NSGA individual, double[][] inputData) //{ // // Chỗ này chính là chỗ cần dùng với RBF đây // RadialNetwork rn = new RadialNetwork(m_RadialNetwork); // double fitness = 0.0; // rn.SetWeights(individual.values); // fitness = rn.Accuracy(inputData); // return fitness; //} //tinh mse cho moi ca the private double CalculateMSE(Individual_NSGA individual, double[][] inputData) { // Chỗ này chính là chỗ cần dùng với RBF đây RadialNetwork rn = new RadialNetwork(m_RadialNetwork); double fitness = 0.0; rn.SetWeights(individual.values); fitness = rn.getMSEOf(inputData); return(fitness); }
public void Population_init(int n_gens, int numberOfObjectives) { Random r = new Random(); Individual_NSGA individualItem; for (int i = 0; i < population_size; i++) { individualItem = new Individual_NSGA(n_gens, numberOfObjectives); individualItem.Individual_init(r); IndividualList.Add(individualItem); } }
public Individual_NSGA(Individual_NSGA w) { this.n_gens = w.n_gens; this.values = w.Values; this.numberOfObjectives = w.numberOfObjectives; this.Objective = new double[this.numberOfObjectives]; for (int i = 0; i < this.Objective.Length; i++) { this.Objective[i] = w.Objective[i]; } this.CrowdingDistance = w.CrowdingDistance; this.OverallConstraintViolation = w.OverallConstraintViolation; }
private void DoMutation(Individual_NSGA individual) { double rnd, delta1, delta2, mut_pow, deltaq; double y, yl, yu, val, xy; Individual_NSGA x = new Individual_NSGA(individual); for (int var = 0; var < individual.N_gens; var++) { if (m_Random.NextDouble() <= mutationProb) { y = x.Values[var]; yl = x.GetLowerBound(); yu = x.GetUpperBound(); delta1 = (y - yl) / (yu - yl); delta2 = (yu - y) / (yu - yl); rnd = m_Random.NextDouble(); mut_pow = 1.0 / (eta_m + 1.0); if (rnd <= 0.5) { xy = 1.0 - delta1; val = 2.0 * rnd + (1.0 - 2.0 * rnd) * (Math.Pow(xy, (distributionIndex + 1.0))); deltaq = Math.Pow(val, mut_pow) - 1.0; } else { xy = 1.0 - delta2; val = 2.0 * (1.0 - rnd) + 2.0 * (rnd - 0.5) * (Math.Pow(xy, (distributionIndex + 1.0))); deltaq = 1.0 - (Math.Pow(val, mut_pow)); } y = y + deltaq * (yu - yl); if (y < yl) { y = yl; } if (y > yu) { y = yu; } x.Values[var] = y; //x.SetValue(var, y); } } }
/// <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); }
private Individual_NSGA[] DoCrossover(double crossoverProbability, Individual_NSGA parent1, Individual_NSGA parent2) { Individual_NSGA[] offSpring = new Individual_NSGA[2]; offSpring[0] = new Individual_NSGA(parent1); offSpring[1] = new Individual_NSGA(parent2); int i; double rand; double y1, y2, yL, yu; double c1, c2; double alpha, beta, betaq; double valueX1, valueX2; Individual_NSGA x1 = new Individual_NSGA(parent1); Individual_NSGA x2 = new Individual_NSGA(parent2); Individual_NSGA offs1 = new Individual_NSGA(offSpring[0]); Individual_NSGA offs2 = new Individual_NSGA(offSpring[1]); int numberOfVariables = x1.N_gens; if (m_Random.NextDouble() <= crossoverProbability) { for (i = 0; i < numberOfVariables; i++) { valueX1 = x1.Values[i]; valueX2 = x2.Values[i]; if (m_Random.NextDouble() <= 0.5) { if (Math.Abs(valueX1 - valueX2) > EPS) { if (valueX1 < valueX2) { y1 = valueX1; y2 = valueX2; } else { y1 = valueX2; y2 = valueX1; } yL = x1.GetLowerBound(); //lowerBound yu = x1.GetUpperBound(); //upperBound rand = m_Random.NextDouble(); beta = 1.0 + (2.0 * (y1 - yL) / (y2 - y1)); alpha = 2.0 - Math.Pow(beta, -(distributionIndex + 1.0)); if (rand <= (1.0 / alpha)) { betaq = Math.Pow((rand * alpha), (1.0 / (distributionIndex + 1.0))); } else { betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0))); } c1 = 0.5 * ((y1 + y2) - betaq * (y2 - y1)); beta = 1.0 + (2.0 * (yu - y2) / (y2 - y1)); alpha = 2.0 - Math.Pow(beta, -(distributionIndex + 1.0)); if (rand <= (1.0 / alpha)) { betaq = Math.Pow((rand * alpha), (1.0 / (distributionIndex + 1.0))); } else { betaq = Math.Pow((1.0 / (2.0 - rand * alpha)), (1.0 / (distributionIndex + 1.0))); } c2 = 0.5 * ((y1 + y2) + betaq * (y2 - y1)); if (c1 < yL) { c1 = yL; } if (c2 < yL) { c2 = yL; } if (c1 > yu) { c1 = yu; } if (c2 > yu) { c2 = yu; } if (m_Random.NextDouble() <= 0.5) { offs1.Values[i] = c2; offs2.Values[i] = c1; //offs1.SetValue(i, c2); //offs2.SetValue(i, c1); } else { offs1.Values[i] = c1; offs2.Values[i] = c2; //offs1.SetValue(i, c1); //offs2.SetValue(i, c2); } } else { offs1.Values[i] = valueX1; offs2.Values[i] = valueX2; //offs1.SetValue(i, valueX1); //offs2.SetValue(i, valueX2); } } else { offs1.Values[i] = valueX2; offs2.Values[i] = valueX1; //offs1.SetValue(i, valueX2); //offs2.SetValue(i, valueX1); } } } return(offSpring); }