Beruflich Dokumente
Kultur Dokumente
newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
;******************
;Laberinto
;******************
(defparameter*OPEN*'(nil));;Fronteradebusqueda
(defparameter*CLOSED*'(nil));;Memoriadeanalizados
(defparameter*nNodos*0);;Numerodenodoscreados
(defparameter*SOLUCION*'(nil));;Guardalosnodosdelarutasolucion
(defvar*Laberinto*(makearray'(100100):elementtype'list:initialelementnil))
(defvar*INICIO*'())
(defvar*META*'())
(defvar*renglones*0)
(defvar*columnas*0)
(defunNodoSiguiente()(setq*nNodos*(+*nNodos*1)))
(defunreaddatafile(path)
(let()
(withopenfile(streampath)
(setq*renglones*(readstreamnilnil))
(setq*columnas*(readstreamnilnil))
(setf*INICIO*(list(readstreamnilnil)(readstreamnilnil)))
(setf*META*(list(readstreamnilnil)(readstreamnilnil)))
(setf*Laberinto*(adjustarray*Laberinto*(list*renglones*
*columnas*)))
(readlinestreamnilnil);;lecturaenfalsodepleca
(loopforrenglonfrom0to(*renglones*1)do
(loopforcolumnafrom0to(*columnas*1)do
(setf(aref*Laberinto*rengloncolumna)
(readstreamnilnil))
))
(defunInicializa()(setf(first*OPEN*)nil)
(setf(first*CLOSED*)nil)
(setq*nNodos*0)
(setf(first*SOLUCION*)nil)
(READDATAFILE"/home/newbie/Desktop/laberinto1.data")
)
;estado:(1234)
;posicion:1|2|3|4
(defunbloqueado?(estadoposicion)(cond((nullestado)nil)
posicion)T)
(restestado)posicion))
))
((=(firstestado)
(T(bloqueado?
(defunMoverArriba(estado)(NodoSiguiente)
(if(AND(not(=(nth0(nth1estado))0))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1
(nth1estado)))1));;arriba
(not(bloqueado?(aref*Laberinto*((nth0(nth1estado))1)
(nth1(nth1estado)))3));;abajao
(list(nth4estado)(list((nth0(nth1estado))1)(nth1(nth1estado)))
(+(nth2estado)1)'arriba*nNodos*)
nil
)
)
(defunMoverArribaDerecha(estado)(NodoSiguiente)
(if(AND(not(=(nth0(nth1estado))0))
(not(=(nth1(nth1estado))(*columnas*1)))
(OR
(AND
(not(bloqueado?(aref*Laberinto*((nth0(nth1estado))1)(+(nth1
http://newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
1/6
02/11/2016
newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
(nth1estado))1))3));;abajo
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1(nth1
estado)))2))
(AND
(not(bloqueado?(aref*Laberinto*((nth0(nth1estado))1)(+(nth1(nth1
estado))1))4));;izq
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1(nth1estado)))
1))
(list(nth4estado)(list((nth0(nth1estado))1)(+(nth1(nth1estado))
1))(+(nth2estado)1)'arribaderecha*nNodos*)
nil
)
)
(defunMoverDerecha(estado)(NodoSiguiente)
(if(AND(not(=(nth1(nth1estado))(*columnas*1)))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(+(nth
1(nth1estado))1))4));;der
(list(nth4estado)(list(nth0(nth1estado))(+(nth1(nth1estado))1))
(+(nth2estado)1)'derecha*nNodos*)
nil
)
)
(defunMoverAbajoDerecha(estado)(NodoSiguiente)
(if(AND(not(=(nth0(nth1estado))(*renglones*1)))
(not(=(nth1(nth1estado))(*columnas*1)))
(OR
(AND
(not(bloqueado?(aref*Laberinto*(+(nth0(nth1estado))1)(+(nth1
(nth1estado))1))1))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1(nth1
estado)))2))
(AND
(not(bloqueado?(aref*Laberinto*(+(nth0(nth1estado))1)(+(nth1
(nth1estado))1))4))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1(nth1
estado)))3))
(list(nth4estado)(list(+(nth0(nth1estado))1)(+(nth1(nth1estado))
1))(+(nth2estado)1)'abajoderecha*nNodos*)
nil
)
)
(defunMoverAbajo(estado)(NodoSiguiente)
(if(AND(not(=(nth0(nth1estado))(*renglones*1)))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1
(nth1estado)))3));;arriba
(list(nth4estado)(list(+(nth0(nth1estado))1)(nth1(nth1estado)))
(+(nth2estado)1)'abajo*nNodos*)
nil
)
)
(defunMoverAbajoIzquierda(estado)(NodoSiguiente)
(if(AND(not(=(nth0(nth1estado))(*renglones*1)))
(not(=(nth1(nth1estado))0))
(OR
(AND
http://newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
2/6
02/11/2016
newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
(not(bloqueado?(aref*Laberinto*(+(nth0(nth1estado))1)(
(nth1(nth1estado))1))1))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1
(nth1estado)))4))
(AND
(not(bloqueado?(aref*Laberinto*(+(nth0(nth1estado))1)(
(nth1(nth1estado))1))2))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1
(nth1estado)))3))
(list(nth4estado)(list(+(nth0(nth1estado))1)((nth1(nth1estado))
1))(+(nth2estado)1)'abajoizquierda*nNodos*)
nil
)
)
(defunMoverIzquierda(estado)(NodoSiguiente)
(if(AND(not(=(nth1(nth1estado))0))
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))((nth
1(nth1estado))1))2));;der
(list(nth4estado)(list(nth0(nth1estado))((nth1(nth1estado))1))
(+(nth2estado)1)'izquierda*nNodos*)
nil
)
)
(defunMoverArribaIzquierda(estado)(NodoSiguiente)
(if(AND(not(=(nth0(nth1estado))0))
(not(=(nth1(nth1estado))0))
(OR
(AND
(not(bloqueado?(aref*Laberinto*((nth0(nth1estado))1)((nth1
(nth1estado))1))2));;abajo
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1(nth1
estado)))1))
(AND
(not(bloqueado?(aref*Laberinto*((nth0(nth1estado))1)((nth1(nth
1estado))1))3));;izq
(not(bloqueado?(aref*Laberinto*(nth0(nth1estado))(nth1(nth1
estado)))4))
(list(nth4estado)(list((nth0(nth1estado))1)((nth1(nth1estado))
1))(+(nth2estado)1)'arribaizquierda*nNodos*)
nil
)
)
(defunCrearEstado(idPadreposicionActualprofundidadoperador)(NodoSiguiente)(listidPadre
posicionActualprofundidadoperador*nNodos*))
(defunInsertaNodo(nodo*OPEN*metodo)
(cond((and(equalmetodo'profundo))
(setf(first*OPEN*)(append(first
*OPEN*)(listnodo))))
((and(or(equalmetodo'ancho)(equalmetodo
'estrella)(equalmetodo'bestfirst)))
(setf(first*OPEN*)(append(first
*OPEN*)(listnodo))))
))
(defunExtraeNodo(*OPEN*metodo)
(cond((equalmetodo'profundo)(first(first*OPEN*)))
((and(null(last(first*OPEN*)))(or(equal
metodo'ancho)
http://newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
3/6
02/11/2016
newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
(equalmetodo'estrella)
(equalmetodo'bestfirst)))(first(first*OPEN*)))
((or(equalmetodo'ancho)(equalmetodo
'estrella)(equalmetodo'bestfirst))(first(last(first*OPEN*))));out<cola<in
))
(defunActualiza(nodo*OPEN**CLOSED*)
(cond((nullnodo)nil)
(t(setf(first*CLOSED*)(append(listnodo)
(first*CLOSED*)))
(setf(first*OPEN*)(removenodo(first
*OPEN*)))
)))
;;RegresaTsielestadoseencuentradentrodelalistadenodos
;;dada,delocontrarioregresanil
(defunrecuerdo?(estadolistanodos)(cond((nulllistanodos)nil)
estado)(second(firstlistanodos)))T)
listanodos)))
))
((equal(second
(T(recuerdo?estado(rest
;;Filtradelalistadeestadosaquellosnodosyaanalizados
;;enestecasoaquelloscontenidosenlistanodos
(defunFiltraRecuerdos(listaestadoslistanodos)(cond((nulllistaestados)nil)
((null(firstlistanodos))listaestados)
((recuerdo?(firstlistaestados)(firstlistanodos))
(FiltraRecuerdos(restlistaestados)listanodos))
(T(cons(firstlistaestados)
(FiltraRecuerdos(restlistaestados)listanodos)))
))
(defundistancia(nodoestado)
(abs(+((nth0(nth1nodo))(nth0estado))
((nth1(nth1nodo))(nth1estado))
))
)
(defunaptitud(nodo)(distancianodo*META*))
(defuncosto(nodo)(nth2nodo))
(defunpertinencia(nodo)
(if(not(nullnodo))(list(nth0nodo)
(defunpertinenciaBest(nodo)
(if(not(nullnodo))(list(nth0nodo)
(nth1nodo)
(nth2nodo)
(nth3nodo)
(nth4nodo)
(aptitudnodo)
)nil))
(nth1nodo)
(nth2nodo)
(nth3nodo)
(nth4nodo)
(+(costonodo)
(aptitudnodo))
)nil))
(defunExpande(nodometodo)
http://newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
4/6
02/11/2016
newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
(cond
((equalmetodo'profundo)
(list
(MoverArribanodo)
(MoverArribaDerechanodo)
(MoverDerechanodo)
(MoverAbajoDerechanodo)
(MoverAbajonodo)
(MoverAbajoIzquierdanodo)
(MoverIzquierdanodo)
(MoverArribaIzquierdanodo)
))
((equalmetodo'ancho)
(list
(MoverArribaIzquierdanodo)
(MoverIzquierdanodo)
(MoverAbajoIzquierdanodo)
(MoverAbajonodo)
(MoverAbajoDerechanodo)
(MoverDerechanodo)
(MoverArribaDerechanodo)
(MoverArribanodo)
))
((equalmetodo'estrella)
(sort
(removenil
(list
(pertinencia(MoverArribanodo))
(pertinencia(MoverArribaDerechanodo))
(pertinencia(MoverDerechanodo))
(pertinencia(MoverAbajoDerechanodo))
(pertinencia(MoverAbajonodo))
(pertinencia(MoverAbajoIzquierdanodo))
(pertinencia(MoverIzquierdanodo))
(pertinencia(MoverArribaIzquierdanodo))
#'>:key#'(lambda(x)(nth5x)))
)
((equalmetodo'bestfirst)
(sort
(removenil
(list
(pertinenciaBest(MoverArribanodo))
(pertinenciaBest(MoverArribaDerechanodo))
(pertinenciaBest(MoverDerechanodo))
(pertinenciaBest(MoverAbajoDerechanodo))
(pertinenciaBest(MoverAbajonodo))
(pertinenciaBest(MoverAbajoIzquierdanodo))
(pertinenciaBest(MoverIzquierdanodo))
(pertinenciaBest(MoverArribaIzquierdanodo))
)
)
#'>:key#'(lambda(x)(nth5x)))
)
))
;;Regresaelnodoconelidentificadelpadredelnododado
;;Encasodenoencontrarlodevuelvenil
(defunrecuerdoIDAn?(nodolistanodos)(cond((nulllistanodos)nil)
nodo)(nth4(firstlistanodos)))(firstlistanodos))
nodo(restlistanodos)))
))
http://newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
((equal(nth0
(T(recuerdoIDAn?
5/6
02/11/2016
newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
;;Agregaelnododadoalalistadenodossolucion
(defunagregaNodoSolucion(nodo)(setf(first*SOLUCION*)(append(first*SOLUCION*)(list(nth3
nodo)))))
;;Rastrealarutadesdeelnododadohastalaraiz,eimprimeunoporuno
(defunRastreaSolucion(nodo*CLOSED*)(cond((nullnodo)nil)
(T;;
(print(nth3nodo))
(agregaNodoSolucionnodo)
(RastreaSolucion(recuerdoIDAn?nodo(first*CLOSED*))*CLOSED*))
))
;;Imprimelalistadada(SOLUCION)
(defunimprimeSolucion(solucion)
(imprimeSolucion(restsolucion)))
(cond((nullsolucion)nil)
(T(print(firstsolucion))
))
(defunLaberinto(metodo)
(let((nodoNIL)(sucesores'()))
(Inicializa)
(InsertaNodo(CrearEstado0*INICIO*0nil)*OPEN*metodo)
(loopuntil(null(first*OPEN*))do
(setqnodo(ExtraeNodo*OPEN*metodo))
(cond
((equal(nth1nodo)*META*)
(print'***********SOLUCION*********)
(RastreaSolucionnodo*CLOSED*)
(imprimeSolucion(reverse(butlast(first*SOLUCION*))))
(print(first*SOLUCION*))
(setf(first*OPEN*)nil)
)
(t(setqsucesores(removenil(Expandenodometodo)))
(Actualizanodo*OPEN**CLOSED*)
(setqsucesores(FiltraRecuerdossucesores*CLOSED*))
(setqsucesores(removenilsucesores))
(dolist(nsucesores)
(InsertaNodon*OPEN*metodo))
(if(equalmetodo'estrella)(setf(first*OPEN*)(sort(first
*OPEN*)#'>:key#'(lambda(x)(nth5x)))))
);tend
);condend
);loopend
(print'**************ESTADISTICAS**************)
(print'NodosCreados)
(print*nNodos*)
(print'NodosAnalizados)
(print(length(first*CLOSED*)))
(print'NodosPendientes)
(print(*nNodos*(length(first*CLOSED*))))
(returnfromLaberintonil)
))
http://newbiegeek.com/wpcontent/uploads/2013/05/laberinto.txt
6/6