Beispiel #1
5
  /**
   * 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
Beispiel #2
0
  private static void persistNet(int nodos, int[][] enlaces, String nombre) {

    HashMap<String, Nodo> nodoMap = new HashMap<String, Nodo>();
    Red red = new Red();
    red.setNombre(nombre);

    em.getTransaction().begin();
    for (int i = 1; i <= nodos; i++) {
      Nodo nodo = new Nodo();
      nodo.setLabel("" + i);
      nodoMap.put("" + i, nodo);
      red.addNodo(nodo);
      System.out.println("=>" + nodo.toString());
    }
    em.persist(red);
    em.getTransaction().commit();

    em.getTransaction().begin();
    for (int i = 0; i < enlaces.length; i++) {
      Nodo a = nodoMap.get("" + enlaces[i][0]);
      Nodo b = nodoMap.get("" + enlaces[i][1]);
      CanalOptico canal = new CanalOptico(a, b, numeroFibras, numeroLongitudesDeOnda);
      a.addCanal(canal);
      b.addCanal(canal);
      canal.setCosto(enlaces[i][2]);
      red.addCanal(canal);
    }
    em.persist(red);
    em.getTransaction().commit();
    // casos de prueba;
    prueba_CNnunez10();
    prueba_CNnunez20();
    prueba_CNnunez30();
    prueba_CNnunez40();
  }