Lab 5
Lab 5
Objective:
LAB ASSESMENT:
Ability to
Conduct
Experiment
Ability to
assimilate the
results
Effective use of
lab equipment and
follows
the lab safety rules
Data presentation
Experimental results
Conclusion
Date: Signature:
Lab no. 05: Constraint Satisfaction Problem in Artificial Intelligence
Objective: Finding a solution that meets a set of constraints is the goal of constraint satisfaction problems
(CSPs), a type of AI issue. Finding values for a group of variables that fulfill a set of restrictions or rules is
the aim of constraint satisfaction problems. For tasks including resource allocation, planning, scheduling,
and decision-making, CSPs are frequently employed in AI.
There are mainly three basic components in the constraint satisfaction problem:
Variables:
The things that need to be determined are variables. Variables in a CSP are the objects that must have values
assigned to them in order to satisfy a particular set of constraints.
Domains:
The range of potential values that a variable can have is represented by domains.
Constraints:
The guidelines that control how variables relate to one another are known as constraints. Constraints in a
CSP define the ranges of possible values for variables.
N Queen Problem:
The is_safe function checks if it's safe to place a queen on a given position (row, col) of a chessboard for the
N-Queens problem. The N-Queens problem involves placing N queens on an N x N chessboard such that no
two queens can attack each other. In chess, a queen can attack any other piece in the same row, column, or
diagonal
The loop checks if there is any other queen in the same column. If any row r in the specified column
has a queen (board[r][col] == 1), the position is not safe, and the function returns False.
The while loop checks for any queens on the upper left diagonal (diagonal going up to the left). It
starts from the given position (row, col) and moves towards the top-left corner of the board (x and
y decrease simultaneously). If a queen is found on this diagonal (board[x][y] == 1), the position is
not safe
This while loop checks for any queens on the upper right diagonal (diagonal going up to the
right). It starts from the given position (row, col) and moves towards the top-right corner (x
decreases while y increases). If a queen is found on this diagonal (board[x][y] == 1), the position
is not safe.
The n_Queen function solves the N-Queens problem by placing N queens on an N x N chessboard
such that no two queens can attack each other. It uses recursion and backtracking to explore all
possible solutions.
After the repetitive call returns, we reset board[row][i] to 0 to remove the queen and backtrack.
This backtracking step is crucial because it allows the algorithm to explore other potential
placements for queens in different columns of the current row.
Output:
Lab Task:
1. Write down the code for graph coloring problem. Explain code in steps properly.
Output:
Conclusions:
In this lab on Constraint Satisfaction Problems (CSP), we implemented solutions for two classical problems: the
Graph Coloring Problem and the N-Queens Problem. In the graph coloring task, we used a greedy algorithm to assign
colors to graph nodes, ensuring no adjacent nodes shared the same color, while in the N-Queens problem, we
employed backtracking to place N queens on a chessboard such that no two queens threatened each other. Both
problems demonstrated the structure of CSPs, with variables, domains, and constraints at their core. The lab
highlighted different search techniques, such as greedy and backtracking algorithms, to efficiently solve CSPs. These
techniques are applicable in real-world scenarios like scheduling and resource allocation, where constraints must be
satisfied. Overall, this lab deepened our understanding of modeling and solving constraint-based problems using
Python.