0% found this document useful (0 votes)
67 views26 pages

AI Lab Manual

Uploaded by

preranam464
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)
67 views26 pages

AI Lab Manual

Uploaded by

preranam464
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/ 26

Shetkari Shikshan Prasarak Mandal’s

Jaywant College of Engineering and Polytechnic


(Approved by AICTE, New Delhi, DTE, Govt. of Maharashtra, Affiliated to BATU, Lonere & MSBTE Mumbai)

Lab Manual
Artificial Intelligence
(BTCO701)

Department of Computer Engineering

B.Tech– VII Sem


(2024-25)

Subject Incharge HOD


Ms. Girisha. S. Shinde
LIST OF EXPERIMENTS/ PROGRAMS

Artificial Intelligence Lab ( BTCOC701) Semester : VII

Sr.NO PROGRAM

1 Study of PROLOG.

2 Write a program to solve 8 queens problem

3 Solve any problem using depth first search.

4 Solve any problem using best first search.

5 Solve 8-puzzle problem using best first search

6 Solve Robot (traversal) problem using means End Analysis

7 Solve traveling salesman problem.


Experiment no 1

Title: Study of PROLOG

Objective:

The objective of this experiment is to explore the fundamental concepts and capabilities of
Prolog, a declarative programming language often used for symbolic reasoning and knowledge
representation. The experiment aims to understand Prolog's syntax, its ability to express
logical relationships, and its suitability for solving logic-based problems.

Theory:

Prolog is a logic programming language designed for expressing and solving problems using
formal logic. It operates on the principle of defining facts and rules as predicates and then
using logical queries to infer new information. Prolog uses a "backward chaining" approach,
where it starts with a query and recursively searches for facts and rules that lead to a valid
solution.

The core components of Prolog include:

1. Facts: Statements that are true by definition.

2. Rules: Logical relationships and conditions that define how facts are connected.

3. Queries: Logical questions posed to Prolog to find solutions.

4. Variables: Symbols that can take on various values during inference.

5. Unification:The process by which Prolog matches variables with values.

Prolog is commonly used in various fields, including artificial intelligence, natural language
processing, and expert systems, due to its ability to represent and reason about complex
knowledge and logical relationships.

1. Environment Setup:

- Use a standard Prolog interpreter, such as SWI-Prolog, for the experiment.

2. Basic Prolog Syntax:

- Introduce basic Prolog syntax, including facts, rules, queries, and variables.

- Create and execute simple Prolog programs to illustrate these concepts.


3. Logic-Based Problems:

- Solve a series of logic-based problems using Prolog, including puzzles and logical
deductions.

- Document the process of defining facts, rules, and queries for each problem.

4. Complexity and Scalability:

- Gradually increase the complexity of the logic-based problems.

- Analyze how Prolog handles problems with increased complexity.

5. Evaluation and Conclusion:

- Summarize the experiment's findings regarding Prolog's suitability for expressing and
solving logic-based problems.

- Reflect on the strengths and limitations of Prolog as a tool for symbolic reasoning and
knowledge representation.

Conclusion:

This experiment provides an introductory exploration of Prolog as a declarative programming


language for solving logic-based problems. Prolog's ability to represent facts, rules, and
queries in a logical form offers a unique approach to problem-solving and knowledge
representation. It is particularly well-suited for domains that require symbolic reasoning and
logical inference.
Experiment no. 02
Title: Understanding and Solving the 8-Queens Problem in Prolog

Objective:
The primary objective of this experiment is to delve into the 8-Queens problem, a classic puzzle
in the realm of chessboard puzzles and combinatorial optimization. The experiment aims to
understand the problem's significance, formulate constraints in Prolog, and develop a Prolog
program to find solutions, thereby showcasing the language's suitability for solving complex
combinatorial problems.

Theory:
The 8-Queens Problem Significance:
The 8-Queens problem is a well-known member of the N-Queens family of puzzles. It holds
enduring significance due to its inherent complexity and its association with various fields:

Combinatorial Optimization: The 8-Queens problem is a quintessential example of a constraint


satisfaction problem, challenging participants to find arrangements that satisfy all constraints
while exploring a vast solution space.

Educational Tool: The puzzle serves as an excellent educational tool for teaching problem-
solving, algorithm design, and computer science concepts such as backtracking and recursion. It
helps students develop skills in logical reasoning and programming.

Artificial Intelligence: The 8-Queens problem is a benchmark problem for testing search
algorithms, including constraint propagation and constraint satisfaction algorithms. It is used to
evaluate the efficiency of various AI techniques.

Constraint Programming: In the domain of constraint programming, the 8-Queens problem


serves as a fundamental example, illustrating how constraints can be formulated and solved
efficiently.

Prolog's Role:
Prolog, as a logic programming language, is particularly suited for solving problems that involve
logical constraints and symbolic reasoning. Its distinctive features include:

Declarative Syntax: Prolog allows the user to express the problem's logical constraints
declaratively, providing a high-level representation of the problem.

Backtracking: Prolog's search mechanism utilizes backtracking, which is essential for exploring
solution spaces systematically, making it well-suited for constraint
satisfaction problems like the 8-Queens puzzle.

Unification: Prolog's unification mechanism simplifies the process of matching variables and
values, helping to model constraints effectively.

Defining Logical Constraints:

Develop Prolog predicates to define and enforce the constraints of the 8-Queens problem,
ensuring that no two queens threaten each other based on their positions.
Search for Solutions:
Implement a Prolog program that utilizes the defined constraints to systematically search for
valid solutions to the 8-Queens problem using backtracking.
Generating and Visualizing Solutions:
Execute the Prolog program to generate and visualize solutions to the 8-Queens
problem on the chessboard.
Performance Evaluation:
Assess the performance of the Prolog program in terms of execution time and memory usage,
highlighting the efficiency of Prolog in solving constraint satisfaction problems.

Conclusion:
The 8-Queens problem is a captivating puzzle with significant implications in the domain
of constraint satisfaction and combinatorial optimization. Through this experiment, we have
successfully demonstrated how Prolog, a logic programming language, excels at addressing
complex problems with logical constraints. The Prolog program efficiently finds solutions to
the 8-Queens problem, showcasing Prolog's prowess in representing constraints and
conducting depth-first search for solutions.
Problem : 8 queen problem using prolog.
Output :
Experiment No 3

Title: Solving Graph Problems Using Depth-First Search in Prolog

Objective:
The objective of this experiment is to explore the application of the Depth-First Search (DFS)
algorithm in addressing graph-related problems within the context of the Prolog programming
language. By focusing on a specific problem – finding a path between two nodes in a graph –
the experiment aims to not only showcase the practical implementation of DFS in Prolog but
also to delve into the theoretical underpinnings that make Prolog a powerful language for
expressing and solving such problems.

Theory:
Graphs as Logical Structures:
Graphs, with their nodes and edges, naturally lend themselves to representation in Prolog.
Prolog's ability to define relationships through facts and rules makes it an apt choice for
modeling graph structures. In this experiment, the edge/2 predicate encapsulates the
connectivity between nodes, offering a clear and intuitive representation of the graph.

prolog code :
% Define a simple graph represented by edges edge(a,
b).
edge(b, c).
edge(c, d).
edge(d, e).
edge(e, f).
edge(f, g).

Depth-First Search Algorithm in Prolog:


DFS, a classic graph traversal algorithm, is characterized by its depth-first exploration strategy.
The Prolog implementation leverages the language's recursive nature to emulate the
algorithm's behavior effectively. The dfs/3 predicate acts as the entry point, initializing the DFS
process, and the recursive helper predicate dfs_helper/4 embodies the traversal logic, capturing
the exploration of nodes and backtracking.

prolog code:

% DFS implementation to find a path between two nodes dfs(Start,


Goal, Path) :-
dfs_helper(Start, Goal, [Start], Path).
dfs_helper(Node, Node, Path, Path).
dfs_helper(CurrentNode, Goal, Visited, Path) :-
edge(CurrentNode, NextNode),
\+ member(NextNode, Visited),
dfs_helper(NextNode, Goal, [NextNode | Visited], Path).

Prolog's Declarative Nature:


Prolog's declarative paradigm allows problem-solving through the specification of relationships
and constraints rather than explicit procedural instructions. This makes Prolog particularly well-
suited for expressing graph-related problems. The DFS algorithm, expressed recursively,
aligns seamlessly with Prolog's logical and declarative nature, allowing for an elegant and
concise representation of the search process.

Conclusion:
In conclusion, this experiment not only provides a practical demonstration of the DFS algorithm
in Prolog but also emphasizes the theoretical alignment between Prolog's declarative nature and
the logical representation of graph structures. The integration of Prolog and DFS showcases the
expressive power of the language in solving complex problems, opening avenues for further
exploration of graph algorithms and logical programming paradigms.
Solve any problem using DFS
1) DFS implementation to find a path between two nodes
Output :
Experiment No 4
Title: Solving Problems Using Best-First Search in Prolog

Objective:
The objective of this experiment is to showcase the implementation of the Best-First Search
algorithm in Prolog for solving problems. Specifically, the experiment aims to demonstrate the
algorithm's effectiveness in finding an optimal solution path by utilizing heuristics. Through
a practical implementation, the experiment seeks to illustrate how Prolog's logical and
declarative nature can be leveraged for heuristic- guided problem-solving.

Theory:
Best-First Search (BFS) is a heuristic search algorithm widely utilized for navigating large
and complex search spaces. Originating from artificial intelligence and graph theory, BFS
employs a priority queue to guide the search systematically, prioritizing states with the most
promising heuristic values. In Prolog, a logical and declarative programming language, the
implementation of BFS is particularly expressive due to its inherent recursive structure and
support for dynamic predicates.

The algorithm revolves around the concept of heuristics, which are functions that estimate the
desirability or cost of reaching a goal state from a given state. In this experiment, the heuristic/2
predicate encapsulates this estimation function. Heuristic values are crucial for BFS as they
guide the exploration towards states that are perceived to be closer to the optimal solution,
providing a heuristic-driven strategy for intelligent decision-making.

Dynamic predicates such as visited/1 play a vital role in BFS implementations. They help
keep track of explored states, preventing revisitation and ensuring that the algorithm does not
waste computational resources on redundant paths. The avoidance of revisiting states
contributes significantly to the efficiency of the search, especially in scenarios with intricate
state spaces.

The BFS process begins with the best_first_search/3 predicate, serving as the entry point
by initiating the search from the start state towards the goal state. The recursive exploration of
possible state transitions is handled by the best_first_search_helper/3 predicate. As Prolog is
inherently recursive, this structure aligns seamlessly with the nature of BFS, allowing for a
systematic exploration of state space.

The priority queue used in BFS is maintained through the sorting mechanism implemented in
compare_nodes/3. This predicate ensures that the priority queue remains in ascending order
based on heuristic values. The sorted queue guides the
algorithm to consistently select the most promising state for exploration, contributing to the
algorithm's efficiency in finding optimal solutions.

BFS in Prolog takes advantage of the language's declarative paradigm, allowing for a clear
and concise representation of complex search strategies. The integration of heuristics within
Prolog's logical framework facilitates the seamless expression of intelligent decision-making in
problem-solving scenarios.

Conclusion:
In conclusion, this experiment showcases the successful integration of the Best-First Search
algorithm with Prolog's logical and declarative capabilities. The experiment highlights how
Prolog's dynamic predicates and recursive structure contribute to the efficient implementation
of heuristic-guided algorithms.
Solve any problem using BFS
Output :
Experiment No 5
Title: Solving the 8 Puzzle Problem Using Best-First Search in Prolog

Objective:
This experiment aims to showcase the application of the Best-First Search algorithm in Prolog
to solve the classic 8 Puzzle Problem. The primary objective is to demonstrate the
effectiveness of Best-First Search, guided by heuristics, in finding an optimal solution path
from an initial state to a goal state. The experiment leverages Prolog's logical and declarative
nature to implement a heuristic-driven search for solving complex puzzle scenarios.

Theory:
The 8 Puzzle Problem, a well-known example in the field of artificial intelligence and puzzle-
solving, involves a 3x3 grid with eight numbered tiles and one empty space. The challenge is to
rearrange the tiles from an arbitrary initial configuration to a predefined goal state through a
series of permissible moves.

In the context of this experiment, the states of the puzzle are represented as lists in Prolog. The
heuristic function, embodied in the heuristic/2 predicate, quantifies the desirability of a state by
calculating the Manhattan distance between the current state and the goal state. The Manhattan
distance is the sum of the horizontal and vertical distances between each tile's current and
target positions, providing an effective heuristic for guiding the search.

The Best-First Search algorithm, implemented through the best_first_search/3 and


best_first_search_helper/3 predicates, systematically explores the puzzle's state space. Dynamic
predicates like visited/1 assist in tracking previously explored configurations, preventing
redundant searches and enhancing the efficiency of the algorithm.

The move/2 predicate defines the possible moves in the puzzle, allowing the algorithm to
generate successor states. The neighbor/2 predicate determines the possible positions to
which the empty tile can be moved, ensuring that the generated states are valid.

The sorting mechanism in the compare_nodes/3 predicate maintains a priority queue, ordering
states based on their heuristic values. This ensures that the algorithm explores states with lower
heuristic values first, aiming for an optimal solution.

The experiment encapsulates the 8 Puzzle Problem within Prolog's logical and declarative
framework, providing a concise and expressive representation of the heuristic-driven Best-First
Search algorithm. The resulting solution path illustrates the
sequence of moves leading from the initial state to the goal state, offering valuable insights into
efficient problem-solving strategies.

Conclusion:
In conclusion, the experiment showcases the successful integration of Best-First Search with
Prolog's logical and declarative features to solve the 8 Puzzle Problem. The theoretical
foundation explores the intricacies of the puzzle, detailing the heuristic considerations and the
algorithmic approach.
Solving the 8 Puzzle Problem Using Best-First Search in Prolog
Output :
Experiment No. 6
Title : Solve Robot (traversal) problem using means End Analysis

Means-Ends Analysis (MEA) is a technique used in Artificial Intelligence for controlling search
in problem
solving computer programs. It is also a technique used at least since the 1950s as a creativity
tool, most
frequently mentioned in engineering books on design methods. Means-Ends Analysis is also a
way to clarify one's thoughts when embarking on a mathematical proof.
Problem-solving as search An important aspect of intelligent behavior as studied in AI is goal-
based problem solving, a framework in which the solution of a problem can be described by
finding a sequence of actions that lead to a desirable goal. A goal-seeking system is supposed to
be connected to its outside environment by sensory channels through
which it receives information about the environment and motor channels through which it
acts on the environment. (The term "afferent" is used to describe "inward" sensory flows, and
"efferent" is used to describe "outward" motor commands.) In addition, the system has some
means of storing in a memory information about the state of the environment (afferent
information) and information about actions (efferent information). Ability
to attain goals depends on building up associations, simple or complex, between particular
changes in states and particular actions that will bring these changes about. Search is the process
of discovery and assembly of sequences of actions that will lead from a given state to a desired
state. While this strategy may be appropriate
for machine learning and problem solving, it is not always suggested for humans (e.g.
cognitive load theory and its implications).

How MEA works The MEA technique is a strategy to control search in problem-solving. Given
a current state and a goal state, an action is chosen which will reduce the difference
between the two. The action is performed on the current state to produce a new state, and the
process is recursively applied to this new state and the goal state.
Note that, in
order for MEA to be effective, the goal-seeking system must have a means of associating to
any kind of detectable difference those actions that are relevant to reducing that difference. It
must also have means for detecting the progress it is making (the changes
in the differences between the actual and the desired state), as some attempted sequences of
actions may fail and, hence, some alternate sequences may be tried. When knowledge is
available concerning the importance of differences, the most important difference is selected
first to further improve the average performance of MEA over other brute-force search
strategies. However, even without the ordering of differences according to importance, MEA
improves over other search heuristics (again in the average case) by focusing the problem
solving on the actual differences between the current state and that
of the goal. Some AI systems using MEA The MEA technique as a problem-solving
strategy was first introduced in 1963 by Allen Newell and Herbert Simon in their computer
problem-solving program General Problem Solver (GPS). In that implementation, the
correspondence between differences and actions, also called operators, is provided a priori as
knowledge in the system. (In GPS this knowledge was in the form of table of connections.)
When the action and side-effects of applying an operator are penetrable, the search may select
the relevant operators by inspection of the operators and do without a table of connections.
This latter case, of which the canonical example is STRIPS, an automated planning computer
program, allows task- independent correlation of differences to the operators
which reduce them.
Experiment No 7
Title: Solving the Travelling Salesman Problem in Prolog

Objective:
The objective of this experiment is to solve the classic Travelling Salesman Problem (TSP).
The primary goal is to find an optimal tour that visits each city exactly once and returns to the
starting city, minimizing the total travel distance.

Theory:
The Travelling Salesman Problem is a well-known combinatorial optimization problem where a
salesman must visit a set of cities and return to the starting city, minimizing the total distance
travelled. Best-First Search is a heuristic-driven algorithm that can effectively navigate the
complex solution space of the TSP.

In Prolog, cities can be represented as nodes, and the distances between cities can be stored in a
distance matrix. The heuristic function guides the search by estimating the remaining cost to
reach the goal state, where all cities are visited. A common heuristic is the Minimum Spanning
Tree (MST) heuristic, which estimates the cost based on the minimum spanning tree of the
remaining unvisited cities.
The best_first_search_tsp/3 predicate initiates the search, exploring possible tours using the
MST heuristic to guide the selection of promising routes. The evaluate_tour/3 predicate
calculates the cost of a tour based on the distance matrix. The search continues until all
cities are visited, and the tour with the minimum cost is identified.

The Travelling Salesman Problem (TSP) is a classic optimization problem that holds significant
importance in various fields, including logistics, transportation, and network optimization. In
this extended theory section, we'll delve deeper into the challenges posed by the TSP and
the rationale behind the heuristic-driven approach using Best- First Search in Prolog.
The TSP involves finding the shortest possible tour that visits each city exactly once and returns
to the starting city. With n cities, the total number of possible tours is
n! (n factorial), making an exhaustive search computationally infeasible for a large number of
cities. This combinatorial explosion necessitates the use of heuristic algorithms that guide the
search towards promising solutions while avoiding the exhaustive exploration of the entire
solution space.
One common heuristic for the TSP is the Minimum Spanning Tree (MST) heuristic. The MST
heuristic estimates the lower bound on the total distance of a tour by calculating the
minimum spanning tree of the remaining unvisited cities. The intuition behind this heuristic is
that the minimum spanning tree connects all cities with the minimum possible total edge cost,
representing a lower limit on the tour distance.
The Best-First Search algorithm is a natural fit for TSP due to its ability to systematically
explore the solution space guided by heuristics. In this Prolog implementation, the algorithm
starts with the initial city and progressively builds a tour by selecting the next city based on the
MST heuristic. The search continues until all cities are visited, and the algorithm maintains the
best tour found so far.

Conclusion:
In This experiment successfully applies the Best-First Search algorithm in Prolog to solve
the Travelling Salesman Problem. The heuristic-driven approach, guided by the MST
heuristic, allows for efficient exploration of the solution space. The experiment highlights the
versatility of Prolog in representing and solving complex optimization problems and provides
insights into the application of heuristic-driven algorithms for route optimization.
Experiment No 7
Travelling salesman problem
Output :

You might also like