We are now at the beginning of our N-queens program. By default, the variable selection strategy is to select the variable with the smallest domain, and the value selection strategy is to try the values from the smallest one onwards. All variables still have the same domain and so the selected variable, highlighted in red, is the first one. Now we click on the STEP button. This tells Grace to execute the next labelling step. The variable matrix and the varstack change as follows:
The selected variable was instantiated to the first value in its domain, this modification was propagated to all other variables, the display is updated and the next selected variable is highlighted. The variable stack now shows the first selected variable: its initial domain was 1..8, it is now instantiated to 1 and all other values will be tried on backtracking, so we see the value 1 coloured in red and the rest is blue.
Note also one more thing: when we move the mouse pointer into the first varstack row, it is highlighted in gray and so is the first variable in the queens matrix:
All active display elements that display a domain variable
are hyperlinks:
when the pointer enters a hyperlink, all other widgets
on the screen which display the same variable
are highlighted.
This allows a quick orientation on the screen especially when
there are many different variables.
When we now click twice on STEP, next two variables will be labelled and the display shows
The values 2 and 3 for the second variable are displayed in white because they have been removed from the variable domain by the first labelling step, i.e. before the variable was labelled. The same holds for the values 1..4 of the third variable. The value 5 of the third variable is displayed in gray. This means that this value has been tried but it was rejected by one of the constraints and thus the next value has been automatically tried. The number of backtracks shown in the control panel does not change, though, because this was only a shallow backtracking inside one call to the value selection predicate.
In the next step, however, the situation is different. After clicking on STEP, we can see that no new variable has been displayed in the variable stack:
Instead, the value 6 for the third variable changed to gray and its current value is 7. This means that all values for the new selected variable have been tried and failed, the execution backtracked to the previous variable and its next value (7) and now the next variable is again selected for labelling. This was not shallow backtracking and thus the control panel now shows 1 backtrack.
The next labelling steps will also backtrack, as you can see by pressing several times the STEP button. We will now let the program run until a solution is found, but we will keep updating the variable stack display to see the visualisation of the search. When you press RUN, the program will run until a solution is found. The program executes 19 labelling steps until it finds a solution:
A MPEG movie (19k) of this program run is here and the search for all solutions (207k) is here
Further solutions are found in steps 26, 31, 32, ....
The 8-queens puzzle has quite a number of solutions. If we are interested in finding them all and we do not want to stop at each solution, we can use the OPTIONS... menu: selecting the menu item All Solutions tells Grace to find all solutions and to stop only when the whole search space has been explored. We select this item and then click on the RUN button. We can see that each time a new solution is found, the status items in the control panel and the variable matrix are updated, and the program stops after finding all 92 solutions. The control panel then shows