The Queens Problem and functional indexing #443
sbmack
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This is a formulation of the Queens problem. n Queens must be placed on an n x n chessboard such that no Queens share the same row, column or diagonal. The Show and Tell feature is that the diagonal constraints illustrate how to create a Left Hand Side (LHS) variable index as a function of another variable index. This allows the creation of constraint sets with differing numbers of terms in each constraint. Note that the index derived from parameter values must be defined first.
In the model below the size parameter is the dimension of the board. The size is initialized to 8, but it can be increased to test solution times for larger boards. The
fixed
parameter is the index of a square that forces the square into the solution. It is randomly generated and used in a constraint in the formulation. The purpose is to generate alternative solutions by forcing a randomly assigned square into the solution.The ggplot function plots the solution as a grid board for visual inspection. Running the entire code block repeatedly will generate alternative solutions.
Comments, suggestions appreciated.
SteveM
Beta Was this translation helpful? Give feedback.
All reactions