/* 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, "")); }