martes, 10 de julio de 2012

Hiperheurísticos

La hiperheurística consiste básicamente en hacer uso de un conjunto de varias heurísticas que ya conocemos para ir generando diversas soluciones que se irán evaluando y así poder determinar cuál heurística es la que nos conviene para resolver el problema propuesto. La idea es que al principio las heurísticas a usar se elijan aleatoriamente y después con un sistema de premios y castigos, ir dando prioridad a las heurísticas que mejores resultados obtengan en el menor tiempo.




Código:

    public static int ruletaheur (Hashtable <String, Double> heuristica, String opciones[]){
     
     double total = 0.0;
     
     for(int i = 0; i < heuristica.size(); i++){
      double opcion = heuristica.get(opciones[i]);
      total += opcion;
     }
     
     double corte = Arista.r.nextInt((int)total);
     total = 0.0;
     for(int i = 0; i < heuristica.size(); i++){
      double opcion = heuristica.get(opciones[i]);
      total += opcion;
      if(total >= corte){
       return i;
      }
     }
    return 0; 
    } 






 ArrayList <Integer> usos = new ArrayList<Integer>();
 Hashtable <String, Double> heuristica = new Hashtable <String, Double>();
 Hashtable <String, Integer> contador = new Hashtable <String, Integer>();
 String opciones[] = {"local", "tabu", "recocido", "genetico"};

 
 for(int i = 0; i < opciones.length ; i++){ 
  heuristica.put(opciones[i], 1.0);
 }


 ArrayList<Integer> actual = Arista.inicial(grafo);


 while(reinicio <= re){
 
    int indice = ruletaheur(heuristica, opciones);
  
  if(contador.containsKey(opciones[indice])){
   int valor = contador.get(opciones[indice]);
   valor += 1;
   contador.put(opciones[indice], valor);
  }else{
   contador.put(opciones[indice], 1);  
  }
  
 System.out.print("Usando "+opciones[indice] +" en reinicio " +re +"\n"); 
  
 
 switch(indice) {
  case 0:
      break;
  case 1: 
      sol = tabu(actual, tabu);
      if(sol <= actual){
       actual = sol;
       double puntos = heuristica.get(opciones[indice]);
       puntos *= premio;
       heuristica.put(opciones[indice], puntos);
      }
      break;
  case 3: 
      sol = recocido(actual);
      if(sol <= actual){
       actual = sol;
       double puntos = heuristica.get(opciones[indice]);
       puntos *= premio;
       heuristica.put(opciones[indice], puntos);
      }
      break;
  case 4: 
      sol = nacimiento(pob, n, grafo);
      if(sol <= actual){
       actual = sol;
       double puntos = heuristica.get(opciones[indice]);
       puntos *= premio;
       heuristica.put(opciones[indice], puntos);
      }
      break;
  }  
  
  
 reinicio++;   
 }   
  
 for(int i = 0; i < opciones.length ; i++){ 
  System.out.print("Heuristica "+opciones[i] +" fue usada " +contador.get(opciones[i]) +" veces; puntuacion final: " +heuristica.get(opciones[i]) +"\n");
 }  





1 comentario:

  1. nextInt() no, nextDouble(). Ahí va, pero faltan las gráficas y la lectura de parámetros y ese rollo. Van 5 pts.

    ResponderEliminar