0% found this document useful (0 votes)
10 views

Pdflabreport AI

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Pdflabreport AI

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

SPA College

(Affiliated to Tribhuvan University)


Dhangadh-2, Kailali, Nepal

Lab Report
On
Artificial Intelligence

For the partial fulfilment of requirements for the degree of Bachelor of


Information Management under Tribhuvan University

Submitted to:
Department of Bachelor of Information Management
SPA College
Under the Supervision of:
Mr. Keshab Pal
Lecturer, BIM Department

Submitted By:
Jeewan Bhatta
BIM 5th Semester
August, 2024
LETTER OF RECOMMENDATION
Date: 05 /03 /2081
This is to certify that the “Artificial Intelligence” has been carried out by Mr.
Jeewan Bhatta in partial fulfilment of the degree of Bachelor of Information
Management for Tribhuvan University, during the academic year 2081. We
further declare that the work reported in this lab report has not been submitted
and will not be submitted, either in part or in full, for the award of any other
degree or diploma in this institute or any other institute or university.

To the best of our knowledge and belief, this work embodies the work of the
candidate himself/herself, has duly been completed, fulfils the requirement of the
ordinance relating to the bachelor’s degree of the university, and is up to the
standard in respect of content, presentation, and language for being referred to the
examiner.

………………………….. ……………………………
Keshab Pal Dharma Raj Bhatta
(Lecturer, Lab tutor) (Program Director, BIM)

…………………………

External
1) Creating multiple family trees in Python

Creating multiple family trees in Python involves representing family members and their
relationships. Here's a basic outline on how you can implement this:

1.Define the Family Member Structure: Create a structure to represent each family member.
This structure can include pointers to other family members to represent relationships.

2.Create Family Trees: Implement functions to create and manage multiple family trees.

3.Add Members and Relationships: Implement functions to add members and define their
relationships within a family tree.

4. Display Family Trees: Implement functions to display the family trees for easy visualization.

Here's a basic example.

Code:-
class Family:
def __init__(self, name, gender):
self.name = name
self.gender = gender
self.parents = []
self.children = []

def add_parent(self, parent):


self.parents.append(parent)

def add_child(self, child):


self.children.append(child)

def __repr__(self):
return self.name

class FamilyMember:
def __init__(self):
self.members = {}

def add_member(self, name, gender):


if name not in self.members:
self.members[name] = Family(name, gender)
return self.members[name]

def relationship(self, parent_name, child_name):


parent = self.members.get(parent_name)
child = self.members.get(child_name)
if parent and child:
parent.add_child(child)
child.add_parent(parent)
else:
print("Error: One or both members not found in the family tree.")

def get_member(self, name):


return self.members.get(name)

def __repr__(self):
return '\n'.join(f"{member}: Parents: {[parent.name for parent in
member.parents]}, Children: {[child.name for child in member.children]}"
for member in self.members.values())
family_1 = FamilyMember()
family_2 = FamilyMember()

family_1.add_member("Basudev", "Male")
family_1.add_member("Puspa", "Female")
family_1.add_member("Jeewan", "Male")

family_1.relationship("Basudev", "Jeewan")
family_1.relationship("Puspa", "Jeewan")

family_2.add_member("Naresh", "Male")
family_2.add_member("Parbati", "Female")
family_2.add_member("Prakash", "Male")

family_2.relationship("Naresh", "Prakash")
family_2.relationship("Parbati", "Prakash")

print("first family tree:")


print(family_1)

print("\nsecond family tree:")


print(family_2)

Output:-
2) Factorial program
To write a Python program that generates and displays the factorial series, you need to
understand the concept of factorials. The factorial of a non-negative integer \( n \) is the product
of all positive integers less than or equal to \( n \), and it's denoted by \( n! \). For example, \(
5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 \).

Here's how you can write a Python program to generate and display the factorial series for
numbers from 0 to a specified limit:

Step-by-Step Implementation

1.Include necessary headers: Include the standard input/output library.

2.Function to calculate factorial: Write a function to calculate the factorial of a number.

3.Main function: Get the limit from the user and display the factorial series.

Code:-
def factorial(n):

if n == 0 or n==1 :

return 1

else:

return n * factorial(n-1)

number = int(input("Enter a number: "))

print(f"sereies of factorial uppto {number} number is:")

for i in range(1, number+1):

print(f"{i}!={factorial(i)}")

Output:-
3) Fibonacci series
To write a Python program that generates and displays the Fibonacci series, you need to
understand the concept of the Fibonacci sequence. The Fibonacci sequence is a series of
numbers in which each number is the sum of the two preceding ones, usually starting with 0
and 1.

Here's how you can write a Python program to generate and display the Fibonacci series up to
a specified number of terms:

Example Program.

Code:-
def fibonacci(num):
a, b = 0, 1
result = []
if num==0:
return 0
elif num==1:
return 1
else:
for _ in range(num):
result.append(a)
a, b = b, a + b
return result

num = int(input("Enter the number of terms up to which you want to print


fibonacci series: "))
print("Fibonacci series up to", num, "terms is:")
print(fibonacci(num))

Output:-
4) Ancestor Program in Python
Creating an ancestor program in Python involves managing a family tree structure and
allowing for queries about the ancestors of a specific family member. This can be done by
defining a `FamilyMember` structure and implementing functions to manage the family tree
and query ancestors.

Here's an example program that demonstrates how to implement this:

Example Program

1.Define the Family Member Structure: Define a structure to represent each family
member.

2.Add Family Members: Implement functions to add members and define their
relationships.

3.Find Ancestors: Implement functions to find and display the ancestors of a specific
member.

4.Display Ancestors: Implement a main function to interact with the user and display the
results.

Code:-
class Family:

def __init__(self, name, gender):

self.name = name

self.gender = gender

self.parents = []

def add_parent(self, parent):

self.parents.append(parent)
def __repr__(self):

return self.name

class Familymember:

def __init__(self):

self.members = {}

def add_member(self, name, gender):

if name not in self.members:

self.members[name] = Family(name, gender)

return self.members[name]

def add_relationship(self, parent_name, child_name):

parent = self.members.get(parent_name)

child = self.members.get(child_name)

if parent and child:

child.add_parent(parent)

else:

print("Error: One or both members not found in the family tree.")

def get_member(self, name):

return self.members.get(name)

def find_ancestors(member, ancestors=None):

if ancestors is None:

ancestors = set()
for parent in member.parents:

if parent not in ancestors:

ancestors.add(parent)

find_ancestors(parent, ancestors)

return ancestors

def main():

# Create the family tree as the oblect of class Family memeber

family_tree = Familymember()

#To Add family members in the family

family_tree.add_member("BasuDev", "Male")

family_tree.add_member("Jeewan", "Male")

family_tree.add_member("Bishnu", "Male")

family_tree.add_member("Laxmi", "Female")

family_tree.add_member("Ganesh", "Male")

family_tree.add_member("Kumar", "Male")

family_tree.add_member("Saraswati", "Female")

# To Add relationships between parent and child

family_tree.add_relationship("BasuDev", "Jeewan")

family_tree.add_relationship("Bishnu", "Ganesh")

family_tree.add_relationship("Laxmi", "Ganesh")

family_tree.add_relationship("Kumar", "Bishnu")

family_tree.add_relationship("Saraswati", "Bishnu")

# To Query the name of ancestors


member_name = input("Enter the name of the family member to find their
ancestors: ")

member = family_tree.get_member(member_name)

if member:

ancestors = find_ancestors(member)

if ancestors:

print(f"Ancestors of {member_name}: {', '.join([ancestor.name for


ancestor in ancestors])}")

else:

print(f"{member_name} has no recorded ancestors.")

else:

print(f"Member {member_name} not found in the family tree.")

# To Run the program by calling the main function

if __name__ == "__main__":

main()

Output:-
5) Tower of Hanoi program in Python
The Tower of Hanoi is a classic problem in computer science and mathematics, involving
three pegs and a number of disks of different sizes. The objective is to move all the disks
from the first peg to the third peg, following these rules:

1. Only one disk can be moved at a time.

2. A disk can only be placed on top of a larger disk or on an empty peg.

3. All disks start on the first peg, and they must end on the third peg.

Code:-
def TowerOfHanoi(n, source, target, auxiliary):
if n > 0:
# Move n - 1 disks from source to auxiliary, so they are out of the way
TowerOfHanoi(n - 1, source, auxiliary, target)
# Move the nth disk from source to target
print(f"Move disk {n} from rod {source} to rod {target}")

# Move the n - 1 disks that we left on auxiliary to target


TowerOfHanoi(n - 1, auxiliary, target, source)

# To print the code by calling the method


n = 3
TowerOfHanoi(n, 'A', 'C', 'B')
Output:-
6) Monkey Banana program in Python
The Monkey Banana Problem is a classic AI problem that involves a monkey attempting to
reach a bunch of bananas hanging from the ceiling. The monkey must use a series of actions to
get to the bananas, such as climbing onto a box, moving the box, or climbing the box to grab
the bananas.

State Representation

- `Monkey on the ground`

- `Monkey on the box`

- `Box at a specific position`

- `Monkey has bananas`

Actions

- `Move to box`

- `Push box to banana position`

- `Climb box`

- `Grab bananas`

Code:-
class MonkeyBananaProblem:
def __init__(self, monkey_pos, box_pos, banana_pos):
self.monkey_pos = monkey_pos
self.box_pos = box_pos
self.banana_pos = banana_pos
self.monkey_on_box = False
self.monkey_has_bananas = False

def move_to_box(self):
print(f"Monkey moves from {self.monkey_pos} to the box at
{self.box_pos}.")
self.monkey_pos = self.box_pos

def push_box_to_bananas(self):
print(f"Monkey pushes the box from {self.box_pos} to the banana
position at {self.banana_pos}.")
self.box_pos = self.banana_pos

def climb_box(self):
print("Monkey climbs onto the box.")
self.monkey_on_box = True
def grab_bananas(self):
if self.monkey_on_box and self.box_pos == self.banana_pos:
print("Monkey grabs the bananas!")
self.monkey_has_bananas = True
else:
print("Invalid move. Monkey is not on the box at the banana
position.")

def is_goal_state(self):
return self.monkey_has_bananas

def solve_monkey_banana_problem():
monkey_pos = "A"
box_pos = "B"
banana_pos = "C"

problem = MonkeyBananaProblem(monkey_pos, box_pos, banana_pos)

actions = [problem.move_to_box, problem.push_box_to_bananas,


problem.climb_box, problem.grab_bananas]
for action in actions:
action()

if problem.is_goal_state():
print("Monkey successfully grabbed the bananas!")
else:
print("Monkey couldn't reach the bananas.")

if __name__ == "__main__":
solve_monkey_banana_problem()

Output:-
7) Realization of logic gates in Python

Implementing logic gates in Python can be done by creating functions that emulate the
behavior of each logic gate. Below are examples of how to implement basic logic gates
(AND, OR, NOT, NAND, NOR, XOR, XNOR) in Python.

Code:-
def AND(a, b):
if a == 1 and b == 1:
return 1
else:
return 0

def OR(a, b):


if a == 1 or b == 1:
return 1
else:
return 0

def NOT(a):
if a == 1:
return 0
else:
return 1

def NAND(a, b):


return NOT(AND(a, b))

def NOR(a, b):


return NOT(OR(a, b))

def XOR(a, b):


if a != b:
return 1
else:
return 0

def XNOR(a, b):


return NOT(XOR(a, b))

# for Testing the logic gates by calling the functions


print("AND Gate:")
print(f"(0,0):{AND(0, 0)}") # 0
print(f"(0,1):{AND(0, 1)}") # 0
print(f"(1,0):{AND(1, 0)}") # 0
print(f"(1,1):{AND(1, 1)}") # 1
print("\nOR Gate:")
print(f"(0,0):{OR(0, 0)}") # 0
print(f"(0,1):{OR(0, 1)}") # 1
print(f"(1,0):{OR(1, 0)}") # 1
print(f"(1,1):{OR(1, 1)}") # 1

print("\nNOT Gate:")
print(f"(0):{NOT(0)}") # 1
print(f"(1):{NOT(1)}") # 0

print("\nNAND Gate:")
print(f"(0,0):{NAND(0, 0)}") # 1
print(f"(0,1):{NAND(0, 1)}") # 1
print(f"(1,0):{NAND(1, 0)}") # 1
print(f"(1,1):{NAND(1, 1)}") # 0

print("\nNOR Gate:")
print(f"(0,0):{NOR(0, 0)}") # 1
print(f"(0,1):{NOR(0, 1)}") # 0
print(f"(1,0):{NOR(1, 0)}") # 0
print(f"(1,1):{NOR(1, 1)}") # 0

print("\nXOR Gate:")
print(f"(0,0):{XOR(0, 0)}") # 0
print(f"(0,1):{XOR(0, 1)}") # 1
print(f"(1,0):{XOR(1, 0)}") # 1
print(f"(1,1):{XOR(1, 1)}") # 0

print("\nXNOR Gate:")
print(f"(0,0):{XNOR(0, 0)}") # 1
print(f"(0,1):{XNOR(0, 1)}") # 0
print(f"(1,0):{XNOR(1, 0)}") # 0
print(f"(1,1):{XNOR(1, 1)}") # 1
Output:-

You might also like