0% found this document useful (0 votes)
15 views16 pages

Ai Lab Assignment-1 (22bce20065)

The document discusses goal formation and problem formation in AI, using the 8 queens problem as an example to illustrate how to define goals and translate them into solvable problems. It also lists different search algorithms in AI, categorizing them into uninformed and informed searches, and provides code examples for solving the 8 queens problem and the 8 puzzle problem using breadth-first search. The importance of understanding these concepts is emphasized for AI agents to navigate complex tasks effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

Ai Lab Assignment-1 (22bce20065)

The document discusses goal formation and problem formation in AI, using the 8 queens problem as an example to illustrate how to define goals and translate them into solvable problems. It also lists different search algorithms in AI, categorizing them into uninformed and informed searches, and provides code examples for solving the 8 queens problem and the 8 puzzle problem using breadth-first search. The importance of understanding these concepts is emphasized for AI agents to navigate complex tasks effectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

ARTIFICIAL INTELLIGENCE

LAB ASSIGNMENT – 1

Name: M.J.N.SANJAY
Reg No: 22BCE20065

1)What is Goal formation and problem formation


in AI? Explain Goal formation and problem
formation 8 queens problem
Ans:
Goal Formation and Problem Formation in AI:
Both goal formation and problem formation are crucial steps
in the reasoning and problem-solving process of AI agents.
Let's break them down:

Goal Formation:
Definition: Deciding on the desired final state the agent
wants to achieve.
Involves:
Identifying the objective or purpose of the task.
Specifying the desired outcome in a well-defined way.
Can be influenced by prior experience, reward models, and
external environment.
Problem Formation:
Definition: Transforming the desired goal into a well-defined
problem that can be solved.
Involves:
Identifying the initial state (current world state).
Defining the set of possible actions the agent can take.
Determining the state transitions resulting from each action.
Specifying a success criterion for achieving the goal.
Understanding with the 8 Queens Problem:

Imagine an AI trying to solve the 8 queens problem.

Goal formation:
The goal is to place 8 queens on a chessboard such that no
two queens can attack each other (no two queens in the
same row, column, or diagonal).
Problem formation:
The initial state is an empty chessboard.
Possible actions are placing a queen on any of the 64 squares.
State transitions involve placing a queen and updating the
board, considering queen placements as potential conflicts.
The success criterion is having all 8 queens placed without
conflicts.
By forming a clear goal and transforming it into a well-defined
problem, the AI agent can utilize search algorithms to explore
possible actions and find a sequence that achieves the
desired goal of placing all queens safely.
Importance of Interplay:
Goal formation and problem formation are interconnected
and often happen iteratively. An initial problem formulation
might reveal the need for a more specific goal, and achieving
a sub-goal might require reformulating the remaining
problem. This dynamic process allows the AI agent to adapt
and refine its approach towards achieving the overall goal.
Conclusion:
Understanding goal formation and problem formation is
crucial for comprehending how AI agents tackle complex
tasks. By defining clear goals and translating them into
actionable problems, AI can navigate complex environments
and achieve desired outcomes.
2)list different searching algorthims in AI
Ans:
Types of search algorithms:
Based on the search problems we can classify the search
algorithms into uninformed (Blind search) search and
informed search (Heuristic search) algorithms.
Uninformed/Blind Search:
The uninformed search does not contain any domain
knowledge such as closeness, the location of the goal. It
operates in a brute-force way as it only includes information
about how to traverse the tree and how to identify leaf and
goal nodes. Uninformed search applies a way in which search
tree is searched without any information about the search
space like initial state operators and test for the goal, so it is
also called blind search.It examines each node of the tree
until it achieves the goal node.
It can be divided into five main types:
1)Breadth-first search
2)Uniform cost search
3)Depth-first search
4)Iterative deepening depth-first search
5)Bidirectional Search
Informed Search:
Informed search algorithms use domain knowledge. In an
informed search, problem information is available which can
guide the search. Informed search strategies can find a
solution more efficiently than an uninformed search strategy.
Informed search is also called a Heuristic search.
A heuristic is a way which might not always be guaranteed for
best solutions but guaranteed to find a good solution in
reasonable time.
Informed search can solve much complex problem which
could not be solved in another way.
An example of informed search algorithms is a traveling
salesman problem.
1)Greedy Search
2)A* Search

3)write a program to solve 8 queens problem


Code:-
N = 8 # (size of the chessboard)
def solveNQueens(board, col):
if col == N:
print(board)
return True
for i in range(N):
if isSafe(board, i, col):
board[i][col] = 1
if solveNQueens(board, col + 1):
return True
board[i][col] = 0
return False

def isSafe(board, row, col):


for x in range(col):
if board[row][x] == 1:
return False
for x, y in zip(range(row, -1, -1), range(col, -1, -1)):
if board[x][y] == 1:
return False
for x, y in zip(range(row, N, 1), range(col, -1, -1)):
if board[x][y] == 1:
return False
return True

board = [[0 for x in range(N)] for y in range(N)]


if not solveNQueens(board, 0):
print("No solution found")
Result:-

4)write a programe to solve 8 puzzle problem(bfs)


Code
Code:-
import sys
import numpy as np

class Node:
def __init__(self, state, parent, action):
self.state = state
self.parent = parent
self.action = action
class StackFrontier:
def __init__(self):
self.frontier = []

def add(self, node):


self.frontier.append(node)

def contains_state(self, state):


return any((node.state[0] == state[0]).all() for node
in self.frontier)

def empty(self):
return len(self.frontier) == 0

def remove(self):
if self.empty():
raise Exception("Empty Frontier")
else:
node = self.frontier[-1]
self.frontier = self.frontier[:-1]
return node

class QueueFrontier(StackFrontier):
def remove(self):
if self.empty():
raise Exception("Empty Frontier")
else:
node = self.frontier[0]
self.frontier = self.frontier[1:]
return node

class Puzzle:
def __init__(self, start, startIndex, goal, goalIndex):
self.start = [start, startIndex]
self.goal = [goal, goalIndex]
self.solution = None

def neighbors(self, state):


mat, (row, col) = state
results = []
if row > 0:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row - 1][col]
mat1[row - 1][col] = 0
results.append(('up', [mat1, (row - 1, col)]))
if col > 0:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row][col - 1]
mat1[row][col - 1] = 0
results.append(('left', [mat1, (row, col - 1)]))
if row < 2:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row + 1][col]
mat1[row + 1][col] = 0
results.append(('down', [mat1, (row + 1, col)]))
if col < 2:
mat1 = np.copy(mat)
mat1[row][col] = mat1[row][col + 1]
mat1[row][col + 1] = 0
results.append(('right', [mat1, (row, col + 1)]))
return results

def print(self):
solution = self.solution if self.solution is not None
else None
print("Start State:\n", self.start[0], "\n")
print("Goal State:\n", self.goal[0], "\n")
print("\nStates Explored: ", self.num_explored, "\
n")
print("Solution:\n ")
for action, cell in zip(solution[0], solution[1]):
print("action: ", action, "\n", cell[0], "\n")
print("Goal Reached!!")

def does_not_contain_state(self, state):


for st in self.explored:
if (st[0] == state[0]).all():
return False
return True

def solve(self):
self.num_explored = 0
start = Node(state=self.start, parent=None,
action=None)
frontier = QueueFrontier()
frontier.add(start)

self.explored = []

while True:
if frontier.empty():
raise Exception("No solution")

node = frontier.remove()
self.num_explored += 1

if (node.state[0] == self.goal[0]).all():
actions = []
cells = []
while node.parent is not None:
actions.append(node.action)
cells.append(node.state)
node = node.parent
actions.reverse()
cells.reverse()
self.solution = (actions, cells)
return

self.explored.append(node.state)

for action, state in self.neighbors(node.state):


if not frontier.contains_state(state) and
self.does_not_contain_state(state):
child = Node(state=state,
parent=node, action=action)
frontier.add(child)

start = np.array([[1, 2, 3], [8, 0, 4], [7, 6, 5]])


goal = np.array([[2, 8, 1], [0, 4, 3], [7, 6, 5]])

startIndex = (1, 1)
goalIndex = (1, 0)
p = Puzzle(start, startIndex, goal, goalIndex)
p.solve()
p.print()
Result:-

You might also like