/** * Constructor. Creates a new instance of Spea2Fitness for a given <code>SolutionSet</code>. * * @param solutionSet The <code>SolutionSet</code> */ public Spea2Fitness(SolutionSet solutionSet) { distance = distance_.distanceMatrix(solutionSet); solutionSet_ = solutionSet; for (int i = 0; i < solutionSet_.size(); i++) { solutionSet_.get(i).setLocation(i); } // for } // Spea2Fitness
/** * Performs the operation * * @param object Object representing a SolutionSet. * @return the best solution found */ public Object execute(Object object, CITO_CAITO problem) { SolutionSet solutionSet = (SolutionSet) object; if (solutionSet.size() == 0) { return null; } int bestSolution; bestSolution = 0; for (int i = 1; i < solutionSet.size(); i++) { if (comparator_.compare(solutionSet.get(i), solutionSet.get(bestSolution)) < 0) bestSolution = i; } // for return bestSolution; } // Execute
public static void main(String[] args) throws JMException, ClassNotFoundException { Problem problem; // The problem to solve Algorithm algorithm; // The algorithm to use Operator crossover; // Crossover operator Operator mutation; // Mutation operator Operator selection; // Selection operator // int bits ; // Length of bit string in the OneMax problem // bits = 512 ; // problem = new OneMax(bits); problem = new Sphere("Real", 20); // problem = new Easom("Real") ; // problem = new Griewank("Real", 10) ; algorithm = new DE(problem); // Asynchronous cGA /* Algorithm parameters*/ algorithm.setInputParameter("populationSize", 100); algorithm.setInputParameter("maxEvaluations", 1000000); // Crossover operator crossover = CrossoverFactory.getCrossoverOperator("DifferentialEvolutionCrossover"); crossover.setParameter("CR", 0.1); crossover.setParameter("F", 0.5); crossover.setParameter("DE_VARIANT", "rand/1/bin"); // Add the operators to the algorithm selection = SelectionFactory.getSelectionOperator("DifferentialEvolutionSelection"); algorithm.addOperator("crossover", crossover); algorithm.addOperator("selection", selection); /* Execute the Algorithm */ long initTime = System.currentTimeMillis(); SolutionSet population = algorithm.execute(); long estimatedTime = System.currentTimeMillis() - initTime; System.out.println("Total execution time: " + estimatedTime); /* Log messages */ System.out.println("Objectives values have been writen to file FUN"); population.printObjectivesToFile("FUN"); System.out.println("Variables values have been writen to file VAR"); population.printVariablesToFile("VAR"); } // main
/** * Gets 'size' elements from a population of more than 'size' elements using for this de * enviromentalSelection truncation * * @param size The number of elements to get. */ public SolutionSet environmentalSelection(int size) { if (solutionSet_.size() < size) { size = solutionSet_.size(); } // Create a new auxiliar population for no alter the original population SolutionSet aux = new SolutionSet(solutionSet_.size()); int i = 0; while (i < solutionSet_.size()) { if (solutionSet_.get(i).getFitness() < 1.0) { aux.add(solutionSet_.get(i)); solutionSet_.remove(i); } else { i++; } // if } // while if (aux.size() < size) { Comparator comparator = new FitnessComparator(); solutionSet_.sort(comparator); int remain = size - aux.size(); for (i = 0; i < remain; i++) { aux.add(solutionSet_.get(i)); } return aux; } else if (aux.size() == size) { return aux; } double[][] distance = distance_.distanceMatrix(aux); List<List<DistanceNode>> distanceList = new LinkedList<List<DistanceNode>>(); for (int pos = 0; pos < aux.size(); pos++) { aux.get(pos).setLocation(pos); List<DistanceNode> distanceNodeList = new ArrayList<DistanceNode>(); for (int ref = 0; ref < aux.size(); ref++) { if (pos != ref) { distanceNodeList.add(new DistanceNode(distance[pos][ref], ref)); } // if } // for distanceList.add(distanceNodeList); } // for for (int q = 0; q < distanceList.size(); q++) { Collections.sort(distanceList.get(q), distanceNodeComparator); } // for while (aux.size() > size) { double minDistance = Double.MAX_VALUE; int toRemove = 0; i = 0; Iterator<List<DistanceNode>> iterator = distanceList.iterator(); while (iterator.hasNext()) { List<DistanceNode> dn = iterator.next(); if (dn.get(0).getDistance() < minDistance) { toRemove = i; minDistance = dn.get(0).getDistance(); // i y toRemove have the same distance to the first solution } else if (dn.get(0).getDistance() == minDistance) { int k = 0; while ((dn.get(k).getDistance() == distanceList.get(toRemove).get(k).getDistance()) && k < (distanceList.get(i).size() - 1)) { k++; } if (dn.get(k).getDistance() < distanceList.get(toRemove).get(k).getDistance()) { toRemove = i; } // if } // if i++; } // while int tmp = aux.get(toRemove).getLocation(); aux.remove(toRemove); distanceList.remove(toRemove); Iterator<List<DistanceNode>> externIterator = distanceList.iterator(); while (externIterator.hasNext()) { Iterator<DistanceNode> interIterator = externIterator.next().iterator(); while (interIterator.hasNext()) { if (interIterator.next().getReference() == tmp) { interIterator.remove(); continue; } // if } // while } // while } // while return aux; } // environmentalSelection
/** Assigns fitness for all the solutions. */ public void fitnessAssign() { double[] strength = new double[solutionSet_.size()]; double[] rawFitness = new double[solutionSet_.size()]; double kDistance; // Calculate the strength value // strength(i) = |{j | j <- SolutionSet and i dominate j}| for (int i = 0; i < solutionSet_.size(); i++) { for (int j = 0; j < solutionSet_.size(); j++) { if (dominance_.compare(solutionSet_.get(i), solutionSet_.get(j)) == -1) { strength[i] += 1.0; } // if } // for } // for // Calculate the raw fitness // rawFitness(i) = |{sum strenght(j) | j <- SolutionSet and j dominate i}| for (int i = 0; i < solutionSet_.size(); i++) { for (int j = 0; j < solutionSet_.size(); j++) { if (dominance_.compare(solutionSet_.get(i), solutionSet_.get(j)) == 1) { rawFitness[i] += strength[j]; } // if } // for } // for // Add the distance to the k-th individual. In the reference paper of SPEA2, // k = sqrt(population.size()), but a value of k = 1 recommended. See // http://www.tik.ee.ethz.ch/pisa/selectors/spea2/spea2_documentation.txt int k = 1; for (int i = 0; i < distance.length; i++) { Arrays.sort(distance[i]); kDistance = 1.0 / (distance[i][k] + 2.0); // Calcule de D(i) distance // population.get(i).setFitness(rawFitness[i]); solutionSet_.get(i).setFitness(rawFitness[i] + kDistance); } // for } // fitnessAsign
/** * @param args Command line arguments. The first (optional) argument specifies the problem to * solve. * @throws JMException * @throws IOException * @throws SecurityException Usage: three options - jmetal.metaheuristics.mocell.MOCell_main - * jmetal.metaheuristics.mocell.MOCell_main problemName - * jmetal.metaheuristics.mocell.MOCell_main problemName ParetoFrontFile */ public static void main(String[] args) throws JMException, IOException, ClassNotFoundException { Problem problem; // The problem to solve Algorithm algorithm; // The algorithm to use Operator mutation; // Mutation operator QualityIndicator indicators; // Object to get quality indicators // Logger object and file to store log messages logger_ = Configuration.logger_; fileHandler_ = new FileHandler("PAES_main.log"); logger_.addHandler(fileHandler_); indicators = null; if (args.length == 1) { Object[] params = {"Real"}; problem = (new ProblemFactory()).getProblem(args[0], params); } // if else if (args.length == 2) { Object[] params = {"Real"}; problem = (new ProblemFactory()).getProblem(args[0], params); indicators = new QualityIndicator(problem, args[1]); } // if else { // Default problem problem = new Kursawe("ArrayReal", 3); // problem = new Fonseca("Real"); // problem = new Kursawe("BinaryReal",3); // problem = new Water("Real"); // problem = new ZDT4("Real", 1000); // problem = new WFG1("Real"); // problem = new DTLZ1("Real"); // problem = new OKA2("Real") ; } // else algorithm = new PAES(problem); // Algorithm parameters algorithm.setInputParameter("archiveSize", 100); algorithm.setInputParameter("biSections", 5); algorithm.setInputParameter("maxEvaluations", 25000); // Mutation (Real variables) mutation = MutationFactory.getMutationOperator("PolynomialMutation"); mutation.setParameter("probability", 1.0 / problem.getNumberOfVariables()); mutation.setParameter("distributionIndex", 20.0); // Mutation (BinaryReal variables) // mutation = MutationFactory.getMutationOperator("BitFlipMutation"); // mutation.setParameter("probability",0.1); // Add the operators to the algorithm algorithm.addOperator("mutation", mutation); // Execute the Algorithm long initTime = System.currentTimeMillis(); SolutionSet population = algorithm.execute(); long estimatedTime = System.currentTimeMillis() - initTime; // Result messages // STEP 8. Print the results logger_.info("Total execution time: " + estimatedTime + "ms"); logger_.info("Variables values have been writen to file VAR"); population.printVariablesToFile("VAR"); logger_.info("Objectives values have been writen to file FUN"); population.printObjectivesToFile("FUN"); if (indicators != null) { logger_.info("Quality indicators"); logger_.info("Hypervolume: " + indicators.getHypervolume(population)); logger_.info("GD : " + indicators.getGD(population)); logger_.info("IGD : " + indicators.getIGD(population)); logger_.info("Spread : " + indicators.getSpread(population)); logger_.info("Epsilon : " + indicators.getEpsilon(population)); } // if } // main
/** * @author Juanjo This method selects N solutions from a set M, where N <= M using the same method * proposed by Qingfu Zhang, W. Liu, and Hui Li in the paper describing MOEA/D-DRA (CEC 09 * COMPTETITION) An example is giving in that paper for two objectives. If N = 100, then the * best solutions attenting to the weights (0,1), (1/99,98/99), ...,(98/99,1/99), (1,0) are * selected. * <p>Using this method result in 101 solutions instead of 100. We will just compute 100 even * distributed weights and used them. The result is the same * <p>In case of more than two objectives the procedure is: 1- Select a solution at random 2- * Select the solution from the population which have maximum distance to it (whithout * considering the already included) * @param n: The number of solutions to return * @return A solution set containing those elements */ SolutionSet finalSelection(int n) throws JMException { SolutionSet res = new SolutionSet(n); if (problem_.getNumberOfObjectives() == 2) { // subcase 1 double[][] intern_lambda = new double[n][2]; for (int i = 0; i < n; i++) { double a = 1.0 * i / (n - 1); intern_lambda[i][0] = a; intern_lambda[i][1] = 1 - a; } // for // we have now the weights, now select the best solution for each of them for (int i = 0; i < n; i++) { Solution current_best = population.get(0); int index = 0; double value = fitnessFunction(current_best, intern_lambda[i]); for (int j = 1; j < n; j++) { double aux = fitnessFunction( population.get(j), intern_lambda[i]); // we are looking the best for the weight i if (aux < value) { // solution in position j is better! value = aux; current_best = population.get(j); } } res.add(new Solution(current_best)); } } else { // general case (more than two objectives) Distance distance_utility = new Distance(); int random_index = PseudoRandom.randInt(0, population.size() - 1); // create a list containing all the solutions but the selected one (only references to them) List<Solution> candidate = new LinkedList<Solution>(); candidate.add(population.get(random_index)); for (int i = 0; i < population.size(); i++) { if (i != random_index) { candidate.add(population.get(i)); } } // for while (res.size() < n) { int index = 0; Solution selected = candidate.get(0); // it should be a next! (n <= population size!) double distance_value = distance_utility.distanceToSolutionSetInObjectiveSpace(selected, res); int i = 1; while (i < candidate.size()) { Solution next_candidate = candidate.get(i); double aux = distance_value = distance_utility.distanceToSolutionSetInObjectiveSpace(next_candidate, res); if (aux > distance_value) { distance_value = aux; index = i; } i++; } // add the selected to res and remove from candidate list res.add(new Solution(candidate.remove(index))); } // } return res; }
/** * @param args Command line arguments. * @throws JMException * @throws IOException * @throws SecurityException Usage: three choices - jmetal.metaheuristics.nsgaII.NSGAII_main - * jmetal.metaheuristics.nsgaII.NSGAII_main problemName - * jmetal.metaheuristics.nsgaII.NSGAII_main problemName paretoFrontFile */ public static void main(String[] args) throws JMException, IOException, ClassNotFoundException { Problem problem; // The problem to solve Algorithm algorithm; // The algorithm to use Operator crossover; // Crossover operator Operator mutation; // Mutation operator Operator selection; // Selection operator QualityIndicator indicators; // Object to get quality indicators // Logger object and file to store log messages logger_ = Configuration.logger_; fileHandler_ = new FileHandler("IBEA.log"); logger_.addHandler(fileHandler_); indicators = null; if (args.length == 1) { Object[] params = {"Real"}; problem = (new ProblemFactory()).getProblem(args[0], params); } // if else if (args.length == 2) { Object[] params = {"Real"}; problem = (new ProblemFactory()).getProblem(args[0], params); indicators = new QualityIndicator(problem, args[1]); } // if else { // Default problem problem = new Kursawe("Real", 3); // problem = new Kursawe("BinaryReal", 3); // problem = new Water("Real"); // problem = new ZDT1("ArrayReal", 100); // problem = new ConstrEx("Real"); // problem = new DTLZ1("Real"); // problem = new OKA2("Real") ; } // else algorithm = new IBEA(problem); // Algorithm parameters algorithm.setInputParameter("populationSize", 100); algorithm.setInputParameter("archiveSize", 100); algorithm.setInputParameter("maxEvaluations", 25000); // Mutation and Crossover for Real codification crossover = CrossoverFactory.getCrossoverOperator("SBXCrossover"); crossover.setParameter("probability", 1.0); crossover.setParameter("distribuitionIndex", 20.0); mutation = MutationFactory.getMutationOperator("PolynomialMutation"); mutation.setParameter("probability", 1.0 / problem.getNumberOfVariables()); mutation.setParameter("distributionIndex", 20.0); /* Mutation and Crossover Binary codification */ /* crossover = CrossoverFactory.getCrossoverOperator("SinglePointCrossover"); crossover.setParameter("probability",0.9); mutation = MutationFactory.getMutationOperator("BitFlipMutation"); mutation.setParameter("probability",1.0/80); */ /* Selection Operator */ selection = new BinaryTournament(new FitnessComparator()); // Add the operators to the algorithm algorithm.addOperator("crossover", crossover); algorithm.addOperator("mutation", mutation); algorithm.addOperator("selection", selection); // Execute the Algorithm long initTime = System.currentTimeMillis(); SolutionSet population = algorithm.execute(); long estimatedTime = System.currentTimeMillis() - initTime; // Print the results logger_.info("Total execution time: " + estimatedTime + "ms"); logger_.info("Variables values have been writen to file VAR"); population.printVariablesToFile("VAR"); logger_.info("Objectives values have been writen to file FUN"); population.printObjectivesToFile("FUN"); if (indicators != null) { logger_.info("Quality indicators"); logger_.info("Hypervolume: " + indicators.getHypervolume(population)); logger_.info("GD : " + indicators.getGD(population)); logger_.info("IGD : " + indicators.getIGD(population)); logger_.info("Spread : " + indicators.getSpread(population)); logger_.info("Epsilon : " + indicators.getEpsilon(population)); } // if } // main