Ejemplo n.º 1
0
  /** Fitness Function */
  void Evaluate() {
    double performance;
    int i, j;

    for (i = 0; i < long_poblacion; i++) {
      /* if the chromosome aren't evaluated, it's evaluate */
      if (New[i].n_e == 1) {
        New[i].Perf = fun_adap.eval(New[i].Gene);
        performance = New[i].Perf;
        New[i].n_e = 0;
        Trials++; /* we increment the number of evaluated chromosomes */

      } else performance = New[i].Perf;

      /* we calculate the position of the best individual */
      if (i == 0) {
        Best_current_perf = performance;
        Best_guy = 0;
      } else if (performance < Best_current_perf) {
        Best_current_perf = performance;
        Best_guy = i;
      }
    }
  }
Ejemplo n.º 2
0
  public void run() {
    int i, j;
    double ec, el, ec_tst, el_tst;

    /* We read the configutate file and we initialize the structures and variables */
    leer_conf();
    if (tabla.salir == false) {
      Gen = 0;

      /* Generation of the initial population */
      alg_gen.Initialize();

      /* Evaluation of the initial population */
      alg_gen.Evaluate();

      Gen++;

      /* Main of the genetic algorithm */
      do {
        /* Interchange of the new and old population */
        alg_gen.Intercambio();

        /* Selection by means of Baker */
        alg_gen.Select();

        /* Crossover */
        if (tipoc > 0) alg_gen.Max_Min_Crossover();
        else alg_gen.Cruce_Multipunto();

        /* Mutation */
        alg_gen.Mutacion();

        /* Elitist selection */
        alg_gen.Elitist();

        /* Evaluation of the current population */
        alg_gen.Evaluate();

        /* we increment the counter */
        Gen++;
      } while (Gen <= n_generaciones);

      /* we calcule the MSEs */
      fun_adap.Decodifica(alg_gen.solucion());
      fun_adap.Error_tra();
      ec = fun_adap.EC;
      el = fun_adap.EL;

      tabla_tst = new MiDataset(fich_datos_tst, false);
      fun_adap.Error_tst(tabla_tst);
      ec_tst = fun_adap.EC;
      el_tst = fun_adap.EL;

      fun_adap.Cubrimientos_Base();

      /* we write the RB */
      cadenaReglas = base_reglas.BRtoString();
      cadenaReglas +=
          "\nMSEtra: "
              + ec
              + "  MSEtst: "
              + ec_tst
              + "\nAverage covering degree: "
              + fun_adap.medcb
              + " Minimum covering degree: "
              + fun_adap.mincb;

      Fichero.escribeFichero(fichero_reglas, cadenaReglas);

      /* we write the obligatory output files*/
      String salida_tra = tabla.getCabecera();
      salida_tra += fun_adap.getSalidaObli(tabla);
      Fichero.escribeFichero(fich_tra_obli, salida_tra);

      String salida_tst = tabla_tst.getCabecera();
      salida_tst += fun_adap.getSalidaObli(tabla_tst);
      Fichero.escribeFichero(fich_tst_obli, salida_tst);

      /* we write the MSEs in specific files */
      Fichero.AnadirtoFichero(ruta_salida + "PcomunR.txt", "" + base_reglas.n_reglas + "\n");
      Fichero.AnadirtoFichero(ruta_salida + "PcomunTRA.txt", "" + ec + "\n");
      Fichero.AnadirtoFichero(ruta_salida + "PcomunTST.txt", "" + ec_tst + "\n");
    }
  }