-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAI QUIZ (2020CS625).txt
More file actions
162 lines (132 loc) · 4.16 KB
/
AI QUIZ (2020CS625).txt
File metadata and controls
162 lines (132 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
TASK1-----------------------------
def ASTAR(startingnode, goalnode):
set1 = set(startingnode)
set2 = set()
SND = {}
parents = {}
SND[startingnode] = 0
parents[startingnode] = startingnode
while len(set1) > 0:
n = None
for v in set1:
if n == None or SND[v] + heuristicfunction(v) < SND[n] + heuristicfunction(n):
n = v
if n == goalnode or Graph [n] == None:
pass
else:
for (m,cost) in childern(n):
if m not in set1 and m not in set2:
set1.add(m)
parents[m] = n
SND[m] = SND[n] + cost
else:
if SND[m] > SND[n] + cost:
SND[m] = SND[n] + cost
parents[m] = n
if m in set2:
set2.remove(m)
set1.add(m)
if n == goalnode:
path = []
while parents[n] != n:
path.append(n)
n = parents[n]
path.append(startingnode)
path.reverse()
print('Final Path:',path)
return path
set1.remove(n)
set2.add(n)
def childern(v):
if v in Graph :
return Graph [v]
else:
return None
def heuristicfunction(n):
Heuristic_values={
'A':10.4,
'B':6.7,
'C':4.0,
'S':11.0,
'D':8.9,
'E':6.9,
'F':3.0,
'G':0,
}
return Heuristic_values[n]
Graph = {
'S': [('A', 3), ('D', 4)],
'A': [('D', 5),('B', 4)],
'D': [('E', 2)],
'B': [('E', 5),('C',4)],
'E': [('F',4)],
'F':[('G',3)],
}
ASTAR('S', 'G')
TASK2--------------
class Node:
def __init__(self,data,level,fval):
self.data = data
self.level = level
self.fval = fval
print(self.data)
def generate_child(self):
x,y = self.find(self.data,'_')
val_list = [[x,y-1],[x,y+1],[x-1,y],[x+1,y]]
children = []
for i in val_list:
child = self.shuffle(self.data,x,y,i[0],i[1])
if child is not None:
child_node = Node(child,self.level+1,0)
children.append(child_node)
return children
def shuffle(self,puz,x1,y1,x2,y2):
if x2 >= 0 and x2 < len(self.data) and y2 >= 0 and y2 < len(self.data):
temp_puz = []
temp_puz = self.copy(puz)
temp = temp_puz[x2][y2]
temp_puz[x2][y2] = temp_puz[x1][y1]
temp_puz[x1][y1] = temp
return temp_puz
else:
return None
def copy(self,root):
temp = []
for i in root:
t = []
for j in i:
t.append(j)
temp.append(t)
return temp
def find(self,puz,x):
for i in range(0,len(self.data)):
for j in range(0,len(self.data)):
if puz[i][j] == x:
return i,j
class Puzzle:
def __init__(self,size):
self.n = size
self.open = []
self.closed = []
def accept(self):
puz = []
for i in range(0,self.n):
temp = input().split(" ")
puz.append(temp)
return puz
def f(self,start,goal):
return self.h(start.data,goal)+start.level
def h(self,start,goal):
temp = 0
for i in range(0,self.n):
for j in range(0,self.n):
if start[i][j] != goal[i][j] and start[i][j] != '_':
temp += 1
return temp
def process(self):
print("Enter the start state matrix \n")
start = self.accept()
print("Enter the goal state matrix \n")
goal = self.accept()
puz=puzzle()
puz.process()