コード例 #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
  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);
  }
コード例 #3
0
ファイル: NSGAII.java プロジェクト: patrom/neo
  /**
   * 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
   */
  public SolutionSet execute() throws JMException, ClassNotFoundException {
    int populationSize;
    int maxEvaluations;
    int evaluations;

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

    SolutionSet population;
    SolutionSet offspringPopulation;
    SolutionSet union;

    Distance distance = new Distance();

    // Read the parameters
    populationSize = ((Integer) getInputParameter("populationSize")).intValue();
    maxEvaluations = ((Integer) getInputParameter("maxEvaluations")).intValue();
    indicators = (QualityIndicator) getInputParameter("indicators");

    // Initialize the variables
    population = new SolutionSet(populationSize);
    evaluations = 0;
    requiredEvaluations = 0;

    // Read the operators
    List<Operator> mutationOperators = new ArrayList<Operator>();
    mutationOperators.add(operators_.get("oneNoteMutation"));
    mutationOperators.add(operators_.get("harmonyNoteToPitch"));
    mutationOperators.add(operators_.get("swapHarmonyNotes"));
    mutationOperators.add(operators_.get("melodyNoteToHarmonyNote"));
    mutationOperators.add(operators_.get("pitchSpaceMutation"));
    Operator crossoverOperator = operators_.get("crossover");
    Operator selectionOperator = operators_.get("selection");

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

    int changeCount = 0;

    // Generations ...
    // while ((evaluations < maxEvaluations) && changeCount < 10 *
    // populationSize) {
    while ((evaluations < maxEvaluations)) {
      List<Solution> copySolutions = copyList(population);
      // Create the offSpring solutionSet
      offspringPopulation = new SolutionSet(populationSize);
      Solution[] parents = new Solution[2];
      for (int i = 0; i < (populationSize / 2); i++) {
        if (evaluations < maxEvaluations) {
          // obtain parents
          parents[0] = (Solution) selectionOperator.execute(population);
          parents[1] = (Solution) selectionOperator.execute(population);
          Solution[] offSpring = (Solution[]) crossoverOperator.execute(parents);
          for (Operator operator : mutationOperators) {
            operator.execute(offSpring[0]);
            operator.execute(offSpring[1]);
          }
          if (decorated) {
            decorator.decorate(offSpring[0]);
            decorator.decorate(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;
        }
      }

      // 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));
        }

        // Decrement remain
        remain = remain - front.size();

        // Obtain the next front
        index++;
        if (remain > 0) {
          front = ranking.getSubfront(index);
        }
      }

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

        remain = 0;
      }

      // 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);
        double p = indicators.getTrueParetoFrontHypervolume();
        if (HV >= (0.98 * indicators.getTrueParetoFrontHypervolume())) {
          requiredEvaluations = evaluations;
        }
      }
      // check changes pareto front
      // SolutionSet paretoFront = ranking.getSubfront(0);
      List<Solution> frontSolutions = copyList(population);
      boolean changed = hasPopulationChanged(copySolutions, frontSolutions);
      LOGGER.fine(
          "Population changed: "
              + changed
              + ", evaluations: "
              + evaluations
              + ", change count:"
              + changeCount);
      if (changed) {
        changeCount = 0;
      } else {
        changeCount++;
      }
    }

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

    // Return the first non-dominated front
    Ranking ranking = new Ranking(population);
    return ranking.getSubfront(0);
  }