Jayant Aiiii
Jayant Aiiii
ARTIFICIAL INTELLIGENCE
PRACTICAL FILE
SUBJECT CODE: BCAP - 254
L-3 Write a program for Best First Search and A* Search Algorithm
12-02-24
Write a program for list methods (Add, Append, Extend & Delete) 08-04-24
L-
10
Write a program to implement First Order Predicate using
L- 15-04-24
11 I. Forward Chaining ii. Backward Chaining
Q1: Write steps to install Python and required libraries for AI. Write
instructions for setting up a Python virtual environment.
Sol:
Steps to install Python and required libraries for AI
1. Install Python
• Here are some commonly used libraries for AI development and their installation
commands:
- NumPy: For numerical computing
pip install numpy
1
07113702022 Jayant Chaudhary BCA – M2
- Keras: High-level neural networks API (usually used with TensorFlow or Theano)
pip install keras
4. Verify Installations
• After installing the libraries, it's a good idea to verify that they were
installed correctly. You can do this by importing them in a Python script or
in a Jupyter Notebook and running some basic commands.
2
07113702022 Jayant Chaudhary BCA – M2
• That's it! You should now have Python installed along with some essential
libraries for AI development. Remember that the specific libraries you need
may vary depending on your project requirements.
virtualenv myenv
• Replace `myenv` with the desired name for your virtual environment.
source myenv/bin/activate
• After activation, you should see the name of your virtual environment in
the terminal or command prompt prompt.
4. Install Dependencies:
3
07113702022 Jayant Chaudhary BCA – M2
• While your virtual environment is active, you can install Python packages just like you
pip install numpy pandas matplotlib
deactivate
4
07113702022 Jayant Chaudhary BCA – M2
5
07113702022 Jayant Chaudhary BCA – M2
Q2: Write a program to implement Breadth First Search and Depth First
Search.
Source Code:
from collections import defaultdict
class Graph:
def init (self):
self.graph = defaultdict(list)
def add_edge(self, u, v):
self.graph[u].append(v)
def bfs(self, start):
visited = [False] * (len(self.graph))
queue = []
queue.append(start)
visited[start] = True
while queue:
start = queue.pop(0)
print(start, end=" ")
for i in self.graph[start]:
if not visited[i]:
queue.append(i)
visited[i] = True
def dfs_util(self, v, visited):
visited[v] = True
print(v, end=" ")
for i in self.graph[v]:
if not visited[i]:
self.dfs_util(i, visited)
def dfs(self, start):
visited = [False] * (len(self.graph))
self.dfs_util(start, visited)
g = Graph()
g.add_edge(0, 1)
g.add_edge(0, 2)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(2, 3)
g.add_edge(3, 3)
print("Breadth First Search:")
g.bfs(2)
print("\nDepth First Search:")
g.dfs(2)
6
07113702022 Jayant Chaudhary BCA – M2
Output:
7
07113702022 Jayant Chaudhary BCA – M2
Q3: Write a program for Best First Search and A* Search Algorithm.
Source Code:
Best First Search:
from queue import PriorityQueue
v = 14
graph = [[] for i in range(v)]
def best_first_search(actual_Src, target, n):
visited = [False] * n
pq = PriorityQueue()
pq.put((0, actual_Src))
visited[actual_Src] = True
while pq.empty() == False:
u = pq.get()[1]
print(u, end=" ")
if u == target:
break
for v, c in graph[u]:
if visited[v] == False:
visited[v] = True
pq.put((c, v))
print()
def addedge(x, y, cost):
graph[x].append((y, cost))
graph[y].append((x, cost))
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)
source = 0
target = 9
best_first_search(source, target, v)
8
07113702022 Jayant Chaudhary BCA – M2
Output:
A* Search Algorithm:
import heapq
class Graph:
def init (self):
self.graph = {}
def add_edge(self, u, v, w):
if u in self.graph:
self.graph[u].append((v, w))
else:
self.graph[u] = [(v, w)]
def astar_search(self, start, goal):
pq = [(0, start)]
visited = set()
while pq:
f_cost, current = heapq.heappop(pq)
if current == goal:
return True
if current not in visited:
visited.add(current)
for neighbor, weight in self.graph[current]:
if neighbor not in visited:
heapq.heappush(pq, (f_cost + weight + self.heuristic(neighbor, goal), neighbor))
return False
def heuristic(self, u, v):
x1, y1 = u
x2, y2 = v
return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
g = Graph()
g.add_edge((0, 0), (1, 0), 1)
g.add_edge((1, 0), (2, 0), 1)
g.add_edge((1, 1), (2, 1), 1)
g.add_edge((2, 0), (2, 1), 1)
g.add_edge((2, 1), (2, 2), 1)
start = (0, 0)
goal = (2, 2)
9
07113702022 Jayant Chaudhary BCA – M2
Output:
10
07113702022 Jayant Chaudhary BCA – M2
Output:
11
07113702022 Jayant Chaudhary BCA – M2
12
07113702022 Jayant Chaudhary BCA – M2
13
07113702022 Jayant Chaudhary BCA – M2
Output:
14
07113702022 Jayant Chaudhary BCA – M2
Q7: Write a program to implement Hill Climbing and Steepest Ascent Hill
Climbing Algorithm.
Source Code:
Hill Climbing Algorithm:
import random
def hill_climbing(problem, max_iterations=1000):
current_solution = problem.random_solution()
current_value = problem.evaluate(current_solution)
for _ in range(max_iterations):
neighbors = problem.get_neighbors(current_solution)
next_solution = max(neighbors, key=lambda x: problem.evaluate(x))
next_value = problem.evaluate(next_solution)
if next_value <= current_value:
break
current_solution = next_solution
current_value = next_value
return current_solution
class OptimizationProblem:
def random_solution(self):
return [random.uniform(0, 10), random.uniform(0, 10)]
def evaluate(self, solution):
x, y = solution
return -((x - 5) ** 2 + (y - 5) ** 2)
def get_neighbors(self, solution):
x, y= solution
neighbors = [
[x + random.uniform(-1, 1), y + random.uniform(-1, 1)] for _ in range(10)
]
return neighbors
problem = OptimizationProblem()
best_solution = hill_climbing(problem)
print("--> Best Solution:", best_solution)
print("--> Best Value:", problem.evaluate(best_solution))
Output:
15
07113702022 Jayant Chaudhary BCA – M2
Output:
16
07113702022 Jayant Chaudhary BCA – M2
17
07113702022 Jayant Chaudhary BCA – M2
Q9: Write a program for list operations (Nested List, Length, Concatenation,
Membership, Iteration, Indexing, and Slicing).
Source Code:
nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print("-> 1. Nested List:", nested_list)
print("-> 2. Length of the nested list:", len(nested_list))
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print("-> 3. Concatenated list:", concatenated_list)
element = 3
if element in concatenated_list:
print(f"-> {element} is present in the concatenated list")
else:
print(f"-> {element} is not present in the concatenated list")
print("-> 5. Iterating through the concatenated list: ")
for item in concatenated_list:
print("\t",item,end = " ")
index = 2
print("\n-> 6. Element at index", index, ":", concatenated_list[index])
start_index = 1
end_index = 4
print("-> 7. Sliced list from index", start_index, "to", end_index, ":",
concatenated_list[start_index:end_index])
Output:
18
07113702022 Jayant Chaudhary BCA – M2
Q10: Write a program for list methods (Add, Append, Extend & Delete).
Source Code:
my_list = [1, 2, 3]
my_list.append(4)
print("-> After appending 4:", my_list)
another_list = [5, 6, 7]
my_list.extend(another_list)
print("-> After extending by [5, 6, 7]:", my_list)
my_list.insert(2, 8)
print("-> After inserting 8 at index 2:", my_list)
my_list.remove(3)
print("-> After removing 3:", my_list)
del my_list[1]
print("-> After deleting element at index 1:", my_list)
Output:
19
07113702022 Jayant Chaudhary BCA – M2
20
07113702022 Jayant Chaudhary BCA – M2
Output:
21