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