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"); }
nextInt() no, nextDouble(). Ahí va, pero faltan las gráficas y la lectura de parámetros y ese rollo. Van 5 pts.
ResponderEliminar