Advertisement
dzocesrce

[VI] Water Molecule

Jan 22nd, 2025 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.05 KB | None | 0 0
  1. #super raboti ama duri da se iskuca ko so treba...
  2. def pomesti_levo(atom,obstacles,atom2,atom3):
  3.     x=atom[0]
  4.     y=atom[1]
  5.     temp_atom = []
  6.     temp_atom.append(x)
  7.     temp_atom.append(y)
  8.     while(temp_atom[0]>=1):
  9.         #print(temp_atom)
  10.         temp_atom[0]-=1
  11.         if tuple(temp_atom) in obstacles or tuple(temp_atom)==atom2 or tuple(temp_atom)==atom3:
  12.             temp_atom[0]+=1
  13.             break
  14.     return tuple(temp_atom)
  15.  
  16. def pomesti_desno(atom, obstacles, atom2, atom3):
  17.     x = atom[0]
  18.     y = atom[1]
  19.     temp_atom = []
  20.     temp_atom.append(x)
  21.     temp_atom.append(y)
  22.     while (temp_atom[0] <=7):
  23.         # print(temp_atom)
  24.         temp_atom[0] += 1
  25.         if tuple(temp_atom) in obstacles or tuple(temp_atom) == atom2 or tuple(temp_atom) == atom3:
  26.             temp_atom[0] -= 1
  27.             break
  28.     return tuple(temp_atom)
  29.  
  30.  
  31. def pomesti_gore(atom, obstacles, atom2, atom3):
  32.     x = atom[0]
  33.     y = atom[1]
  34.     temp_atom = []
  35.     temp_atom.append(x)
  36.     temp_atom.append(y)
  37.     while (temp_atom[1] <=5):
  38.         # print(temp_atom)
  39.         temp_atom[1] += 1
  40.         if tuple(temp_atom) in obstacles or tuple(temp_atom) == atom2 or tuple(temp_atom) == atom3:
  41.             temp_atom[1] -= 1
  42.             break
  43.     return tuple(temp_atom)
  44.  
  45.  
  46. def pomesti_dolu(atom, obstacles, atom2, atom3):
  47.     x = atom[0]
  48.     y = atom[1]
  49.     temp_atom = []
  50.     temp_atom.append(x)
  51.     temp_atom.append(y)
  52.     while (temp_atom[1] >=1):
  53.         # print(temp_atom)
  54.         temp_atom[1] -= 1
  55.         if tuple(temp_atom) in obstacles or tuple(temp_atom) == atom2 or tuple(temp_atom) == atom3:
  56.             temp_atom[1] += 1
  57.             break
  58.     return tuple(temp_atom)
  59.  
  60. class Water(Problem):
  61.     def __init__(self, initial, house=None):
  62.         super().__init__(initial, house)
  63.  
  64.     def goal_test(self, state):
  65.         hydrogen1 = state[0]
  66.         hydrogen2 = state[1]
  67.         oxygen = state[2]
  68.         return hydrogen1[1]==oxygen[1] and hydrogen2[1]==oxygen[1] and hydrogen1[0]+1==oxygen[0] and oxygen[0]+1==hydrogen2[0]
  69.  
  70.  
  71.     def successor(self, state):
  72.         succ = {}
  73.         hydrogen1 = state[0]
  74.         hydrogen2 = state[1]
  75.         oxygen = state[2]
  76.         obstacles= state[3]
  77.  
  78.         new_hyrogen1= pomesti_levo(hydrogen1,obstacles,oxygen,hydrogen2)
  79.         if new_hyrogen1!=hydrogen1:
  80.             succ["Pomesti H1 levo"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
  81.  
  82.         new_hydrogen2= pomesti_levo(hydrogen2,obstacles,hydrogen1,oxygen)
  83.         if new_hydrogen2!=hydrogen2:
  84.             succ["Pomesti H2 levo"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
  85.  
  86.         new_oxygen= pomesti_levo(oxygen,obstacles,hydrogen1,hydrogen2)
  87.         if new_oxygen!=oxygen:
  88.             succ["Pomesti O levo"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
  89.  
  90.  
  91.         new_hyrogen1= pomesti_desno(hydrogen1,obstacles,oxygen,hydrogen2)
  92.         if new_hyrogen1!=hydrogen1:
  93.             succ["Pomesti H1 desno"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
  94.  
  95.         new_hydrogen2= pomesti_desno(hydrogen2,obstacles,hydrogen1,oxygen)
  96.         if new_hydrogen2!=hydrogen2:
  97.             succ["Pomesti H2 desno"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
  98.  
  99.         new_oxygen= pomesti_desno(oxygen,obstacles,hydrogen1,hydrogen2)
  100.         if new_oxygen!=oxygen:
  101.             succ["Pomesti O desno"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
  102.  
  103.  
  104.         new_hyrogen1= pomesti_gore(hydrogen1,obstacles,oxygen,hydrogen2)
  105.         if new_hyrogen1!=hydrogen1:
  106.             succ["Pomesti H1 gore"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
  107.  
  108.         new_hydrogen2= pomesti_gore(hydrogen2,obstacles,hydrogen1,oxygen)
  109.         if new_hydrogen2!=hydrogen2:
  110.             succ["Pomesti H2 gore"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
  111.  
  112.         new_oxygen= pomesti_gore(oxygen,obstacles,hydrogen1,hydrogen2)
  113.         if new_oxygen!=oxygen:
  114.             succ["Pomesti O gore"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
  115.  
  116.  
  117.         new_hyrogen1= pomesti_dolu(hydrogen1,obstacles,oxygen,hydrogen2)
  118.         if new_hyrogen1!=hydrogen1:
  119.             succ["Pomesti H1 dolu"] = (new_hyrogen1,hydrogen2,oxygen,obstacles)
  120.  
  121.         new_hydrogen2= pomesti_dolu(hydrogen2,obstacles,hydrogen1,oxygen)
  122.         if new_hydrogen2!=hydrogen2:
  123.             succ["Pomesti H2 dolu"] = (hydrogen1,new_hydrogen2,oxygen,obstacles)
  124.  
  125.         new_oxygen= pomesti_dolu(oxygen,obstacles,hydrogen1,hydrogen2)
  126.         if new_oxygen!=oxygen:
  127.             succ["Pomesti O dolu"] = (hydrogen1,hydrogen2,new_oxygen,obstacles)
  128.  
  129.  
  130.  
  131.         return succ
  132.     def actions(self, state):
  133.         return self.successor(state).keys()
  134.  
  135.     def result(self, state, action):
  136.         return self.successor(state)[action]
  137.  
  138.  
  139. if __name__ == '__main__':
  140.  
  141.     hydrogen1= (2,1)
  142.     hydrogen2= (2,6)
  143.     oxygen= (7,2)
  144.     obstacles= ((0,1),(1,1),(3,1),(6,1),(4,2),(6,2),(1,3),(6,3),(7,3),(2,5),(8,5),(3,6),(5,6),(7,6))
  145.  
  146.     water = Water((hydrogen1,hydrogen2,oxygen,obstacles))
  147.  
  148.     answer = breadth_first_graph_search(water)
  149.     if answer is None:
  150.         print("None")
  151.     else:
  152.         print(answer.solution())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement