IoT Lab Manual Aktu
IoT Lab Manual Aktu
SESSION 2024-25
LAB REPORT
SEMESTER :VII
LIST OF EXPERIMENTS
2 16/09/2024
Write simple fact for the statements using
PROLOG.
3 Write predicates One converts centigrade 07/10/2024
temperatures to Fahrenheit, the other checks if a
temperature is below freezing.
% Query examples:
% ?- parent(john, mary).
% ?- parent(mary, X).
4. Detailed Explanation
I. parent(john, mary). indicates John is a parent of Mary.
II. Queries like ?- parent(mary, X). search for all values of X that satisfy the
relationship.
5. Execution Steps
I. Save the program as facts.pl.
II. Load it into Prolog: [facts].
III. Run queries interactively.
lOMoARcPSD|458 417 36
% Query examples:
% ?- to_fahrenheit(0, F).
% ?- below_freezing(-5).
4. Detailed Explanation
I. to_fahrenheit/2 computes the Fahrenheit equivalent of a given Centigrade value.
II. below_freezing/1 checks if a temperature is below 0°C.
5. Execution Steps
I. Save the program as temperature.pl.
II. Load it into Prolog and run the provided queries.
lOMoARcPSD|458 417 36
1. Objective
Solve the Monkey Banana Problem using state-space representation in Prolog.
2. Theory
The Monkey Banana Problem is a classic AI problem where a monkey needs to perform
a series of actions to obtain a banana.
3. Program
% Actions: monkey moves, climbs, or grabs banana
state(at(monkey, ground), has(banana, no)).
action(grab, state(at(monkey, top), has(banana, no)), state(at(monkey, top), has(banana,
yes))).
1. Objective
Create a knowledge base for diagnosing diseases based on symptoms.
2. Theory
Turbo Prolog allows creating decision-making systems by defining symptoms and
diagnoses as rules and facts.
3. Program
% Facts
symptom(john, fever).
symptom(john, cough).
% Query example:
% ?- diagnosis(john, flu).
4. Detailed Explanation
I. Each symptom/2 fact associates a person with a symptom.
II. diagnosis/2 uses logical conjunction to infer a disease.
5. Execution Steps
I. Save the program as diagnosis.pl.
II. Run it in Turbo Prolog to diagnose based on input symptoms.
lOMoARcPSD|458 417 36
1. Objective
Implement programs to calculate factorial and Fibonacci series values.
2. Theory
Recursive definitions in Prolog simplify computation for mathematical sequences.
3. Factorial Program
factorial(0, 1).
factorial(N, F) :- N > 0, N1 is N - 1, factorial(N1, F1), F is N * F1.
% Query example:
% ?- factorial(5, F).
3. Fibonacci Program
fibonacci(0, 0).
fibonacci(1, 1).
fibonacci(N, F) :- N > 1, N1 is N - 1, N2 is N - 2, fibonacci(N1, F1), fibonacci(N2, F2), F
is F1 + F2.
% Query example:
% ?- fibonacci(6, F).
4. Detailed Explanation
I. Factorial uses recursive multiplication.
II. Fibonacci sums the previous two values in the series.
5. Execution Steps
I. Save the programs as factorial.pl and fibonacci.pl
II. Load and query them in Prolog.
lOMoARcPSD|458 417 36
1. Objective
Solve the 4-Queen problem using backtracking in Prolog.
2. Theory
The N-Queen problem involves placing N queens on an N×N chessboard such that no
two queens threaten each other.
3. Program
nqueen(N, Qs) :- length(Qs, N), place_queens(Qs), safe_queens(Qs).
place_queens([]).
place_queens([Q|Qs]) :- place_queens(Qs), member(Q, [1,2,3,4]).
safe_queens([]).
safe_queens([Q|Qs]) :- safe(Q, Qs, 1), safe_queens(Qs).
% Query example:
% ?- nqueen(4, Qs).
4. Detailed Explanation
I. place_queens/1 generates permutations for queen positions.
II. safe_queens/1 ensures no two queens threaten each other.
5. Execution Steps
I. Save the program as nqueen.pl.
II. Query it with different values for N.
lOMoARcPSD|458 417 36
1. Objective
Solve the Traveling Salesman Problem using Prolog.
2. Theory
The TSP aims to find the shortest possible route visiting each city exactly once and
returning to the origin.
3. Program
tsp(Start, Path, Cost) :- findall([P, C], path(Start, P, C), AllPaths), shortest(AllPaths, Path,
Cost).
% Query example:
% ?- tsp(a, Path, Cost).
4. Detailed Explanation
I. path/3 generates all possible routes.
II. shortest/3 finds the route with the minimum cost.
5. Execution Steps
I. Save the program as tsp.pl.
II. Load and execute queries for various starting cities.
lOMoARcPSD|458 417 36
% Query example:
% (water-jug 0 0 2)
4. Detailed Explanation
I. Each recursive call represents a state transition.
II. Termination occurs when the goal measurement is achieved.
5. Execution Steps
III. Save the program as water_jug.lisp.
I. Load and execute it in a LISP interpreter.
lOMoARcPSD|458 417 36
1. Objective
Implement best-first search for graph traversal.
2. Theory
Best-First Search uses heuristics to explore the most promising paths in a graph.
3. Program
(defun best-first-search (start goal graph)
(if (equal start goal)
(list start)
(let ((neighbors (cdr (assoc start graph))))
(append (list start)
(best-first-search (car neighbors) goal graph)))))
% Example Graph:
% (best-first-search 'a 'd '((a b c) (b d) (c e))).
4. Detailed Explanation
I. The function evaluates neighbors based on a heuristic.
II. The path with the least estimated cost is explored first.
5. Execution Steps
I. Save the program as best_first_search.lisp.
II. Load and execute it in a LISP interpreter.