📄 nsgaii.java
字号:
/** * NsgaII.java * @author Juan J. Durillo * @version 1.0 */package jmetal.metaheuristics.nsgaII;import jmetal.base.*;import jmetal.util.*;/** * This class implements the NSGA-II algorithm. */public class NSGAII extends Algorithm { /** * stores the problem to solve */ private Problem problem_; /** * Constructor * @param problem Problem to solve */ public NSGAII(Problem problem){ this.problem_ = problem; } // NSGAII /** * Runs of the NSGA-II algorithm. * @return a <code>SolutionSet</code> that is a set of non dominated solutions * as a result of the algorithm execution * @throws JMException */ public SolutionSet execute() throws JMException { int populationSize ; int maxEvaluations ; int evaluations ; SolutionSet population ; SolutionSet offspringPopulation ; SolutionSet union ; Operator mutationOperator ; Operator crossoverOperator ; Operator selectionOperator ; Distance distance = new Distance() ; //Read the parameters populationSize = ((Integer)this.getInputParameter("populationSize")).intValue(); maxEvaluations = ((Integer)this.getInputParameter("maxEvaluations")).intValue(); //Initialize the variables population = new SolutionSet(populationSize); evaluations = 0; //Read the operators mutationOperator = this.operators_.get("mutation"); crossoverOperator = this.operators_.get("crossover"); selectionOperator = this.operators_.get("selection"); // Create the initial solutionSet Solution newSolution; for (int i = 0; i < populationSize; i++) { newSolution = new Solution(problem_); problem_.evaluate(newSolution); problem_.evaluateConstraints(newSolution); evaluations++; population.add(newSolution); } //for // Generations ... while (evaluations < maxEvaluations) { // Create the offSpring solutionSet offspringPopulation = new SolutionSet(populationSize); Solution [] parents = new Solution[2]; for (int i = 0; i < (populationSize/2); i++){ //obtain parents parents[0] = (Solution)selectionOperator.execute(population); parents[1] = (Solution)selectionOperator.execute(population); if (evaluations < maxEvaluations) { Solution [] offSpring = (Solution []) crossoverOperator.execute(parents); mutationOperator.execute(offSpring[0]); mutationOperator.execute(offSpring[1]); problem_.evaluate(offSpring[0]); problem_.evaluateConstraints(offSpring[0]); problem_.evaluate(offSpring[1]); problem_.evaluateConstraints(offSpring[1]); offspringPopulation.add(offSpring[0]); offspringPopulation.add(offSpring[1]); evaluations += 2; } else { offspringPopulation.add(new Solution(parents[0])); offspringPopulation.add(new Solution(parents[1])); } // if } // for // Create the solutionSet union of solutionSet and offSpring union = ((SolutionSet)population).union(offspringPopulation); // Ranking the union Ranking ranking = new Ranking(union); int remain = populationSize; 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, problem_.getNumberOfObjectives()); //Add the individuals of this front for (int k = 0; k < front.size(); k++ ) { population.add(front.get(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_.getNumberOfObjectives()); front.sort(new jmetal.base.operator.comparator.CrowdingComparator()); for (int k = 0; k < remain; k++) { population.add(front.get(k)); } // for remain = 0; } // if } // while // Return the first non-dominated front Ranking ranking = new Ranking(population); return ranking.getSubfront(0); } // execute} // NSGA-II
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -