You are on page 1of 3

Foundations of AI

Solving Shikaku Game as a CSP


Devangini Patel

Fig. 1 Sample Shikaku grid

Abstract In this report, how CSP has been applied to Shikaku is discussed. Also how the performance can be improved by adding minimum value heuristic is shown.
1

Keywords Search Constraint Satisfaction Problem Shikaku Minimum value heuristic

1 Shikaku 1.1 Introduction to the game Shikaku is a Japanese game invented by Nikoli. There is a rectangular grid in which some of the cells are lled with numbers. The aim of the game is to draw rectangles around those numbers such that the area of the rectangle is equal to that number and no two rectangles overlap. Each rectangle should contain only one number. [13]. An example of such starting conguration for shikaku is shown in Fig . 1. An end conguration for the same is shown in Fig. 2.
Fig. 2 Solved conguration of the sample shown in Fig. 1

2 Implementation details of the game This section concentrates on the possible ways to search the possible search space for the goal conguration. Modelling this problem as either simple DFS or BFS is dicult. The most simple way is to check out all the possible congurations as a CSP problem. Simple CSP
Devangini Patel ECS, Southampton University UK E-mail: dp2c12@soton.ac.uk Note: After a discussion with you, I have solved Shikaku as a constraint satisfaction problem instead of search tree.
1

with backtracking is similar to DFS with backtracking with the simple step of checking whether all the constraints are satised and that only one child node of the fringe is generated rather than expanding the entire fringe. Here, the only constraint that needs to be checked is whether the rectangle to be drawn at a particular step is feasible taking into considerations all the rectangles that are already drawn. Also, minimum value heuristic is used to reduce the number of nodes expanded. To improve the performance, the programs stack has been used instead of creating a stack for maintaining the states.

2.1 Backtracking search Basic functionality that must be implemented in CSP search for this problem: Order the numbers: First order the numbers row wise and then column wise if two numbers are in the same row. Get factors of a number: This is because the number represents the area of the rectangle, so by getting

2 initialize numbers from grid consider rst number

Devangini Patel

grid

numbers

call CSP(rst number)

stop Fig. 3 The dierent top space and left space combinations for 1x3 rectangle. Fig. 5 Flow for initialization of CSP backtrack

rectangle is drawn and a class representing the solution are sent. 2.2 Backtracking with minimum value heuristics In the minimum value heuristics, at each stage, the number of rectangles (also considering the combinations of left and top spaces) that can be constructed for each of the remaining numbers is calculated and this value is used as the heuristic value. The number with the minimum count is taken instead of the next number. So the ow of the code remains the same as backtrack search with the dierence in the order of selection of the numbers i.e. when CSP is called the next number is not passed but the number, with the minimum number of rectangles that can be constructed, is sent. 3 Performance and Results Both strategies are compared taking dierent samples and also changing the complexities. All of the samples are taken from [4]. 3.1 Comparison of nodes expanded versus strategies 9 dierent 5x5 grids were taken and CSP and minimum values CSP were run on them. The graph of no. of nodes expanded for dierent samples is shown in Fig. 7. It can be clearly seen that minimum value CSP reduces the number of nodes expanded substantially. 3.2 Comparison of nodes expanded versus size of grid for all strategies 9 dierent grids of 5x5, 7x7 and 10x10 sizes each were taken as inputs for CSP and minimum value heuristic

Fig. 4 A 1x3 rectangle with 0 left space and 0 top space which can be constructed.

the set of all possible factors it is possible to know which rectangles can be constructed. Get left space and top space for a rectangle of a particular size:The possible congurations of the 1x3 rectangle in this grid is shown in Fig. 3. In this case, it can be seen that there left space of 2 and top space of 0 for the green rectangle. Check if rectangle can be made: We need to know if a rectangle of size nxm and particular left space and top space can be constructed with the present conguration, e.g. we can construct a 1x3 rectangle with 0 leftspace and 0 top space as shown in Fig. 4. Also, it is necessary to know if any of the cells contained in the rectangle, which is to be formed, is already a part of another rectangle and it doesnt contain any other number. So the step by step algorithm for backtrack search is show in owchart in Fig. 5 and 6. In the owchart, only the next number is passed but in the code, the next number, the new conguration of the grid after the

Solving Shikaku Game as a CSP

get number

get list of all factors

take rst factor

nd maximum possible left and top spaces for this rectangle

take rst possibility

Fig. 7 The number of nodes expanded in CSP and minimum value CSP algorithms for dierent samples.

get next factor

yes

are all possibilities of left an top spaces over?

no

try next possibilty

no

can rectangle of size (factor, number/factor) be drawn?

no yes Fig. 8 The number of nodes expanded in CSP and minimum value CSP algorithms for dierent samples of varying complexities. is solution found? CSP(next number) yes yes return found Fig. 6 Flow for CSP backtrack code are all numbers covered?

no

References
1. Wiki Shikaku. http://en.wikipedia.org/wiki/ Shikaku/. [Online]. 2. Shikaku room. http://shikakuroom.com/. [Online]. 3. Ocial Shikaku Site. http://www.nikoli.co.jp/en/ puzzles/shikaku.html. [Online]. 4. Shikaku playing site. http://www.puzzle-shikaku. com/. [Online].

CSP and the number of nodes expanded in both cases are compared in Fig. 8. Set 1 corresponds to 5x5 samples, 2 to 7x7 and 3 to 10x10 respectively. It can be concluded that minimum value heuristic is not dependent on the size of the grid and performs better than simple CSP.

You might also like