/** * KMeans constructor: the cluster centroids are obtained for the given dataset. Firstly, the * cluster's centroids are randomly chosen. Then the centroids are updated as the mean vlaue of * nearest examples in the dataset. The updating is carried out until no changes in the centroids * is achieved. * * @param X The dataset to be clusterized * @param nclusters The desired number of clusters * @param vrand The Randomize object to be used */ public KMeans(double[][] X, int nclusters, Randomize vrand) { rand = vrand; train = X; clusters = nclusters; cclusters = new double[nclusters][X[0].length]; for (int i = 0; i < nclusters; i++) { int pos = (int) (rand.Rand() * X.length); for (int j = 0; j < cclusters[i].length; j++) cclusters[i][j] = X[pos][j]; } int[] C = new int[X.length]; int[] C_old = new int[X.length]; for (int i = 0; i < X.length; i++) { C_old[i] = nearestCentroid(X[i]); } centroidsUpdating(C_old); int cambios = 0, iteracion = 0; do { iteracion++; System.out.println("Iter=" + iteracion + " changes=" + cambios); cambios = 0; for (int i = 0; i < X.length; i++) { C[i] = nearestCentroid(X[i]); if (C[i] != C_old[i]) cambios++; C_old[i] = C[i]; } centroidsUpdating(C); } while (cambios > 0); }
/** * This public static method runs the algorithm that this class concerns with. * * @param args Array of strings to sent parameters to the main program. The path of the * algorithm's parameters file must be given. */ public static void main(String args[]) { boolean tty = false; ProcessConfig pc = new ProcessConfig(); System.out.println("Reading configuration file: " + args[0]); if (pc.fileProcess(args[0]) < 0) return; int algo = pc.parAlgorithmType; rand = new Randomize(); rand.setSeed(pc.parSeed); ClusterKMeans km = new ClusterKMeans(); km.clustering_kmeans(tty, pc); }
/** * This public static method runs the algorithm that this class concerns with. * * @param args Array of strings to sent parameters to the main program. The path of the * algorithm's parameters file must be given. */ public static void main(String args[]) { boolean tty = false; ProcessConfig pc = new ProcessConfig(); System.out.println("Reading configuration file: " + args[0]); if (pc.fileProcess(args[0]) < 0) return; int algo = pc.parAlgorithmType; rand = new Randomize(); rand.setSeed(pc.parSeed); ModelFuzzyPittsBurgh pi = new ModelFuzzyPittsBurgh(); pi.fuzzyPittsburghModelling(tty, pc); }
/** Reads the data of the configuration file */ public void leer_conf() { int i, j; String cadenaEntrada, valor; double cruce, mutacion, a, b, tau; int long_poblacion, tipo_fitness; // we read the file in a String cadenaEntrada = Fichero.leeFichero(fichero_conf); StringTokenizer sT = new StringTokenizer(cadenaEntrada, "\n\r=", false); // we read the algorithm's name sT.nextToken(); sT.nextToken(); // we read the name of the training and test files sT.nextToken(); valor = sT.nextToken(); StringTokenizer ficheros = new StringTokenizer(valor, "\t ", false); ficheros.nextToken(); fich_datos_chequeo = ((ficheros.nextToken()).replace('\"', ' ')).trim(); fich_datos_tst = ((ficheros.nextToken()).replace('\"', ' ')).trim(); fichero_br = ((ficheros.nextToken()).replace('\"', ' ')).trim(); // we read the name of the output files sT.nextToken(); valor = sT.nextToken(); ficheros = new StringTokenizer(valor, "\t ", false); fich_tra_obli = ((ficheros.nextToken()).replace('\"', ' ')).trim(); fich_tst_obli = ((ficheros.nextToken()).replace('\"', ' ')).trim(); fichero_reglas = ((ficheros.nextToken()).replace('\"', ' ')).trim(); ruta_salida = fich_tst_obli.substring(0, fich_tst_obli.lastIndexOf('/') + 1); // we read the seed of the random generator sT.nextToken(); valor = sT.nextToken(); semilla = Double.parseDouble(Quita_blancos(valor)); Randomize.setSeed((long) semilla); ; // we read the Number of Iterations sT.nextToken(); valor = sT.nextToken(); n_generaciones = Long.parseLong(Quita_blancos(valor)); // we read the Population Size sT.nextToken(); valor = sT.nextToken(); long_poblacion = Integer.parseInt(Quita_blancos(valor)); // we read the Tau parameter for the minimun maching degree required to the KB sT.nextToken(); valor = sT.nextToken(); tau = Double.parseDouble(Quita_blancos(valor)); // we read the Parameter a sT.nextToken(); valor = sT.nextToken(); a = Double.parseDouble(Quita_blancos(valor)); // we read the Parameter b sT.nextToken(); valor = sT.nextToken(); b = Double.parseDouble(Quita_blancos(valor)); // we read the Type of Fitness Function sT.nextToken(); valor = sT.nextToken(); tipo_fitness = Integer.parseInt(Quita_blancos(valor)); // we read the Cross Probability sT.nextToken(); valor = sT.nextToken(); cruce = Double.parseDouble(Quita_blancos(valor)); // we read the Mutation Probability sT.nextToken(); valor = sT.nextToken(); mutacion = Double.parseDouble(Quita_blancos(valor)); // we create all the objects tipoc = 1; tabla = new MiDataset(fich_datos_chequeo, true); if (tabla.salir == false) { base_reglas = new BaseR(fichero_br, tabla); fun_adap = new Adap(tabla, base_reglas, tau, tipo_fitness); alg_gen = new AG(long_poblacion, base_reglas.n_reglas, cruce, mutacion, a, b, fun_adap); } }
/** * Main Function * * @param args the Command line arguments. Only one is processed: the name of the file containing * the * <p>parameters */ public static void main(String[] args) throws IOException { double[][] X; double[][] Y; int nInpt, nOutpl, ndata, i, j; Rbfn net; try { // Help required if (args.length > 0) { if (args[0].equals("--help") || args[0].equals("-help") || args[0].equals("-h") || args[0].equals("-?")) { doHelp(); return; } } System.out.println("- Executing doRbfnDec " + args.length); // Reading parameters String paramFile = (args.length > 0) ? args[0] : "parameters.txt"; setParameters(paramFile); System.out.println(" - Parameters file: " + paramFile); // Random generator setup if (reallySeed) { Randomize.setSeed((long) seed); } // Reading Training dataset ProcDataset Dtrn = new ProcDataset(trnFile, true); // Training System.out.println("Modeling Dataset"); Dtrn.processModelDataset(); nInpt = Dtrn.getninputs(); nOutpl = 1; // PD.getnvariables()-nInpt; ndata = Dtrn.getndata(); Y = new double[ndata][1]; X = Dtrn.getX(); double[] auxY; auxY = Dtrn.getY(); for (i = 0; i < ndata; i++) Y[i][0] = auxY[i]; // Building and training the net net = new Rbfn(nNeuronsIni, X, ndata, nInpt, nOutpl); net.decremental(X, Y, ndata, percent, alfa); double[] obtained = new double[ndata]; net.testModeling(X, ndata, obtained); Dtrn.generateResultsModeling(outTrnFile, auxY, obtained); // TEST ProcDataset Dtst = new ProcDataset(tstFile, false); Dtst.processModelDataset(); nInpt = Dtst.getninputs(); nOutpl = 1; // PD.getnvariables()-nInpt; ndata = Dtst.getndata(); X = Dtst.getX(); auxY = Dtst.getY(); Y = new double[ndata][1]; for (i = 0; i < ndata; i++) Y[i][0] = auxY[i]; obtained = new double[ndata]; net.testModeling(X, ndata, obtained); Dtst.generateResultsModeling(outTstFile, auxY, obtained); RBFUtils.createOutputFile(trnFile, outRbfFile); net.printRbfn(outRbfFile); if (Dtrn.datasetType() == 2) System.out.println("This is not a clustering algorithm"); System.out.println( "- End of doRbfnDec. See results in output files named according to " + paramFile + " parameters file."); } catch (Exception e) { throw new InternalError(e.toString()); } }
/** Process the training and test files provided in the parameters file to the constructor. */ public void process() { // declarations double[] outputs; double[] outputs2; Instance neighbor; double dist, mean; int actual; Randomize rnd = new Randomize(); Instance ex; gCenter kmeans = null; int iterations = 0; double E; double prevE; int totalMissing = 0; boolean allMissing = true; rnd.setSeed(semilla); // PROCESS try { // Load in memory a dataset that contains a classification problem IS.readSet(input_train_name, true); int in = 0; int out = 0; ndatos = IS.getNumInstances(); nvariables = Attributes.getNumAttributes(); nentradas = Attributes.getInputNumAttributes(); nsalidas = Attributes.getOutputNumAttributes(); X = new String[ndatos][nvariables]; // matrix with transformed data kmeans = new gCenter(K, ndatos, nvariables); timesSeen = new FreqList[nvariables]; mostCommon = new String[nvariables]; // first, we choose k 'means' randomly from all // instances totalMissing = 0; for (int i = 0; i < ndatos; i++) { Instance inst = IS.getInstance(i); if (inst.existsAnyMissingValue()) totalMissing++; } if (totalMissing == ndatos) allMissing = true; else allMissing = false; for (int numMeans = 0; numMeans < K; numMeans++) { do { actual = (int) (ndatos * rnd.Rand()); ex = IS.getInstance(actual); } while (ex.existsAnyMissingValue() && !allMissing); kmeans.copyCenter(ex, numMeans); } // now, iterate adjusting clusters' centers and // instances to them prevE = 0; iterations = 0; do { for (int i = 0; i < ndatos; i++) { Instance inst = IS.getInstance(i); kmeans.setClusterOf(inst, i); } // set new centers kmeans.recalculateCenters(IS); // compute RMSE E = 0; for (int i = 0; i < ndatos; i++) { Instance inst = IS.getInstance(i); E += kmeans.distance(inst, kmeans.getClusterOf(i)); } iterations++; // System.out.println(iterations+"\t"+E); if (Math.abs(prevE - E) == 0) iterations = maxIter; else prevE = E; } while (E > minError && iterations < maxIter); for (int i = 0; i < ndatos; i++) { Instance inst = IS.getInstance(i); in = 0; out = 0; for (int j = 0; j < nvariables; j++) { Attribute a = Attributes.getAttribute(j); direccion = a.getDirectionAttribute(); tipo = a.getType(); if (direccion == Attribute.INPUT) { if (tipo != Attribute.NOMINAL && !inst.getInputMissingValues(in)) { X[i][j] = new String(String.valueOf(inst.getInputRealValues(in))); } else { if (!inst.getInputMissingValues(in)) X[i][j] = inst.getInputNominalValues(in); else { actual = kmeans.getClusterOf(i); X[i][j] = new String(kmeans.valueAt(actual, j)); } } in++; } else { if (direccion == Attribute.OUTPUT) { if (tipo != Attribute.NOMINAL && !inst.getOutputMissingValues(out)) { X[i][j] = new String(String.valueOf(inst.getOutputRealValues(out))); } else { if (!inst.getOutputMissingValues(out)) X[i][j] = inst.getOutputNominalValues(out); else { actual = kmeans.getClusterOf(i); X[i][j] = new String(kmeans.valueAt(actual, j)); } } out++; } } } } } catch (Exception e) { System.out.println("Dataset exception = " + e); e.printStackTrace(); System.exit(-1); } write_results(output_train_name); /** ************************************************************************************ */ // does a test file associated exist? if (input_train_name.compareTo(input_test_name) != 0) { try { // Load in memory a dataset that contains a classification problem IStest.readSet(input_test_name, false); int in = 0; int out = 0; ndatos = IStest.getNumInstances(); nvariables = Attributes.getNumAttributes(); nentradas = Attributes.getInputNumAttributes(); nsalidas = Attributes.getOutputNumAttributes(); for (int i = 0; i < ndatos; i++) { Instance inst = IStest.getInstance(i); in = 0; out = 0; for (int j = 0; j < nvariables; j++) { Attribute a = Attributes.getAttribute(j); direccion = a.getDirectionAttribute(); tipo = a.getType(); if (direccion == Attribute.INPUT) { if (tipo != Attribute.NOMINAL && !inst.getInputMissingValues(in)) { X[i][j] = new String(String.valueOf(inst.getInputRealValues(in))); } else { if (!inst.getInputMissingValues(in)) X[i][j] = inst.getInputNominalValues(in); else { actual = kmeans.getClusterOf(i); X[i][j] = new String(kmeans.valueAt(actual, j)); } } in++; } else { if (direccion == Attribute.OUTPUT) { if (tipo != Attribute.NOMINAL && !inst.getOutputMissingValues(out)) { X[i][j] = new String(String.valueOf(inst.getOutputRealValues(out))); } else { if (!inst.getOutputMissingValues(out)) X[i][j] = inst.getOutputNominalValues(out); else { actual = kmeans.getClusterOf(i); X[i][j] = new String(kmeans.valueAt(actual, j)); } } out++; } } } } } catch (Exception e) { System.out.println("Dataset exception = " + e); e.printStackTrace(); System.exit(-1); } write_results(output_test_name); } }