All
All
class SimpleProblemSolvingAgent:
def __init__(self):
self.seq = [] # Action sequence (initially empty)
self.state = None # Current world state
self.goal = None # Goal (initially null)
self.problem = None # Problem formulation (initially null)
# Example usage
if __name__ == "__main__":
agent = SimpleProblemSolvingAgent()
Output
Action: move to living room
Action: vacuum
# Example usage
start_node = 'A'
goal_node = 'G'
depth_limit = 2
UCS Algorithm
import heapq
:param graph: A dictionary where keys are nodes and values are lists of
(neighbor, cost) tuples.
:param start: The starting node.
:param goal: The goal node.
:return: A tuple containing the least-cost path and its total cost.
"""
# Priority queue to store (cost, node, path)
priority_queue = [(0, start, [])]
visited = set()
while priority_queue:
# Dequeue the node with the smallest cost
cost, node, path = heapq.heappop(priority_queue)
if node in visited:
continue
# Goal test
if node == goal:
return path, cost
# Expand neighbors
for neighbor, edge_cost in graph.get(node, []):
if neighbor not in visited:
heapq.heappush(priority_queue, (cost + edge_cost, neighbor, path))
start_node = 'A'
goal_node = 'G'
class TicTacToe:
def __init__(self):
self.board = []
def create_board(self):
for i in range(3):
row = []
for j in range(3):
row.append('-')
self.board.append(row)
def get_random_first_player(self):
return random.randint(0, 1)
n = len(self.board)
# checking rows
for i in range(n):
win = True
for j in range(n):
if self.board[i][j] != player:
win = False
break
if win:
return win
# checking columns
for i in range(n):
win = True
for j in range(n):
if self.board[j][i] != player:
win = False
break
if win:
return win
# checking diagonals
win = True
for i in range(n):
if self.board[i][i] != player:
win = False
break
if win:
return win
win = True
for i in range(n):
if self.board[i][n - 1 - i] != player:
win = False
break
if win:
return win
return False
def is_board_filled(self):
for row in self.board:
for item in row:
if item == '-':
return False
return True
def show_board(self):
for row in self.board:
for item in row:
print(item, end=" ")
print()
def start(self):
self.create_board()
self.show_board()
OutPut
Player O turn
- - -
- - -
- - -
Enter row and column numbers to fix spot: 1 1
Player X turn
O - -
- - -
- - -
Enter row and column numbers to fix spot: 1 2
Player O turn
O X -
- - -
- - -
Enter row and column numbers to fix spot: 2 2
Player X turn
O X -
- O -
- - -
Enter row and column numbers to fix spot: 3 3
Player O turn
O X -
- O -
- - X
Enter row and column numbers to fix spot: 1 3
Player X turn
O X O
- O -
- - X
Enter row and column numbers to fix spot: 3 1
Player O turn
O X O
- O -
X - X
Enter row and column numbers to fix spot: 3 2
Player X turn
O X O
- O -
X O X
Enter row and column numbers to fix spot: 2 3
Player O turn
O X O
- O X
X O X
Enter row and column numbers to fix spot: 2 1
Match Draw!
O X O
O O X
X O X
OUTPUT:
1 2 3
5 6 0
7 8 4
1 2 3
5 0 6
7 8 4
1 2 3
5 8 6
7 0 4
1 2 3
5 8 6
7 4
OUTPUT
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B