2022-CSC14003-20CLC-Lab01-đã chuyển đổi

You might also like

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

Course: CSC14003 – Introduction to Artificial Intelligence

Class 20CLC– Term II/2021-2022

Lab01: The Knight’s Tour

The Knight’s tour [1]


A knight's tour is a sequence of moves of a knight on a chessboard such that the knight visits every
square exactly once. If the knight ends on a square that is one knight's move from the beginning
square (so that it could tour the board again immediately, following the same path), the tour is
closed; otherwise, it is open.
The knight's tour problem is the mathematical problem of finding a knight's tour. Creating a
program to find a knight's tour is a common problem given to computer science students.[3]
Variations of the knight's tour problem involve chessboards of different sizes than the usual 8  8,
as well as irregular (non-rectangular) boards.
The knight's tour problem is an instance of the more general Hamiltonian path problem in
graph theory. The problem of finding a closed knight's tour is similarly an instance of the
Hamiltonian cycle problem. Unlike the general Hamiltonian path problem, the knight's tour
problem can be solved in linear time.

The existence of solutions: It is proved that for any m  n board with m ≤ n, a closed knight's tour
is always possible unless one or more of these three conditions are met:
- m and n are both odd
- m = 1, 2, or 4
- m = 3 and n = 4, 6, or 8.
It is also proved that on any rectangular board whose smaller dimension is at least 5, there is a
(possibly open) knight's tour.

Implementation requirements
You are asked to implement the following strategies to find any knight's tour (either closed or
open) starting at square (x, y) available on a rectangular board of size m × m . The parameters, x, y
and m, are user-specified positive integers (m ≥ 5 and 1 ≤ x, y ≤ m).
The following example shows a knight’s tour starting at (x, y) = (5, 1) on a board of size 8 × 8.

59 14 63 32 1 16 19 34
62 31 60 15 56 33 2 17
13 58 55 64 49 18 35 20
30 61 42 57 54 51 40 3
43 12 53 50 41 48 21 36
26 29 44 47 52 39 4 7
11 46 27 24 9 6 37 22
28 25 10 45 38 23 8 5

1
A. Backtracking [2]
This strategy addresses the problem in an incremental way. We initially start from an empty
solution vector and one by one add items (that is, each item is a Knight’s move). When adding an
item, we check if the current solution violates the problem constraint, if it does then we remove the
item and try other alternatives. If none of the alternatives works out, we go to the previous stage
and remove the item added in the previous stage. No solution exists if the initial stage is reached
during the backtracking (note that, it is different from reaching the initial stage in the last step for a
closed tour). If adding an item does not violate constraints, we recursively add items one by one.
We stop the search and print the solution when the solution vector becomes complete.

B. Warnsdorff’s Heuristic [3]


The Warndorff’s Rule says that we can start from any initial position of the knight on the board and
we always move to an adjacent, unvisited square with minimal degree (minimum number of
unvisited adjacent). This algorithm may also more generally be applied to any graph.
Consider the following definitions
- A position Q is accessible from a position P if P can move to Q by a single Knight’s move,
and Q has not yet been visited.
- The accessibility of a position P is the number of positions accessible from P.
The steps to implement the Warnsdorff’s Heuristic are as follows.
1. Set P to be a random initial position on the board
2. Mark the board at P with the move number “1”
3. Do following for each move number from 2 to the number of squares on the board:
a. Let S be the set of positions accessible from P.
b. Set P to be the position in S with minimum accessibility
c. Mark the board at P with the current move number
4. Return the marked board — each square will be marked with the move number on
which it is visited.

Report requirements
We consider two strategies, backtracking and Warnsdorff’s Heuristic, and three different board
sizes (including 8, 15 and 25).
For each strategy and for each board size, run the knight’s tour with five different initial positions.
Report the average number of moves and the average running time in each case to both a table and
a chart.

Problem representation
Input: The parameters, x, y and m, are obtained from the command line of the following format
executable filename -px x -py y -s m
where executable filename is the name of the executable file, which is exactly your Student ID,
and the knight starts at square (x, y) on the board of size m. -px, -py and -s are predefined
indicators of corresponding parameters. For example, 1853001.py -px 5 -py 1 -s 8
The input values are assumed to be valid. You do not have to check them.

2
Output: The result is written to a the text file studentID_strategy.txt, where
studentID is your Student ID and strategy is either backtrack (for the backtracking
strategy) or heuristic (for the Warnsdorff’s Heuristic). The format of the file is as follows.
 The first line lists the three values, x, y and m, which are separated by white spaces.
 The second line shows the number of moves made by the specified strategy (backtrack
moves are not included if using backtracking)
 The third line shows the actual running time measured in milliseconds.
 The next lines present the result as a square matrix of size m × m. Each line shows a row in
the matrix, in which values are separated by white spaces. Any square can be either -1
(unvisited) or some value in the range [1, m2] (visited, the step in the tour).

References
[1] https://en.wikipedia.org/wiki/Knight%27s_tour
[2] https://www.geeksforgeeks.org/the-knights-tour-problem-backtracking-1/
[3] https://www.geeksforgeeks.org/warnsdorffs-algorithm-knights-tour-problem/

Rubric
 Your results will be checked automatically by a program, and thus please be sure that your
input and output data format conform the lab requirements.
 Any work that commits one or more of the following issues is considered totally unqualified
and hence gets 0% credit.
- Any plagiarism: Refer other student’s work or any other sources of materials without
citation, or copy them. The lab assistants decide the threshold of similarity.
- Uncompilable (syntax errors) or unable to produce the output (run-time errors)
- The input and output do not follow the specifications.
# Criteria % credits
1 Manipulate the input and output 10
2 Implement the backtracking 10
3 Implement the Warnsdorff’s Heuristic 10
4 Provide valid results for the backtracking strategy 20
5 Provide valid results for the Warnsdorff’s Heuristic 20
6 Provide all evidential files in the OUTPUT folder 10
7 Report sufficient information in the document 20
(This will be ignored if your work does not meet Criteria 6)

Submission preparation
This is an INDIVIDUAL assignment.
Prepare a folder that includes the following subfolders
- SOURCE: all Python (or C/C++) files should be put in here
- OUTPUT: all evidential files necessary for the report
- DOCUMENT: a PDF-formatted file that presents a check list of what you have/have not done,
and a statistical report described above.
Name the main folder following your Student ID and compress it in common format.

You might also like