Pavan Kumar V

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

ACHARYA INSTITUTE OF TECHNOLOGY

Acharya Dr.Sarvepalli Radhakrishnan Road, Soladevanahalli,


Achit Nagar (Post), Bangalore – 560107

DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND MACHINE LEARNING


LAB ASSIGNMENT
Alive
Semester &
Sub with Code AI LAB-BAD402 4/B assignment 01
Sec:
No:
AY: 2023-2024 Semester: EVEN

Experiment No:6

What is the Traveling Salesman Problem (TSP)?

Define the following terms: graph, vertex, edge, path, cycle, and weighted graph.

What is the difference between a directed and an undirected graph?

List some real-world applications of the TSP.

What are the pros and cons of using heuristics for solving the TSP?

Write a Python function to represent a graph using an adjacency matrix.

Write a Python function to calculate the distance of a given tour in a TSP.

Experiment No: 07
Implementation of problem-solving strategies: either using Forward Chaining or Backward Chaining

What is the difference between forward chaining and backward chaining?

In what scenarios would you prefer to use forward chaining over backward chaining, and vice versa?

Given the propositions: "If it rains, the ground will be wet" and "It is raining", use Modus Ponens to
infer a conclusion.

Given the predicates: "All humans are mortal" and "Socrates is a human", use universal instantiation
to draw a conclusion.’

Write a Python function to represent a set of facts and rules.

Describe common inference rules such as Modus Ponens, Modus Tollens, and the Law of Syllogism.

Department of Artificial Intelligence & Machine Learning


Traveling Salesman Problem (TSP)

The Traveling Salesman Problem (TSP) is a classic optimization problem in which a salesman is
required to visit a set of cities exactly once and return to the starting city, with the objective of
minimizing the total travel distance. The TSP is NP-hard, meaning that there is no known efficient
solution to solve all instances of the problem in polynomial time.

Definitions

Graph

A graph is a mathematical structure used to model pairwise relationships between objects. It consists
of a set of vertices (nodes) and a set of edges (connections) that connect pairs of vertices.

Vertex

A vertex (or node) is a fundamental part of a graph representing an entity or location.

Edge

An edge is a connection between two vertices in a graph. It can be directed or undirected.

Path

A path in a graph is a sequence of vertices connected by edges, where each vertex is visited at most
once.

Cycle

A cycle is a path in a graph that starts and ends at the same vertex, with all other vertices visited
exactly once.

Department of Artificial Intelligence & Machine Learning


Weighted Graph

A weighted graph is a graph in which each edge has an associated numerical value (weight), often
representing distance, cost, or time.

Directed vs. Undirected Graph

Directed Graph

In a directed graph, edges have a direction, meaning they go from one vertex to another. The order of
vertices in an edge matters (e.g., an edge from vertex A to vertex B is different from an edge from
vertex B to vertex A).

Undirected Graph

In an undirected graph, edges do not have a direction. The connection between two vertices is
bidirectional (e.g., an edge between vertex A and vertex B is the same as an edge between vertex B
and vertex A).

Real-World Applications of the TSP

1. Logistics and Transportation**: Optimizing delivery routes for goods and services.

2. Manufacturing**: Sequencing the production of parts to minimize setup time.

3. Circuit Design**: Minimizing the length of wire required to connect components on a circuit
board.

4. DNA Sequencing**: Reconstructing the shortest possible sequence that contains a set of given
subsequences.

5. Tour Planning**: Designing optimal travel itineraries for tourists.

Pros and Cons of Using Heuristics for Solving the TSP

Department of Artificial Intelligence & Machine Learning


Pros

Speed: Heuristics can provide good solutions quickly, making them suitable for large instances
where exact methods are impractical.

- Simplicity: Heuristics are often easier to implement and understand compared to exact algorithms.

- Flexibility: Heuristics can be adapted to specific problem instances and constraints.

Cons

Optimality: Heuristics do not guarantee finding the optimal solution, only a good approximation.

Performance Variability: The quality of the solution provided by heuristics can vary depending on
the problem instance.

-Parameter Tuning: Many heuristics require careful tuning of parameters to perform well, which can
be time-consuming.

Python Function to Represent a Graph Using an Adjacency Matrix

def create_adjacency_matrix(num_vertices, edges, directed=False):

# Initialize an n x n matrix with zeros

matrix = [[0] * num_vertices for _ in range(num_vertices)]

for edge in edges:

u, v, weight = edge

matrix[u][v] = weight

if not directed:

matrix[v][u] = weight

Department of Artificial Intelligence & Machine Learning


return matrix

num_vertices = 4

edges = [(0, 1, 10), (1, 2, 20), (2, 3, 30), (3, 0, 40)]

adj_matrix = create_adjacency_matrix(num_vertices, edges)

print(adj_matrix)

## Python Function to Calculate the Distance of a Given Tour in a TSP

```python

def calculate_tour_distance(tour, adjacency_matrix):

total_distance = 0

num_vertices = len(tour)

for i in range(num_vertices):

u = tour[i]

v = tour[(i + 1) % num_vertices] # Wrap around to the start

total_distance += adjacency_matrix[u][v]

return total_distance

tour = [0, 1, 2, 3]

distance = calculate_tour_distance(tour, adj_matrix)

print(distance)

Department of Artificial Intelligence & Machine Learning


Department of Artificial Intelligence & Machine Learning

You might also like