Created
February 13, 2015 12:33
-
-
Save RussellAndrewEdson/46fe9f22a596c28292dd to your computer and use it in GitHub Desktop.
Constructing the last-column backward diagonal constraints for the N-Queens Puzzle.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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