Guest User

Untitled

a guest
Jun 14th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. public class BackTracking {
  2.  
  3.     private List<Coche> resultado = new ArrayList<Coche>(); //Lista con mi resultado
  4.     public List<Coche> recursividad(Integer indice,List<Coche> lista,Integer presupueto){
  5.  
  6.         if((indice == Test.lista.size() || presupueto == Test.presupuesto) && getPrimero(lista)){ //Cuando haya recorrido toda la lista o haya gastado todo el presupuesto
  7.  
  8.             //Obtengo la lista con mayor numero de votos
  9.             if(resultado.size() == 0 || lista.stream().mapToDouble(x->x.getSatisfaccionClientes()).sum() > resultado.stream().mapToDouble(x->x.getSatisfaccionClientes()).sum()){
  10.                 resultado.clear(); //Elimino la lista que hubiese antes
  11.                 resultado.addAll(lista); //Añado la nueva lista
  12.             }
  13.  
  14.        }else{
  15.                 for(Boolean a : getAlternativa(lista,indice,presupueto)){ //Las alternativas
  16.  
  17.                     if(a == true){
  18.                         presupueto+=Test.lista.get(indice).getCoste(); //Añado
  19.                         lista.add(Test.lista.get(indice));
  20.                     }
  21.                     recursividad(indice+1, lista,presupueto);
  22.                     if(a == true){
  23.                         presupueto-=Test.lista.get(indice).getCoste(); //Elimino
  24.                         lista.remove(Test.lista.get(indice));
  25.                     }
  26.                 }
  27.         }
  28.         return resultado;
  29.  
  30.     }
  31.    
  32.     public List<Boolean> getAlternativa(List<Coche> lista,Integer indice,Integer presupueto){
  33.         List<Boolean> res = new ArrayList<Boolean>();
  34.         if(indice >= 0 && indice < Test.lista.size()){
  35.             if(presupueto + Test.lista.get(indice).getCoste() <= Test.presupuesto){
  36.                 res.add(true);
  37.             }
  38.             res.add(false);
  39.         }
  40.         return res;
  41.     }
  42.    
  43.     //Al menos un coche de cada tipo de coche
  44.     public Boolean getPrimero(List<Coche> lista){
  45.             Boolean res = false;
  46.             Integer num1= 0;
  47.             Integer num2= 0;
  48.             Integer num3= 0;
  49.             Integer num4= 0;
  50.             for(Coche a : lista){
  51.                    if(a.getTipoCoche().equals(Coche.TipoCoche.Berlina)){
  52.                        num1++;
  53.                    }
  54.                    if(a.getTipoCoche().equals(Coche.TipoCoche.Compacto)){
  55.                        num2++;
  56.                    }
  57.                    if(a.getTipoCoche().equals(Coche.TipoCoche.Familiar)){
  58.                        num3++;
  59.                    }
  60.                    if(a.getTipoCoche().equals(Coche.TipoCoche.Utilitario)){
  61.                        num4++;
  62.                    }
  63.             }
  64.             if(num1 >= 1 && num2 >= 1 && num3 >= 1 && num4 >= 1){
  65.                 res = true;
  66.             }
  67.             return res;
  68.     }
  69.    
  70. }
Add Comment
Please, Sign In to add comment