Un piccolo problema di planning: Brain Twist
Transcript
Un piccolo problema di planning: Brain Twist
Università degli Studi di Udine Dipartimento di Matematica e Informatica Un piccolo problema di planning: Brain Twist Andrea Viel Anno accademico 2015-2016 Introduzione I I L’obiettivo del gioco è quello di avere un colore uniforme su tutte le facce (alternativamente sulle punte). E’ simile a un cubo di Rubik, solo piramidale. Il gioco I I E’ possibile ruotare i vertici in senso orario o antiorario. Le tessere hanno un colore esterno e interno, dunque il gioco può aprirsi (flippare) e mostrare il colore che prima era nascosto all’interno. Il gioco Dimostrazione Demo Rappresentazione del gioco P2 10 P8 12 11 9 8 P7 4 3 P1 1 P5 5 P3 P4 2 7 I I P6 6 Ci sono 12 tessere per 12 posizioni. Ci sono 4 vertici in una configurazione e altri 4 che appaiono dopo il flip. Rappresentazione del gioco %p a s s i d e l g i o c o time ( 1 . . n ) . %l e t e s s e r e c o l o r a t e d i c u i è composto i l %hanno un c o l o r e e s t e r n o e uno i n t e r n o t e s s ( r c ) . t e s s ( bc ) . t e s s ( f c ) . t e s s ( r g ) . t e s s ( ag ) . t e s s ( f g ) . t e s s ( as ) . t e s s ( bs ) . t e s s ( f s ) . t e s s ( r v ) . t e s s ( av ) . t e s s ( bv ) . %c o l o r i e s t e r n i rosso ( rc ; rg ; rv ) . b l u ( bc ; bv ; b s ) . f u c s i a ( fg ; fc ; f s ) . a r a n c i o n e ( ag ; a s ; av ) . %c o l o r i i n t e r n i c e l e s t e ( r c ; bc ; f c ) . v e r d e ( r v ; av ; bv ) . g i a l l o ( r g ; ag ; f g ) . smeraldo ( as ; bs ; f s ) . puzzle Rappresentazione del gioco %s e è f l i p p a t o o p p u r e no , da c u i d i p e n d e s e è v i s i b i l e colore interno o esterno flip (0..1) . il % 12 p o s i z i o n i p e r i 12 t a s s e l l i pos ( 1 . . 1 2 ) . % 8 v e r t i c i r u o t a b i l i , 4 p e r c o n f i g u r a z i o n e . La mossa 0 è i l flip mossa ( − 8 . . 8 ) . %n e g a t i v o s i g n i f i c a r o t a z i o n e a n t i o r a r i a % una c o n f i g u r a z i o n e e s i s t e n t e p e r o g n i i s t a n t e % a l l ’ i s t a n t e Ti l a t e s s e r a X s t a n e l l a p o s i z i o n e P 1{ on ( Ti , X , P) : p o s (P) }1: − t i m e ( Ti ) , t e s s (X) . % a l l ’ i s t a n t e Ti i l g i o c o è f l i p p a t o o no 1{ on ( Ti , f , F ) : f l i p ( F ) }1: − t i m e ( Ti ) . La scelta %s c e g l i e una s i n g o l a a z i o n e non d e t e r m i n i s t i c a m e n t e a o g n i i s t a n t e in base a l f l i p s c e l t o (X , Ti ) :− t i m e ( Ti ) , mossa (X) , p o s s i b i l e (X , Ti ) , no t d i f f s c e l t o (X , Ti ) . d i f f s c e l t o (X , Ti ) :− t i m e ( Ti ) , mossa (X) , mossa (Y) , p o s s i b i l e (X , Ti ) , p o s s i b i l e (Y , Ti ) , s c e l t o (Y , Ti ) , X!=Y . p o s s i b i l e ( 0 , Ti ) :− t i m e ( Ti ) . % f l i p è s e m p r e p o s s i b i l e %s e f l i p =0 ho a c c e s s o a i v e r t i c i 1 . . 4 p o s s i b i l e (X , Ti ) :− mossa (X) , t i m e ( Ti ) , on ( Ti , f , 0 ) , X >= −4, X !=0 , X <= 4 . % e v i c e v e r s a f l i p =1 a c c e d o a i v e r t i c i 5 . . 8 p o s s i b i l e (X , Ti ) :− mossa (X) , t i m e ( Ti ) , on ( Ti , f , 1 ) , X >= 5 . p o s s i b i l e (X , Ti ) :− mossa (X) , t i m e ( Ti ) , on ( Ti , f , 1 ) , X <= −5. Vincoli Aggiuntivi % e v i t a r e mosse r i d o n d a n t i :− s c e l t o (X , Ti +1) , s c e l t o (X , Ti ) , mossa (X) , t i m e ( Ti ) . %f a r e due v o l t e l a s t e s s a mossa non ha s e n s o s e è un f l i p ma nemmeno s e è una r o t a z i o n e p e r c h è è come f a r n e una s o l a ma i n s e n s o o p p o s t o :− s c e l t o (X , Ti +1) , s c e l t o (−X , Ti ) , mossa (X) , t i m e ( Ti ) , X != 0 . % non ha s e n s o f a r e e d i s f a r e una r o t a z i o n e % v i n c o l i a g g i u n t i v i , non s t r e t t a m e n t e n e c e s s a r i ma velocizzano la ricerca 1{ s c e l t o (X , Ti ) : mossa (X) }1: − t i m e ( Ti ) . %v i n c o l o 1 8{ d i f f s c e l t o (X , Ti ) : mossa (X) }8: − t i m e ( Ti ) . %v i n c o l o 2 9{ p o s s i b i l e (X , Ti ) : mossa (X) }9: − t i m e ( Ti ) . %v i n c o l o 3 Le mosse Flip %f l i p p a r e cambia s o l o i l f l i p ma non l o s t a t o d e l l e on ( Ti +1 , f , 0 ) :− s c e l t o ( 0 , Ti ) , on ( Ti , f , 1 ) , t i m e ( Ti ) . on ( Ti +1 , f , 1 ) :− s c e l t o ( 0 , Ti ) , on ( Ti , f , 0 ) , t i m e ( Ti ) . on ( Ti +1 ,X , P) :− s c e l t o ( 0 , Ti ) , on ( Ti , X , P) , t i m e ( Ti ) , t e s s (X) , p o s (P) . tessere %v i c e v e r s a o g n i a l t r a mossa non cambia i l f l i p on ( Ti +1 , f , F ) :− s c e l t o (R , Ti ) , on ( Ti , f , F ) , t i m e ( Ti ) , f l i p ( F ) , mossa (R) , R != 0 . Le mosse Numerazione delle posizioni I Un’ottimizzazione consiste nel considerare la posizione 1, in ogni istante, quella in cui si trova la tessera rc e le altre di conseguenza. I Complica la codifica delle rotazioni dei vertici ma semplifica la ricerca eliminando le simmetrie. I Le configurazioni possibili finali diventano solamente due al posto di 24. Le mosse Rotazione %l a p o s i z i o n e 1 è s e m p r e o c c u p a t a da r c on ( Ti , r c , 1 ) :− t i m e ( Ti ) . %l a t e s s e r a i n p o s i z i o n e 2 è s o s t i t u i t a n e l l e r o t a z i o n i 5 e 6 on ( Ti +1 ,X , 2 ) :− s c e l t o ( 5 , Ti ) , on ( Ti , X , 9 ) , t i m e ( Ti ) , t e s s (X) . %s i g n i f i c a c h e con l a r o t a z i o n e 5 l a t e s s e r a c h e s t a v a i n p o s i z i o n e 9 va in posizione 2 on ( Ti +1 ,X , 2 ) :− s c e l t o ( 6 , Ti ) , on ( Ti , X , 7 ) , t i m e ( Ti ) , t e s s (X) . on ( Ti +1 ,X , 2 ) :− s c e l t o ( −6 , Ti ) , on ( Ti , X , 6 ) , t i m e ( Ti ) , t e s s (X) . on ( Ti +1 ,X , 2 ) :− s c e l t o ( −5 , Ti ) , on ( Ti , X , 1 1 ) , t i m e ( Ti ) , t e s s (X) . %con l e a l t r e r o t a z i o n i non s i muove on ( Ti +1 ,X , 2 ) :− s c e l t o (R , Ti ) , on ( Ti , X , 2 ) , t i m e ( Ti ) , t e s s (X) , mossa (R) , R != 5 , R != 6 , R != 0 , R != −5, R != −6. Goal g o a l :− on ( n + 1 , X2 , 2 ) , on ( n + 1 , X3 , 3 ) , on ( n + 1 , X4 , 4 ) , on ( n + 1 , X5 , 5 ) , on ( n + 1 , X6 , 6 ) , on ( n + 1 , X7 , 7 ) , on ( n + 1 , X8 , 8 ) , on ( n + 1 , X9 , 9 ) , on ( n + 1 , X10 , 1 0 ) , on ( n + 1 , X11 , 1 1 ) , on ( n + 1 , X12 , 1 2 ) , %f a c c e s t e s s o c o l o r e o u t c o l o r ( r c , X4 , X5 ) , o u t c o l o r ( X2 , X6 , X7 ) , o u t c o l o r ( X3 , X8 , X9 ) , o u t c o l o r ( X10 , X11 , X12 ) , i n c o l o r ( r c , X2 , X3 ) , i n c o l o r ( X4 , X9 , X10 ) , i n c o l o r ( X5 , X6 , X11 ) , i n c o l o r ( X7 , X8 , X12 ) , %p u n t e s t e s s o c o l o r e %i n c o l o r ( r c , X4 , X5 ) , i n c o l o r ( X2 , X6 , X7 ) , i n c o l o r ( X3 , X8 , X9 ) , i n c o l o r ( X10 , X11 , X12 ) , %o u t c o l o r ( r c , X2 , X3 ) , o u t c o l o r ( X4 , X9 , X10 ) , o u t c o l o r ( X5 , X6 , X11 ) , o u t c o l o r ( X7 , X8 , X12 ) , t e s s ( X2 ) , t e s s ( X3 ) , t e s s ( X4 ) , t e s s ( X5 ) , t e s s ( X6 ) , t e s s ( X7 ) , t e s s ( X8 ) , t e s s ( X9 ) , t e s s ( X10 ) , t e s s ( X11 ) , t e s s ( X12 ). Fine