Untitled
unknown
plain_text
3 years ago
12 kB
4
Indexable
package pasteleros_RyP; import java.io.BufferedReader; import java.io.IOException; import java.util.Random; import java.util.Scanner; public class creaPasteleria { private boolean[] asignados; private int[] pasteleros; private TNodo nodo; private int[][] TTabla; private int numPasteleros,numPasteles,costeTotal; private int[] pedido; private boolean traza = false; private String salida; BufferedReader lector; /** * Se encarga de construir los elementos necesarios para ejecutar el algoritmo y lanza su ejecucion * @param lector * @param traza * @param salida * @throws IOException */ public creaPasteleria(BufferedReader lector, boolean traza, String salida) throws IOException { super(); this.lector = lector; this.traza = traza; this.salida = salida; String aux = lector.readLine(); // Leemos el primer dato de entrada que debería ser el número total de pasteleros if (aux != null) { if (this.isNumberOk(aux) == false || aux.contains(".")) throw new IOException("Error, dato leido incorrecto."); this.setPasteleros(Integer.parseInt(aux)); this.asignados = new boolean[numPasteleros+1]; this.pasteleros = new int[numPasteleros+1]; for(int i=1;i<=this.numPasteleros;i++) { this.asignados[i] = false; this.pasteleros[i] = 0; } } else throw new NullPointerException("Error, fichero vacio."); // Leemos el segundo dato de entrada que debería ser el número total de tipos de pasteles aux = lector.readLine(); if (aux != null) { if (this.isNumberOk(aux) == false || aux.contains(".")) throw new IOException("Error, dato leido incorrecto."); this.setPasteles(Integer.parseInt(aux)); } else throw new NullPointerException("Error, fichero vacio."); //Leemos el tercer dato de entrada que son los pedidos. aux = lector.readLine(); if (aux != null) { String s[] = aux.split("-"); if(s.length != numPasteleros) throw new IOException("Error, el número de pedidos ha de coincidir con el número de pasteleros."); else { this.pedido = new int [s.length+1]; for (int i=1; i<= s.length; i++) { if(Integer.parseInt(s[i-1]) < 1 || Integer.parseInt(s[i-1]) > numPasteles) throw new IOException("Error, el número de pedido ha de estar contenido en la cantidad indicada."); else pedido[i] = Integer.parseInt(s[i-1]); } } } else throw new NullPointerException("Error, fichero vacio."); this.TTabla = new int [numPasteleros+1][numPasteles+1]; // Leemos el número de pasteleros indicados, cada uno con sus costes asociados. for (int i = 1; i <= this.getNumPasteleros(); i++) { aux = lector.readLine(); String s[] = aux.split(" "); //if(s.length != pasteleros) throw new IOException("Error, se han de especificar el mismo número de trabajadores con sus costes que los indicados al principio."); if(s.length != numPasteles) throw new IOException("Error, se han de especificar costes para el mismo numero de pasteles que los indicados al principio."); for (int j = 1; j <= s.length; j++) { if (this.isNumberOk(s[j-1]) == false || Integer.parseInt(s[j-1]) <= 0) throw new IOException("Error, el coste ha de ser de al menos 1. Solo se admiten numeros enteros."); TTabla[i][j] = Integer.parseInt(s[j-1]); } } this.costeTotal = 0; nodo = new TNodo(this.getNumPasteleros()); nodo.setK(0); nodo.setPasteleros(pasteleros); nodo.setAsignados(asignados); nodo.setCosteT(costeTotal); //debug(); Monticulo mon = new Monticulo(0,factorial(this.numPasteleros+1)); /* int aux2,num2; for(int z=0;z<=10000;z++) { for(int i=1;i<8;i++) { TNodo n = new TNodo(this.getNumPasteleros()); Random rg = new Random(); int num = rg.nextInt(10)+1; n.setEstOpt(num); mon.insertar(n); } aux2 = 0; num2 = 0; for(int i=1;i<8;i++) { num2 = mon.obtenerCima().getEstOpt(); if(num2<aux2) { System.out.println("Error en el montículo"); } aux2 = num2; } } */ TNodo a = new TNodo(this.getNumPasteleros()); TNodo b = new TNodo(this.getNumPasteleros()); TNodo c = new TNodo(this.getNumPasteleros()); TNodo d = new TNodo(this.getNumPasteleros()); TNodo e = new TNodo(this.getNumPasteleros()); TNodo f = new TNodo(this.getNumPasteleros()); TNodo g = new TNodo(this.getNumPasteleros()); a.setEstOpt(2); b.setEstOpt(8); c.setEstOpt(5); d.setEstOpt(2); e.setEstOpt(1); f.setEstOpt(8); g.setEstOpt(9); mon.insertar(a); mon.insertar(b); mon.insertar(c); mon.insertar(d); mon.insertar(e); mon.insertar(f); mon.insertar(g); System.out.println(mon.obtenerCima().getEstOpt()); System.out.println(mon.obtenerCima().getEstOpt()); System.out.println(mon.obtenerCima().getEstOpt()); System.out.println(mon.obtenerCima().getEstOpt()); System.out.println(mon.obtenerCima().getEstOpt()); System.out.println(mon.obtenerCima().getEstOpt()); System.out.println(mon.obtenerCima().getEstOpt()); debug(); try { asignaPasteleros(TTabla,pedido,pasteleros,costeTotal); } catch (CloneNotSupportedException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } debug(); //escribeSalida(); } public creaPasteleria(boolean traza, String salida) throws IOException { /* super(); this.traza = traza; this.salida = salida; Scanner lector = new Scanner (System.in); System.out.println("Introduce el número de objetos"); float peso,beneficio,capacidad; int entrada = lector.nextInt(); if(entrada <= 0) { lector.close(); throw new IOException("Error, dato leido incorrecto. El número de objetos ha de ser entero y mayor a 0."); } else this.setNumObjetos(entrada); for(int i = 1; i <= this.getNumObjetos(); i++) { System.out.println("Introduce el peso del objeto "+i+" :"); peso = lector.nextFloat(); if(peso < 0) { lector.close(); throw new IOException("Error, dato leido incorrecto. El peso ha de ser mayor o igual a 0."); } System.out.println("Introduce el beneficio del objeto "+i+" :"); beneficio = lector.nextFloat(); if(beneficio < 0) { lector.close(); throw new IOException("Error, dato leido incorrecto. El beneficio ha de ser mayor o igual a 0."); } this.creaObjeto(peso, beneficio); } System.out.println("Introduce la capacidad de la mochila"); capacidad = lector.nextFloat(); if(capacidad < 0) { lector.close(); throw new IOException("Error, dato leido incorrecto. La capacidad ha de ser mayor o igual a 0."); } else this.setCapacidad(capacidad); lector.close(); resuelveMochila(); escribeSalida(); */ } public int getNumPasteleros() { return numPasteleros; } public void setPasteleros(int pasteleros) { this.numPasteleros = pasteleros; } public int getPasteles() { return numPasteles; } public void setPasteles(int pasteles) { this.numPasteles = pasteles; } private void asignaPasteleros(int [][] costes, int[] pedido, int[] pasteleros, int costeT) throws CloneNotSupportedException { Monticulo m; TNodo nodo = new TNodo(getNumPasteleros()); TNodo hijo = new TNodo(getNumPasteleros()); int cota,estPes; m = new Monticulo(0,factorial(this.numPasteleros)); costeT = 0; //nodo.setPasteleros(pasteleros); nodo.pasteleros = pasteleros; for(int i=1; i<=this.numPasteleros;i++) { //nodo.getAsignados()[i] = false; nodo.asignados[i] = false; } //nodo.setK(0); nodo.k = 0; //nodo.setCosteT(0); nodo.costeT = 0; //nodo.setEstOpt(estimacionOpt(costes,pedido,nodo.getK(),nodo.getCosteT())); nodo.estOpt = estimacionOpt(costes,pedido,nodo.k,nodo.costeT); m.insertar(nodo); //cota = estimacionPes(costes,pedido,nodo.getK(),nodo.getCosteT()); cota = estimacionPes(costes,pedido,nodo.k,nodo.costeT); while(!m.monticuloVacio() && (estimacionOpt(costes,pedido,m.primero().k,m.primero().costeT) <= cota)) { nodo = m.obtenerCima(); //se generan las extensiones válidas del nodo.Para cada pastelero no asignado se crea un nodo. hijo.setK(nodo.getK()+1); hijo.setPasteleros(nodo.getPasteleros()); hijo.setAsignados(nodo.getAsignados()); for(int i=1;i<=this.numPasteleros;i++) { if(!hijo.getAsignados()[i]) { hijo.getPasteleros()[hijo.getK()] = i; hijo.getAsignados()[i] = true; hijo.setCosteT(nodo.getCosteT()+costes[i][pedido[hijo.getK()]]); if(hijo.getK() == this.numPasteleros) { if(cota >= hijo.getCosteT()) { this.pasteleros = hijo.getPasteleros(); costeT = hijo.getCosteT(); this.costeTotal = costeT; cota = costeT; debug(); } } else { //la solució no está completa hijo.setEstOpt(estimacionOpt(costes,pedido,hijo.getK(),hijo.getCosteT())); TNodo clon = (TNodo)hijo.clone(); m.insertar(clon); estPes = estimacionPes(costes,pedido,hijo.getK(),hijo.getCosteT()); if(cota > estPes) { cota = estPes; } } hijo.getAsignados()[i] = false; } } } System.out.println("patata"); } private int estimacionOpt(int[][] costes, int[] pedido, int k, int costeT) { int estimacion, menorC; estimacion = costeT; for(int i=k+1;i<=this.numPasteleros;i++) { menorC = costes[1][pedido[i]]; for(int j=2;j<=this.numPasteleros;j++) { if(menorC > costes[j][pedido[i]]) { menorC = costes[j][pedido[i]]; } } estimacion += menorC; } return estimacion; } private int estimacionPes(int[][] costes, int[] pedido, int k, int costeT) { int estimacion, mayorC; estimacion = costeT; for(int i=k+1;i<=this.numPasteleros;i++) { mayorC = costes[1][pedido[i]]; for(int j=2;j<=this.numPasteleros;j++) { if(mayorC < costes[j][pedido[i]]) { mayorC = costes[j][pedido[i]]; } } estimacion += mayorC; } return estimacion; } private boolean isNumberOk(String string) { if (string == null || string.isEmpty()) { return false; } if (string.charAt(0) == '-') { string.charAt(0); return false; } for (int i = 0; i < string.length(); i++) { if (!Character.isDigit(string.charAt(i)) && !(string.charAt(i) == '.')) { return false; } } return true; } private void debug() { System.out.println("------------------------------------------------------------------------------------------------------------------------------------------"); System.out.println("Debug Activado"); System.out.println("El número de pasteleros es: "+numPasteleros); System.out.println("El número de pasteles es: "+numPasteles); for (int i=1; i< pedido.length; i++) { System.out.println("El valor del pedido "+(i)+" es: "+pedido[i]); } for (int i=1; i<= numPasteleros;i++) { for(int j=1; j<= numPasteles;j++) { System.out.print(this.TTabla[i][j]+" "); } System.out.printf("%n"); } System.out.println("El vector pasteleros es:"); for(int i=1; i<= numPasteleros;i++) { System.out.print("["+this.pasteleros[i]+"]"); } System.out.println(""); System.out.println("El coste total es: "+this.costeTotal); System.out.println("Fin Debug"); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------"); } private int factorial(int n) { int fact=1; for (int i = 1; i <= n; i++) { fact = fact * i; } return fact; } }
Editor is loading...