Advertisement
krstevskim

lab2

Apr 2nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.32 KB | None | 0 0
  1. def updateP1(P1):
  2.     x = P1[0]
  3.     y = P1[1]
  4.     n = P1[2]
  5.     if (y == 4 and n == 1):
  6.         n = n * (-1)
  7.     if (y == 0 and n == -1):
  8.         n = n * (-1)
  9.     ynew = y + n
  10.     return (x, ynew, n)
  11.  
  12.  
  13. def updateP2(P2):
  14.     x = P2[0]
  15.     y = P2[1]
  16.     n = P2[2]
  17.     if (x == 5 and y == 4 and n == 1):
  18.         n = n * (-1)
  19.     if (y == 0 and x == 9 and n == -1):
  20.         n = n * (-1)
  21.     xnew = x - n
  22.     ynew = y + n
  23.     return (xnew, ynew, n)
  24.  
  25.  
  26. def updateP3(P3):
  27.     x = P3[0]
  28.     y = P3[1]
  29.     n = P3[2]
  30.     if (x == 5 and n == -1):
  31.         n = n * (-1)
  32.     if (x == 9 and n == 1):
  33.         n = n * (-1)
  34.     xnew = x + n
  35.     return (xnew, y, n)
  36.  
  37.  
  38. def isValid(x, y, P1, P2, P3):
  39.     t = 1
  40.     if (x == P1[0] and y == P1[1]) or (x == P1[0] and y == P1[1] + 1):
  41.         t = 0
  42.     if (x == P2[0] and y == P2[1]) or (x == P2[0] and y == P2[1] + 1) or (x == P2[0] + 1 and y == P2[1]) or (
  43.             x == P2[0] + 1 and y == P2[1] + 1):
  44.         t = 0
  45.     if (x == P3[0] and y == P3[1]) or (x == P3[0] + 1 and y == P3[1]):
  46.         t = 0
  47.     return t
  48.  
  49.  
  50. class Istrazuvac(Problem):
  51.     def __init__(self, initial, goal):
  52.         self.initial = initial
  53.         self.goal = goal
  54.  
  55.     def successor(self, state):
  56.         successors = dict()
  57.         X = state[0]
  58.         Y = state[1]
  59.         P1 = state[2]
  60.         P2 = state[3]
  61.         P3 = state[4]
  62.         P1new = updateP1(P1)
  63.         P2new = updateP2(P2)
  64.         P3new = updateP3(P3)
  65.  
  66.         # Levo
  67.         if Y - 1 >= 0:
  68.             Ynew = Y - 1
  69.             Xnew = X
  70.             if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
  71.                 successors['Levo'] = (Xnew, Ynew, P1new, P2new, P3new)
  72.         # Desno
  73.         if X < 5 and Y < 5:
  74.             Ynew = Y + 1
  75.             Xnew = X
  76.             if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
  77.                 successors['Desno'] = (Xnew, Ynew, P1new, P2new, P3new)
  78.         elif X >= 5 and Y < 10:
  79.             Xnew = X
  80.             Ynew = Y + 1
  81.             if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
  82.                 successors['Desno'] = (Xnew, Ynew, P1new, P2new, P3new)
  83.         # Dolu
  84.         if X < 10:
  85.             Xnew = X + 1
  86.             Ynew = Y
  87.             if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
  88.                 successors['Dolu'] = (Xnew, Ynew, P1new, P2new, P3new)
  89.         # Gore
  90.         if Y >= 6 and X > 5:
  91.             Xnew = X - 1
  92.             Ynew = Y
  93.             if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
  94.                 successors['Gore'] = (Xnew, Ynew, P1new, P2new, P3new)
  95.         elif Y < 6 and X > 0:
  96.             Xnew = X - 1
  97.             Ynew = Y
  98.             if (isValid(Xnew, Ynew, P1new, P2new, P3new)):
  99.                 successors['Gore'] = (Xnew, Ynew, P1new, P2new, P3new)
  100.         return successors
  101.  
  102.     def actions(self, state):
  103.         return self.successor(state).keys()
  104.  
  105.     def result(self, state, action):
  106.         possible = self.successor(state)
  107.         return possible[action]
  108.  
  109.     def goal_test(self, state):
  110.         g = self.goal
  111.         return (state[0] == g[0] and state[1] == g[1])
  112.  
  113.  
  114. IstrazuvacInstance = Istrazuvac((choveche_redica, choveche_kolona, (2, 2, -1), (7, 2, 1), (7, 8, 1)),
  115.                                     (kukja_redica, kukja_kolona))
  116.  
  117. answer = breadth_first_graph_search(IstrazuvacInstance)
  118. print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement