0% found this document useful (0 votes)
36 views

Assignment - 3 Solution

The document provides information about constraint satisfaction problems and algorithms to solve them. It defines constraint satisfaction as assigning values to variables while satisfying constraints. It describes hill climbing and best first search algorithms. Hill climbing continuously improves the current solution until a peak is reached, while best first search uses a priority queue and heuristic to search the problem space.

Uploaded by

Ritesh Sutrave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Assignment - 3 Solution

The document provides information about constraint satisfaction problems and algorithms to solve them. It defines constraint satisfaction as assigning values to variables while satisfying constraints. It describes hill climbing and best first search algorithms. Hill climbing continuously improves the current solution until a peak is reached, while best first search uses a priority queue and heuristic to search the problem space.

Uploaded by

Ritesh Sutrave
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Solution of Assignment No.

1) Describe Constraint satisfaction along with an example.

ANS: Constraint satisfaction is a technique where a problem is solved when its values satisfy
certain constraints or rules of the problem. Such type of technique leads to a deeper
understanding of the problem structure as well as its complexity.
Constraint satisfaction depends on three components:

 X: It is a set of variables.
 D: It is a set of domains where the variables reside. There is a specific domain for
each variable.
 C: It is a set of constraints which are followed by the set of variables.

A finite set of variables which stores the solution (V = {V1, V2, V3,....., Vn})

A set of discrete values known as domain from which the solution is picked (D = {D1, D2,
D3,.....,Dn})

A finite set of constraints (C = {C1, C2, C3,......, Cn})

In maths or other the elements in the domain can be both continuous and discrete but in AI,
we generally only deal with discrete values.

Here all these sets should be finite except for the domain set. Each variable in the variable set
can have different domains.

The following problems can be solved using CSP:

1. Crypt Arithmetic (Coding alphabets to numbers.)

2. n-Queen (In an n-queen problem, n queens should be placed in an nXn matrix such that no
queen shares the same row, column or diagonal.)

3. Map Colouring (colouring different regions of map, ensuring no adjacent regions have the
same color)

4. Crossword (everyday puzzles appearing in newspapers)

5. Sudoku (a number grid)

6. Latin Square Problem

# Converting Process & Example

A problem to be converted to CSP requires the following steps:


Step 1: Create a variable set.

Step 2: Create a domain set.

Step 3: Create a constraint set with variables and domains (if possible) after considering the
constraints.

Step 4: Find an optimal solution.

The requirements to solve a constraint satisfaction problem (CSP) are:

 A state-space
 The notion of the solution.

A state in state-space is defined by assigning values to some or all variables such as {X1=v1,
X2=v2, and so on…}.

An assignment of values to a variable can be done in three ways:

1. Consistent or Legal Assignment: An assignment which does not violate any constraint
or rule is called Consistent or legal assignment.
2. Complete Assignment: An assignment where every variable is assigned with a value,
and the solution to the CSP remains consistent. Such assignment is known as
Complete assignment.
3. Partial Assignment: An assignment which assigns values to some of the variables
only. Such type of assignments is called Partial assignments.

# Types of Domains in CSP

There are following two types of domains which are used by the variables:

1. Discrete Domain: It is an infinite domain which can have one state for multiple
variables. For example, a start state can be allocated infinite times for each variable.
2. Finite Domain: It is a finite domain which can have continuous states describing one
domain for one specific variable. It is also called a continuous domain.

# Constraint Types in CSP:


With respect to the variables, basically there are following types of constraints:

1. Unary Constraints: It is the simplest type of constraints that restricts the value of a
single variable.
2. Binary Constraints: It is the constraint type which relates two variables. A value x2
will contain a value which lies between x1 and x3.
3. Global Constraints: It is the constraint type which involves an arbitrary number of
variables.

# Some special types of solution algorithms are used to solve the following types of
constraints:

1. Linear Constraints: These type of constraints are commonly used in linear


programming where each variable containing an integer value exists in linear form
only.
2. Non-linear Constraints: These type of constraints are used in non-linear programming
where each variable (an integer value) exists in a non-linear form.

#CSP Problems:
Constraint satisfaction includes those problems which contains some constraints while
solving the problem. CSP includes the following problems:

1) Graph Colouring: The problem where the constraint is that no adjacent sides can have
the same color.
2) Sudoku Playing: The gameplay where the constraint is that no number from 0-9 can
be repeated in the same row or column.

3) Crossword: In crossword problem, the constraint is that there should be the correct
formation of the words, and it should be meaningful.

2) Explain Hill climbing and Best first search algorithms in detail.


Ans: A hill-climbing algorithm is an Artificial Intelligence (AI) algorithm that increases
in value continuously until it achieves a peak solution. This algorithm is used to optimize
mathematical problems and in other real-life applications like marketing and job
scheduling.

A hill-climbing algorithm is a local search algorithm that moves continuously upward


(increasing) until the best solution is attained. This algorithm comes to an end when the peak
is reached.

A Hill Climbing algorithm example can be a traveling salesman’s problem where we may
need to minimize or maximize the distance travelled by the salesman.

This algorithm has a node that comprises two parts: state and value. It begins with a non-
optimal state (the hill’s base) and upgrades this state until a certain precondition is met.
The heuristic function is used as the basis for this precondition. The process of continuous
improvement of the current state of iteration can be termed as climbing. This explains why
the algorithm is termed as a hill-climbing algorithm.

A hill-climbing algorithm has four main features:

1. It employs a greedy approach: This means that it moves in a direction in which the
cost function is optimized. The greedy approach enables the algorithm to establish
local maxima or minima.
2. No Backtracking: A hill-climbing algorithm only works on the current state and
succeeding states (future). It does not look at the previous states.
3. Feedback mechanism: The algorithm has a feedback mechanism that helps it decide
on the direction of movement (whether up or down the hill). The feedback mechanism
is enhanced through the generate-and-test technique.
4. Incremental change: The algorithm improves the current solution by incremental
changes.

A state-space diagram provides a graphical representation of states and the optimization


function. If the objective function is the y-axis, we aim to establish the local maximum and
global maximum.
The following diagram shows a simple state-space diagram. The objective function has been
shown on the y-axis, while the state-space represents the x-axis.

Simple hill climbing

This is a simple form of hill climbing that evaluates the neighboring solutions. If the next
neighbor state has a higher value than the current state, the algorithm will move. The
neighboring state will then be set as the current one.

Algorithm

 Conduct an assessment of the current state. Stop the process and indicate success if it
is a goal state.
 Perform looping on the current state if the assessment in step 1 did not establish a goal
state.
 Continue looping to attain a new solution.
 Assess the new solution. If the new state has a higher value than the current state in
steps 1 and 2, then mark it as a current state.
 Continue steps 1 to 4 until a goal state is attained. If this is the case, then exit the
process.
Applications

Marketing

A hill-climbing algorithm can help a marketing manager to develop the best marketing plans.
This algorithm is widely used in solving Traveling-Salesman problems. It can help by
optimizing the distance covered and improving the travel time of sales team members. The
algorithm helps establish the local minima efficiently.

Robotics

Hill climbing is useful in the effective operation of robotics. It enhances the coordination of
different systems and components in robots.

Job Scheduling

The hill climbing algorithm has also been applied in job scheduling. This is a process in
which system resources are allocated to different tasks within a computer system. Job
scheduling is achieved through the migration of jobs from one node to a neighbouring node.
A hill-climbing technique helps establish the right migration route.

We can also express the process in Pseudocode:


Best first search algorithms:
BFS uses the concept of a Priority queue and heuristic search. To search the graph space, the
BFS method uses two lists for tracking the traversal. An ‘Open’ list that keeps track of the
current ‘immediate’ nodes available for traversal and a ‘CLOSED’ list that keeps track of the
nodes already traversed.
Best First Search Algorithm

1. Create 2 empty lists: OPEN and CLOSED


2. Start from the initial node (say N) and put it in the ‘ordered’ OPEN list
3. Repeat the next steps until the GOAL node is reached
1. If the OPEN list is empty, then EXIT the loop returning ‘False’
2. Select the first/top node (say N) in the OPEN list and move it to the CLOSED list.
Also, capture the information of the parent node
3. If N is a GOAL node, then move the node to the Closed list and exit the loop
returning ‘True’. The solution can be found by backtracking the path
4. If N is not the GOAL node, expand node N to generate the ‘immediate’ next
nodes linked to node N and add all those to the OPEN list
5. Reorder the nodes in the OPEN list in ascending order according to an evaluation
function f(n)
This algorithm will traverse the shortest path first in the queue. The time complexity of the
algorithm is given by O(n*logn).

Advantages:
1. Can switch between BFS and DFS, thus gaining the advantages of both.
2. More efficient when compared to DFS.
Disadvantages:
1. Chances of getting stuck in a loop are higher.

Example: Best-First Search

Here C is the initial or source node and L and Z are goal nodes.
Open: C

Closed: —

Now, C is added to Closed, and B, T, O, E and P are added to Open.

Open: T, O, E, B, P

Closed: C

Now, T has the least distance hence, T is added to Closed.

Open: O, E, B, P

Closed: C, T
As T does not have any successors, the next node from open that is O is removed
from Open and added to closed.

Open: E, B, P

Closed: C, T, O

The successors of node O that is node I and N are added to Open.

Open: I, E, B, P, N

Closed: C, T, O
Now, node I is removed from Open and added to closed.

Open: E, B, P, N

Closed: C, T, O, I

The successor of I that is Z is added to Open.

Open: Z, E, B, P, N

Closed: C, T, O, I
Now, node Z is removed from Open and added to closed.

Open: E, B, P, N

Closed: C, T, O, I, Z

The Goal is found. The final path is C – O – I – Z.

3) Explain state space approach for solving any AI problem.

Ans: referred PDF File (A-3-Q-3)


Example: The 15 Puzzle
The start state is some (almost) random configuration of the tiles and the goal state
is as shown.

State Space Search Rules are

 Move empty space up


 Move empty space down
 Move empty space right
 Move empty space left
These Rules apply if empty space is not against the edge.

4) Explain A* searching technique in detail with example. Discuss conditions for the
optimality of this technique.

Ans: A* Algorithm-
 A* Algorithm is one of the best and popular techniques used for path finding and graph
traversals.
 A lot of games and web-based maps use this algorithm for finding the shortest path
efficiently.
 It is essentially a best first search algorithm.

Working-

A* Algorithm works as-


 It maintains a tree of paths originating at the start node.
 It extends those paths one edge at a time.
 It continues until its termination criterion is satisfied.

A* Algorithm extends the path that minimizes the following function-


f(n) = g(n) + h(n)
Here,
 ‘n’ is the last node on the path
 g(n) is the cost of the path from start node to node ‘n’
 h(n) is a heuristic function that estimates cost of the cheapest path from node ‘n’ to the
goal node

Algorithm-

 The implementation of A* Algorithm involves maintaining two lists- OPEN and


CLOSED.
 OPEN contains those nodes that have been evaluated by the heuristic function but
have not been expanded into successors yet.
 CLOSED contains those nodes that have already been visited.

The algorithm is as follows-

Step-01:

 Define a list OPEN.


 Initially, OPEN consists solely of a single node, the start node S.
Step-02:

If the list is empty, return failure and exit.

Step-03:

 Remove node n with the smallest value of f(n) from OPEN and move it to list
CLOSED.
 If node n is a goal state, return success and exit.

Step-04:

Expand node n.

Step-05:

 If any successor to n is the goal node, return success and the solution by tracing the
path from goal node to S.
 Otherwise, go to Step-06.

Step-06:

For each successor node,


 Apply the evaluation function f to the node.
 If the node has not been in either list, add it to OPEN.

Step-07:

Go back to Step-02.

Example-01:

Given an initial state of a 8-puzzle problem and final state to be reached-


Find the most cost-effective path to reach the final state from initial state using A*
Algorithm.
Consider g(n) = Depth of node and h(n) = Number of misplaced tiles.

Solution-

 A* Algorithm maintains a tree of paths originating at the initial state.


 It extends those paths one edge at a time.
 It continues until final state is reached.
Example-02:

Consider the following graph-


The numbers written on edges represent the distance between the nodes.
The numbers written on nodes represent the heuristic value.
Find the most cost-effective path to reach from start state A to final state J using A*
Algorithm.

Solution-

Step-01:

 We start with node A.


 Node B and Node F can be reached from node A.

A* Algorithm calculates f(B) and f(F).


 f(B) = 6 + 8 = 14
 f(F) = 3 + 6 = 9

Since f(F) < f(B), so it decides to go to node F.


Path- A → F

Step-02:

Node G and Node H can be reached from node F.

A* Algorithm calculates f(G) and f(H).


 f(G) = (3+1) + 5 = 9
 f(H) = (3+7) + 3 = 13

Since f(G) < f(H), so it decides to go to node G.

Path- A → F → G

Step-03:

Node I can be reached from node G.

A* Algorithm calculates f(I).


f(I) = (3+1+3) + 1 = 8
It decides to go to node I.

Path- A → F → G → I

Step-04:

Node E, Node H and Node J can be reached from node I.

A* Algorithm calculates f(E), f(H) and f(J).


 f(E) = (3+1+3+5) + 3 = 15
 f(H) = (3+1+3+2) + 3 = 12
 f(J) = (3+1+3+3) + 0 = 10
Since f(J) is least, so it decides to go to node J.

Path- A → F → G → I → J
This is the required shortest path from node A to node J.

5) Will Breadth First Search always find Optimal Solution. Why? Elaborate with a good
example.

Ans: Breadth First Search always find Optimal Solution because, BFS is a traversing
algorithm where you should start traversing from a selected node (source or starting node) and
traverse the graph layer wise thus exploring the neighbour nodes (nodes which are directly
connected to source node). You must then move towards the next-level neighbour nodes.

As the name BFS suggests, you are required to traverse the graph breadthwise as follows:

1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer
Breadth-first search algorithm likes to stay as close as possible to the starting point.
Some of the situations that I can think of are:

1. Social networking websites can use it for finding the people in the specified distance.
2. It can be useful in tormenting/peer-to-peer network to look for neighbouring computers.
3. GPS navigation systems can use it to find nearby locations.
Consider the following diagram.

The distance between the nodes in layer 1 is comparitively lesser than the distance between the
nodes in layer 2. Therefore, in BFS, you must traverse all the nodes in layer 1 before you move
to the nodes in layer 2.
Step 1: In the graph, every vertex or node is known. First, initialize a queue.

Step 2: In the graph, start from source node A and mark it as visited.
Step 3: Then you can observe B and E, which are unvisited nearby nodes from A.
You have two nodes in this example, but here choose B, mark it as visited, and
enqueue it alphabetically.

Step 4: Node E is the next unvisited neighboring node from A. You enqueue it after
marking it as visited.
Step 5: A now has no unvisited nodes in its immediate vicinity. As a result, you
dequeue and locate A.
Step 6: Node C is an unvisited neighboring node from B. You enqueue it after
marking it as visited.
Step 7: Node D is an unvisited neighboring node from C. You enqueue it after
marking it as visited.

Step 8: If all of D's adjacent nodes have already been visited, remove D from the
queue.
Step 9: Similarly, all nodes near E, B, and C nodes have already been visited;
therefore, you must remove them from the queue.

Step 10: Because the queue is now empty, the bfs traversal has ended.
6) Consider the following graph.

Starting from state A, execute DFS. The goal node is G. Show the order in which the
nodes are expanded? Assume that the alphabetically smaller node is expanded first to
break ties.

7) Review different Search Strategies.

a) Blind Search
1) Depth first search
2) Breadth first search
3) Iterative deepening search
4) Bidirectional search
b) Informed Search.

8) Explain AO* searching technique in detail with example.

MCQ Questions:

1) Which search is implemented with an empty first-in-first-out queue?

a) Depth-first search b) Breadth-first search c) Bidirectional search d) None of the mentioned

2) Which search strategy is also called as blind search?

a) Uninformed search b) Informed search

c) Simple reflex search d) All of the mentioned

3) A* algorithm is based on _____.

a) Breadth-First-Search b) Depth-First–Search c) Best-First-Search d) Hill climbing

4) A* is optimal if h(n) is an allowable heuristic-that is, providing that h(n) under no


circumstances underestimates the cost to reach the goal.

a) True b) False c) Partially true e) None of these


5) What is the evaluation function in A* approach?

a) Heuristic function b) Path cost from start node to current node

c) Path cost from start node to current node + Heuristic cost

d) Average of Path cost from start node to current node and Heuristic cost

6) Neural Networks are complex _____ with many parameters.

a) Linear Functions b) Nonlinear Functions c) Discrete Functions d) Exponential Functions

7) Which of the following graphs are useful for solving decomposable problems.

a) AND b) OR c) Directed d) AND-OR

8) AND-OR graphs are the part of the _____________

a) Production rules b) Problem characteristics c) Problem reduction d) Fuzzy logic

9) Constraint satisfaction is a _____________ procedure that operates in a space of


constraint sets.

a) Hash b) Constraint c) Control d) Search

10) The _____________ representation mapping maps from facts to representations.

a) backward b) formal c) heuristic d) forward

11) The _____________ representation mapping maps from representations to facts.

a) Backward b) formal c) heuristic d) forward

You might also like