numpyUtilsUpdated 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- numpyUtilsUpdated/__init__.py +5 -0
- numpyUtilsUpdated/ai/AlphaBetaPr.py +42 -0
- numpyUtilsUpdated/ai/BFS_8pz.py +46 -0
- numpyUtilsUpdated/ai/DFS_8pz.py +50 -0
- numpyUtilsUpdated/ai/DLimitS_8pz.py +48 -0
- numpyUtilsUpdated/ai/GBFS.py +33 -0
- numpyUtilsUpdated/ai/GraphColor.py +42 -0
- numpyUtilsUpdated/ai/Minimax.py +71 -0
- numpyUtilsUpdated/ai/Prologs.py +53 -0
- numpyUtilsUpdated/ai/UCS.py +38 -0
- numpyUtilsUpdated/ai/VaccumCleaner.py +59 -0
- numpyUtilsUpdated/ai/Wumpus.py +89 -0
- numpyUtilsUpdated/ai/__init__.py +19 -0
- numpyUtilsUpdated/ai/ticTacToe.py +101 -0
- numpyUtilsUpdated/ai/waterJug.py +59 -0
- numpyUtilsUpdated/cs/__init__.py +17 -0
- numpyUtilsUpdated/cs/ass1.py +102 -0
- numpyUtilsUpdated/cs/ass2.py +71 -0
- numpyUtilsUpdated/cs/ass3.py +137 -0
- numpyUtilsUpdated/cs/ass4.py +103 -0
- numpyUtilsUpdated/daa/APSP_floyd_warshall.py +64 -0
- numpyUtilsUpdated/daa/BinarySearch.py +24 -0
- numpyUtilsUpdated/daa/Dijkstras.py +87 -0
- numpyUtilsUpdated/daa/Greedy_01knapsack.py +62 -0
- numpyUtilsUpdated/daa/Greedy_FKanpsack.py +71 -0
- numpyUtilsUpdated/daa/Job_Seq.py +71 -0
- numpyUtilsUpdated/daa/KnapDP.py +32 -0
- numpyUtilsUpdated/daa/Kruskals.py +108 -0
- numpyUtilsUpdated/daa/Matrix.py +116 -0
- numpyUtilsUpdated/daa/Prims.py +92 -0
- numpyUtilsUpdated/daa/Quick_Sort.py +62 -0
- numpyUtilsUpdated/daa/SSSP_bellman_ford.py +81 -0
- numpyUtilsUpdated/daa/String_editing_problem.py +75 -0
- numpyUtilsUpdated/daa/TSP.py +31 -0
- numpyUtilsUpdated/daa/__init__.py +36 -0
- numpyUtilsUpdated/daa/getInfo.py +0 -0
- numpyUtilsUpdated/daa/min_max.py +41 -0
- numpyutilsupdated-0.0.1.dist-info/METADATA +103 -0
- numpyutilsupdated-0.0.1.dist-info/RECORD +41 -0
- numpyutilsupdated-0.0.1.dist-info/WHEEL +4 -0
- numpyutilsupdated-0.0.1.dist-info/licenses/LICENSE +674 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
import math
|
|
3
|
+
def alpha_beta_pruning(index, depth, alpha, beta, maximizing_player):
|
|
4
|
+
if depth == 0 or index >= len(scores):
|
|
5
|
+
return scores[index] if index < len(scores) else 0
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
left_index, right_index = 2 * index, 2 * index + 1
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
if maximizing_player:
|
|
12
|
+
max_eval = -float('inf')
|
|
13
|
+
for child_index in [left_index, right_index]:
|
|
14
|
+
if child_index < len(scores):
|
|
15
|
+
ab_val = alpha_beta_pruning(child_index, depth - 1, alpha, beta, False)
|
|
16
|
+
max_eval = max(max_eval, ab_val)
|
|
17
|
+
alpha = max(alpha, max_eval)
|
|
18
|
+
if beta <= alpha:
|
|
19
|
+
break
|
|
20
|
+
return max_eval
|
|
21
|
+
else:
|
|
22
|
+
min_eval = float('inf')
|
|
23
|
+
for child_index in [left_index, right_index]:
|
|
24
|
+
if child_index < len(scores):
|
|
25
|
+
ab_val = alpha_beta_pruning(child_index, depth - 1, alpha, beta, True)
|
|
26
|
+
min_eval = min(min_eval, ab_val)
|
|
27
|
+
beta = min(beta, min_eval)
|
|
28
|
+
if beta <= alpha:
|
|
29
|
+
break
|
|
30
|
+
return min_eval
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
scores = list(map(int, input("Enter Scores: ").split()))
|
|
34
|
+
depth = int(math.log2(len(scores)))
|
|
35
|
+
print("The Optimal Value is:", alpha_beta_pruning(0, depth, -float('inf'), float('inf'), True))
|
|
36
|
+
|
|
37
|
+
'''
|
|
38
|
+
|
|
39
|
+
def getCode():
|
|
40
|
+
global code
|
|
41
|
+
print(code)
|
|
42
|
+
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
from collections import deque
|
|
3
|
+
def get_neighbors(s):
|
|
4
|
+
i = s.index(0)
|
|
5
|
+
neighbors = []
|
|
6
|
+
for d in [-3, 3, -1, 1]:
|
|
7
|
+
j = i + d
|
|
8
|
+
if 0 <= j < 9 and abs(j//3 - i//3) + abs(j%3 - i%3) == 1:
|
|
9
|
+
n = [*s]
|
|
10
|
+
n[i], n[j] = n[j], n[i]
|
|
11
|
+
neighbors.append(n)
|
|
12
|
+
return neighbors
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def bfs_8_puzzle(start, goal):
|
|
16
|
+
if sum(a > b for i, a in enumerate(start[:-1]) for b in start[i+1:] if a and b) % 2:
|
|
17
|
+
return print("Unsolvable")
|
|
18
|
+
q = deque([(start, [])])
|
|
19
|
+
v = {tuple(start)}
|
|
20
|
+
while q:
|
|
21
|
+
s, p = q.popleft()
|
|
22
|
+
if s == goal:
|
|
23
|
+
for i, t in enumerate([start] + p):
|
|
24
|
+
print(f"Step {i + 1}:")
|
|
25
|
+
for j in range(0, 9, 3):
|
|
26
|
+
print(' '.join(map(str, t[j:j+3])))
|
|
27
|
+
print()
|
|
28
|
+
return
|
|
29
|
+
for n in get_neighbors(s):
|
|
30
|
+
nt = tuple(n)
|
|
31
|
+
if nt not in v:
|
|
32
|
+
q.append((n, p + [n]))
|
|
33
|
+
v.add(nt)
|
|
34
|
+
print("No solution")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
print("Enter 3 rows :")
|
|
38
|
+
start = sum([[int(x) for x in input(f"Row {i+1}: ").split()] for i in range(3)], [])
|
|
39
|
+
goal = [1,2,3,4,5,6,7,8,0]
|
|
40
|
+
bfs_8_puzzle(start, goal)
|
|
41
|
+
'''
|
|
42
|
+
|
|
43
|
+
def getCode():
|
|
44
|
+
global code
|
|
45
|
+
print(code)
|
|
46
|
+
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
def get_matrix(prompt):
|
|
3
|
+
print(prompt)
|
|
4
|
+
return [list(map(int, input().split())) for _ in range(3)]
|
|
5
|
+
|
|
6
|
+
def find_blank(state):
|
|
7
|
+
for i, row in enumerate(state):
|
|
8
|
+
if 0 in row:
|
|
9
|
+
return i, row.index(0)
|
|
10
|
+
|
|
11
|
+
def generate_moves(state):
|
|
12
|
+
x, y = find_blank(state)
|
|
13
|
+
moves, directions = [], {'Up': (-1, 0), 'Down': (1, 0), 'Left': (0, -1), 'Right': (0, 1)}
|
|
14
|
+
|
|
15
|
+
for move, (dx, dy) in directions.items():
|
|
16
|
+
nx, ny = x + dx, y + dy
|
|
17
|
+
if 0 <= nx < 3 and 0 <= ny < 3:
|
|
18
|
+
new_state = [row[:] for row in state]
|
|
19
|
+
new_state[x][y], new_state[nx][ny] = new_state[nx][ny], new_state[x][y]
|
|
20
|
+
moves.append((move, new_state))
|
|
21
|
+
|
|
22
|
+
return moves[::-1]
|
|
23
|
+
|
|
24
|
+
def dfs(initial, goal):
|
|
25
|
+
stack, visited = [(initial, [], 0)], {tuple(map(tuple, initial))}
|
|
26
|
+
|
|
27
|
+
while stack:
|
|
28
|
+
state, path, step = stack.pop()
|
|
29
|
+
print(f"Step {step}: {path[-1] if path else 'Start'}\n" + "\n".join(" ".join(map(str, row)) for row in state) + "\n")
|
|
30
|
+
|
|
31
|
+
if state == goal:
|
|
32
|
+
print("Goal reached!")
|
|
33
|
+
return
|
|
34
|
+
|
|
35
|
+
for move, new_state in generate_moves(state):
|
|
36
|
+
state_tuple = tuple(map(tuple, new_state))
|
|
37
|
+
if state_tuple not in visited:
|
|
38
|
+
visited.add(state_tuple)
|
|
39
|
+
stack.append((new_state, path + [move], step + 1))
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
initial, goal = get_matrix("Enter initial state (3x3, space as 0):"), get_matrix("Enter goal state (3x3, space as 0):")
|
|
43
|
+
print("\nSolving 8-puzzle using DFS:\n")
|
|
44
|
+
dfs(initial, goal)
|
|
45
|
+
'''
|
|
46
|
+
|
|
47
|
+
def getCode():
|
|
48
|
+
global code
|
|
49
|
+
print(code)
|
|
50
|
+
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
def get_neighbors(state):
|
|
3
|
+
i = state.index(0)
|
|
4
|
+
moves = {'up': -3, 'down': 3, 'left': -1, 'right': 1}
|
|
5
|
+
return [swap(state, i, i + d) for m, d in moves.items()
|
|
6
|
+
if 0 <= i + d < 9 and abs((i + d) // 3 - i // 3) + abs((i + d) % 3 - i % 3) == 1]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def swap(state, i, j):
|
|
10
|
+
s = state[:]
|
|
11
|
+
s[i], s[j] = s[j], s[i]
|
|
12
|
+
return s
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def print_matrix(state):
|
|
16
|
+
for i in range(0, 9, 3):
|
|
17
|
+
print(' '.join(map(str, state[i:i+3])))
|
|
18
|
+
print()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def dfs_8_puzzle(start, goal, depth_limit):
|
|
22
|
+
stack, visited = [(start, [])], {tuple(start)}
|
|
23
|
+
while stack:
|
|
24
|
+
state, path = stack.pop()
|
|
25
|
+
if state == goal:
|
|
26
|
+
for i, s in enumerate(path):
|
|
27
|
+
print(f"Step {i+1}:")
|
|
28
|
+
print_matrix(s)
|
|
29
|
+
return
|
|
30
|
+
if len(path) < depth_limit:
|
|
31
|
+
for n in get_neighbors(state)[::-1]:
|
|
32
|
+
if tuple(n) not in visited:
|
|
33
|
+
visited.add(tuple(n))
|
|
34
|
+
stack.append((n, path + [n]))
|
|
35
|
+
print("No solution found within depth limit.")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
print("Enter 3 rows:")
|
|
39
|
+
start = [int(x) for _ in range(3) for x in input(f"Row {_+1}: ").split()]
|
|
40
|
+
depth_limit = int(input("Enter Depth limit: "))
|
|
41
|
+
goal = [1, 2, 3, 4, 5, 6, 7, 8, 0]
|
|
42
|
+
dfs_8_puzzle(start, goal, depth_limit)
|
|
43
|
+
'''
|
|
44
|
+
|
|
45
|
+
def getCode():
|
|
46
|
+
global code
|
|
47
|
+
print(code)
|
|
48
|
+
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
graph = {'A': {'B': 6, 'F': 3}, 'B': {'C': 3, 'D': 2}, 'C': {'E': 5}, 'D': {'E': 8},
|
|
3
|
+
'E': {'J': 5, 'I': 5}, 'F': {'G': 1, 'H': 7}, 'G': {'I': 3}, 'H': {'I': 2},
|
|
4
|
+
'I': {'J': 3}, 'J': {}}
|
|
5
|
+
heuristic = {'A': 10, 'B': 8, 'C': 5, 'D': 7, 'E': 3, 'F': 6, 'G': 5, 'H': 3, 'I': 1, 'J': 0}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def gbfs(start, dest):
|
|
9
|
+
open_list, closed = [start], []
|
|
10
|
+
while open_list:
|
|
11
|
+
cur = min(open_list, key=heuristic.get)
|
|
12
|
+
open_list.remove(cur)
|
|
13
|
+
closed.append(cur)
|
|
14
|
+
if cur == dest:
|
|
15
|
+
return closed
|
|
16
|
+
open_list.extend(adj for adj in graph[cur] if adj not in open_list + closed)
|
|
17
|
+
return None
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
start, end = input("Enter Starting Point: "), input("Enter Destination: ")
|
|
21
|
+
path = gbfs(start, end)
|
|
22
|
+
if path:
|
|
23
|
+
for n in path:
|
|
24
|
+
print(' '.join(f'[{n}, {heuristic[n]}]' ))
|
|
25
|
+
else:
|
|
26
|
+
print("None")
|
|
27
|
+
|
|
28
|
+
'''
|
|
29
|
+
|
|
30
|
+
def getCode():
|
|
31
|
+
global code
|
|
32
|
+
print(code)
|
|
33
|
+
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
n = 7
|
|
3
|
+
m = 3
|
|
4
|
+
variables = ["A", "B", "C", "D", "E", "F", "G"]
|
|
5
|
+
|
|
6
|
+
graph = [
|
|
7
|
+
[0, 1, 1, 0, 0, 0, 0],
|
|
8
|
+
[1, 0, 1, 1, 0, 0, 0],
|
|
9
|
+
[1, 1, 0, 1, 1, 1, 0],
|
|
10
|
+
[0, 1, 1, 1, 1, 0, 0],
|
|
11
|
+
[0, 0, 1, 1, 0, 1, 0],
|
|
12
|
+
[0, 0, 1, 0, 1, 0, 0],
|
|
13
|
+
[0, 0, 0, 0, 0, 0, 0]
|
|
14
|
+
]
|
|
15
|
+
colors = ["R", "Y", "P"]
|
|
16
|
+
color = [0]*n
|
|
17
|
+
|
|
18
|
+
def is_Safe(curr, color, c):
|
|
19
|
+
return all(graph[curr][i] == 0 or color[i] != c for i in range(n))
|
|
20
|
+
|
|
21
|
+
def graphColor(curr, color):
|
|
22
|
+
if curr == n:
|
|
23
|
+
return True
|
|
24
|
+
for c in range(1, m+1):
|
|
25
|
+
if is_Safe(curr, color, c):
|
|
26
|
+
color[curr] = c
|
|
27
|
+
if graphColor(curr + 1, color):
|
|
28
|
+
return True
|
|
29
|
+
color[curr] = 0
|
|
30
|
+
return False
|
|
31
|
+
|
|
32
|
+
if graphColor(0, color):
|
|
33
|
+
for i in range(n):
|
|
34
|
+
print(f"{variables[i]}: {colors[color[i] - 1]}")
|
|
35
|
+
else:
|
|
36
|
+
print("Solution does not exist")
|
|
37
|
+
|
|
38
|
+
'''
|
|
39
|
+
|
|
40
|
+
def getCode():
|
|
41
|
+
global code
|
|
42
|
+
print(code)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
def show(b):
|
|
3
|
+
for r in b:
|
|
4
|
+
print(" | ".join(r))
|
|
5
|
+
print("- " * 9)
|
|
6
|
+
|
|
7
|
+
def win(b, p):
|
|
8
|
+
for i in range(3):
|
|
9
|
+
if all(b[i][j]==p for j in range(3)): return True
|
|
10
|
+
if all(b[j][i]==p for j in range(3)): return True
|
|
11
|
+
if all(b[i][i]==p for i in range(3)): return True
|
|
12
|
+
if all(b[i][2-i]==p for i in range(3)): return True
|
|
13
|
+
return False
|
|
14
|
+
|
|
15
|
+
def full(b):
|
|
16
|
+
return all(c != " " for r in b for c in r)
|
|
17
|
+
|
|
18
|
+
def minimax(b, turn):
|
|
19
|
+
if win(b, "X"): return 1
|
|
20
|
+
if win(b, "O"): return -1
|
|
21
|
+
if full(b): return 0
|
|
22
|
+
best = -2 if turn else 2
|
|
23
|
+
p = "X" if turn else "O"
|
|
24
|
+
for i in range(3):
|
|
25
|
+
for j in range(3):
|
|
26
|
+
if b[i][j] == " ":
|
|
27
|
+
b[i][j] = p
|
|
28
|
+
score = minimax(b, not turn)
|
|
29
|
+
b[i][j] = " "
|
|
30
|
+
if turn: best = max(best, score)
|
|
31
|
+
else: best = min(best, score)
|
|
32
|
+
return best
|
|
33
|
+
|
|
34
|
+
def best_move(b):
|
|
35
|
+
best, pos = -2, None
|
|
36
|
+
for i in range(3):
|
|
37
|
+
for j in range(3):
|
|
38
|
+
if b[i][j] == " ":
|
|
39
|
+
b[i][j] = "X"
|
|
40
|
+
score = minimax(b, False)
|
|
41
|
+
b[i][j] = " "
|
|
42
|
+
if score > best:
|
|
43
|
+
best = score
|
|
44
|
+
pos = (i, j)
|
|
45
|
+
return pos
|
|
46
|
+
|
|
47
|
+
b = [[" "] * 3 for _ in range(3)]
|
|
48
|
+
print("Tic-Tac-Toe (O=You, X=AI)")
|
|
49
|
+
show(b)
|
|
50
|
+
|
|
51
|
+
while True:
|
|
52
|
+
r, c = map(int, input("Row Col: ").split())
|
|
53
|
+
if b[r][c] != " ":
|
|
54
|
+
print("Invalid"); continue
|
|
55
|
+
b[r][c] = "O"
|
|
56
|
+
show(b)
|
|
57
|
+
if win(b, "O"): print("You win!"); break
|
|
58
|
+
if full(b): print("Tie!"); break
|
|
59
|
+
m = best_move(b)
|
|
60
|
+
if m: b[m[0]][m[1]] = "X"
|
|
61
|
+
print("AI move:")
|
|
62
|
+
show(b)
|
|
63
|
+
if win(b, "X"): print("AI wins!"); break
|
|
64
|
+
if full(b): print("Tie!"); break
|
|
65
|
+
|
|
66
|
+
'''
|
|
67
|
+
|
|
68
|
+
def getCode():
|
|
69
|
+
global code
|
|
70
|
+
print(code)
|
|
71
|
+
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
code = """
|
|
2
|
+
%facts
|
|
3
|
+
owns(nono,m).
|
|
4
|
+
missile(m).
|
|
5
|
+
enemy(nono,america).
|
|
6
|
+
american(west).
|
|
7
|
+
|
|
8
|
+
%rules
|
|
9
|
+
sells(west,X,nono):-
|
|
10
|
+
missile(X),owns(nono,X).
|
|
11
|
+
weapon(X):-
|
|
12
|
+
missile(X).
|
|
13
|
+
hostile(X):-
|
|
14
|
+
enemy(X,america).
|
|
15
|
+
criminal(X):-
|
|
16
|
+
american(X),weapon(Y),sells(X,Y,Z),hostile(Z).
|
|
17
|
+
|
|
18
|
+
%Query
|
|
19
|
+
is_criminal(X):-
|
|
20
|
+
criminal(X).
|
|
21
|
+
|
|
22
|
+
---------------------------------------------------
|
|
23
|
+
|
|
24
|
+
% Facts
|
|
25
|
+
man(marcus).
|
|
26
|
+
pompeian(marcus).
|
|
27
|
+
|
|
28
|
+
ruler(caesar).
|
|
29
|
+
tried_assassinate(marcus, caesar).
|
|
30
|
+
|
|
31
|
+
% Rules
|
|
32
|
+
|
|
33
|
+
roman(X) :-
|
|
34
|
+
pompeian(X).
|
|
35
|
+
loyal_to(X, caesar) :-
|
|
36
|
+
roman(X),
|
|
37
|
+
\+ tried_assassinate(X, caesar).
|
|
38
|
+
|
|
39
|
+
hate(X, caesar) :-
|
|
40
|
+
roman(X),
|
|
41
|
+
\+ loyal_to(X, caesar).
|
|
42
|
+
|
|
43
|
+
% Goal
|
|
44
|
+
did_hate(marcus, caesar) :-
|
|
45
|
+
hate(marcus, caesar).
|
|
46
|
+
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
def getCode():
|
|
50
|
+
global code
|
|
51
|
+
print(code)
|
|
52
|
+
|
|
53
|
+
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
def get_matrix(): return [list(map(int, input().split())) for _ in range(3)]
|
|
3
|
+
|
|
4
|
+
def find_blank(state): return next((i, j) for i in range(3) for j in range(3) if not state[i][j])
|
|
5
|
+
|
|
6
|
+
def swap(state, x1, y1, x2, y2):
|
|
7
|
+
new_state = [row[:] for row in state]
|
|
8
|
+
new_state[x1][y1], new_state[x2][y2] = new_state[x2][y2], new_state[x1][y1]
|
|
9
|
+
return new_state
|
|
10
|
+
|
|
11
|
+
def print_state(state): print("\n".join(" ".join(map(str, row)) for row in state), "\n")
|
|
12
|
+
|
|
13
|
+
def uniform_cost_search(initial, goal):
|
|
14
|
+
queue, visited = [(0, initial, [])], set()
|
|
15
|
+
while queue:
|
|
16
|
+
queue.sort()
|
|
17
|
+
cost, state, path = queue.pop(0)
|
|
18
|
+
if state == goal: [print_state(step) for step in path + [state]]; return
|
|
19
|
+
state_tuple = tuple(map(tuple, state))
|
|
20
|
+
if state_tuple in visited: continue
|
|
21
|
+
visited.add(state_tuple)
|
|
22
|
+
x, y = find_blank(state)
|
|
23
|
+
for nx, ny in [(x-1,y), (x+1,y), (x,y-1), (x,y+1)]:
|
|
24
|
+
if 0 <= nx < 3 and 0 <= ny < 3:
|
|
25
|
+
queue.append((cost+1, swap(state, x, y, nx, ny), path + [state]))
|
|
26
|
+
|
|
27
|
+
print("Enter initial state:")
|
|
28
|
+
initial_state = get_matrix()
|
|
29
|
+
print("Enter goal state:")
|
|
30
|
+
goal_state = get_matrix()
|
|
31
|
+
print("Solution Steps:")
|
|
32
|
+
uniform_cost_search(initial_state, goal_state)
|
|
33
|
+
|
|
34
|
+
'''
|
|
35
|
+
def getCode():
|
|
36
|
+
global code
|
|
37
|
+
print(code)
|
|
38
|
+
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
|
|
3
|
+
def initialize_environment():
|
|
4
|
+
n_rooms = int(input("No.of rooms: "))
|
|
5
|
+
room_dic = {}
|
|
6
|
+
for i in range(1, n_rooms + 1):
|
|
7
|
+
r_num = f"Room{i}"
|
|
8
|
+
status = input(f"Enter status of {r_num} (Clean/Dirty): ").strip().capitalize()
|
|
9
|
+
room_dic[r_num] = status
|
|
10
|
+
return room_dic
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def sense(room_dic, cur_loc):
|
|
14
|
+
return room_dic[cur_loc]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def suck(room_dic, cur_loc):
|
|
18
|
+
if room_dic[cur_loc] == "Dirty":
|
|
19
|
+
print(f"Cleaning {cur_loc}")
|
|
20
|
+
room_dic[cur_loc] = "Clean"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def isdone(room_dic):
|
|
24
|
+
return all(status == "Clean" for status in room_dic.values())
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def move(room_dic, cur_loc):
|
|
28
|
+
room_index = list(room_dic)
|
|
29
|
+
cur_index = room_index.index(cur_loc)
|
|
30
|
+
next_loc = room_index[(cur_index + 1) % len(room_index)]
|
|
31
|
+
print(f"Moving from {cur_loc} to {next_loc}")
|
|
32
|
+
return next_loc
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def act(room_dic, cur_loc):
|
|
36
|
+
if sense(room_dic, cur_loc) == "Dirty":
|
|
37
|
+
suck(room_dic, cur_loc)
|
|
38
|
+
return cur_loc
|
|
39
|
+
else:
|
|
40
|
+
return move(room_dic, cur_loc)
|
|
41
|
+
|
|
42
|
+
if __name__ == "__main__":
|
|
43
|
+
room_dic = initialize_environment()
|
|
44
|
+
cur_loc = list(room_dic.keys())[0]
|
|
45
|
+
step = 1
|
|
46
|
+
print("\nInitial State:", room_dic)
|
|
47
|
+
while not all(status == "Clean" for status in room_dic.values()):
|
|
48
|
+
print(f"\nStep {step}:")
|
|
49
|
+
print(f"Current Location: {cur_loc}")
|
|
50
|
+
cur_loc = act(room_dic, cur_loc)
|
|
51
|
+
print("Environment State:", room_dic)
|
|
52
|
+
step += 1
|
|
53
|
+
print("\nFINAL STATE:", room_dic,"\n")
|
|
54
|
+
|
|
55
|
+
'''
|
|
56
|
+
|
|
57
|
+
def getCode():
|
|
58
|
+
global code
|
|
59
|
+
print(code)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
code = '''
|
|
2
|
+
import random
|
|
3
|
+
|
|
4
|
+
grid_size = 4
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def generate_world():
|
|
8
|
+
world = [[' ' for _ in range(grid_size)] for _ in range(grid_size)]
|
|
9
|
+
|
|
10
|
+
# Place Wumpus, Gold, and Pits randomly
|
|
11
|
+
elements = ['W', 'G', 'P', 'P']
|
|
12
|
+
positions = random.sample([(i, j) for i in range(grid_size) for j in range(grid_size) if (i, j) != (0, 0)], len(elements))
|
|
13
|
+
|
|
14
|
+
for (x, y), elem in zip(positions, elements):
|
|
15
|
+
world[x][y] = elem
|
|
16
|
+
|
|
17
|
+
return world
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def display_percepts(world, x, y):
|
|
21
|
+
percepts = []
|
|
22
|
+
|
|
23
|
+
if any(world[i][j] == 'W' for i, j in get_neighbors(x, y)):
|
|
24
|
+
percepts.append("Stench detected!")
|
|
25
|
+
if any(world[i][j] == 'P' for i, j in get_neighbors(x, y)):
|
|
26
|
+
percepts.append("Breeze detected!")
|
|
27
|
+
|
|
28
|
+
return percepts
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def get_neighbors(x, y):
|
|
32
|
+
neighbors = []
|
|
33
|
+
for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
|
|
34
|
+
nx, ny = x + dx, y + dy
|
|
35
|
+
if 0 <= nx < grid_size and 0 <= ny < grid_size:
|
|
36
|
+
neighbors.append((nx, ny))
|
|
37
|
+
return neighbors
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def display_state(x, y):
|
|
41
|
+
for i in range(grid_size):
|
|
42
|
+
for j in range(grid_size):
|
|
43
|
+
if (i, j) == (x, y):
|
|
44
|
+
print('A', end=' ')
|
|
45
|
+
else:
|
|
46
|
+
print('.', end=' ')
|
|
47
|
+
print()
|
|
48
|
+
print()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
if __name__ == "__main__":
|
|
52
|
+
world = generate_world()
|
|
53
|
+
x, y = 0, 0 # Starting position
|
|
54
|
+
|
|
55
|
+
while True:
|
|
56
|
+
display_state(x, y)
|
|
57
|
+
percepts = display_percepts(world, x, y)
|
|
58
|
+
for p in percepts:
|
|
59
|
+
print(p)
|
|
60
|
+
|
|
61
|
+
move = input("Move (up/down/left/right): ").strip().lower()
|
|
62
|
+
if move == "up" and x > 0:
|
|
63
|
+
x -= 1
|
|
64
|
+
elif move == "down" and x < grid_size - 1:
|
|
65
|
+
x += 1
|
|
66
|
+
elif move == "left" and y > 0:
|
|
67
|
+
y -= 1
|
|
68
|
+
elif move == "right" and y < grid_size - 1:
|
|
69
|
+
y += 1
|
|
70
|
+
else:
|
|
71
|
+
print("Invalid move. Try again.")
|
|
72
|
+
continue
|
|
73
|
+
|
|
74
|
+
if world[x][y] == 'W':
|
|
75
|
+
print("You were eaten by the Wumpus! Game Over.")
|
|
76
|
+
break
|
|
77
|
+
elif world[x][y] == 'P':
|
|
78
|
+
print("You fell into a pit! Game Over.")
|
|
79
|
+
break
|
|
80
|
+
elif world[x][y] == 'G':
|
|
81
|
+
print("You found the Gold! You Win!")
|
|
82
|
+
break
|
|
83
|
+
|
|
84
|
+
'''
|
|
85
|
+
|
|
86
|
+
def getCode():
|
|
87
|
+
global code
|
|
88
|
+
print(code)
|
|
89
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
files = '''
|
|
2
|
+
AlphaBetaPr
|
|
3
|
+
BFS_8pz
|
|
4
|
+
DFS_8pz
|
|
5
|
+
DLimitS_8pz
|
|
6
|
+
GBFS
|
|
7
|
+
GraphColor
|
|
8
|
+
Minimax
|
|
9
|
+
Prologs
|
|
10
|
+
ticTacToe
|
|
11
|
+
UCS
|
|
12
|
+
VaccumCleaner
|
|
13
|
+
waterJug
|
|
14
|
+
Wumpus
|
|
15
|
+
|
|
16
|
+
'''
|
|
17
|
+
|
|
18
|
+
print(f"[ai] Modules: {files}")
|
|
19
|
+
print("example usage: `import numpyUtilsNani.ai.{Module}.getcode()`....")
|