Exemple #1
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());
  }
Exemple #2
0
  /**
   * Evaluates a chromosome
   *
   * @param datos Reference to the training set
   * @param real Reference to the training set (real valued)
   * @param nominal Reference to the training set (nominal valued)
   * @param nulos Reference to the training set (null values)
   * @param clases Output attribute of each instance
   * @param alfa Alpha value of the fitness function
   * @param kNeigh Number of neighbors for the KNN algorithm
   * @param nClases Number of classes of the problem
   * @param distanceEu True= Euclidean distance; False= HVDM
   */
  public void evalua(
      double datos[][],
      double real[][],
      int nominal[][],
      boolean nulos[][],
      int clases[],
      double alfa,
      int kNeigh,
      int nClases,
      boolean distanceEu) {

    int i, j, l, m;
    int aciertos = 0;
    double M, s;
    double conjS[][];
    double conjR[][];
    int conjN[][];
    boolean conjM[][];
    int clasesS[];
    int vecinos[];
    int claseObt;
    int vecinoCercano;
    double dist, minDist;

    M = (double) datos.length;
    s = (double) genesActivos();

    if (kNeigh > 1) {
      vecinos = new int[kNeigh];
      conjS = new double[(int) s][datos[0].length];
      conjR = new double[(int) s][datos[0].length];
      conjN = new int[(int) s][datos[0].length];
      conjM = new boolean[(int) s][datos[0].length];
      clasesS = new int[(int) s];
      for (j = 0, l = 0; j < datos.length; j++) {
        if (cuerpo[j]) { // the instance must be copied to the solution
          for (m = 0; m < datos[j].length; m++) {
            conjS[l][m] = datos[j][m];
            conjR[l][m] = real[j][m];
            conjN[l][m] = nominal[j][m];
            conjM[l][m] = nulos[j][m];
          }
          clasesS[l] = clases[j];
          l++;
        }
      }

      for (i = 0; i < datos.length; i++) {
        claseObt =
            KNN.evaluacionKNN2(
                kNeigh,
                conjS,
                conjR,
                conjN,
                conjM,
                clasesS,
                datos[i],
                real[i],
                nominal[i],
                nulos[i],
                nClases,
                distanceEu,
                vecinos);
        if (claseObt >= 0) if (clases[i] == claseObt) aciertos++;
      }
    } else {
      for (i = 0; i < datos.length; i++) {
        vecinoCercano = -1;
        minDist = Double.POSITIVE_INFINITY;
        for (j = 0; j < datos.length; j++) {
          if (cuerpo[j]) { // It is in S
            dist =
                KNN.distancia(
                    datos[i],
                    real[i],
                    nominal[i],
                    nulos[i],
                    datos[j],
                    real[j],
                    nominal[j],
                    nulos[j],
                    distanceEu);
            if (dist < minDist && dist != 0) {
              minDist = dist;
              vecinoCercano = j;
            }
          }
        }
        if (vecinoCercano >= 0) if (clases[i] == clases[vecinoCercano]) aciertos++;
      }
    }

    calidad = ((double) (aciertos) / M) * alfa * 100.0;
    calidad += ((1.0 - alfa) * 100.0 * (M - s) / M);
    cruzado = false;
  } // end-method
Exemple #3
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);
  }
Exemple #4
0
  public void ejecutar() {

    int i, j, l;
    boolean marcas[];
    boolean marcas2[];
    boolean marcastmp[];
    boolean incorrect[];
    int nSel;
    double conjS[][];
    double conjR[][];
    int conjN[][];
    boolean conjM[][];
    int clasesS[];
    Vector<Integer> vecinos[];
    int next;
    int maxneigh;
    int pos;
    int borrado;
    int claseObt;
    int nClases;

    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++;

    /*Inicialization of the flagged instances vector for a posterior copy*/
    marcas = new boolean[datosTrain.length];
    marcas2 = new boolean[datosTrain.length];
    incorrect = new boolean[datosTrain.length];
    marcastmp = new boolean[datosTrain.length];
    Arrays.fill(marcas, true);
    Arrays.fill(marcas2, true);
    Arrays.fill(incorrect, false);
    Arrays.fill(marcastmp, true);
    vecinos = new Vector[datosTrain.length];
    for (i = 0; i < datosTrain.length; i++) vecinos[i] = new Vector<Integer>();

    for (i = 0; i < datosTrain.length; i++) {
      next = nextNeighbour(marcas, datosTrain, i, vecinos[i]);
      for (j = 0; j < datosTrain.length; j++) marcastmp[j] = marcas[j];
      while (next >= 0 && clasesTrain[next] == clasesTrain[i]) {
        vecinos[i].add(new Integer(next));
        marcastmp[next] = false;
        next = nextNeighbour(marcastmp, datosTrain, i, vecinos[i]);
      }
    }

    maxneigh = vecinos[0].size();
    pos = 0;
    for (i = 1; i < datosTrain.length; i++) {
      if (vecinos[i].size() > maxneigh) {
        maxneigh = vecinos[i].size();
        pos = i;
      }
    }

    while (maxneigh > 0) {
      for (i = 0; i < vecinos[pos].size(); i++) {
        borrado = vecinos[pos].elementAt(i).intValue();
        marcas[borrado] = false;
        for (j = 0; j < datosTrain.length; j++) {
          vecinos[j].removeElement(new Integer(borrado));
        }
        vecinos[borrado].clear();
      }
      vecinos[pos].clear();

      maxneigh = vecinos[0].size();
      pos = 0;
      for (i = 1; i < datosTrain.length; i++) {
        if (vecinos[i].size() > maxneigh) {
          maxneigh = vecinos[i].size();
          pos = i;
        }
      }
    }

    /*Building of the S set from the flags*/
    nSel = 0;
    for (i = 0; i < datosTrain.length; i++) if (marcas[i]) nSel++;

    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 (marcas[i]) { // the instance will 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++;
      }
    }

    for (i = 0; i < datosTrain.length; i++) {
      /*Apply 1-NN to the instance*/
      claseObt =
          KNN.evaluacionKNN2(
              1,
              conjS,
              conjR,
              conjN,
              conjM,
              clasesTrain,
              datosTrain[i],
              realTrain[i],
              nominalTrain[i],
              nulosTrain[i],
              nClases,
              true);
      if (claseObt != clasesTrain[i]) {
        incorrect[i] = true;
      }
    }

    for (i = 0; i < datosTrain.length; i++) vecinos[i] = new Vector<Integer>();

    for (i = 0; i < datosTrain.length; i++) {
      if (incorrect[i]) {
        next = nextNeighbour(marcas2, datosTrain, i, vecinos[i]);
        for (j = 0; j < datosTrain.length; j++) marcastmp[j] = marcas2[j];
        while (next >= 0 && clasesTrain[next] == clasesTrain[i]) {
          vecinos[i].add(new Integer(next));
          marcastmp[next] = false;
          next = nextNeighbour(marcastmp, datosTrain, i, vecinos[i]);
        }
      }
    }

    maxneigh = vecinos[0].size();
    pos = 0;
    for (i = 1; i < datosTrain.length; i++) {
      if (vecinos[i].size() > maxneigh) {
        maxneigh = vecinos[i].size();
        pos = i;
      }
    }

    while (maxneigh > 0) {
      for (i = 0; i < vecinos[pos].size(); i++) {
        borrado = vecinos[pos].elementAt(i).intValue();
        marcas2[borrado] = false;
        for (j = 0; j < datosTrain.length; j++) {
          vecinos[j].removeElement(new Integer(borrado));
        }
        vecinos[borrado].clear();
      }
      vecinos[pos].clear();

      maxneigh = vecinos[0].size();
      pos = 0;
      for (i = 1; i < datosTrain.length; i++) {
        if (vecinos[i].size() > maxneigh) {
          maxneigh = vecinos[i].size();
          pos = i;
        }
      }
    }

    for (i = 0; i < marcas.length; i++) marcas[i] |= (marcas2[i] & incorrect[i]);

    /*Building of the S set from the flags*/
    nSel = 0;
    for (i = 0; i < datosTrain.length; i++) if (marcas[i]) nSel++;

    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 (marcas[i]) { // the instance will 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(
        "Reconsistent "
            + 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, 1);
    }

    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, 1);
    }

    KNN.writeOutput(ficheroSalida[1], realClass, prediction, entradas, salida, relation);
  }