コード例 #1
5
ファイル: NSGAII_G10.java プロジェクト: deserver/qopmo_p234
  /**
   * Runs 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
   */
  @Test
  public Poblacion execute() throws JMException, ClassNotFoundException {
    int populationSize;
    int maxEvaluations;
    int evaluations;
    int probMutacion;
    int nrocaso;
    int corridas;

    int requiredEvaluations; // Use in the example of use of the
    // indicators object (see below)

    // Poblacion population;
    // SolutionSet population;
    Poblacion offspringPopulation;
    Poblacion union;
    Poblacion copyPopulation;

    Operator mutationOperator;
    Operator crossoverOperator;
    Operator selectionOperator;

    Distance distance = new Distance();

    // Read the parameters
    populationSize = ((Integer) getInputParameter("populationSize")).intValue();
    maxEvaluations = ((Integer) getInputParameter("maxEvaluations")).intValue();
    probMutacion = ((Integer) getInputParameter("probMutacion")).intValue();
    nrocaso = ((Integer) getInputParameter("nrocaso")).intValue();
    corridas = ((Integer) getInputParameter("corridas")).intValue();

    // Initialize the variables
    // population = new SolutionSet(populationSize);
    evaluations = 0;
    caso = casosDePrueba[nrocaso];

    // Initialize Nsfnet
    NSFNET = em.find(Red.class, 1); // NSFnet
    NSFNET.inicializar();

    long time_start, time_end = 0;
    // captura tiempo Inicial
    time_start = System.currentTimeMillis();
    // 0. Obtener Poblacion Inicial
    this.obtenerPoblacion(populationSize);

    requiredEvaluations = 0;

    // Read the operators
    // mutationOperator = operators_.get("mutation");
    // crossoverOperator = operators_.get("crossover");
    // selectionOperator = operators_.get("selection");
    OperadorSeleccion seleccionOp = new TorneoBinario();

    // Create the initial solutionSet
    Solution newSolution;
    // population = new Poblacion(populationSize);
    for (int i = 0; i < populationSize; i++) {
      newSolution = new Solution(problem_);
      problem_.evaluate(newSolution);
      problem_.evaluateConstraints(newSolution);
      evaluations++;
      population.add(newSolution);
    } // for

    Ranking ranking = new Ranking(population);
    evaluations = 0;
    int cantIt = 0;
    int size, tamanho = 0;
    // Generations

    System.out.println(caso + "-" + corridas + " Test Genetico.");

    while (evaluations < tiempoTotal[nrocaso]) {

      size = population.getIndividuos().size();
      tamanho = (size * size) + size;
      offspringPopulation = new Poblacion(populationSize * populationSize + populationSize);
      copyPopulation = new Poblacion(populationSize * populationSize + populationSize);
      // offspringPopulation.copiarPoblacion(population);;

      // for (int i = 0; i < (populationSize); i++) {
      if (evaluations < tiempoTotal[nrocaso]) {

        for (Individuo ind : population.getIndividuos()) {
          Solution s = (Solution) ind;
          s.setNumberOfObjectives(problem_.getNumberOfObjectives());
          problem_.evaluate(s);
          // if (s.getCosto()<2.8)
          // System.out.println(s.getCosto());
          if (s.getCosto() > 0) offspringPopulation.add(s);
        }

        Collection<Individuo> selectos = seleccionOp.seleccionar(population);

        population.cruzar(selectos, probMutacion);
        /*copyPopulation.copiarPoblacion(offspringPopulation);
        Poblacion mejores = getFront(copyPopulation);
        population.siguienteGeneracion(mejores);*/
        population.siguienteGeneracion();

        evaluations++;
      } // if

      // } // for

      for (Individuo ind : population.getIndividuos()) {
        Solution s = (Solution) ind;
        s.setNumberOfObjectives(problem_.getNumberOfObjectives());
        problem_.evaluate(s);
        if (s.getCosto() > 0) offspringPopulation.add(s);
      }
      // Create the solutionSet union of solutionSet and offSpring
      // union = ((SolutionSet) population).union(offspringPopulation);

      union = offspringPopulation.union(population);
      // Ranking the union
      ranking = new Ranking(union);

      int remain = populationSize;
      int index = 0;
      Poblacion front = null;
      population.clear();

      // Obtain the next front
      front = ranking.getSubfront(index);

      while (front != null && (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

      if (front != null) {
        // 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 CrowdingComparator());
          for (int k = 0; k < remain; k++) {
            population.add(front.get(k));
          } // for
        }
        remain = 0;
      } // if

      // This piece of code shows how to use the indicator object into the code
      // of NSGA-II. In particular, it finds the number of evaluations required
      // by the algorithm to obtain a Pareto front with a hypervolume higher
      // than the hypervolume of the true Pareto front.
      /*if ((indicators != null) &&
          (requiredEvaluations == 0)) {
        double HV = indicators.getHypervolume(population);
        if (HV >= (0.98 * indicators.getTrueParetoFrontHypervolume())) {
          requiredEvaluations = evaluations;
        } // if
      } // if*/

      if (evaluations % maxEvaluations == 0) {
        System.out.println();
        System.out.print("Población Nro: " + evaluations + " ");
        // System.out.println("MEJOR--> " + p.getMejor().toString());
        System.out.print("Costo-MEJOR==> ");
        ranking = new Ranking(population);
        if (ranking.getSubfront(0) != null) {
          ranking.getSubfront(0).printParcialResults();
          ranking.getSubfront(0).printVariablesToFile("VAR_p3" + "_" + caso);
        }
        // ((Solution) p.getMejor()).imprimirCosto();
      }
    } // while
    cantIt++;

    // captura tiempo final
    // time_end = System.currentTimeMillis();
    // Calculo del Tiempo
    /*long tiempo = time_end - time_start;
    long hora = tiempo / 3600000;
    long restohora = tiempo % 3600000;
    long minuto = restohora / 60000;
    long restominuto = restohora % 60000;
    long segundo = restominuto / 1000;
    long restosegundo = restominuto % 1000;
    String time = hora + ":" + minuto + ":" + segundo + "." + restosegundo;
    time = " Tiempo: " + time;
    String fin = caso + " FIN - Test Genetico. Tiempo:" + time;
    fin += " - Nº Generaciones: " + evaluations;
    System.out.println(fin);
    */
    System.out.println("Evaluaciones: " + evaluations);

    // Return as output parameter the required evaluations
    setOutputParameter("evaluations", requiredEvaluations);

    // Return the first non-dominated front
    ranking = new Ranking(population);
    if (ranking.getSubfront(0) != null) ranking.getSubfront(0).printFeasibleFUN("FUN_NSGAII");

    return ranking.getSubfront(0);
  } // execute
コード例 #2
0
ファイル: NSGAII_G10.java プロジェクト: deserver/qopmo_p234
  /*
   * Obtiene la población Inicial a partir de la Prueba cargada.
   */
  private void obtenerPoblacion(int tamanho) {

    // 0. Obtener individuos Iniciales.
    Set<Individuo> individuos = this.obtenerPrueba(tamanho);

    // 1. Se crea la Poblacion Inicial con los individuos iniciales.
    population = new Poblacion(individuos, tamanho);
    // 2. Se carga la Red en la Poblacion.
    Poblacion.setRed(NSFNET);
    // 3. Se generan los caminos de la poblacion inicial.
    population.generarPoblacion(esquema);
    // 4. Se imprime la Poblacion Inicial
    // System.out.println(p.toString());
  } // obtenerPoblacion
コード例 #3
0
  /**
   * @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

    // Logger object and file to store log messages
    // logger_      = Configuration.logger_ ;
    fileHandler_ = new FileHandler("NSGAII_main.log");
    // logger_.addHandler(fileHandler_) ;

    problem = new QOP();
    int corridas;
    String caso;
    nrocaso = 0;
    Poblacion population = new Poblacion(50);
    while (nrocaso < casosDePrueba.length) {
      corridas = 1;
      long initTime2 = System.currentTimeMillis();
      while (corridas < 11) {
        algorithm = new NSGAII_G10(problem, nrocaso);
        caso = casosDePrueba[nrocaso];

        // algorithm = new ssNSGAII(problem);

        // Algorithm parameters
        algorithm.setInputParameter("populationSize", 5);
        algorithm.setInputParameter("maxEvaluations", 2500);
        algorithm.setInputParameter("probMutacion", 1); // 10%
        algorithm.setInputParameter("nrocaso", nrocaso);
        algorithm.setInputParameter("corridas", corridas);

        // 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);*/
        // algorithm.addOperator("torneobinario", torneobinario);

        // Add the indicator object to the algorithm

        // System.out.println(" "+corridas);
        // Execute the Algorithm
        long initTime = System.currentTimeMillis();
        // System.out.println(caso + "-" + corridas + " Test Genetico.");

        population = algorithm.execute();
        long estimatedTime = System.currentTimeMillis() - initTime;

        // Result messages
        if (population != null) {
          // logger_.info("Total execution time: "+estimatedTime + "ms");
          // logger_.info("Variables values have been writen to file VAR");
          population.printVariablesToFile("VAR_p3" + caso);
          // logger_.info("Objectives values have been writen to file FUN");
          population.printObjectivesToFile("FUN_p3" + caso);
        } else {
          System.out.println("No arrojo resultados");
        }

        /*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*/
        corridas++;
      }
      long estimatedTime2 = System.currentTimeMillis() - initTime2;
      long tiempo = estimatedTime2;
      long hora = tiempo / 3600000;
      long restohora = tiempo % 3600000;
      long minuto = restohora / 60000;
      long restominuto = restohora % 60000;
      long segundo = restominuto / 1000;
      long restosegundo = restominuto % 1000;
      String time = hora + ":" + minuto + ":" + segundo + "." + restosegundo;
      time = " Tiempo: " + time;
      String fin = casosDePrueba[nrocaso] + " FIN - Test Genetico. Tiempo:" + time;
      // fin += " - Nº Generaciones: " + evaluations;
      System.out.println(fin);
      if (population != null) population.printFinalResults();

      nrocaso++;
    }

    System.out.println("FIN Prueba Algoritmo Genetico. (Segment-Oriented).");
  } // main
コード例 #4
0
ファイル: NSGAII_G10.java プロジェクト: deserver/qopmo_p234
  public Poblacion getFront(Poblacion population) {
    Ranking ranking = new Ranking(population);
    Distance distance = new Distance();

    int remain = population.size();
    int index = 0;
    Poblacion front = null;
    Poblacion poblacion = new Poblacion(population.size());

    front = ranking.getSubfront(index);

    while (front != null && (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++) {
        poblacion.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

    if (front != null) {
      // 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 CrowdingComparator());
        for (int k = 0; k < remain; k++) {
          poblacion.add(front.get(k));
        } // for
      }
      remain = 0;
    } // if

    return ranking.getSubfront(0);
  }