/// <summary> /// Generated new the children population list based on the parents population list. /// This node needs to be used in a loop along with Assign Fitness Function node and /// Sorting node. /// </summary> /// <param name="populationList">Parents population list.</param> /// <param name="lowerLimits">List of lower limits for new generation.</param> /// <param name="upperLimits">List of upper limits for new generation.</param> /// <returns>Generated population list.</returns> public static List <List <double> > GenerationAlgorithm(List <List <double> > populationList, List <int> lowerLimits, List <int> upperLimits) { int numVar = lowerLimits.Count; int numObj = populationList.Count - numVar; //create the NSGA-II algorithm Algorithm algorithm = CreateAlgorithm(numObj, lowerLimits, upperLimits); //change solutions list to solutionSet SolutionSet population = SolutionListToPop(populationList, algorithm, numVar); Operator mutationOperator = algorithm.operators["mutation"]; Operator crossoverOperator = algorithm.operators["crossover"]; Operator selectionOperator = algorithm.operators["selection"]; int populationSize = population.size(); SolutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 2); i++) { // selection parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); // crossover Solution[] offSpring = (Solution[])crossoverOperator.execute(parents); // mutation mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); offspringPopulation.@add(offSpring[0]); offspringPopulation.add(offSpring[1]); } return(PopToSolutionList(offspringPopulation)); }
/// <summary> /// Generated new the children population list based on the parents population list. /// This node needs to be used in a loop along with Assign Fitness Function node and /// Sorting node. /// </summary> /// <param name="populationList">Parents population list.</param> /// <param name="lowerLimits">List of lower limits for new generation.</param> /// <param name="upperLimits">List of upper limits for new generation.</param> /// <returns>Generated population list.</returns> public static List<List<double>> GenerationAlgorithm(List<List<double>> populationList, List<double> lowerLimits, List<double> upperLimits) { int numVar = lowerLimits.Count; int numObj = populationList.Count - numVar; //create the NSGA-II algorithm Algorithm algorithm = CreateAlgorithm(numObj, lowerLimits, upperLimits); //change solutions list to solutionSet SolutionSet population = SolutionListToPop(populationList, algorithm, numVar); Operator mutationOperator = algorithm.operators["mutation"]; Operator crossoverOperator = algorithm.operators["crossover"]; Operator selectionOperator = algorithm.operators["selection"]; int populationSize = population.size(); SolutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 2); i++) { // selection parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); // crossover Solution[] offSpring = (Solution[])crossoverOperator.execute(parents); // mutation mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); offspringPopulation.@add(offSpring[0]); offspringPopulation.add(offSpring[1]); } return PopToSolutionList(offspringPopulation); }
/// <summary> /// Sorts the combination of parents and child population set based on Pareto Fron Ranking. /// </summary> /// <param name="population">Parent solution set.</param> /// <param name="offspringPop">Offspring solution set.</param> /// <param name="lowerLimits">List of lower limits.</param> /// <param name="upperLimits">List of upper limits.</param> /// <returns></returns> public static List <List <double> > Sorting(List <List <double> > populationList, List <List <double> > offspringList, List <int> lowerLimits, List <int> upperLimits) { int numVar = lowerLimits.Count; int numObj = populationList.Count - numVar; Algorithm algorithm = CreateAlgorithm(numObj, lowerLimits, upperLimits); //change solutions list to solutionSet SolutionSet population = SolutionListToPop(populationList, algorithm, numVar); SolutionSet offspringPop = SolutionListToPop(offspringList, algorithm, numVar); // Creating the solutionSet union of solutionSet and offSpring SolutionSet union = ((SolutionSet)population).union(offspringPop); // Ranking the union Ranking ranking = new Ranking(union); int remain = population.size(); int index = 0; SolutionSet front = null; population.clear(); // Obtain the next front front = ranking.getSubfront(index); //* while ((remain > 0) && (remain >= front.size())) { //Assign crowding distance to individuals Distance.crowdingDistanceAssignment(front, algorithm.problem_.numberOfObjectives_); //Add the individuals of this front for (int k = 0; k < front.size(); k++) { population.@add(front[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 Distance.crowdingDistanceAssignment(front, algorithm.problem_.numberOfObjectives_); IComparer comp = new CrowdingDistanceComparator(); front.solutionList_.Sort(comp.Compare); for (int k = 0; k < remain; k++) { population.@add(front[k]); } // for remain = 0; } List <List <double> > pList = PopToSolutionList(population); return(PopToSolutionList(population)); }
// NSGAII public override SolutionSet execute() { int populationSize; int maxEvaluations; int evaluations; SolutionSet population; SolutionSet offspringPopulation; SolutionSet union; SolutionSet allTest = new SolutionSet(); Operator mutationOperator; Operator crossoverOperator; Operator selectionOperator; populationSize = (int)inputParameters_["populationSize"]; maxEvaluations = (int)inputParameters_["maxEvaluations"]; // Initializing variables population = new SolutionSet(populationSize); evaluations = 0; //System.Console.WriteLine("Solves Name:" + problem_.problemName_); //System.Console.WriteLine("Pop size : " + populationSize); //System.Console.WriteLine("Max Evals: " + maxEvaluations); // Reading operators mutationOperator = operators["mutation"]; crossoverOperator = operators["crossover"]; selectionOperator = operators["selection"]; //System.Console.WriteLine("Crossover parameters: " + crossoverOperator); //System.Console.WriteLine("Mutation parameters: " + mutationOperator); // Creating the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(problem_); //newSolution.variable_[0] = 1; problem_.evaluate(newSolution); //newSolution.objective_[0] = 10; //Mohammad //slnLst.Add(newSolution); //System.Console.WriteLine ("" + i + ": " + newSolution); //problem_.evaluateConstraints(newSolution); evaluations++; population.add(newSolution); } //for // Main loop while (evaluations < maxEvaluations) { // Creating the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 2); i++) { if (evaluations < maxEvaluations) { //if ((evaluations % 1000) == 0) // Console.WriteLine("Evals: " + evaluations); // selection parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); // crossover Solution[] offSpring = (Solution[])crossoverOperator.execute(parents); // mutation mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); //Environment.Exit(0); // evaluation problem_.evaluate(offSpring[0]); problem_.evaluate(offSpring[1]); offspringPopulation.@add(offSpring[0]); offspringPopulation.add(offSpring[1]); evaluations += 2; } // if } // for // Creating the solutionSet union of solutionSet and offSpring union = ((SolutionSet)population).union(offspringPopulation); //Mohammad allTest = ((SolutionSet)allTest).union(union); //System.Console.WriteLine ("Union size:" + union.size ()); // Ranking the union Ranking ranking = new Ranking(union); int remain = populationSize; int index = 0; SolutionSet front = null; //Distance distance = new Distance (); population.clear(); // Obtain the next front front = ranking.getSubfront(index); //* while ((remain > 0) && (remain >= front.size())) { //Assign crowding distance to individuals Distance.crowdingDistanceAssignment(front, problem_.numberOfObjectives_); //Add the individuals of this front for (int k = 0; k < front.size(); k++) { population.@add(front[k]); } // for //Decrement remain remain = remain - front.size(); //Obtain the next front index++; if (remain > 0) { front = ranking.getSubfront(index); } // if } // while // Remain is less than front(index).size, insert only the best one if (remain > 0) { // front contains individuals to insert Distance.crowdingDistanceAssignment(front, problem_.numberOfObjectives_); IComparer comp = new CrowdingDistanceComparator(); front.solutionList_.Sort(comp.Compare); for (int k = 0; k < remain; k++) { population.@add(front[k]); } // for remain = 0; } // if } // while // Return as output parameter the required evaluations outputParameters_["evaluations"] = evaluations; // Return the first non-dominated front Ranking ranking2 = new Ranking(population); //Console.WriteLine(slnLst[0].objective_.GetValue(0)); //File.WriteAllLines("C:/text.txt",slnLst.ConvertAll(Convert.ToString)); return ranking2.getSubfront(0); }
// NSGAII public override SolutionSet execute() { int populationSize; int maxEvaluations; int evaluations; SolutionSet population; SolutionSet offspringPopulation; SolutionSet union; SolutionSet allTest = new SolutionSet(); Operator mutationOperator; Operator crossoverOperator; Operator selectionOperator; populationSize = (int)inputParameters_["populationSize"]; maxEvaluations = (int)inputParameters_["maxEvaluations"]; // Initializing variables population = new SolutionSet(populationSize); evaluations = 0; //System.Console.WriteLine("Solves Name:" + problem_.problemName_); //System.Console.WriteLine("Pop size : " + populationSize); //System.Console.WriteLine("Max Evals: " + maxEvaluations); // Reading operators mutationOperator = operators["mutation"]; crossoverOperator = operators["crossover"]; selectionOperator = operators["selection"]; //System.Console.WriteLine("Crossover parameters: " + crossoverOperator); //System.Console.WriteLine("Mutation parameters: " + mutationOperator); // Creating the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(problem_); //newSolution.variable_[0] = 1; problem_.evaluate(newSolution); //newSolution.objective_[0] = 10; //Mohammad //slnLst.Add(newSolution); //System.Console.WriteLine ("" + i + ": " + newSolution); //problem_.evaluateConstraints(newSolution); evaluations++; population.add(newSolution); } //for // Main loop while (evaluations < maxEvaluations) { // Creating the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize); Solution[] parents = new Solution[2]; for (int i = 0; i < (populationSize / 2); i++) { if (evaluations < maxEvaluations) { //if ((evaluations % 1000) == 0) // Console.WriteLine("Evals: " + evaluations); // selection parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); // crossover Solution[] offSpring = (Solution[])crossoverOperator.execute(parents); // mutation mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); //Environment.Exit(0); // evaluation problem_.evaluate(offSpring[0]); problem_.evaluate(offSpring[1]); offspringPopulation.@add(offSpring[0]); offspringPopulation.add(offSpring[1]); evaluations += 2; } // if } // for // Creating the solutionSet union of solutionSet and offSpring union = ((SolutionSet)population).union(offspringPopulation); //Mohammad allTest = ((SolutionSet)allTest).union(union); //System.Console.WriteLine ("Union size:" + union.size ()); // Ranking the union Ranking ranking = new Ranking(union); int remain = populationSize; int index = 0; SolutionSet front = null; //Distance distance = new Distance (); population.clear(); // Obtain the next front front = ranking.getSubfront(index); //* while ((remain > 0) && (remain >= front.size())) { //Assign crowding distance to individuals Distance.crowdingDistanceAssignment(front, problem_.numberOfObjectives_); //Add the individuals of this front for (int k = 0; k < front.size(); k++) { population.@add(front[k]); } // for //Decrement remain remain = remain - front.size(); //Obtain the next front index++; if (remain > 0) { front = ranking.getSubfront(index); } // if } // while // Remain is less than front(index).size, insert only the best one if (remain > 0) { // front contains individuals to insert Distance.crowdingDistanceAssignment(front, problem_.numberOfObjectives_); IComparer comp = new CrowdingDistanceComparator(); front.solutionList_.Sort(comp.Compare); for (int k = 0; k < remain; k++) { population.@add(front[k]); } // for remain = 0; } // if } // while // Return as output parameter the required evaluations outputParameters_["evaluations"] = evaluations; // Return the first non-dominated front Ranking ranking2 = new Ranking(population); //Console.WriteLine(slnLst[0].objective_.GetValue(0)); //File.WriteAllLines("C:/text.txt",slnLst.ConvertAll(Convert.ToString)); return(ranking2.getSubfront(0)); }