Commits

Anonymous committed e7548d6

49 steps. 10 to go. canmove now returns movments longer than 1.

Comments (0)

Files changed (2)

src/carparkstatespace.py

 
 
 
-'''this is wierd'''
 class SetParkingStates:
     def __init__(self,s):
         self.d = {}
         return old_nd <= state.nd
 
 class CarParkReversibleForwardStateSpace(IReversibleForwardStateSpace):
-    '''this is correct'''
+ 
     class Car:
         def __init__(self, id ,length , type, pos):
             self.id=id
             self.pos=pos
         def __repr__(self):
             return "Coche {0} - Orientacion {1} - Cordenadas {2}\n".format(self.id,self.type, self.pos)
-            
-    '''this may need revision --- revised by Azas'''        
+           
     class State:
         def __init__(self, car_list):
             self.mat=[]
                 r+="{}".format(i)
                 r+="\n"
             return r
-    '''this may also need revision'''
+  
     def __init__(self, nd,car_list):
             self.nd=nd
             self.list=car_list
     def canmove(self,car,s):
         l=[]
         if car.type=='h':
-            if car.pos[1]+car.length<=5:
-                if s.mat[car.pos[0]][car.pos[1]+car.length] == 0: 
-                    l.append("right")
-            if car.pos[1]-1>=0:
-                if s.mat[car.pos[0]][car.pos[1]-1]== 0:
-                    l.append("left")
+            for i in range(car.pos[1]+car.length,6):
+                if s.mat[car.pos[0]][i] == 0: 
+                    l.append(("right",1+i-(car.pos[1]+car.length)))
+                else: break
+            for i in range(car.pos[1]-1,-1,-1):
+                if s.mat[car.pos[0]][i]== 0:
+                    l.append(("left",abs(i-(car.pos[1]))))
+                else: break
         if car.type=='v':
-            if car.pos[0]-1>=0:
-                if  s.mat[car.pos[0]-1][car.pos[1]] == 0:
-                    l.append("up")
-            if car.pos[0]+car.length<=5:
-                if s.mat[car.pos[0]+car.length][car.pos[1]]== 0:
-                    l.append("down")
+            for i in range(car.pos[0]-1,-1,-1):
+                if  s.mat[i][car.pos[1]] == 0:
+                    l.append(("up",abs(1+i-(car.pos[0]-1))))
+                else: break
+            for i in range(car.pos[0]+car.length,6):
+                if s.mat[i][car.pos[1]]== 0:
+                    l.append(("down",1+i-(car.pos[0]+car.length)))
+                else: break
         return l    
 
     def decisions(self, s:'State'):
                     for posible in posibles:
                         decisiones.append((car.id,posible))
         
-            print("Posibles decisiones: ", decisiones)
+#            print("Posibles decisiones: ", decisiones)
         return decisiones
         
             
     def decide(self, s:'State', d:'decision'):
          car=s.list[d[0]-1]
          
-         print("Decide:\n",s,d)
-         if d[1] =="up":
-             car.pos =(car.pos[0]-1, car.pos[1])
-         elif d[1] =="right":
-             car.pos =(car.pos[0], car.pos[1]+1)
-         elif d[1] =="down":
-             car.pos =(car.pos[0]+1, car.pos[1])
-         elif d[1] =="left": 
-             car.pos =(car.pos[0], car.pos[1]-1)   
+#         print("Decide:\n",s,d)
+         if d[1][0] =="up":
+             car.pos =(car.pos[0]-d[1][1], car.pos[1])
+         elif d[1][0] =="right":
+             car.pos =(car.pos[0], car.pos[1]+d[1][1])
+         elif d[1][0] =="down":
+             car.pos =(car.pos[0]+d[1][1], car.pos[1])
+         elif d[1][0] =="left": 
+             car.pos =(car.pos[0], car.pos[1]-d[1][1])   
          s.nd+=1    
          self.used.append(d)
          s.mat=[]
              else:
                  for i in range(car.length):
                      s.mat[car.pos[0]][car.pos[1]+i]=car.id
-         print("Decisiones: ", s.nd)
+#         print("Decisiones: ", s.nd)
          return s
 
     def undo(self, s:'State',d):
-        print("Undo:",d)
+#        print("Undo:",d)
         car=self.list[d[0]-1]
-        if d[1] =="up":
-           car.pos =(car.pos[0]+1, car.pos[1])
-        elif d[1] =="right":
-            car.pos =(car.pos[0], car.pos[1]-1)
-        elif d[1] =="down":
-            car.pos =(car.pos[0]-1, car.pos[1])
-        elif d[1] =="left":
-          car.pos =(car.pos[0], car.pos[1]+1)       
+        if d[1][0] =="up":
+           car.pos =(car.pos[0]+d[1][1], car.pos[1])
+        elif d[1][0] =="right":
+            car.pos =(car.pos[0], car.pos[1]-d[1][1])
+        elif d[1][0] =="down":
+            car.pos =(car.pos[0]-d[1][1], car.pos[1])
+        elif d[1][0] =="left":
+          car.pos =(car.pos[0], car.pos[1]+d[1][1])       
         s.mat=[]    
         for i in range(6):s.mat.append([0]*6)
         for car in self.list:
 from algoritmia.schemes.backtracking import BacktrackingEnumerator
 
 
-nd=80
+nd=49
 car_list=[]
 car_list.append(CarParkReversibleForwardStateSpace.Car(1, 2, "h", (2,3)))
 car_list.append(CarParkReversibleForwardStateSpace.Car(2, 3, "h", (0,0)))
 car_list.append(CarParkReversibleForwardStateSpace.Car(9, 2, "v", (4,2)))
 car_list.append(CarParkReversibleForwardStateSpace.Car(10, 2, "h", (4,4)))
 car_list.append(CarParkReversibleForwardStateSpace.Car(11, 3, "h", (5,3)))
-'''^^ok'''
 
-enumerator = BacktrackingEnumerator(createSet=SetParkingStates, createSolution=lambda space, d, s: s)
+
+enumerator = BacktrackingEnumerator(createSet=SetParkingStates, createSolution=lambda space, d,i, s: s)
 
 space= CarParkReversibleForwardStateSpace(nd, car_list)
-print(space.list)
+
 print (next(enumerator.enumerate((space))))
 
-#space= CarParkReversibleForwardStateSpace(nd, car_list)
 
-#enumerator = BacktrackingEnumerator(createSet=SetParkingStates,createSolution=lambda space, d, s: s)
-#print(enumerator.first(space))
 
-#solution=CarParkSolver()
-#print(solution.solve(nd, car_list))
 
-
-
-