├── README.md ├── jupyter.py ├── waterjugautomated.py └── Waterjug.py /README.md: -------------------------------------------------------------------------------- 1 | # Ai_Lab -------------------------------------------------------------------------------- /jupyter.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[48]: 5 | 6 | 7 | graph=[['A','B',1,3], 8 | ['A','C',2,4], 9 | ['A','H',7,0], 10 | ['B','D',4,2], 11 | ['B','E',6,6], 12 | ['C','F',3,3], 13 | ['C','G',2,1], 14 | ['D','E',7,6], 15 | ['D','H',5,0], 16 | ['F','H',1,0], 17 | ['G','H',2,0]] 18 | 19 | 20 | 21 | 22 | 23 | 24 | # In[49]: 25 | 26 | 27 | temp=[] 28 | temp1=[] 29 | for i in graph: 30 | temp.append(i[0]) 31 | temp1.append(i[1]) 32 | 33 | 34 | # In[50]: 35 | 36 | 37 | print(temp) 38 | 39 | 40 | # In[51]: 41 | 42 | 43 | nodes=set(temp).union(set(temp1)) 44 | 45 | 46 | # In[52]: 47 | 48 | 49 | nodes 50 | 51 | 52 | # In[57]: 53 | 54 | 55 | def A_star(graph,costs,openn,closed,cur_node): 56 | if cur_node in openn: 57 | openn.remove(cur_node) 58 | closed.add(cur_node) 59 | for i in graph: 60 | if(i[0]==cur_node and costs[i[0]]+i[2]+i[3]'+i[1] 64 | costs[cur_node]=999999 65 | small=min(costs,key=costs.get) 66 | if small not in closed: 67 | A_star(graph,costs,openn,closed,small) 68 | costs=dict() 69 | temp_cost=dict() 70 | path=dict() 71 | for i in nodes: 72 | costs[i]=999999 73 | path[i]=' ' 74 | 75 | 76 | # In[58]: 77 | 78 | 79 | openn=set() 80 | closed=set() 81 | start_node=input("Enter the Start Node:") 82 | openn.add(start_node) 83 | 84 | 85 | # In[59]: 86 | 87 | 88 | path[start_node]=start_node 89 | costs[start_node]=0 90 | 91 | 92 | # In[60]: 93 | 94 | 95 | A_star(graph,costs,openn,closed,start_node) 96 | goal_node=input("Enter the Goal Node: ") 97 | print("Path with least cost is:",path[goal_node]) 98 | 99 | 100 | # In[ ]: 101 | 102 | 103 | 104 | 105 | -------------------------------------------------------------------------------- /waterjugautomated.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import deque 3 | a=int(input("enter jug A capacitiy:")) 4 | b=int(input("enter jug B capacitiy:")) 5 | ai=int(input("initially water in jug A :")) 6 | bi=int(input("initially water in jug B :")) 7 | af=int(input("final state of jug A :")) 8 | bf=int(input("final state of jug B :")) 9 | 10 | if a<=0 or b<=0: 11 | print("jug capacities must be positive.") 12 | exit(1) 13 | if ai<0 or bi<0 or af<0 or bf<0: 14 | print("negative values must are not allowed.") 15 | exit(1) 16 | if ai==af and bi==bf : 17 | print("initial state is already the final state: juga(ai) and jugb=(bi)") 18 | exit() 19 | 20 | def bfs_wjug(a,b,ai,bi,af,bf): 21 | visited = set() 22 | queue = deque([(ai,bi, [])]) 23 | while queue: 24 | curr_ai,curr_bi,operations = queue.popleft() 25 | if(curr_ai,curr_bi) in visited: 26 | continue 27 | visited.add((curr_ai,curr_bi)) 28 | if curr_ai == af and curr_bi == bf: 29 | for i,op in enumerate(operations): 30 | print(f"step {i +1}: {op}") 31 | print(f"final state reached: jug A = {curr_ai},jug b = {curr_bi}") 32 | return 33 | 34 | possible_operations = [ 35 | (a,curr_bi,"fill jug A"),(curr_ai,b, "fill jug B"),(0,curr_bi, "empty jug A"),(curr_ai,0,"empty jug B"), 36 | (curr_ai - min(curr_ai,b-curr_bi), 37 | curr_bi + min(curr_ai,b-curr_bi),"pour from A to B"), 38 | 39 | (curr_ai + min(curr_bi,a - curr_ai),curr_bi - min(curr_bi,a-curr_ai)," pour from B to A"), 40 | ] 41 | 42 | for next_ai,next_bi,op in possible_operations: 43 | if(next_ai,next_bi) not in visited: 44 | queue.append((next_ai,next_bi,operations + [op])) 45 | print(" no solution found.") 46 | return 47 | gcd = math.gcd(a, b) 48 | if (af<=a and bf <=b) and (af%gcd ==bf % gcd == 0): 49 | bfs_wjug(a,b,ai,bi,af,bf) 50 | else: 51 | print("the final state is not achievable with the given capacities,") 52 | exit() 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /Waterjug.py: -------------------------------------------------------------------------------- 1 | import math 2 | a=int(input("enter jug A capacity: ")) 3 | b=int(input("enter jug B capacity: ")) 4 | ai=int(input("initial water in jug A : ")) 5 | bi=int(input("initial water in jug B : ")) 6 | af=int(input("final state of jug A : ")) 7 | bf=int(input("final state of jug B : ")) 8 | 9 | if a<=0 or b<=0: 10 | print("jug capacities must be positive.") 11 | exit(1) 12 | if ai<0 or bi<0 or af<0 or bf<0: 13 | print("negative values are not allowed.") 14 | exit(1) 15 | #define the water 16 | def wjug(a,b,ai,bi,af,bf): 17 | print("list of operation you can Do:\n") 18 | print("1. fill jug A completely") 19 | print("2. fill jug B completely") 20 | print("3. empty jug A completely") 21 | print("4. empty jug B completely") 22 | print("5. pour from jug A till jug B is full or A becomes empty") 23 | print("6. pour from jug B till jug A is full or B becomes empty") 24 | print("7. pour all from jug B to jug A") 25 | print("8. pour all from jug A to jug B") 26 | 27 | while (ai !=af or bi !=bf): 28 | op = int(input("enter the operation(1-8): ")) 29 | 30 | if op ==1: 31 | ai=a 32 | elif op ==2: 33 | bi =b 34 | elif op ==3: 35 | ai = 0 36 | elif op == 4: 37 | bi = 0 38 | elif op == 5: 39 | pour_amount = min(ai,b - bi) 40 | ai -= pour_amount 41 | bi += pour_amount 42 | elif op == 6: 43 | pour_amount = min(bi,a - ai) 44 | bi -= pour_amount 45 | ai += pour_amount 46 | elif op == 7: 47 | pour_amount = min(ai,b - bi) 48 | ai += pour_amount 49 | bi -= pour_amount 50 | elif op == 8: 51 | pour_amount = min(bi,a - ai) 52 | bi += pour_amount 53 | ai -= pour_amount 54 | else: 55 | print("invalid operation. please choose a number between 1-8") 56 | continue 57 | print(f"jug A: {ai}, jug B: {bi}") 58 | if ai == af and bi == bf: 59 | print("final state reached: jug A=",ai,",jug B =",bi) 60 | return 61 | 62 | print("final state reached : jug A = ", ai,",jug B =",bi) 63 | gcb = math.gcd(a,b) 64 | if (af <= a and bf <= b) and (af % gcb == bf % gcb ==0): 65 | wjug(a,b,ai,bi,af,bf) 66 | else: 67 | print("the final state is not achievable with the given capacities.") 68 | exit(1) 69 | 70 | 71 | 72 | 73 | 74 | --------------------------------------------------------------------------------