You are on page 1of 29

DEPARTMENT OF COMPUTER

SCIENCE AND APPLICATIONS


PANJAB UNIVERSITY, CHANDIGARH

(SESSION 2020 – 21)

Artificial Intelligence
Assignments

SUBMITTED TO: SUBMITTED BY:


Prof. Syamla Devi Mohit Saini
MCA –4th Sem (Morning)
Roll No.: 20
2

INDEX
S. No. Programs Page No.
1. Explore and discuss Physical symbol 4
system Hypothesis.
2. Discuss the applications of Game playing. 5
3. Explain the Artificial Intelligence (AI) 6
technique for Tic-Tac-Toe Game playing
with example.
4. Explain the following search techniques 8
with example for each.
(i) Depth limited search
(ii) Greedy Best First Search
5. Compare and contrast Best First Search, 14
(BFS), Greedy BFS and A* Algorithms.
6. Consider any two example sentences 16
(different from those discussed in class)
and convert them into predicate logic
form and then into clausal form.
7. Consider any two example sentences 18
(different from those discussed in class)
and represent them using semantic
networks.
8. Give a detailed example of a script 19
different from that discussed in the
Class.
9. Provide (i) top-down parsing and (ii) 21
bottom-up parsing for the following
sentence.
3

“The breeze is cool.”

S. No. Lisp Assignments Page No.


10. Write and execute any five example Lisp 23
programs from those discussed in the
class.

1. Program to convert temperature from


Celsius to Fahrenheit.
2. Factorial of a number.
3. Find Sum of n number.
4. Find m raise to power n.
5. Append two list.
11. Write and execute any five example Lisp 25
programs different from those discussed
in the class.

1. Program to print multiplication table of


given number.
2. Print Fibonacci Series upto given number.
3. Tower of Hanoi.
4. Find root of a quadratic equation.
5. Print area of circle.
4

Q1: Explore and discuss Physical Symbol System Hypothesis.


Ans.:
“A physical symbol system has the necessary and sufficient
means for general intelligent action.”
 This hypothesis has a great position in philosophy of artificial
intelligence.
 Formulated by - Allen Newell and Herbert A. Simon.
 They proposed that symbol manipulation is the essence of both
human and machine intelligence, which mean that human
thinking is symbol processing.
 This is very strong claim as it implies both that :
o Human thinking is a kind of symbol manipulation (because
symbol system is necessary for intelligence).
o Machines can be intelligent (because a symbol system is
sufficient for intelligence).
 Basic idea includes:
o Symbols are physical patterns.
o Symbols can be combined to form complex symbol
structures.
o The system contains processes for manipulating complex
symbol structures.
o The process for representing complex symbol structures
can themselves be symbolically represented within the
system.
5

Q2: Discuss the applications of game playing in AI.


Ans.: Applications of game playing in AI:
 Training Simulators for educations, military training, driving
automobiles, flight stimulation etc.
 Virtual Environments ( real world illusions and effects) for
animations, gaming, research etc.
 Human level intelligence illusions ( create more responsive NPC
to achieve making of smarter AI characters)
 Stimulate critical situation stimulations and decision making to
achieve critical point decision making in AI.
 They provide advanced, existing test regions for developing
several ideas and techniques that are useful elsewhere.
 They are one of the few domains that allow us to build agents.
 Studying games teaches us how to deal with other agents trying
to foil our plans.
 Games present an ideal environment where hostile agents may
compete.
6

Q3: Explain the Artificial Intelligence (AI) technique for Tic-


Tac-Toe Game playing with example.
Ans.: Minimax in Tic-Tac-Toe:
 Minimax is a kind of backtracking algorithm that is used in
decision making and game theory to find the optimal move for a
player, assuming that your opponent also plays optimally. It is
widely used in two player turn-based games such as Tic-Tac-Toe,
Backgammon, Mancala, Chess, etc.
 In Minimax the two players are called maximizer and minimizer.
The maximizer tries to get the highest score possible while the
minimizer tries to do the opposite and get the lowest score
possible.
 Every board state has a value associated with it. In a given state if
the maximizer has upper hand then, the score of the board will
tend to be some positive value. If the minimizer has the upper
hand in that board state then it will tend to be some negative
value. The values of the board are calculated by some heuristics
which are unique for every type of game.

For Tic Tac Toe a board evaluation function is as below:

100A+10B+C–(100D+10E+F)

X and 0 are players

A is the number of lines of three X’s

B is the number of unblocked lined with two X’s.

C is the number of unblocked lines with single X.

Similarly D,E,F give number of lines of 0’s.


7

Example game tree for tic tac toe:

 Minmax algorithm, presented above, requires expanding the


entire state-space.
 Severe limitation, especially for problems with a large state-
space.
 Some nodes in the search can be proven to be irrelevant to the
outcome of the search.

Alpha-Beta Strategy:

 Maintain two bounds : alpha and beta.

How to prune the unnecessary path

 If beta value of any MIN node below a MAX node is less than or
equal to its alpha value, then prune the path below the MIN node.
 If alpha value of any MAX node below a MIN node exceeds the
beta value of the MIN node, then prune the nodes below the MAX
node.
8

Q4: Explain the following search techniques with example


for each.
(i) Depth-limited Search:

A depth-limited search algorithm is similar to depth-first search with


a predetermined limit. Depth-limited search can solve the drawback
of the infinite path in the Depth-first search. In this algorithm, the
node at the depth limit will treat as it has no successor nodes
further.

Depth-limited search can be terminated with two Conditions of


failure:

 Standard failure value: It indicates that problem does not have


any solution.
 Cutoff failure value: It defines no solution for the problem
within a given depth limit.

Advantages:

 Depth-limited search is Memory efficient.

Disadvantages:

 Depth-limited search also has a disadvantage of


incompleteness.
 It may not be optimal if the problem has more than one
solution.
9

Example:

Completeness: DLS search algorithm is complete if the solution is


above the depth-limit.

Time Complexity: Time complexity of DLS algorithm is O(b^ℓ).

Space Complexity: Space complexity of DLS algorithm is O(b×ℓ).

Optimal: Depth-limited search can be viewed as a special case of


DFS, and it is also not optimal even if ℓ>d.
10

(ii) Greedy Best-first Search:

Greedy best-first search algorithm always selects the path which


appears best at that moment. It is the combination of depth-first
search and breadth-first search algorithms. It uses the heuristic
function and search. Best-first search allows us to take the
advantages of both algorithms. With the help of best-first search, at
each step, we can choose the most promising node. In the best first
search algorithm, we expand the node which is closest to the goal
node and the closest cost is estimated by heuristic function, i.e.

f(n)= h(n).

Were, h(n)= estimated cost from node n to the goal.

The greedy best first algorithm is implemented by the priority queue.

Greedy Best first search algorithm:

Step 1: Place the starting node into the OPEN list.

Step 2: If the OPEN list is empty, Stop and return failure.

Step 3: Remove the node n, from the OPEN list which has the lowest
value of h(n), and places it in the CLOSED list.

Step 4: Expand the node n, and generate the successors of node n.

Step 5: Check each successor of node n, and find whether any node
is a goal node or not. If any successor node is goal node, then return
success and terminate the search, else proceed to Step 6.

Step 6: For each successor node, algorithm checks for evaluation


function f(n), and then check if the node has been in either OPEN or
11

CLOSED list. If the node has not been in both list, then add it to the
OPEN list.

Step 7: Return to Step 2.

Advantages:

 Best first search can switch between BFS and DFS by gaining
the advantages of both the algorithms.
 This algorithm is more efficient than BFS and DFS algorithms.

Disadvantages:

 It can behave as an unguided depth-first search in the worst


case scenario.
 It can get stuck in a loop as DFS.
 This algorithm is not optimal.

Example:

Consider the below search problem, and we will traverse it using


greedy best-first search. At each iteration, each node is expanded
using evaluation function f(n)=h(n) , which is given in the below
table.
12

In this search example, we are using two lists which are OPEN and
CLOSED Lists. Following are the iteration for traversing the above
example.
13

Expand the nodes of S and put in the CLOSED list

Initialization: Open [A, B], Closed [S]

Iteration 1: Open [A], Closed [S, B]

Iteration 2: Open [E, F, A], Closed [S, B]

: Open [E, A], Closed [S, B, F]

Iteration 3: Open [I, G, E, A], Closed [S, B, F]

: Open [I, E, A], Closed [S, B, F, G]

Hence the final solution path will be: S----> B----->F----> G

Time Complexity: The worst case time complexity of Greedy best


first search is O(b^m).

Space Complexity: The worst case space complexity of Greedy best


first search is O(b^m). Where, m is the maximum depth of the search
space.

Complete: Greedy best-first search is also incomplete, even if the


given state space is finite.

Optimal: Greedy best first search algorithm is not optimal.


14

Q5: Compare and contrast Best First Search, (BFS), Greedy


BFS and A* Algorithms.
Ans.:
(i) Best-first Search Algorithm:
In the case of Best First Search, when we are at a node, we
can consider any of the adjacent as the next node. It explores
paths without considering any cost function. The idea of BFS
is to use an evaluation function to decide which adjacent is
most promising and then explore. Best First Search falls
under the category of Heuristic Search or Informed Search. It
allows revising the decision of the algorithm.
f(n)= h(n).

(ii) Greedy Best-first Search algorithm:

 In greedy algorithm, it builds up a solution piece by


piece, always choosing the next piece that offers the
most obvious and immediate benefit. So the problems
where choosing locally optimal also leads to the global
solution is the best fit for Greedy. In this algorithm, the
decisions are final, and not revised.
 Greedy BFS doesn't need an "OPEN list" and should base
its decisions only on the current node as it always
chooses the next piece that has more benefits. Hence,
greedy BFS tries to expand the node that is thought to
15

be closest to the goal, without taking into account


previously gathered knowledge.
 Evaluation function:
F(n) = h(n)

(iii) A* Algorithm:

 At each iteration of its main loop, A* needs to


determine which of its paths to extend. It does so based
on the cost of the path and an estimate of the cost
required to extend the path all the way to the goal.
Specifically, A* selects the path that minimizes
f(n)=g(n)+h(n)

 where n is the next node on the path, g(n) is the cost of


the path from the start node to n, and h(n) is a heuristic
function that estimates the cost of the cheapest path
from n to the goal. A* terminates when the path it
chooses to extend is a path from start to goal or if there
are no paths eligible to be extended. The heuristic
function is problem-specific. If the heuristic function is
admissible, meaning that it never overestimates the
actual cost to get to the goal, A* is guaranteed to return
a least-cost path from start to goal.
16

Q6: Consider any two example sentences (different from


those discussed in class) and convert them into predicate
logic form and then into clausal form.
Ans.:
Sentence 1: “If person is late, he will miss the train.”
Assume person as X, Train as T and

Person (X) : represents “X is person”

Late(X) : represents “X is late”

Miss(X,Y) : represents “X will miss Y”

Train(X) : represents “X is train”

Predicate form:

∀X ( Person(X) ∧ Late(X) ) → ∃T ( Train(T) ∧ Miss(X , T) )

Clausal Form:

∀X ∃T ¬(Person(X) ∧ Late(X) ) V ( Train(T) ) ∧ Miss(X,T) )

Or

∀X ∃T ( ¬Person(X) V ¬Late(X) ) V ( Train(T) ∧ Miss(X,T) )


17

Sentence 2: “If Steve is uncle of Kevin, then Alex and Kevin


are brothers.”
Assume S as Steve, K as Kevin, A as Alex and

Person(X) : represents “X is person”

Uncle(X,Y) : represents “X is uncle of Y”

Brothers(X,Y) : represents “X and Y are Brothers”

Predicate form:
∃S ∃K ( Person(S) ∧ Person(K) ∧ Uncle(S,K) ) → ∃A(Person(A) ∧Brothers(K , A))

Clausal form:

∃S ∃K ∃A ¬(( Person(S) ∧Person(K) ∧ Uncle(S,K) ) V (Person(A) ∧Brothers(K , A))


18

Q7: Consider any two example sentences (different from


those discussed in class) and represent them using semantic
networks.
Ans.:
Sentence 1: “Kevin is younger brother of Alex.”
Semantic Network:

Kevin Brother of Alex

Age Age

A1 A2
Less than

Sentence 2: “Steve is late for meeting.”


Semantic Network:

Late

Instance

Steve Event
Agent

For

Meeting
19

Q8: Give a detailed example of a script different from that


discussed in the class.
Ans.:
 Script: Withdrawing money from bank.
 Props: Counter, Money, Form, Token
 Roles:
o P = Person ( Customer )
o E = Employee
o C = Cashier

 Entry Conditions:
o P has no money.
o The bank is open.

 Result:
o P has more money.

Scene 1: Entering
o P enters the Bank
o P moves to Counter
o P requests to E

Scene 2: Filling Form


o P fill the Form
o P submit Form to E
o E forwards Token to C
20

Scene 3: Receiving money


o Cashier accepts the token and process the request
o Cashier give Money to P

Scene 4: Exiting the bank


o P leaves the bank

Advantages of Scripts:

o Ability to predict events.


o A single coherent interpretation maybe builds up from a
collection of observations.

Disadvantages of Scripts:

o Less general than frames.


o May not be suitable to represent all kinds of knowledge
21

Q9: Provide (i) top-down parsing and (ii) bottom-up parsing


for the following sentence.
“The breeze is cool.”

Ans.:
(i) Top-Down Parsing:
S → NP
→ NP DET
→ ART NOUN DET
→ The NOUN DET
→ The Breeze DET
→ The Breeze ART ADJ
→ The Breeze is ADJ
→ The Breeze is cool
22

(ii) Bottom-Up Parsing:


The Breeze is cool
→ ART Breeze is cool
→ ART NOUN is cool
→ ART NOUN ART cool
→ ART NOUN ART ADJ
→ NP ART ADJ
→ NP DET
→ NP
→S
23

Lisp Assignments:

Software used: GNU CLISP 2.49


Q10: Write and execute any five example Lisp programs
from those discussed in the class.

1. Program to convert temperature from Celsius to


Fahrenheit.
temperature.lisp

(defun temprature(c)

(+ (float (* (/ 9 5) c)) 32))

Output:

2. Factorial of a number.
factorial.lisp

(defun factorial(n)
(if (= n 0) 1
(* n factorial (- n 1))))
24

Output:

3. Find Sum of n number.


sum_number.lisp

(defun sum-n-no(n)(if (= n 1)
1
(+ n (sum-n-no (- n 1)))))

Output:

4. Find m raise to power n.


m_rasie_n.lisp

(defun power(m n)

(let ((res 1))

(dotimes (count n res)

(setf res (* m res )))))


25

Output:

5. Append Two list.


append_two_list.lisp

(defun myappend (L1 L2)


(cond
((not (listp L1)) (format t "cannot append, first argument is
not a list~%" L1))
((not (listp L2)) (format t "cannot append, second argument
is not a list~%" L2))
((null L1) L2)
((null L2) L1)
(t (cons (car L1) (myappend (cdr L1) L2)))))

Output:

Q11: Write and execute any five example Lisp programs


different from those discussed in the class.
26

1. Program to print multiplication table of given number.


multiplication_table.lisp

(defun table(n)

(do ((i 1 (+ i 1)))

((> i 11) 'Done)

(format t "~a X ~a = ~a~%" n i (* n i))))

Output:

2. Print Fibonacci Series upto given number.


fibonacci_series.lisp

(defun fibonacci(n)

(format t "Fibonacci Series upto ~a:" n)

(let ((a 0)(b 1))

(do ((a 0 (+ a (- b a))) (b 1 (+ a b)))

((> b n) a)

(print a))))
27

Output:

3. Tower of Hanoi.
tower_of_hanoi.lisp

; towers of hanoi, moving N disks from src to dest, with spare


available

(defun hanoi (N src dest spare)

(cond

((= N 1) (format t "Move from ~A to ~A.~%" src dest))

(t (block defaultCase
Output:
(hanoi (- N 1) src spare dest)

(hanoi 1 src dest spare)

(hanoi (- N 1) spare dest src)))))

Output:-
28

4. Find Root of a quadratic equation


root_of_quadratic.lisp

(defun roots (a b c)

(cond ((not (and (numberp a) (numberp b) (numberp c)))

(format nil "not calculated (due to error: invalid


coefficients)~%"))

((and (= a 0) (= b 0) (= c 0))

(format nil "Infinite (all values of x)~%"))

((and (= a 0) (= b 0))
Output:
(format nil "(none)~%"))

((= a 0) (/ (- c) b))

(t (let

( (b2m4ac (- (* b b) (* 4 a c))))

(cond ((= 0 b2m4ac) (/ (- b) (* 2 a)))

((> 0 b2m4ac) (format nil "complex roots~%"))

(t (list (/ (- (sqrt b2m4ac) b) (* 2 a))


Output:- (/ (- (+ (sqrt b2m4ac) b)) (* 2 a)))))))))
29

5. Find area of circle.


area_circle.lisp

(defun area(rad)

"Calculates area of a circle with given radius"

(terpri)

(format t "Radius: ~5f" rad)

(format t "~%Area: ~10f" (* 3.141592 rad rad))

Output:

=========================END=====================

You might also like