AI Lab - Garima
AI Lab - Garima
Garima Paudel
Lab 1: To get familiar with SWI-Prolog and setting up the environment for implementing
algorithms.
Introduction:
SWI-Prolog is a free and open-source Prolog programming language implementation. Prolog is a
declarative programming language often used for symbolic reasoning and artificial intelligence
applications.
a. Downloaded SWI-Prolog:
● First of all, we visited the official SWI-Prolog website
(https://www.swi-prolog.org/Download.html).
SWI-Prolog IDE:
Garima Paudel
Hello World Program:
?- consult('your_file.pl').
Garima Paudel
Lab 2: Study of PROLOG Programming language and its Functions. Write simple facts
for the statements using PROLOG.
Introduction
Prolog, which stands for "Programming in Logic," is a high-level programming language that is
associated with artificial intelligence and computational linguistics. Unlike imperative
languages such as C or Java, which specify how to perform tasks, Prolog is declarative,
focusing on what relationships and facts hold. It is particularly well-suited for tasks that involve
complex pattern matching, symbolic reasoning, and rule-based logic, such as natural language
processing, database management, and theorem proving. Prolog programs consist of a series of
rules and facts, and execution involves querying these rules to infer new information or solve
problems by logical deduction. Introduced in the 1970s by Alain Colmerauer and Robert
Kowalski, Prolog has since played a pivotal role in the development of logic programming and
remains a valuable tool in AI research and applications.
A Prolog program consists of facts, rules, and queries. The program's logic is definedthrough
the relationships between these elements.
Facts
Facts are statements about the world that Prolog understands to be true.
% Example facts
likes(john, pizza).
likes(mary, sushi).
Rules
Rules define relationships between facts. They consist of a head and a body.
Garima Paudel
% Example rule
Queries
% Example query
?- friend(john, mary).
Garima Paudel
Variables and Constants
% Example variable
likes(john, X).
% Example constant
likes(john, pizza).
History of Prolog
The heritage of prolog includes the research on theorem provers and some other automated
deduction system that were developed in 1960s and 1970s. The Inference mechanism of the
Prolog is based on Robinson’s Resolution Principle, that was proposed in 1965, and Answer
extracting mechanism by Green (1968). These ideas came together forcefully with the advent
of linear resolution procedures.
The explicit goal-directed linear resolution procedures, gave impetus to the development of a
general purpose logic programming system. The first Prolog was the Marseille Prolog based
on the work by Colmerauer in the year 1970. The manual of this Marseille Prolog interpreter
(Roussel, 1975) was the first detailed description of the Prolog language.
Garima Paudel
Project, that was announced in 1981, adopted Prolog as a development language, and thereby
grabbed considerable attention on the language and its capabilities.
Applications of Prolog:
1. Natural Language Processing (NLP): Parsing and generating language constructs, syntactic
and semantic analysis.
3. Database Queries: Representing and executing complex data retrieval and manipulation.
4. Artificial Intelligence (AI) and Machine Learning (ML): Data set manipulation, decision
making, pattern recognition.
Task
● John is male.
● Jim is male.
● Tony is male.
● Eliza is female.
● Ann is female.
Garima Paudel
● Lisa is female.
Solution:
male(john).
male(jim).
male(tony).
female(eliza).
female(ann).
female(lisa).
parent(john, jim).
parent(john, ann).
parent(eliza, jim).
parent(eliza, ann).
parent(ann, peter).
Garima Paudel
parent(ann, lisa).
parent(jim, tony).
Garima Paudel
Lab 3: Write a program in PROLOG to implement a list and simple arithmetic.
Task A: List Operation (Finding length of list, sum of list, maximum value in list and
reversed list):
Solution:
% Reversing a list
reverse(List, Reversed) :- reverseAcc(List, [], Reversed).
reverseAcc([], Acc, Acc).
reverseAcc([Head|Tail], Acc, Reversed) :- reverseAcc(Tail, [Head|Acc], Reversed).
Garima Paudel
Output:
?- add(5, 3, Result).
?- subtract(10, 4, Result).
?- multiply(6, 2, Result).
?- divide(8, 2, Result).
Solution:
Garima Paudel
% Addition
add(X, Y, Result) :- Result is X + Y.
% Subtraction
subtract(X, Y, Result) :- Result is X - Y.
% Multiplication
multiply(X, Y, Result) :- Result is X * Y.
% Division
divide(X, Y, Result) :- Result is X / Y.
Output:
Task C:
Given the following statements in the prolog:
% Products
product(apple, 50).
product(banana, 30).
product(grape, 25).
Garima Paudel
product(orange, 40).
% Suppliers
supplier(citrusSupplier, [orange]).
% Prices
price(apple, 1.5).
price(banana, 0.8).
price(grape, 2.2).
price(orange, 1.0).
% Discounts
discount(apple, 0.2).
discount(banana, 0.1).
discount(grape, 0.3).
discount(orange, 0.15).
?- suppliedBy(fruitSupplier, ProductList).
=>
suppliedBy(Supplier, ProductList) :-
supplier(Supplier, Products),
Garima Paudel
% Rule to calculate total cost for a specific product based on quantity and discounts
product(Product, Price),
discount(Product, Discount),
Garima Paudel
Lab 4: Write a program in PROLOG to implement Depth First Search.
Theory:
• This algorithm traverses all nodes by advancing along a specific path and uses
backtracking when it reaches the end of that path, allowing exploration of
alternative paths.
Solution:
Garima Paudel
% Depth-first search
dfs(Node, Visited) :- dfs(Node, [], Visited).
dfs_all([],_,_).
dfs_all([H|T], Path, Visited) :- dfs(H, Path, Visited), dfs_all(T, Path, Visited).
Output:
Garima Paudel
Lab 5: Write a program in PROLOG to implement Breadth First Search.
Theory:
• The Breadth First Search (BFS) algorithm is used to search a graph data
structure for a node that meets a set of criteria.
• It starts at the root of the graph and visits all nodes at the current depth level
before moving on to the nodes at the next depth level.
• This is achieved very simply by using a FIFO queue, i.e., new successors go at end
• BFS uses a Queue to remember to get the next node to start a search when a
dead end occurs in any iteration
Solution:
Garima Paudel
edge(b, d).
edge(c, e).
edge(c, f).
edge(d, g).
edge(e, h).
% Breadth-first search
bfs(Start, Visited) :- bfs([Start], [], Visited).
bfs([], _, _).
bfs([Current|Rest], Path, Visited) :- \+ member(Current, Path), write(Current), nl,
bfs_neighbours(Current, [Current|Path], _, RestQueue), append(Rest, RestQueue,
NewQueue), bfs(NewQueue, [Current|Path], Visited).
Output:
Garima Paudel
Garima Paudel
Lab 6: Write a program in PROLOG to implement A* Search.
• Evaluation function:
• Where,
Algorithm
3.2. Otherwise, mark the node as visited and insert its neighbors into the priority
queue.
The priority of each node will be the sum of its cost from the start and the goal.
Garima Paudel
Solution:
% Goal state
goal(h).
Garima Paudel
% Node representation
% node(Current, F, G, Path)
% F: Total cost (G + H)
% G: Cost from the start node
% Path: Path from the start node to the current node
node(a, 0, 0, []).
node(b, 0, 0, []).
node(c, 0, 0, []).
node(d, 0, 0, []).
node(e, 0, 0, []).
node(f, 0, 0, []).
node(g, 0, 0, []).
node(h, 0, 0, []).
% A* search
a_star(Start, Path, Cost) :-
a_star([node(Start, 0, 0, [])], [], Path, Cost).
a_star([node(Current, _, G, Path)|_], _, Path, G) :-
goal(Current).
a_star([node(Current, _, G, Path)|Open], Closed, FinalPath, FinalCost)
:-
findall(node(Next, F, G1, [Current|Path]),
(edge(Current, Next, Cost), \+ member(Next, Closed),
G1 is G + Cost, heuristic(Next, H), F is G1 + H),
Successors),
append(Successors, Open, UpdatedOpen),
sort(UpdatedOpen, SortedOpen),
a_star(SortedOpen, [Current|Closed], FinalPath, FinalCost).
% Heuristic function (straight-line distance)
heuristic(Node, H) :- distance(Node, h, H).
Output:
Garima Paudel
Garima Paudel
Garima Paudel