Ejemplo n.º 1
0
  /**
   * PMX cross operator
   *
   * @param poblacion Population of chromosomes
   * @param newPob New population
   * @param sel1 First parent
   * @param sel2 Second parent
   */
  public void crucePMX(Cromosoma poblacion[], Cromosoma newPob[], int sel1, int sel2) {

    int e1, e2;
    int limSup, limInf;
    int i;
    boolean temp[];

    temp = new boolean[datosTrain.length];
    e1 = Randomize.Randint(0, datosTrain.length - 1);
    e2 = Randomize.Randint(0, datosTrain.length - 1);
    if (e1 > e2) {
      limSup = e1;
      limInf = e2;
    } else {
      limSup = e2;
      limInf = e1;
    }

    for (i = 0; i < datosTrain.length; i++) {
      if (i < limInf || i > limSup) temp[i] = poblacion[sel1].getGen(i);
      else temp[i] = poblacion[sel2].getGen(i);
    }
    newPob[0] = new Cromosoma(temp);

    for (i = 0; i < datosTrain.length; i++) {
      if (i < limInf || i > limSup) temp[i] = poblacion[sel2].getGen(i);
      else temp[i] = poblacion[sel1].getGen(i);
    }
    newPob[1] = new Cromosoma(temp);
  } // end-method
Ejemplo n.º 2
0
  /** Applies a tournament selection, with tournament size of 2 */
  public void tournament_selection() {
    int i, j, k, mejor_torneo;

    int tam_torneo = 2;
    int Torneo[] = new int[tam_torneo];
    boolean repetido;
    RuleSet sample[] = new RuleSet[long_poblacion];

    for (i = 0; i < long_poblacion; i++) {

      Torneo[0] = Randomize.Randint(0, long_poblacion);
      mejor_torneo = Torneo[0];

      for (j = 1; j < tam_torneo; j++) {
        do {
          Torneo[j] = Randomize.Randint(0, long_poblacion);
          repetido = false;
          k = 0;
          while ((k < j) && (!repetido)) {
            if (Torneo[j] == Torneo[k]) repetido = true;
            else k++;
          }
        } while (repetido);

        if (poblacion[Torneo[j]].fitness > poblacion[mejor_torneo].fitness)
          mejor_torneo = Torneo[j];
      }

      sample[i] = new RuleSet(poblacion[mejor_torneo]);
    }
    previousPob = poblacion;
    poblacion = sample;
  }
Ejemplo n.º 3
0
  /**
   * It calculates the cross HUX
   *
   * @param Padre1 it is a father to cross
   * @param Padre2 it is the other father to do the cross
   * @param Hijo1 the son obtained
   * @param Hijo2 the other obtained
   */
  void HUX(char[] Padre1, char[] Padre2, char[] Hijo1, char[] Hijo2) {

    int j, distintos, pos, intercambios;
    char px, py, temp;

    if (flag3 == 1) {
      posiciones = new int[n_reglas_total];
      flag3 = 0;
    }

    distintos = 0;
    for (j = 0; j < n_reglas_total; j++) {
      px = Padre1[j];
      py = Padre2[j];
      Hijo1[j] = px;
      Hijo2[j] = py;
      if (px != py) {
        posiciones[distintos] = j;
        distintos++;
      }
    }
    intercambios = distintos / 2;
    if (distintos > 0 && intercambios == 0) intercambios = 1;
    for (j = 0; j < intercambios; j++) {
      pos = Randomize.Randint(0, --distintos);
      temp = Hijo1[posiciones[pos]];
      Hijo1[posiciones[pos]] = Hijo2[posiciones[pos]];
      Hijo2[posiciones[pos]] = temp;
      posiciones[pos] = posiciones[distintos];
    }
  }
Ejemplo n.º 4
0
  public void TournamentSelection() {

    int i, j, winner, candidate;

    for (i = 0; i < popSize; i++) {
      // There can be only one
      winner = Randomize.Randint(0, popSize);

      for (j = 1; j < tournamentSize; j++) {
        candidate = Randomize.Randint(0, popSize);
        if (population[candidate].compareToIndividual(
                population[winner], Parameters.optimizationMethod)
            > 0) {
          winner = candidate;
        }
      }

      offspringPopulation[i] = cf.cloneClassifier(population[winner]);
    }
  }
Ejemplo n.º 5
0
  /**
   * Shuffles a vector of differences
   *
   * @param diff Vector of differences
   * @param index Final position of the vector
   */
  private void shuffleDiff(int diff[], int index) {

    int pos, tmp;

    for (int i = 0; i < index; i++) {

      pos = Randomize.Randint(0, index);
      tmp = diff[i];
      diff[i] = diff[pos];
      diff[pos] = tmp;
    }
  } // end-method
Ejemplo n.º 6
0
  /**
   * Constructor with parameters. It creates a random general rule
   *
   * @param database Data Base associated to the general rule base that includes this rule
   * @param n_classes Number of classes in the training set
   * @param tnorm T-norm used to compute the compatibility degree
   * @param tconorm T-conorm used to compute the compatibility degree
   * @param rule_weight Way of computing the rule weight
   */
  public Rule(DataBase database, int n_classes, int tnorm, int tconorm, int rule_weight) {
    int length_antecedent;
    boolean[] selected_antecedent = new boolean[database.numVariables()];

    antecedent = new ArrayList<FuzzyAntecedent>(database.numVariables());
    for (int i = 0; i < database.numVariables(); i++) {
      selected_antecedent[i] = false;
    }
    do {
      // Select randomly some variables of the database and create randomly its antecedent
      length_antecedent = Randomize.RandintClosed(1, database.numVariables());

      for (int i = 0; i < length_antecedent; i++) {
        int var_selected;
        FuzzyAntecedent new_antecedent;

        do {
          var_selected = Randomize.Randint(0, database.numVariables());
        } while (selected_antecedent[var_selected]);

        new_antecedent = new FuzzyAntecedent(database, var_selected);

        if (new_antecedent.isValid()) {
          selected_antecedent[var_selected] = true;
          antecedent.add((FuzzyAntecedent) new_antecedent);
        }
      }

    } while (antecedent.size() == 0);

    t_norm = tnorm;
    t_conorm = tconorm;
    ruleWeight = rule_weight;
    level = 1;
    clas = Randomize.Randint(0, n_classes);
    n_e = true;
  }
Ejemplo n.º 7
0
  private void interpola(
      double ra[],
      double rb[],
      int na[],
      int nb[],
      boolean ma[],
      boolean mb[],
      double resS[],
      double resR[],
      int resN[],
      boolean resM[]) {

    int i;
    double diff;
    double gap;
    int suerte;

    for (i = 0; i < ra.length; i++) {
      if (ma[i] == true && mb[i] == true) {
        resM[i] = true;
        resS[i] = 0;
      } else {
        resM[i] = false;
        if (entradas[i].getType() == Attribute.REAL) {
          diff = rb[i] - ra[i];
          gap = Randomize.Rand();
          resR[i] = ra[i] + gap * diff;
          resS[i] =
              (ra[i] + entradas[i].getMinAttribute())
                  / (entradas[i].getMaxAttribute() - entradas[i].getMinAttribute());
        } else if (entradas[i].getType() == Attribute.INTEGER) {
          diff = rb[i] - ra[i];
          gap = Randomize.Rand();
          resR[i] = Math.round(ra[i] + gap * diff);
          resS[i] =
              (ra[i] + entradas[i].getMinAttribute())
                  / (entradas[i].getMaxAttribute() - entradas[i].getMinAttribute());
        } else {
          suerte = Randomize.Randint(0, 2);
          if (suerte == 0) {
            resN[i] = na[i];
          } else {
            resN[i] = nb[i];
          }
          resS[i] = (double) resN[i] / (double) (entradas[i].getNominalValuesList().size() - 1);
        }
      }
    }
  }
Ejemplo n.º 8
0
  /** Deletes a variable from the fuzzy antecedent set of this rule */
  public void deleteVar() {
    int selected_var;

    // Select a variable in the fuzzy antecedent
    selected_var = Randomize.Randint(0, antecedent.size());

    // Delete this label from the Fuzzy set
    antecedent.remove(selected_var);

    weight = 0.0;
    raw_fitness = 0.0;
    penalized_fitness = -1.0;
    n_e = true;
    ideal = 0;
    level = 1;
  }
Ejemplo n.º 9
0
  /**
   * One-point crossover
   *
   * @param cr1 index of parent 1 in poblation
   * @param cr2 index of parent 2 in poblation
   */
  public void onePointCrossover(int cr1, int cr2) {
    RuleSet rule1 = poblacion[cr1];
    RuleSet rule2 = poblacion[cr2];

    // there are 3*number of attribute elements, plus class value in each cromosome
    int cutpoint = Randomize.Randint(0, n_genes);
    int cutpoint_rule = cutpoint / (3 * nAtt + 1);
    int cutpoint_variable = cutpoint % (3 * nAtt + 1);

    // rule1 is replaced from cutpoint (inclusive) to the end of his rule set
    rule1.copyFromPointtoEnd(rule2, cutpoint_rule, cutpoint_variable);
    // rule2 is replaced from the begining of his rule set to cutpoint (not inclusive)
    rule2.copyFromBegintoPoint(rule1, cutpoint_rule, cutpoint_variable);
    // childs must be evaluated
    rule1.setEvaluated(false);
    rule2.setEvaluated(false);
  }
Ejemplo n.º 10
0
  /**
   * Adds a new variable to the fuzzy antecedent set of this rule
   *
   * @param data Data Base associated to the general rule base that includes this rule
   */
  public void addVar(DataBase data) {
    boolean found_var = false;
    int selected_var;

    if (antecedent.size() >= data.numVariables()) {
      System.err.println(
          "We cannot add a new var to this rule since it has all the possible vars in it");
      System.exit(-1);
    }

    // Find a label that we do not have in the label set yet
    do {
      selected_var = Randomize.Randint(0, data.numVariables());
      found_var = false;

      for (int i = 0; i < antecedent.size() && !found_var; i++) {
        if (((FuzzyAntecedent) antecedent.get(i)).getAttribute() == selected_var) {
          found_var = true;
        }
      }
    } while (found_var);

    // Add this variable to the Fuzzy set
    FuzzyAntecedent new_antecedent;
    do {
      new_antecedent = new FuzzyAntecedent(data, selected_var);
    } while (!new_antecedent.isValid());
    antecedent.add(new_antecedent);

    weight = 0.0;
    raw_fitness = 0.0;
    penalized_fitness = -1.0;
    n_e = true;
    ideal = 0;
    level = 1;
  }
Ejemplo n.º 11
0
  public void ejecutar() {

    int i, j, l, m;
    double alfai;
    int nClases;

    int claseObt;

    boolean marcas[];
    boolean notFound;

    int init;
    int clasSel[];

    int baraje[];

    int pos, tmp;
    String instanciasIN[];
    String instanciasOUT[];

    long tiempo = System.currentTimeMillis();

    /* Getting the number of differents classes */

    nClases = 0;

    for (i = 0; i < clasesTrain.length; i++) if (clasesTrain[i] > nClases) nClases = clasesTrain[i];

    nClases++;

    /* Shuffle the train set */

    baraje = new int[datosTrain.length];

    Randomize.setSeed(semilla);

    for (i = 0; i < datosTrain.length; i++) baraje[i] = i;

    for (i = 0; i < datosTrain.length; i++) {

      pos = Randomize.Randint(i, datosTrain.length - 1);

      tmp = baraje[i];

      baraje[i] = baraje[pos];

      baraje[pos] = tmp;
    }

    /*
     * Inicialization of the flagged instaces vector for a posterior
     * elimination
     */

    marcas = new boolean[datosTrain.length];

    for (i = 0; i < datosTrain.length; i++) marcas[i] = false;

    if (datosTrain.length > 0) {

      // marcas[baraje[0]] = true; //the first instance is included always

      nSel = n_p;
      if (nSel < nClases) nSel = nClases;

    } else {

      System.err.println("Input dataset is empty");

      nSel = 0;
    }
    clasSel = new int[nClases];
    System.out.print("Selecting initial neurons... ");
    // at least, there must be 1 neuron of each class at the beginning
    init = nClases;
    for (i = 0; i < nClases && i < datosTrain.length; i++) {
      pos = Randomize.Randint(0, datosTrain.length - 1);
      tmp = 0;
      while ((clasesTrain[pos] != i || marcas[pos]) && tmp < datosTrain.length) {
        pos = (pos + 1) % datosTrain.length;
        tmp++;
      }
      if (tmp < datosTrain.length) marcas[pos] = true;
      else init--;
      // clasSel[i] = i;
    }
    for (i = init; i < Math.min(nSel, datosTrain.length); i++) {
      tmp = 0;
      pos = Randomize.Randint(0, datosTrain.length - 1);
      while (marcas[pos]) {
        pos = (pos + 1) % datosTrain.length;
        tmp++;
      }
      // if(i<nClases){
      // notFound = true;
      // do{
      // for(j=i-1;j>=0 && notFound;j--){
      // if(clasSel[j] == clasesTrain[pos])
      // notFound = false;
      // }
      // if(!notFound)
      // pos = Randomize.Randint (0, datosTrain.length-1);
      // }while(!notFound);
      // }
      // clasSel[i] = clasesTrain[pos];
      marcas[pos] = true;
      init++;
    }
    nSel = init;
    System.out.println("Initial neurons selected: " + nSel);

    /* Building of the S set from the flags */

    conjS = new double[nSel][datosTrain[0].length];

    clasesS = new int[nSel];

    for (m = 0, l = 0; m < datosTrain.length; m++) {

      if (marcas[m]) { // the instance must be copied to the solution

        for (j = 0; j < datosTrain[0].length; j++) {

          conjS[l][j] = datosTrain[m][j];
        }

        clasesS[l] = clasesTrain[m];

        l++;
      }
    }

    alfai = alpha;
    boolean change = true;
    /* Body of the LVQ algorithm. */

    // Train the network
    for (int it = 0; it < T && change; it++) {
      change = false;
      alpha = alfai;
      for (i = 1; i < datosTrain.length; i++) {
        // search for the nearest neuron to training instance
        pos = NN(nSel, conjS, datosTrain[baraje[i]]);
        // nearest neuron labels correctly the class of training
        // instance?

        if (clasesS[pos] != clasesTrain[baraje[i]]) { // NO - repel
          // the neuron
          for (j = 0; j < conjS[pos].length; j++) {
            conjS[pos][j] = conjS[pos][j] - alpha * (datosTrain[baraje[i]][j] - conjS[pos][j]);
          }
          change = true;
        } else { // YES - migrate the neuron towards the input vector
          for (j = 0; j < conjS[pos].length; j++) {
            conjS[pos][j] = conjS[pos][j] + alpha * (datosTrain[baraje[i]][j] - conjS[pos][j]);
          }
        }
        alpha = nu * alpha;
      }
      // Shuffle again the training partition
      baraje = new int[datosTrain.length];

      for (i = 0; i < datosTrain.length; i++) baraje[i] = i;

      for (i = 0; i < datosTrain.length; i++) {

        pos = Randomize.Randint(i, datosTrain.length - 1);

        tmp = baraje[i];

        baraje[i] = baraje[pos];

        baraje[pos] = tmp;
      }
    }
    System.out.println(
        "LVQ " + relation + " " + (double) (System.currentTimeMillis() - tiempo) / 1000.0 + "s");
    // Classify the train data set
    instanciasIN = new String[datosReferencia.length];
    instanciasOUT = new String[datosReferencia.length];
    for (i = 0; i < datosReferencia.length; i++) {
      /* Classify the instance selected in this iteration */
      Attribute a = Attributes.getOutputAttribute(0);

      int tipo = a.getType();
      claseObt = KNN.evaluacionKNN2(1, conjS, clasesS, datosReferencia[i], nClases);
      if (tipo != Attribute.NOMINAL) {
        instanciasIN[i] = new String(String.valueOf(clasesReferencia[i]));
        instanciasOUT[i] = new String(String.valueOf(claseObt));
      } else {
        instanciasIN[i] = new String(a.getNominalValue(clasesReferencia[i]));
        instanciasOUT[i] = new String(a.getNominalValue(claseObt));
      }
    }

    escribeSalida(
        ficheroSalida[0], instanciasIN, instanciasOUT, entradas, salida, nEntradas, relation);

    // Classify the test data set
    normalizarTest();
    instanciasIN = new String[datosTest.length];
    instanciasOUT = new String[datosTest.length];
    for (i = 0; i < datosTest.length; i++) {
      /* Classify the instance selected in this iteration */
      Attribute a = Attributes.getOutputAttribute(0);

      int tipo = a.getType();

      claseObt = KNN.evaluacionKNN2(1, conjS, clasesS, datosTest[i], nClases);
      if (tipo != Attribute.NOMINAL) {
        instanciasIN[i] = new String(String.valueOf(clasesTest[i]));
        instanciasOUT[i] = new String(String.valueOf(claseObt));
      } else {
        instanciasIN[i] = new String(a.getNominalValue(clasesTest[i]));
        instanciasOUT[i] = new String(a.getNominalValue(claseObt));
      }
    }

    escribeSalida(
        ficheroSalida[1], instanciasIN, instanciasOUT, entradas, salida, nEntradas, relation);

    // Print the network to a file
    printNetworkToFile(ficheroSalida[2], referencia.getHeader());
  }
Ejemplo n.º 12
0
  /**
   * The main method of the class that includes the operations of the algorithm. It includes all the
   * operations that the algorithm has and finishes when it writes the output information into
   * files.
   */
  public void run() {

    int nPos = 0;
    int nNeg = 0;
    int i, j, l, m;
    int tmp;
    int posID;
    int positives[];
    int overs[];
    double conjS[][];
    int clasesS[];
    int tamS;

    long tiempo = System.currentTimeMillis();

    /*Count of number of positive and negative examples*/
    for (i = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] == 0) nPos++;
      else nNeg++;
    }
    if (nPos > nNeg) {
      tmp = nPos;
      nPos = nNeg;
      nNeg = tmp;
      posID = 1;
    } else {
      posID = 0;
    }

    /*Localize the positive instances*/
    positives = new int[nPos];
    for (i = 0, j = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] == posID) {
        positives[j] = i;
        j++;
      }
    }

    /*Obtain the oversampling array taking account the previous array*/
    overs = new int[nNeg - nPos];
    Randomize.setSeed(semilla);
    for (i = 0; i < overs.length; i++) {
      tmp = Randomize.Randint(0, nPos - 1);
      overs[i] = positives[tmp];
    }

    tamS = 2 * nNeg;
    /*Construction of the S set from the previous vector S*/
    conjS = new double[tamS][datosTrain[0].length];
    clasesS = new int[tamS];
    for (j = 0; j < datosTrain.length; j++) {
      for (l = 0; l < datosTrain[0].length; l++) conjS[j][l] = datosTrain[j][l];
      clasesS[j] = clasesTrain[j];
    }
    for (m = 0; j < tamS; j++, m++) {
      for (l = 0; l < datosTrain[0].length; l++) conjS[j][l] = datosTrain[overs[m]][l];
      clasesS[j] = clasesTrain[overs[m]];
    }

    System.out.println(
        "RandomOverSampling "
            + relation
            + " "
            + (double) (System.currentTimeMillis() - tiempo) / 1000.0
            + "s");

    OutputIS.escribeSalida(ficheroSalida[0], conjS, clasesS, entradas, salida, nEntradas, relation);
    OutputIS.escribeSalida(ficheroSalida[1], test, entradas, salida, nEntradas, relation);
  }
Ejemplo n.º 13
0
  /**
   * The main method of the class that includes the operations of the algorithm. It includes all the
   * operations that the algorithm has and finishes when it writes the output information into
   * files.
   */
  public void run() {

    int S[];
    int i, j, l, m;
    int nPos = 0, nNeg = 0;
    int posID;
    int nClases;
    int pos;
    int baraje[];
    int tmp;
    double conjS[][];
    int clasesS[];
    int tamS = 0;
    int claseObt;
    int cont;
    int busq;
    boolean marcas[];
    int nSel;
    double conjS2[][];
    int clasesS2[];
    double minDist, dist;

    long tiempo = System.currentTimeMillis();

    /*CNN PART*/

    /*Count of number of positive and negative examples*/
    for (i = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] == 0) nPos++;
      else nNeg++;
    }
    if (nPos > nNeg) {
      tmp = nPos;
      nPos = nNeg;
      nNeg = tmp;
      posID = 1;
    } else {
      posID = 0;
    }

    /*Inicialization of the candidates set*/
    S = new int[datosTrain.length];
    for (i = 0; i < S.length; i++) S[i] = Integer.MAX_VALUE;

    /*Inserting an element of mayority class*/
    Randomize.setSeed(semilla);
    pos = Randomize.Randint(0, clasesTrain.length - 1);
    while (clasesTrain[pos] == posID) pos = (pos + 1) % clasesTrain.length;
    S[tamS] = pos;
    tamS++;

    /*Insert all subset of minority class*/
    for (i = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] == posID) {
        S[tamS] = i;
        tamS++;
      }
    }

    /*Algorithm body. We resort randomly the instances of T and compare with the rest of S.
    If an instance doesn´t classified correctly, it is inserted in S*/
    baraje = new int[datosTrain.length];
    for (i = 0; i < datosTrain.length; i++) baraje[i] = i;
    for (i = 0; i < datosTrain.length; i++) {
      pos = Randomize.Randint(i, clasesTrain.length - 1);
      tmp = baraje[i];
      baraje[i] = baraje[pos];
      baraje[pos] = tmp;
    }

    for (i = 0; i < datosTrain.length; i++) {
      if (clasesTrain[i] != posID) { // only for mayority class instances
        /*Construction of the S set from the previous vector S*/
        conjS = new double[tamS][datosTrain[0].length];
        clasesS = new int[tamS];
        for (j = 0; j < tamS; j++) {
          for (l = 0; l < datosTrain[0].length; l++) conjS[j][l] = datosTrain[S[j]][l];
          clasesS[j] = clasesTrain[S[j]];
        }

        /*Do KNN to the instance*/
        claseObt = KNN.evaluacionKNN(k, conjS, clasesS, datosTrain[baraje[i]], 2);
        if (claseObt != clasesTrain[baraje[i]]) { // fail in the class, it is included in S
          Arrays.sort(S);
          busq = Arrays.binarySearch(S, baraje[i]);
          if (busq < 0) {
            S[tamS] = baraje[i];
            tamS++;
          }
        }
      }
    }

    /*Construction of the S set from the previous vector S*/
    conjS = new double[tamS][datosTrain[0].length];
    clasesS = new int[tamS];
    for (j = 0; j < tamS; j++) {
      for (l = 0; l < datosTrain[0].length; l++) conjS[j][l] = datosTrain[S[j]][l];
      clasesS[j] = clasesTrain[S[j]];
    }

    /*TOMEK LINKS PART*/

    /*Inicialization of the instance flagged vector of the S set*/
    marcas = new boolean[conjS.length];
    for (i = 0; i < conjS.length; i++) {
      marcas[i] = true;
    }
    nSel = conjS.length;

    for (i = 0; i < conjS.length; i++) {
      minDist = Double.POSITIVE_INFINITY;
      pos = 0;
      for (j = 0; j < conjS.length; j++) {
        if (i != j) {
          dist = KNN.distancia(conjS[i], conjS[j]);
          if (dist < minDist) {
            minDist = dist;
            pos = j;
          }
        }
      }
      if (clasesS[i] != clasesS[pos]) {
        if (clasesS[i] != posID) {
          if (marcas[i] == true) {
            marcas[i] = false;
            nSel--;
          }
        } else {
          if (marcas[pos] == true) {
            marcas[pos] = false;
            nSel--;
          }
        }
      }
    }

    /*Construction of the S set from the flags*/
    conjS2 = new double[nSel][conjS[0].length];
    clasesS2 = new int[nSel];
    for (m = 0, l = 0; m < conjS.length; m++) {
      if (marcas[m]) { // the instance will evaluate
        for (j = 0; j < conjS[0].length; j++) {
          conjS2[l][j] = conjS[m][j];
        }
        clasesS2[l] = clasesS[m];
        l++;
      }
    }

    System.out.println(
        "CNN_TomekLinks "
            + relation
            + " "
            + (double) (System.currentTimeMillis() - tiempo) / 1000.0
            + "s");

    OutputIS.escribeSalida(
        ficheroSalida[0], conjS2, clasesS2, entradas, salida, nEntradas, relation);
    OutputIS.escribeSalida(ficheroSalida[1], test, entradas, salida, nEntradas, relation);
  }
Ejemplo n.º 14
0
  public void ejecutar() {

    int i, j, l, m, o;

    int nClases;

    int claseObt;

    boolean marcas[];

    double conjS[][];

    int clasesS[];

    int eleS[], eleT[];

    int bestAc, aciertos;

    int temp[];

    int pos, tmp;

    long tiempo = System.currentTimeMillis();

    /*Getting the number of different classes*/

    nClases = 0;

    for (i = 0; i < clasesTrain.length; i++) if (clasesTrain[i] > nClases) nClases = clasesTrain[i];

    nClases++;

    /*Inicialization of the flagged instance vector of the S set*/

    marcas = new boolean[datosTrain.length];

    for (i = 0; i < datosTrain.length; i++) marcas[i] = false;

    /*Allocate memory for the random selection*/

    m = (int) ((porcentaje * datosTrain.length) / 100.0);

    eleS = new int[m];

    eleT = new int[datosTrain.length - m];

    temp = new int[datosTrain.length];

    for (i = 0; i < datosTrain.length; i++) temp[i] = i;

    /** Random distribution of elements in each set */
    Randomize.setSeed(semilla);

    for (i = 0; i < eleS.length; i++) {

      pos = Randomize.Randint(i, datosTrain.length - 1);

      tmp = temp[i];

      temp[i] = temp[pos];

      temp[pos] = tmp;

      eleS[i] = temp[i];
    }

    for (i = 0; i < eleT.length; i++) {

      pos = Randomize.Randint(m + i, datosTrain.length - 1);

      tmp = temp[m + i];

      temp[m + i] = temp[pos];

      temp[pos] = tmp;

      eleT[i] = temp[m + i];
    }

    for (i = 0; i < eleS.length; i++) marcas[eleS[i]] = true;

    /*Building of the S set from the flags*/

    conjS = new double[m][datosTrain[0].length];

    clasesS = new int[m];

    for (o = 0, l = 0; o < datosTrain.length; o++) {

      if (marcas[o]) { // the instance will be evaluated

        for (j = 0; j < datosTrain[0].length; j++) {

          conjS[l][j] = datosTrain[o][j];
        }

        clasesS[l] = clasesTrain[o];

        l++;
      }
    }

    /*Evaluation of the S set*/

    bestAc = 0;

    for (i = 0; i < datosTrain.length; i++) {

      claseObt = KNN.evaluacionKNN2(k, conjS, clasesS, datosTrain[i], nClases);

      if (claseObt == clasesTrain[i]) // correct clasification
      bestAc++;
    }

    /*Body of the ENNRS algorithm. Change the S set in each iteration for instances
    of the T set until get a complete sustitution*/

    for (i = 0; i < n; i++) {

      /*Preparation the set to interchange*/

      for (j = 0; j < eleS.length; j++) {

        pos = Randomize.Randint(j, eleT.length - 1);

        tmp = eleT[j];

        eleT[j] = eleT[pos];

        eleT[pos] = tmp;
      }

      /*Interchange of instances*/

      for (j = 0; j < eleS.length; j++) {

        tmp = eleS[j];

        eleS[j] = eleT[j];

        eleT[j] = tmp;

        marcas[eleS[j]] = true;

        marcas[eleT[j]] = false;
      }

      /*Building of the S set from the flags*/

      for (o = 0, l = 0; o < datosTrain.length; o++) {

        if (marcas[o]) { // the instance will evaluate

          for (j = 0; j < datosTrain[0].length; j++) {

            conjS[l][j] = datosTrain[o][j];
          }

          clasesS[l] = clasesTrain[o];

          l++;
        }
      }

      /*Evaluation of the S set*/

      aciertos = 0;

      for (j = 0; j < datosTrain.length; j++) {

        claseObt = KNN.evaluacionKNN2(k, conjS, clasesS, datosTrain[j], nClases);

        if (claseObt == clasesTrain[j]) // correct clasification
        aciertos++;
      }

      if (aciertos > bestAc) { // keep S

        bestAc = aciertos;

      } else { // undo changes

        for (j = 0; j < eleS.length; j++) {

          tmp = eleS[j];

          eleS[j] = eleT[j];

          eleT[j] = tmp;

          marcas[eleS[j]] = true;

          marcas[eleT[j]] = false;
        }
      }
    }

    /*Building of the S set from the flags*/
    /*Building of the S set from the flags*/

    for (o = 0, l = 0; o < datosTrain.length; o++) {

      if (marcas[o]) { // the instance will evaluate

        for (j = 0; j < datosTrain[0].length; j++) {

          conjS[l][j] = datosTrain[o][j];
        }

        clasesS[l] = clasesTrain[o];

        l++;
      }
    }

    System.out.println(
        "ENNRS " + relation + " " + (double) (System.currentTimeMillis() - tiempo) / 1000.0 + "s");

    // COn conjS me vale.
    int trainRealClass[][];
    int trainPrediction[][];

    trainRealClass = new int[datosTrain.length][1];
    trainPrediction = new int[datosTrain.length][1];

    // Working on training
    for (i = 0; i < datosTrain.length; i++) {
      trainRealClass[i][0] = clasesTrain[i];
      trainPrediction[i][0] = KNN.evaluate(datosTrain[i], conjS, nClases, clasesS, this.k);
    }

    KNN.writeOutput(ficheroSalida[0], trainRealClass, trainPrediction, entradas, salida, relation);

    // Working on test
    int realClass[][] = new int[datosTest.length][1];
    int prediction[][] = new int[datosTest.length][1];

    // Check  time

    for (i = 0; i < realClass.length; i++) {
      realClass[i][0] = clasesTest[i];
      prediction[i][0] = KNN.evaluate(datosTest[i], conjS, nClases, clasesS, this.k);
    }

    KNN.writeOutput(ficheroSalida[1], realClass, prediction, entradas, salida, relation);
  }
Ejemplo n.º 15
0
  /** Executes the algorithm */
  public void ejecutar() {

    int i, j, l;
    int nClases;
    double conjS[][];
    double conjR[][];
    int conjN[][];
    boolean conjM[][];
    int clasesS[];
    int nSel = 0;
    Cromosoma poblacion[];
    int ev = 0;
    double prob[];
    double NUmax = 1.5;
    double NUmin = 0.5; // used for lineal ranking
    double aux;
    double pos1, pos2;
    int sel1, sel2, comp1, comp2;
    Cromosoma newPob[];

    long tiempo = System.currentTimeMillis();

    /*Getting the number of different clases*/
    nClases = 0;
    for (i = 0; i < clasesTrain.length; i++) if (clasesTrain[i] > nClases) nClases = clasesTrain[i];
    nClases++;

    /*Random inicialization of the population*/
    Randomize.setSeed(semilla);
    poblacion = new Cromosoma[tamPoblacion];
    for (i = 0; i < tamPoblacion; i++) poblacion[i] = new Cromosoma(datosTrain.length);

    /*Initial evaluation of the population*/
    for (i = 0; i < tamPoblacion; i++)
      poblacion[i].evalua(
          datosTrain,
          realTrain,
          nominalTrain,
          nulosTrain,
          clasesTrain,
          alfa,
          kNeigh,
          nClases,
          distanceEu);

    if (torneo) {
      while (ev < nEval) {
        newPob = new Cromosoma[2];

        /*Binary tournament selection*/
        comp1 = Randomize.Randint(0, tamPoblacion - 1);
        do {
          comp2 = Randomize.Randint(0, tamPoblacion - 1);
        } while (comp2 == comp1);
        if (poblacion[comp1].getCalidad() > poblacion[comp2].getCalidad()) sel1 = comp1;
        else sel1 = comp2;
        comp1 = Randomize.Randint(0, tamPoblacion - 1);
        do {
          comp2 = Randomize.Randint(0, tamPoblacion - 1);
        } while (comp2 == comp1);
        if (poblacion[comp1].getCalidad() > poblacion[comp2].getCalidad()) sel2 = comp1;
        else sel2 = comp2;

        if (Randomize.Rand() < pCruce) { // there is cross
          crucePMX(poblacion, newPob, sel1, sel2);
        } else { // there is not cross
          newPob[0] = new Cromosoma(datosTrain.length, poblacion[sel1]);
          newPob[1] = new Cromosoma(datosTrain.length, poblacion[sel2]);
        }

        /*Mutation of the cromosomes*/
        for (i = 0; i < 2; i++) newPob[i].mutacion(pMutacion1to0, pMutacion0to1);

        /*Evaluation of the population*/
        for (i = 0; i < 2; i++)
          if (!(newPob[i].estaEvaluado())) {
            newPob[i].evalua(
                datosTrain,
                realTrain,
                nominalTrain,
                nulosTrain,
                clasesTrain,
                alfa,
                kNeigh,
                nClases,
                distanceEu);
            ev++;
          }

        /*Replace the two worst*/
        Arrays.sort(poblacion);
        poblacion[tamPoblacion - 2] = new Cromosoma(datosTrain.length, newPob[0]);
        poblacion[tamPoblacion - 1] = new Cromosoma(datosTrain.length, newPob[1]);
      }
    } else {
      /*Get the probabilities of lineal ranking in case of not use binary tournament*/
      prob = new double[tamPoblacion];
      for (i = 0; i < tamPoblacion; i++) {
        aux = (double) (NUmax - NUmin) * ((double) i / (tamPoblacion - 1));
        prob[i] = (double) (1.0 / (tamPoblacion)) * (NUmax - aux);
      }
      for (i = 1; i < tamPoblacion; i++) prob[i] = prob[i] + prob[i - 1];

      while (ev < nEval) {
        /*Sort the population by quality criterion*/
        Arrays.sort(poblacion);

        newPob = new Cromosoma[2];
        pos1 = Randomize.Rand();
        pos2 = Randomize.Rand();
        for (j = 0; j < tamPoblacion && prob[j] < pos1; j++) ;
        sel1 = j;
        for (j = 0; j < tamPoblacion && prob[j] < pos2; j++) ;
        sel2 = j;

        if (Randomize.Rand() < pCruce) { // there is cross
          crucePMX(poblacion, newPob, sel1, sel2);
        } else { // there is not cross
          newPob[0] = new Cromosoma(datosTrain.length, poblacion[sel1]);
          newPob[1] = new Cromosoma(datosTrain.length, poblacion[sel2]);
        }

        /*Mutation of the cromosomes*/
        for (i = 0; i < 2; i++) newPob[i].mutacion(pMutacion1to0, pMutacion0to1);

        /*Evaluation of the population*/
        for (i = 0; i < 2; i++)
          if (!(newPob[i].estaEvaluado())) {
            newPob[i].evalua(
                datosTrain,
                realTrain,
                nominalTrain,
                nulosTrain,
                clasesTrain,
                alfa,
                kNeigh,
                nClases,
                distanceEu);
            ev++;
          }

        /*Replace the two worst*/
        poblacion[tamPoblacion - 2] = new Cromosoma(datosTrain.length, newPob[0]);
        poblacion[tamPoblacion - 1] = new Cromosoma(datosTrain.length, newPob[1]);
      }
    }

    nSel = poblacion[0].genesActivos();

    /*Building of S set from the best cromosome obtained*/
    conjS = new double[nSel][datosTrain[0].length];
    conjR = new double[nSel][datosTrain[0].length];
    conjN = new int[nSel][datosTrain[0].length];
    conjM = new boolean[nSel][datosTrain[0].length];
    clasesS = new int[nSel];
    for (i = 0, l = 0; i < datosTrain.length; i++) {
      if (poblacion[0].getGen(i)) { // the instance must be copied to the solution
        for (j = 0; j < datosTrain[0].length; j++) {
          conjS[l][j] = datosTrain[i][j];
          conjR[l][j] = realTrain[i][j];
          conjN[l][j] = nominalTrain[i][j];
          conjM[l][j] = nulosTrain[i][j];
        }
        clasesS[l] = clasesTrain[i];
        l++;
      }
    }

    System.out.println(
        "SGA " + relation + " " + (double) (System.currentTimeMillis() - tiempo) / 1000.0 + "s");

    OutputIS.escribeSalida(
        ficheroSalida[0], conjR, conjN, conjM, clasesS, entradas, salida, nEntradas, relation);
    OutputIS.escribeSalida(ficheroSalida[1], test, entradas, salida, nEntradas, relation);
  } // end-method
Ejemplo n.º 16
0
  /** Function which cross the population */
  public void Cruce() {

    int i, j;
    int temp, mom, dad;
    int numCruces = 0;
    double distancia;

    int aux;
    aux = (Genes * BITS_GEN) + n_reglas_total;
    if (flag2 == 1) {
      String1 = new char[aux];
      String2 = new char[aux];
      flag2 = 0;
    }

    for (i = 0; i < Popsize; i++) sample[i] = i;

    for (i = 0; i < Popsize; i++) {

      j = Randomize.Randint(i, Popsize - 1);
      temp = sample[j];
      sample[j] = sample[i];
      sample[i] = temp;
    }

    for (i = 0; i < 2 * Popsize; i++) Poblacion[i].set_entrado(0);

    /** ********************************************************************* */
    /*  DISPARAR CRUCE */
    POPSIZE = Popsize;
    noCruce = 0;
    for (i = 0; i < Popsize / 2; i++) {
      mom = sample[2 * i];
      dad = sample[2 * i + 1];
      /* COMPROBAR DISTANCIA HAMMING ENTRE LOS PADRES */

      String1 = StringRep(Poblacion[mom].Gene(), Genes);
      String2 = StringRep(Poblacion[dad].Gene(), Genes);

      distancia = DistHam(String1, String2, Genes * BITS_GEN);

      String1 = StringRep(Poblacion[mom].GeneA(), GenesA);
      String2 = StringRep(Poblacion[dad].GeneA(), GenesA);

      double aux1 = DistHam(String1, String2, GenesA * BITS_GEN);

      distancia += aux1;
      distancia /= 2.0;

      if (distancia > (THRESHOLD)) {
        xPC_BLX(
            0.8,
            Poblacion[mom].Gene(),
            Poblacion[dad].Gene(),
            Poblacion[POPSIZE].Gene(),
            Poblacion[POPSIZE + 1].Gene(),
            Genes);
        xPC_BLX(
            0.8,
            Poblacion[mom].GeneA(),
            Poblacion[dad].GeneA(),
            Poblacion[POPSIZE].GeneA(),
            Poblacion[POPSIZE + 1].GeneA(),
            GenesA);
        HUX(
            Poblacion[mom].GeneR(),
            Poblacion[dad].GeneR(),
            Poblacion[POPSIZE].GeneR(),
            Poblacion[POPSIZE + 1].GeneR());

        Poblacion[POPSIZE].set_entrado(1);
        Poblacion[POPSIZE + 1].set_entrado(1);
        POPSIZE = POPSIZE + 2;
        numCruces++;
      } else noCruce++;
    }
  }
Ejemplo n.º 17
0
  /**
   * SMOTE preprocessing procedure
   *
   * @param datosTrain input training dta
   * @param realTrain actual training data
   * @param nominalTrain nominal attribute values
   * @param nulosTrain null values
   * @param clasesTrain training classes
   * @param datosArt synthetic instances
   */
  public void SMOTE(
      double datosTrain[][],
      double realTrain[][],
      int nominalTrain[][],
      boolean nulosTrain[][],
      int clasesTrain[],
      double datosArt[][],
      double realArt[][],
      int nominalArt[][],
      boolean nulosArt[][],
      int clasesArt[],
      int kSMOTE,
      int ASMO,
      double smoting,
      boolean balance,
      int nPos,
      int posID,
      int nNeg,
      int negID,
      boolean distanceEu) {

    int i, j, l, m;
    int tmp, pos;
    int positives[];
    int neighbors[][];
    double genS[][];
    double genR[][];
    int genN[][];
    boolean genM[][];
    int clasesGen[];
    int nn;

    /* Localize the positive instances */
    positives = new int[nPos];
    for (i = 0, j = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] == posID) {
        positives[j] = i;
        j++;
      }
    }

    /* Randomize the instance presentation */
    for (i = 0; i < positives.length; i++) {
      tmp = positives[i];
      pos = Randomize.Randint(0, positives.length - 1);
      positives[i] = positives[pos];
      positives[pos] = tmp;
    }

    /* Obtain k-nearest neighbors of each positive instance */
    neighbors = new int[positives.length][kSMOTE];
    for (i = 0; i < positives.length; i++) {
      switch (ASMO) {
        case 0:
          KNN.evaluacionKNN2(
              kSMOTE,
              datosTrain,
              realTrain,
              nominalTrain,
              nulosTrain,
              clasesTrain,
              datosTrain[positives[i]],
              realTrain[positives[i]],
              nominalTrain[positives[i]],
              nulosTrain[positives[i]],
              Math.max(posID, negID) + 1,
              distanceEu,
              neighbors[i]);
          break;
        case 1:
          evaluacionKNNClass(
              kSMOTE,
              datosTrain,
              realTrain,
              nominalTrain,
              nulosTrain,
              clasesTrain,
              datosTrain[positives[i]],
              realTrain[positives[i]],
              nominalTrain[positives[i]],
              nulosTrain[positives[i]],
              Math.max(posID, negID) + 1,
              distanceEu,
              neighbors[i],
              posID);
          break;
        case 2:
          evaluacionKNNClass(
              kSMOTE,
              datosTrain,
              realTrain,
              nominalTrain,
              nulosTrain,
              clasesTrain,
              datosTrain[positives[i]],
              realTrain[positives[i]],
              nominalTrain[positives[i]],
              nulosTrain[positives[i]],
              Math.max(posID, negID) + 1,
              distanceEu,
              neighbors[i],
              negID);
          break;
      }
    }

    /* Interpolation of the minority instances */
    if (balance) {
      genS = new double[nNeg - nPos][datosTrain[0].length];
      genR = new double[nNeg - nPos][datosTrain[0].length];
      genN = new int[nNeg - nPos][datosTrain[0].length];
      genM = new boolean[nNeg - nPos][datosTrain[0].length];
      clasesGen = new int[nNeg - nPos];
    } else {
      genS = new double[(int) (nPos * smoting)][datosTrain[0].length];
      genR = new double[(int) (nPos * smoting)][datosTrain[0].length];
      genN = new int[(int) (nPos * smoting)][datosTrain[0].length];
      genM = new boolean[(int) (nPos * smoting)][datosTrain[0].length];
      clasesGen = new int[(int) (nPos * smoting)];
    }
    for (i = 0; i < genS.length; i++) {
      clasesGen[i] = posID;
      nn = Randomize.Randint(0, kSMOTE - 1);
      interpola(
          realTrain[positives[i % positives.length]],
          realTrain[neighbors[i % positives.length][nn]],
          nominalTrain[positives[i % positives.length]],
          nominalTrain[neighbors[i % positives.length][nn]],
          nulosTrain[positives[i % positives.length]],
          nulosTrain[neighbors[i % positives.length][nn]],
          genS[i],
          genR[i],
          genN[i],
          genM[i]);
    }

    for (j = 0; j < datosTrain.length; j++) {
      for (l = 0; l < datosTrain[0].length; l++) {
        datosArt[j][l] = datosTrain[j][l];
        realArt[j][l] = realTrain[j][l];
        nominalArt[j][l] = nominalTrain[j][l];
        nulosArt[j][l] = nulosTrain[j][l];
      }
      clasesArt[j] = clasesTrain[j];
    }
    for (m = 0; j < datosArt.length; j++, m++) {
      for (l = 0; l < datosTrain[0].length; l++) {
        datosArt[j][l] = genS[m][l];
        realArt[j][l] = genR[m][l];
        nominalArt[j][l] = genN[m][l];
        nulosArt[j][l] = genM[m][l];
      }
      clasesArt[j] = clasesGen[m];
    }
  }
Ejemplo n.º 18
0
  /** It runs the Qstatistic */
  public void runAlgorithm() {

    int i, j, l, h;
    double conjS[][];
    double conjR[][];
    int conjN[][];
    boolean conjM[][];
    int clasesS[];
    int nSel = 0;
    Chromosome poblacion[];
    int ev = 0;
    Chromosome C[];
    int baraje[];
    int pos, tmp;
    Chromosome newPob[];
    int d;
    int tamC;
    Chromosome pobTemp[];
    int nPos = 0, nNeg = 0, posID, negID;
    double datosArt[][];
    double realArt[][];
    int nominalArt[][];
    boolean nulosArt[][];
    int clasesArt[];
    int tamS;

    long tiempo = System.currentTimeMillis();

    // Randomize.setSeed (semilla);
    posID = clasesTrain[0];
    negID = -1;
    for (i = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] != posID) {
        negID = clasesTrain[i];
        break;
      }
    }
    /* Count of number of positive and negative examples */
    for (i = 0; i < clasesTrain.length; i++) {
      if (clasesTrain[i] == posID) nPos++;
      else nNeg++;
    }
    if (nPos > nNeg) {
      tmp = nPos;
      nPos = nNeg;
      nNeg = tmp;
      tmp = posID;
      posID = negID;
      negID = tmp;
    } else {
      /*
       * tmp = posID; posID = negID; negID = tmp;
       */
    }

    if (hybrid.equalsIgnoreCase("smote + eus")) {
      if (balance) {
        tamS = 2 * nNeg;
      } else {
        tamS = nNeg + nPos + (int) (nPos * smoting);
      }
      datosArt = new double[tamS][datosTrain[0].length];
      realArt = new double[tamS][datosTrain[0].length];
      nominalArt = new int[tamS][datosTrain[0].length];
      nulosArt = new boolean[tamS][datosTrain[0].length];
      clasesArt = new int[tamS];

      SMOTE(
          datosTrain,
          realTrain,
          nominalTrain,
          nulosTrain,
          clasesTrain,
          datosArt,
          realArt,
          nominalArt,
          nulosArt,
          clasesArt,
          kSMOTE,
          ASMO,
          smoting,
          balance,
          nPos,
          posID,
          nNeg,
          negID,
          distanceEu);
    } else {
      datosArt = new double[datosTrain.length][datosTrain[0].length];
      realArt = new double[datosTrain.length][datosTrain[0].length];
      nominalArt = new int[datosTrain.length][datosTrain[0].length];
      nulosArt = new boolean[datosTrain.length][datosTrain[0].length];
      clasesArt = new int[clasesTrain.length];
      for (i = 0; i < datosTrain.length; i++) {
        for (j = 0; j < datosTrain[i].length; j++) {
          datosArt[i][j] = datosTrain[i][j];
          realArt[i][j] = realTrain[i][j];
          nominalArt[i][j] = nominalTrain[i][j];
          nulosArt[i][j] = nulosTrain[i][j];
        }
        clasesArt[i] = clasesTrain[i];
      }
    }

    /* Count of number of positive and negative examples */
    nPos = nNeg = 0;
    for (i = 0; i < clasesArt.length; i++) {
      if (clasesArt[i] == posID) nPos++;
      else nNeg++;
    }

    if (majSelection) d = nNeg / 4;
    else d = datosArt.length / 4;

    /* Random initialization of the population */
    poblacion = new Chromosome[popSize];
    baraje = new int[popSize];
    for (i = 0; i < popSize; i++)
      if (majSelection) poblacion[i] = new Chromosome(nNeg);
      else poblacion[i] = new Chromosome(datosArt.length);

    /* Initial evaluation of the population */
    for (i = 0; i < popSize; i++)
      poblacion[i].evalua(
          datosTrain,
          realTrain,
          nominalTrain,
          nulosTrain,
          clasesTrain,
          datosArt,
          realArt,
          nominalArt,
          nulosArt,
          clasesArt,
          wrapper,
          k,
          evMeas,
          majSelection,
          pFactor,
          P,
          posID,
          nPos,
          distanceEu,
          entradas,
          anteriores,
          salidasAnteriores);

    /* Until stop condition */
    while (ev < nEval) {
      C = new Chromosome[popSize];

      /* Selection(r) of C(t) from P(t) */
      for (i = 0; i < popSize; i++) baraje[i] = i;
      for (i = 0; i < popSize; i++) {
        pos = Randomize.Randint(i, popSize - 1);
        tmp = baraje[i];
        baraje[i] = baraje[pos];
        baraje[pos] = tmp;
      }
      for (i = 0; i < popSize; i++)
        if (majSelection) C[i] = new Chromosome(nNeg, poblacion[baraje[i]]);
        else C[i] = new Chromosome(datosArt.length, poblacion[baraje[i]]);

      /* Structure recombination in C(t) constructing C'(t) */
      tamC = recombinar(C, d, nNeg, nPos, majSelection);
      newPob = new Chromosome[tamC];
      for (i = 0, l = 0; i < C.length; i++) {
        if (C[i].esValido()) { // the cromosome must be copied to the
          // new poblation C'(t)
          if (majSelection) newPob[l] = new Chromosome(nNeg, C[i]);
          else newPob[l] = new Chromosome(datosArt.length, C[i]);
          l++;
        }
      }

      /* Structure evaluation in C'(t) */
      for (i = 0; i < newPob.length; i++) {
        newPob[i].evalua(
            datosTrain,
            realTrain,
            nominalTrain,
            nulosTrain,
            clasesTrain,
            datosArt,
            realArt,
            nominalArt,
            nulosArt,
            clasesArt,
            wrapper,
            k,
            evMeas,
            majSelection,
            pFactor,
            P,
            posID,
            nPos,
            distanceEu,
            entradas,
            anteriores,
            salidasAnteriores);
        ev++;
      }

      /* Selection(s) of P(t) from C'(t) and P(t-1) */
      Arrays.sort(poblacion);
      Arrays.sort(newPob);
      /*
       * If the best of C' is worse than the worst of P(t-1), then there
       * will no changes
       */
      if (tamC == 0 || newPob[0].getCalidad() < poblacion[popSize - 1].getCalidad()) {
        d--;
      } else {
        pobTemp = new Chromosome[popSize];
        for (i = 0, j = 0, l = 0; i < popSize && l < tamC; i++) {
          if (poblacion[j].getCalidad() > newPob[l].getCalidad()) {
            if (majSelection) pobTemp[i] = new Chromosome(nNeg, poblacion[j]);
            else pobTemp[i] = new Chromosome(datosArt.length, poblacion[j]);
            j++;
          } else {
            if (majSelection) pobTemp[i] = new Chromosome(nNeg, newPob[l]);
            else pobTemp[i] = new Chromosome(datosArt.length, newPob[l]);
            l++;
          }
        }
        if (l == tamC) { // there are cromosomes for copying
          for (; i < popSize; i++) {
            if (majSelection) pobTemp[i] = new Chromosome(nNeg, poblacion[j]);
            else pobTemp[i] = new Chromosome(datosArt.length, poblacion[j]);
            j++;
          }
        }
        poblacion = pobTemp;
      }

      /* Last step of the algorithm */
      if (d <= 0) {
        for (i = 1; i < popSize; i++) {
          poblacion[i].divergeCHC(r, poblacion[0], prob0to1Div);
        }
        for (i = 0; i < popSize; i++)
          if (!(poblacion[i].estaEvaluado())) {
            poblacion[i].evalua(
                datosTrain,
                realTrain,
                nominalTrain,
                nulosTrain,
                clasesTrain,
                datosArt,
                realArt,
                nominalArt,
                nulosArt,
                clasesArt,
                wrapper,
                k,
                evMeas,
                majSelection,
                pFactor,
                P,
                posID,
                nPos,
                distanceEu,
                entradas,
                anteriores,
                salidasAnteriores);
            ev++;
          }

        /* Reinicialization of d value */
        if (majSelection) d = (int) (r * (1.0 - r) * (double) nNeg);
        else d = (int) (r * (1.0 - r) * (double) datosArt.length);
      }
    }

    Arrays.sort(poblacion);

    if (majSelection) {
      nSel = poblacion[0].genesActivos() + nPos;

      /* Construction of S set from the best cromosome */
      conjS = new double[nSel][datosArt[0].length];
      conjR = new double[nSel][datosArt[0].length];
      conjN = new int[nSel][datosArt[0].length];
      conjM = new boolean[nSel][datosArt[0].length];
      clasesS = new int[nSel];
      h = 0;
      for (i = 0, l = 0; i < nNeg; i++, h++) {
        for (; clasesArt[h] == posID && h < clasesArt.length; h++) ;
        if (poblacion[0].getGen(i)) { // the instance must be copied to
          // the solution
          for (j = 0; j < datosArt[h].length; j++) {
            conjS[l][j] = datosArt[h][j];
            conjR[l][j] = realArt[h][j];
            conjN[l][j] = nominalArt[h][j];
            conjM[l][j] = nulosArt[h][j];
          }
          clasesS[l] = clasesArt[h];
          l++;
        }
      }
      for (i = 0; i < datosArt.length; i++) {
        if (clasesArt[i] == posID) {
          for (j = 0; j < datosArt[i].length; j++) {
            conjS[l][j] = datosArt[i][j];
            conjR[l][j] = realArt[i][j];
            conjN[l][j] = nominalArt[i][j];
            conjM[l][j] = nulosArt[i][j];
          }
          clasesS[l] = clasesArt[i];
          l++;
        }
      }
    } else {
      nSel = poblacion[0].genesActivos();

      /* Construction of S set from the best cromosome */
      conjS = new double[nSel][datosArt[0].length];
      conjR = new double[nSel][datosArt[0].length];
      conjN = new int[nSel][datosArt[0].length];
      conjM = new boolean[nSel][datosArt[0].length];
      clasesS = new int[nSel];
      for (i = 0, l = 0; i < datosArt.length; i++) {
        if (poblacion[0].getGen(i)) { // the instance must be copied to
          // the solution
          for (j = 0; j < datosArt[i].length; j++) {
            conjS[l][j] = datosArt[i][j];
            conjR[l][j] = realArt[i][j];
            conjN[l][j] = nominalArt[i][j];
            conjM[l][j] = nulosArt[i][j];
          }
          clasesS[l] = clasesArt[i];
          l++;
        }
      }
    }

    if (hybrid.equalsIgnoreCase("eus + smote")) {
      nPos = nNeg = 0;
      for (i = 0; i < clasesS.length; i++) {
        if (clasesS[i] == posID) nPos++;
        else nNeg++;
      }
      if (nPos < nNeg) {
        if (balance) {
          tamS = 2 * nNeg;
        } else {
          tamS = nNeg + nPos + (int) (nPos * smoting);
        }
        datosArt = new double[tamS][datosTrain[0].length];
        realArt = new double[tamS][datosTrain[0].length];
        nominalArt = new int[tamS][datosTrain[0].length];
        nulosArt = new boolean[tamS][datosTrain[0].length];
        clasesArt = new int[tamS];

        SMOTE(
            conjS,
            conjR,
            conjN,
            conjM,
            clasesS,
            datosArt,
            realArt,
            nominalArt,
            nulosArt,
            clasesArt,
            kSMOTE,
            ASMO,
            smoting,
            balance,
            nPos,
            posID,
            nNeg,
            negID,
            distanceEu);

        nSel = datosArt.length;

        /* Construction of S set from the best cromosome */
        conjS = new double[nSel][datosArt[0].length];
        conjR = new double[nSel][datosArt[0].length];
        conjN = new int[nSel][datosArt[0].length];
        conjM = new boolean[nSel][datosArt[0].length];
        clasesS = new int[nSel];
        for (i = 0; i < datosArt.length; i++) {
          for (j = 0; j < datosArt[i].length; j++) {
            conjS[i][j] = datosArt[i][j];
            conjR[i][j] = realArt[i][j];
            conjN[i][j] = nominalArt[i][j];
            conjM[i][j] = nulosArt[i][j];
          }
          clasesS[i] = clasesArt[i];
        }
      }
    }

    /*
     * for (i = 0; i < poblacion.length; i++){ for (j = 0; j <
     * poblacion[0].cuerpo.length; j++){
     * System.out.print((poblacion[i].cuerpo[j] ? 1 : 0)); }
     * System.out.println(" Calidad: " + poblacion[i].calidad); }
     */
    best = poblacion[0].cuerpo.clone();
    bestOutputs = poblacion[0].prediction.clone();
    System.out.println(
        "QstatEUSCHC "
            + relation
            + " "
            + (double) (System.currentTimeMillis() - tiempo) / 1000.0
            + "s");

    OutputIS.escribeSalida(
        ficheroSalida[0], conjR, conjN, conjM, clasesS, entradas, salida, nEntradas, relation);
    // OutputIS.escribeSalida(ficheroSalida[1], test, entradas, salida,
    // nEntradas, relation);
  }