Skip to content

Instantly share code, notes, and snippets.

@RussellAndrewEdson
Created February 13, 2015 12:33
Show Gist options
  • Save RussellAndrewEdson/46fe9f22a596c28292dd to your computer and use it in GitHub Desktop.
Save RussellAndrewEdson/46fe9f22a596c28292dd to your computer and use it in GitHub Desktop.
Constructing the last-column backward diagonal constraints for the N-Queens Puzzle.
/* Here we create the constraints for the "backward diagonals" where
* we move along the last column.
* ie. x_i,n + the sum of x_i+m,n-m <= 1 for all i=1,...,n.
*/
double[][] columnBackwardConstraintsMatrix = new double[n][n*n];
for (int row = 0; row < n; row++) {
for (int column = n*(row+1) - 1; column < n*n; column += (n-1)) {
columnBackwardConstraintsMatrix[row][column] = 1;
}
}
/* Note: Our iteration here always gives us an extra '1' at the end of
* the top row, which we can simply remove here.
*/
columnBackwardConstraintsMatrix[0][n*n - 1] = 0;
for (double[] row : columnBackwardConstraintsMatrix) {
lp.addConstraint(new LinearSmallerThanEqualsConstraint(row, 1, ""));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment