Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.38 KB | None | 0 0
  1. //  NSGAII_main.java
  2. //
  3. //  Author:
  4. //       Antonio J. Nebro <antonio@lcc.uma.es>
  5. //       Juan J. Durillo <durillo@lcc.uma.es>
  6. //
  7. //  Copyright (c) 2011 Antonio J. Nebro, Juan J. Durillo
  8. //
  9. //  This program is free software: you can redistribute it and/or modify
  10. //  it under the terms of the GNU Lesser General Public License as published by
  11. //  the Free Software Foundation, either version 3 of the License, or
  12. //  (at your option) any later version.
  13. //
  14. //  This program is distributed in the hope that it will be useful,
  15. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. //  GNU Lesser General Public License for more details.
  18. //
  19. //  You should have received a copy of the GNU Lesser General Public License
  20. //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  21.  
  22. package jmetal.metaheuristics.nsgaII;
  23.  
  24. import jmetal.core.Algorithm;
  25. import jmetal.core.Operator;
  26. import jmetal.core.Problem;
  27. import jmetal.core.SolutionSet;
  28. import jmetal.operators.crossover.CrossoverFactory;
  29. import jmetal.operators.mutation.MutationFactory;
  30. import jmetal.operators.selection.SelectionFactory;
  31. import jmetal.problems.Kursawe;
  32. import jmetal.problems.ProblemFactory;
  33. import jmetal.problems.ZDT.ZDT3;
  34. import jmetal.qualityIndicator.QualityIndicator;
  35. import jmetal.util.Configuration;
  36. import jmetal.util.JMException;
  37. import jmetal.util.Edge;
  38.  
  39. import java.io.IOException;
  40. import java.security.SecureRandom;
  41. import java.util.ArrayList;
  42. import java.util.HashMap;
  43. import java.util.Random;
  44. import java.util.logging.FileHandler;
  45. import java.util.logging.Logger;
  46.  
  47. /**
  48.  * Class to configure and execute the NSGA-II algorithm.  
  49.  *    
  50.  * Besides the classic NSGA-II, a steady-state version (ssNSGAII) is also
  51.  * included (See: J.J. Durillo, A.J. Nebro, F. Luna and E. Alba
  52.  *                  "On the Effect of the Steady-State Selection Scheme in
  53.  *                  Multi-Objective Genetic Algorithms"
  54.  *                  5th International Conference, EMO 2009, pp: 183-197.
  55.  *                  April 2009)
  56.  */
  57.  
  58. public class NSGAII_main {
  59.   public static Logger      logger_ ;      // Logger object
  60.   public static FileHandler fileHandler_ ; // FileHandler object
  61.  
  62.   /**
  63.    * @param args Command line arguments.
  64.    * @throws JMException
  65.    * @throws IOException
  66.    * @throws SecurityException
  67.    * Usage: three options
  68.    *      - jmetal.metaheuristics.nsgaII.NSGAII_main
  69.    *      - jmetal.metaheuristics.nsgaII.NSGAII_main problemName
  70.    *      - jmetal.metaheuristics.nsgaII.NSGAII_main problemName paretoFrontFile
  71.    */
  72.   public static void main(String [] args) throws
  73.                                   JMException,
  74.                                   SecurityException,
  75.                                   IOException,
  76.                                   ClassNotFoundException {
  77.     Problem   problem   ; // The problem to solve
  78.     Algorithm algorithm ; // The algorithm to use
  79.     Operator  crossover ; // Crossover operator
  80.     Operator  mutation  ; // Mutation operator
  81.     Operator  selection ; // Selection operator
  82.    
  83.     HashMap  parameters ; // Operator parameters
  84.    
  85.     QualityIndicator indicators ; // Object to get quality indicators
  86.  
  87.     // Logger object and file to store log messages
  88.     logger_      = Configuration.logger_ ;
  89.     fileHandler_ = new FileHandler("NSGAII_main.log");
  90.     logger_.addHandler(fileHandler_) ;
  91.    
  92.     /* ~ Creazione del grafo ~ */
  93.    
  94.     // Creazione della lista di archi con relativo peso
  95.     ArrayList<Edge> E = new ArrayList<Edge>();
  96.     E.add(new Edge('A','B',7));
  97.     E.add(new Edge('A','D',5));
  98.     E.add(new Edge('B','D',9));
  99.     E.add(new Edge('B','C',8));
  100.     E.add(new Edge('C','E',5));
  101.     E.add(new Edge('B','E',7));
  102.     E.add(new Edge('D','E',15));
  103.     E.add(new Edge('D','F',6));
  104.     E.add(new Edge('E','F',8));
  105.     E.add(new Edge('E','G',9));
  106.     E.add(new Edge('F','G',11));
  107.    
  108.     // Creazione della lista di vertici
  109.     ArrayList<Character> V = new ArrayList<Character>();
  110.     V.add('A');
  111.     V.add('B');
  112.     V.add('C');
  113.     V.add('D');
  114.     V.add('E');
  115.     V.add('F');
  116.     V.add('G');
  117.    
  118.     //creazione di un Gene
  119.    
  120.     //{0} arco non preso
  121.     //{1} arco considerato
  122.     int bontà=0;
  123.     Integer[][] array_geni = new Integer[100][E.size()];
  124.  
  125.     for(int k=0; k<100; k++) { 
  126.         int number_ones=0;
  127.         Integer[] G = new Integer[E.size()];
  128.         for(int i=0; i<E.size();i++) {
  129.             G[i]=(Math.random()<0.5)?0:1;
  130.             if(G[i] == 1) {
  131.                 number_ones++;
  132.             }
  133.         }
  134.         if(number_ones==7)
  135.             bontà++;
  136.         array_geni[k] = G;
  137.     }
  138.  
  139.     System.out.println("numero di geni buoni: "+bontà);
  140.  
  141.    
  142.        
  143.       indicators = null ;
  144.       // Default problem
  145.       problem = new Kursawe("Real", 3);
  146.       //problem = new Kursawe("BinaryReal", 3);
  147.       //problem = new Water("Real");
  148.       //problem = new ZDT3("ArrayReal", 30);
  149.       //problem = new ConstrEx("Real");
  150.       //problem = new DTLZ1("Real");
  151.       //problem = new OKA2("Real") ;
  152.       System.out.println(problem.getName());
  153.        // else
  154.    
  155.     algorithm = new NSGAII(problem);
  156.     //algorithm = new ssNSGAII(problem);
  157.  
  158.     // Algorithm parameters
  159.     algorithm.setInputParameter("populationSize",100);
  160.     algorithm.setInputParameter("maxEvaluations",25000);
  161.  
  162.     // Mutation and Crossover for Real codification
  163.     parameters = new HashMap() ;
  164.     parameters.put("probability", 0.9) ;
  165.     parameters.put("distributionIndex", 20.0) ;
  166.     crossover = CrossoverFactory.getCrossoverOperator("SBXCrossover", parameters);                  
  167.  
  168.     parameters = new HashMap() ;
  169.     parameters.put("probability", 1.0/problem.getNumberOfVariables()) ;
  170.     parameters.put("distributionIndex", 20.0) ;
  171.     mutation = MutationFactory.getMutationOperator("PolynomialMutation", parameters);                    
  172.  
  173.     // Selection Operator
  174.     parameters = null ;
  175.     selection = SelectionFactory.getSelectionOperator("BinaryTournament2", parameters) ;                          
  176.  
  177.     // Add the operators to the algorithm
  178.     algorithm.addOperator("crossover",crossover);
  179.     algorithm.addOperator("mutation",mutation);
  180.     algorithm.addOperator("selection",selection);
  181.  
  182.     // Add the indicator object to the algorithm
  183.     algorithm.setInputParameter("indicators", indicators) ;
  184.    
  185.     // Execute the Algorithm
  186.     long initTime = System.currentTimeMillis();
  187.     SolutionSet population = algorithm.execute();
  188.     long estimatedTime = System.currentTimeMillis() - initTime;
  189.    
  190.     // Result messages
  191.     logger_.info("Total execution time: "+estimatedTime + "ms");
  192.     logger_.info("Variables values have been writen to file VAR");
  193.     population.printVariablesToFile("VAR");    
  194.     logger_.info("Objectives values have been writen to file FUN");
  195.     population.printObjectivesToFile("FUN");
  196.  
  197.     if (indicators != null) {
  198.       logger_.info("Quality indicators") ;
  199.       logger_.info("Hypervolume: " + indicators.getHypervolume(population)) ;
  200.       logger_.info("GD         : " + indicators.getGD(population)) ;
  201.       logger_.info("IGD        : " + indicators.getIGD(population)) ;
  202.       logger_.info("Spread     : " + indicators.getSpread(population)) ;
  203.       logger_.info("Epsilon    : " + indicators.getEpsilon(population)) ;  
  204.      
  205.       int evaluations = ((Integer)algorithm.getOutputParameter("evaluations")).intValue();
  206.       logger_.info("Speed      : " + evaluations + " evaluations") ;      
  207.     } // if
  208.   } //main
  209. } // NSGAII_main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement