Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // NSGAII_main.java
- //
- // Author:
- // Antonio J. Nebro <antonio@lcc.uma.es>
- // Juan J. Durillo <durillo@lcc.uma.es>
- //
- // Copyright (c) 2011 Antonio J. Nebro, Juan J. Durillo
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Lesser General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Lesser General Public License for more details.
- //
- // You should have received a copy of the GNU Lesser General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- package jmetal.metaheuristics.nsgaII;
- import jmetal.core.Algorithm;
- import jmetal.core.Operator;
- import jmetal.core.Problem;
- import jmetal.core.SolutionSet;
- import jmetal.operators.crossover.CrossoverFactory;
- import jmetal.operators.mutation.MutationFactory;
- import jmetal.operators.selection.SelectionFactory;
- import jmetal.problems.Kursawe;
- import jmetal.problems.ProblemFactory;
- import jmetal.problems.ZDT.ZDT3;
- import jmetal.qualityIndicator.QualityIndicator;
- import jmetal.util.Configuration;
- import jmetal.util.JMException;
- import jmetal.util.Edge;
- import java.io.IOException;
- import java.security.SecureRandom;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Random;
- import java.util.logging.FileHandler;
- import java.util.logging.Logger;
- /**
- * Class to configure and execute the NSGA-II algorithm.
- *
- * Besides the classic NSGA-II, a steady-state version (ssNSGAII) is also
- * included (See: J.J. Durillo, A.J. Nebro, F. Luna and E. Alba
- * "On the Effect of the Steady-State Selection Scheme in
- * Multi-Objective Genetic Algorithms"
- * 5th International Conference, EMO 2009, pp: 183-197.
- * April 2009)
- */
- public class NSGAII_main {
- public static Logger logger_ ; // Logger object
- public static FileHandler fileHandler_ ; // FileHandler object
- /**
- * @param args Command line arguments.
- * @throws JMException
- * @throws IOException
- * @throws SecurityException
- * Usage: three options
- * - 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,
- SecurityException,
- 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
- HashMap parameters ; // Operator parameters
- QualityIndicator indicators ; // Object to get quality indicators
- // Logger object and file to store log messages
- logger_ = Configuration.logger_ ;
- fileHandler_ = new FileHandler("NSGAII_main.log");
- logger_.addHandler(fileHandler_) ;
- /* ~ Creazione del grafo ~ */
- // Creazione della lista di archi con relativo peso
- ArrayList<Edge> E = new ArrayList<Edge>();
- E.add(new Edge('A','B',7));
- E.add(new Edge('A','D',5));
- E.add(new Edge('B','D',9));
- E.add(new Edge('B','C',8));
- E.add(new Edge('C','E',5));
- E.add(new Edge('B','E',7));
- E.add(new Edge('D','E',15));
- E.add(new Edge('D','F',6));
- E.add(new Edge('E','F',8));
- E.add(new Edge('E','G',9));
- E.add(new Edge('F','G',11));
- // Creazione della lista di vertici
- ArrayList<Character> V = new ArrayList<Character>();
- V.add('A');
- V.add('B');
- V.add('C');
- V.add('D');
- V.add('E');
- V.add('F');
- V.add('G');
- //creazione di un Gene
- //{0} arco non preso
- //{1} arco considerato
- int bontà=0;
- Integer[][] array_geni = new Integer[100][E.size()];
- for(int k=0; k<100; k++) {
- int number_ones=0;
- Integer[] G = new Integer[E.size()];
- for(int i=0; i<E.size();i++) {
- G[i]=(Math.random()<0.5)?0:1;
- if(G[i] == 1) {
- number_ones++;
- }
- }
- if(number_ones==7)
- bontà++;
- array_geni[k] = G;
- }
- System.out.println("numero di geni buoni: "+bontà);
- indicators = null ;
- // Default problem
- problem = new Kursawe("Real", 3);
- //problem = new Kursawe("BinaryReal", 3);
- //problem = new Water("Real");
- //problem = new ZDT3("ArrayReal", 30);
- //problem = new ConstrEx("Real");
- //problem = new DTLZ1("Real");
- //problem = new OKA2("Real") ;
- System.out.println(problem.getName());
- // else
- algorithm = new NSGAII(problem);
- //algorithm = new ssNSGAII(problem);
- // Algorithm parameters
- algorithm.setInputParameter("populationSize",100);
- algorithm.setInputParameter("maxEvaluations",25000);
- // Mutation and Crossover for Real codification
- parameters = new HashMap() ;
- parameters.put("probability", 0.9) ;
- parameters.put("distributionIndex", 20.0) ;
- crossover = CrossoverFactory.getCrossoverOperator("SBXCrossover", parameters);
- parameters = new HashMap() ;
- parameters.put("probability", 1.0/problem.getNumberOfVariables()) ;
- parameters.put("distributionIndex", 20.0) ;
- mutation = MutationFactory.getMutationOperator("PolynomialMutation", parameters);
- // Selection Operator
- parameters = null ;
- selection = SelectionFactory.getSelectionOperator("BinaryTournament2", parameters) ;
- // Add the operators to the algorithm
- algorithm.addOperator("crossover",crossover);
- algorithm.addOperator("mutation",mutation);
- algorithm.addOperator("selection",selection);
- // Add the indicator object to the algorithm
- algorithm.setInputParameter("indicators", indicators) ;
- // Execute the Algorithm
- long initTime = System.currentTimeMillis();
- SolutionSet population = algorithm.execute();
- long estimatedTime = System.currentTimeMillis() - initTime;
- // Result messages
- 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)) ;
- int evaluations = ((Integer)algorithm.getOutputParameter("evaluations")).intValue();
- logger_.info("Speed : " + evaluations + " evaluations") ;
- } // if
- } //main
- } // NSGAII_main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement