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 first row. | |
* ie. x_1,j + the sum of x_1+m,j-m <= 1 for all j=1,...,n. | |
*/ | |
double[][] rowBackwardConstraintsMatrix = new double[n][n*n]; | |
for (int row = 0; row < n; row++) { | |
for (int column = row; column < n*row + 1; column += (n-1)) { | |
rowBackwardConstraintsMatrix[row][column] = 1; | |
} | |
} |
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 "forward diagonals" where | |
* we move along the first column. | |
* ie. x_i,1 + the sum of x_i+m,1+m <= 1 for all i=1,...,n. | |
*/ | |
double[][] columnForwardConstraintsMatrix = new double[n][n*n]; | |
for (int row = 0; row < n; row++) { | |
for (int column = n*row; column < n*n; column += (n+1)) { | |
columnForwardConstraintsMatrix[row][column] = 1; | |
} | |
} |
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 "forward diagonals" where | |
* we move along the first row. | |
* ie. x_1,j + the sum of x_1+m,j+m is <= 1 for all j=1,...,n. | |
*/ | |
double[][] rowForwardConstraintsMatrix = new double[n][n*n]; | |
for (int row = 0; row < n; row++) { | |
for (int column = row; column < n*n - row*n; column += (n+1)) { | |
rowForwardConstraintsMatrix[row][column] = 1; | |
} | |
} |
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
/** | |
* Adds the "one queen per column" constraints to the linear program, | |
* ie. the sum from i=1 to n of x_i,j = 1 for all j=1,...,n. | |
*/ | |
public static void makeQueenColumnConstraints(int n, LinearProgram lp) { | |
double[][] columnConstraintsMatrix = new double[n][n*n]; | |
for (int row = 0; row < n; row++) { | |
for (int column = row; column < n*n; column += n) { | |
columnConstraintsMatrix[row][column] = 1; | |
} |
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
/** | |
* Adds the "one queen per row" constraints to the linear program, | |
* ie. the sum from j=1 to n of x_i,j = 1 for all i=1,...,n. | |
*/ | |
public static void makeQueenRowConstraints(int n, LinearProgram lp) { | |
double[][] rowConstraintsMatrix = new double[n][n*n]; | |
for (int row = 0; row < n; row++) { | |
for (int column = n*row; column < n*row + n; column++) { | |
rowConstraintsMatrix[row][column] = 1; | |
} |
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
; Clojure functions for solving the N-Queens Puzzle. | |
(defn adjoin-position | |
"Adds the new (row, column) position to the given positions list." | |
[row column positions] | |
(cons (list row column) positions)) | |
(defn same-row? | |
"Returns true if the given queens q1 and q2 are on the same row." | |
[q1 q2] |
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
import java.util.function.BinaryOperator; | |
import java.util.function.Function; | |
public class FilteredAccumulate { | |
/** | |
* Returns the total obtained by applying the given operation across | |
* the values of f(i), for i = a to b in which i satisfies the given | |
* predicate (using the nullValue to get us started.) | |
*/ |
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
import java.util.function.BinaryOperator; | |
import java.util.function.Function; | |
public class Accumulate { | |
/** | |
* Returns the total obtained by applying the given operation across | |
* the values of f(i), for i = a to b, using the given null value. | |
*/ | |
public static double accumulate( |
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
import java.util.function.Function; | |
public class Product { | |
/** Returns the accumulated product from i = a to b of f(i). */ | |
public static double product(Function<Integer, Double> f, int a, int b) { | |
double result = 1.0; | |
for (int i = a; i <= b; i++) { | |
result *= f.apply(i); |
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
import java.util.function.Function; | |
public class Sum { | |
/** Returns the accumulated sum from i = a to b of f(i). */ | |
public static double sum(Function<Integer, Double> f, int a, int b) { | |
double result = 0.0; | |
for (int i = a; i <= b; i++) { | |
result += f.apply(i); |