/** * obtain the names of the output files from the parameter file * * @param s is the StringTokenizer */ private void getOutputFiles(StringTokenizer s) { String val = s.nextToken(); trainFileNameOutput = s.nextToken().replace('"', ' ').trim(); testFileNameOutput = s.nextToken().replace('"', ' ').trim(); extraFileNameOutput = s.nextToken().replace('"', ' ').trim(); }
/** * obtain a string value from the parameter file * * @param s is the StringTokenizer */ private String getParamString(StringTokenizer s) { String contenido = ""; String val = s.nextToken(); while (s.hasMoreTokens()) contenido += s.nextToken() + " "; return contenido.trim(); }
/** * Constructor of the Class Parametros * * @param nombreFileParametros is the pathname of input parameter file */ Parametros(String nombreFileParametros) { try { int i; String fichero, linea, tok; StringTokenizer lineasFile, tokens; /* read the parameter file using Files class */ fichero = Files.readFile(nombreFileParametros); fichero += "\n"; /* remove all \r characters. it is neccesary for a correst use in Windows and UNIX */ fichero = fichero.replace('\r', ' '); /* extracts the differents tokens of the file */ lineasFile = new StringTokenizer(fichero, "\n"); i = 0; while (lineasFile.hasMoreTokens()) { linea = lineasFile.nextToken(); i++; tokens = new StringTokenizer(linea, " ,\t"); if (tokens.hasMoreTokens()) { tok = tokens.nextToken(); if (tok.equalsIgnoreCase("algorithm")) nameAlgorithm = getParamString(tokens); else if (tok.equalsIgnoreCase("inputdata")) getInputFiles(tokens); else if (tok.equalsIgnoreCase("outputdata")) getOutputFiles(tokens); else if (tok.equalsIgnoreCase("seed")) seed = getParamLong(tokens); else throw new java.io.IOException("Syntax error on line " + i + ": [" + tok + "]\n"); } } } catch (java.io.FileNotFoundException e) { System.err.println(e + "Parameter file"); } catch (java.io.IOException e) { System.err.println(e + "Aborting program"); System.exit(-1); } /** show the read parameter in the standard output */ String contents = "-- Parameters echo --- \n"; contents += "Algorithm name: " + nameAlgorithm + "\n"; contents += "Input Train File: " + trainFileNameInput + "\n"; contents += "Input Test File: " + testFileNameInput + "\n"; contents += "Output Train File: " + trainFileNameOutput + "\n"; contents += "Output Test File: " + testFileNameOutput + "\n"; System.out.println(contents); }
/** 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); } }
private String Quita_blancos(String cadena) { StringTokenizer sT = new StringTokenizer(cadena, "\t ", false); return (sT.nextToken()); }
/** * obtain a long value from the parameter file * * @param s is the StringTokenizer */ private long getParamLong(StringTokenizer s) { String val = s.nextToken(); val = s.nextToken(); return Long.parseLong(val); }
/** * obtain an integer value from the parameter file * * @param s is the StringTokenizer */ private int getParamInt(StringTokenizer s) { String val = s.nextToken(); val = s.nextToken(); return Integer.parseInt(val); }
/** * obtain a float value from the parameter file * * @param s is the StringTokenizer */ private double getParamFloat(StringTokenizer s) { String val = s.nextToken(); val = s.nextToken(); return Float.parseFloat(val); }