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