Skip to content

Instantly share code, notes, and snippets.

View RussellAndrewEdson's full-sized avatar

Russell Andrew Edson RussellAndrewEdson

  • Adelaide, Australia
View GitHub Profile
@RussellAndrewEdson
RussellAndrewEdson / QueensILP_FirstRowBackwardDiag.java
Created February 13, 2015 12:17
Constructing the first-row backward diagonal constraints for the N-Queens Puzzle.
/* 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;
}
}
@RussellAndrewEdson
RussellAndrewEdson / QueensILP_FirstColForwardDiag.java
Last active August 29, 2015 14:15
Constructing the first-column forward diagonal constraints for the N-Queens Puzzle.
/* 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;
}
}
@RussellAndrewEdson
RussellAndrewEdson / QueensILP_FirstRowForwardDiag.java
Created February 13, 2015 11:37
Constructing the first-row forward diagonal constraints for the N-Queens Puzzle.
/* 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;
}
}
@RussellAndrewEdson
RussellAndrewEdson / QueensILP_ColumnConstraints.java
Created February 13, 2015 11:12
Constructing the column constraints for the N-Queens Puzzle.
/**
* 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;
}
@RussellAndrewEdson
RussellAndrewEdson / QueensILP_RowConstraints.java
Last active August 29, 2015 14:15
Constructing the row constraints for the N-Queens Puzzle.
/**
* 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;
}
@RussellAndrewEdson
RussellAndrewEdson / queens.clj
Created February 5, 2015 04:57
Clojure functions for solving the N-Queens Puzzle (from SICP).
; 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]
@RussellAndrewEdson
RussellAndrewEdson / FilteredAccumulate.java
Last active August 29, 2015 14:14
A Java implementation for the higher-order filtered-accumulation function.
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.)
*/
@RussellAndrewEdson
RussellAndrewEdson / Accumulate.java
Last active August 29, 2015 14:14
A Java implementation for the higher-order accumulation function.
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(
@RussellAndrewEdson
RussellAndrewEdson / Product.java
Created January 28, 2015 04:31
A Java implementation for the higher-order product function.
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);
@RussellAndrewEdson
RussellAndrewEdson / Sum.java
Last active August 29, 2015 14:14
A Java implementation for the higher-order summation function.
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);