Está en la página 1de 2

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ia_201502.algoritmos;
import
import
import
import
import
import
import
import
import
import
import
import
import

ia_201502.estructuras.Reporte;
ia_201502.estructuras.Nodo;
ia_201502.puzzle.Puzzle;
ia_201502.util.MapUtil;
ia_201502.util.MatrizUtil;
java.util.ArrayList;
java.util.HashMap;
java.util.LinkedList;
java.util.List;
java.util.Objects;
java.util.PriorityQueue;
java.util.Stack;
objectexplorer.MemoryMeasurer;

/**
*
* @author LAB_UD_19_PC3
*/
public class AStar implements Algoritmo {
Long nodosEnRam = new Long(0);
Integer nodosGenerados = 0;
boolean encuentraSolucion = false;
Double tiempo = 0.0;
int[][] estadoFinal;
int[][] estadoInicial;
Puzzle puzzle;
List<List<Integer>> resultado;
public AStar(Puzzle puzzle, int[][] estadoFinal) {
this.estadoFinal = estadoFinal;
this.estadoInicial = puzzle.getEstado();
this.puzzle = puzzle;
}
@Override
public void correr() {
long tiempoInicio = System.nanoTime();
PriorityQueue<Nodo> queue = new PriorityQueue<>(new Nodo());
Heuristica heuristica = new Heuristica();
Nodo nodo = new Nodo(puzzle.getEstado(), heuristica.calcularHeuristica(p
uzzle.getEstado(), estadoFinal, 3));
queue.add(nodo);
//Hashmap de visitados
HashMap<List<Integer>, Boolean> visitado = new HashMap<>();
//Hashmap de padres para calcular distancia hacia el origen
HashMap<List<Integer>, List<Integer>> padres = new HashMap<>();
//Marca el nodo inicial como si no tuviese padre.
int[][] origen = MatrizUtil.copiarMatriz(puzzle.getEstado(), 3);

padres.put(MatrizUtil.aplanar(origen), MatrizUtil.aplanar(MatrizUtil.gen
erarMatriz(-1)));
while (!queue.isEmpty()) {
Nodo padre = queue.poll();
puzzle.setEstado(padre.getEstado());
if (puzzle.haGanado(estadoFinal)) {
encuentraSolucion = true;
resultado = MapUtil.listaHaciaPadre(padre.getEstado(), origen, p
adres);
break;
} else {
nodosGenerados++;
List<int[][]> hijos = puzzle.getEstadosHijos();
for (int[][] estado : hijos) {
List<Integer> estadoPlano = MatrizUtil.aplanar(estado);
visitado.putIfAbsent(estadoPlano, Boolean.FALSE);
if (Objects.equals(visitado.get(estadoPlano), Boolean.FALSE)
) {
//Agregar este estado como hijo del padre
padres.put(MatrizUtil.aplanar(estado), MatrizUtil.aplana
r(padre.getEstado()));
visitado.put(MatrizUtil.aplanar(estado), Boolean.TRUE);
Integer heuristicaTotal = heuristica.calcularHeuristica(
estado, estadoFinal, 3)
+ MapUtil.distanciaHastaPadre(estado, origen, pa
dres);
queue.add(new Nodo(estado, heuristicaTotal));
nodosEnRam += MemoryMeasurer.measureBytes(new Nodo(estad
o, heuristicaTotal));
}
}
}
}
tiempo = (System.nanoTime() - tiempoInicio)/1000000.0;
}
@Override
public Reporte getReporte() {
Reporte reporte = new Reporte();
reporte.setAlgoritmoUsado("A estrella (A*)");
reporte.setEncontroSolucion(encuentraSolucion ? "S" : "No");
reporte.setEstadoFinal(MatrizUtil.aplanar(estadoFinal).toString());
reporte.setEstadoInicial(MatrizUtil.aplanar(estadoInicial).toString());
reporte.setNodosGenerados(nodosGenerados.toString());
reporte.setNodosGeneradosEnRam(nodosEnRam.toString());
reporte.setResultado(resultado);
Integer movidas = resultado.size() - 1;
reporte.setNumeroMovidas(movidas.toString());
reporte.setTiempo(tiempo.toString());
return reporte;
}
}

También podría gustarte