Skip to content

Commit

Permalink
Renamed N to n
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] committed Jan 5, 2024
1 parent 5d42730 commit 10d801d
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 130 deletions.
14 changes: 7 additions & 7 deletions src/main/java/de/nqueensfaf/Solver.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public abstract class Solver {

protected int N;
protected int n;

private volatile Status state = Status.IDLE;
private Thread asyncSolverThread, bgThread;
Expand Down Expand Up @@ -98,7 +98,7 @@ public final <T extends Solver> T waitFor() {
}

private void preconditions() {
if (N == 0) {
if (n == 0) {
state = Status.IDLE;
throw new IllegalStateException("board size was not set");
}
Expand All @@ -116,7 +116,7 @@ private Thread backgroundThread(boolean updateConsumer, boolean autoSaver) {
return new Thread(() -> {
// for autoSaver
String filePath = config().autoSavePath;
filePath = filePath.replaceAll("\\{N\\}", "" + N);
filePath = filePath.replaceAll("\\{n\\}", "" + n);
float progress = getProgress() * 100;
float tmpProgress = progress;

Expand Down Expand Up @@ -167,13 +167,13 @@ else if (progress >= tmpProgress + config().autoSavePercentageStep) {

protected int solveSmallBoard() {
solutionsSmallN = 0;
int mask = (1 << getN()) - 1;
int mask = (1 << n) - 1;
smallBoardNQ(0, 0, 0, 0, mask, mask);
return solutionsSmallN;
}

private void smallBoardNQ(int ld, int rd, int col, int row, int free, int mask) {
if (row == getN() - 1) {
if (row == n - 1) {
solutionsSmallN++;
return;
}
Expand Down Expand Up @@ -242,13 +242,13 @@ public final <T extends Solver> T setN(int n) {
if (n <= 0 || n > 31) {
throw new IllegalArgumentException("board size must be a number between 0 and 32 (not inclusive)");
}
N = n;
this.n = n;
return (T) this;
}

// getters
public final int getN() {
return N;
return n;
}

public final boolean isIdle() {
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/de/nqueensfaf/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void setTaskFile(File taskFile) {
this.taskFile = taskFile;
}

@Option(names = { "-N", "--board-size" }, paramLabel = "INT", required = false, description = "board size")
private int N = -69;
@Option(names = { "-n", "--board-size" }, paramLabel = "INT", required = false, description = "board size")
private int n = -69;

@Option(names = { "-g", "--gpu" }, required = false, description = "execute on GPU('s)")
private boolean executeOnGpu;
Expand Down Expand Up @@ -85,13 +85,13 @@ public void run() {

// validate settings
if (taskFile == null) {
if (N == -69) {
if (n == -69) {
System.err.println("Missing required option: '--board-size=INT'");
CommandLine.usage(this, System.err);
return;
}
if (N <= 0 || N >= 32) {
System.err.println("Invalid board size! Must be a number N with N > 0 and N < 32.");
if (n <= 0 || n >= 32) {
System.err.println("Invalid board size! Must be a number with > 0 and < 32.");
return;
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public void run() {
if (taskFile != null) {
solver.load(taskFile);
} else {
solver.setN(N);
solver.setN(n);
solver.solve();
}

Expand Down
38 changes: 19 additions & 19 deletions src/main/java/de/nqueensfaf/impl/CPUSolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

public class CPUSolver extends Solver {

// for very small N it is overkill to use this method
// for very small n it is overkill to use this method
// thus we use a straightforward recursive implementation of Jeff Somers Bit
// method for such N
// method for such n
// smallestN marks the border, when to use this simpler solver
private static final int smallestN = 6;

Expand Down Expand Up @@ -55,14 +55,14 @@ public CPUSolver() {
@Override
protected void run() {
start = System.currentTimeMillis();
if (N <= smallestN) { // if N is very small, use the simple Solver from the parent class
if (n <= smallestN) { // if n is very small, use the simple Solver from the parent class
solutions = solveSmallBoard();
end = System.currentTimeMillis();
progress = 1;
return;
}

utils.setN(N);
utils.setN(n);
if (!loaded) {
genConstellations();
}
Expand All @@ -83,7 +83,7 @@ protected void run() {
// start the threads and wait until they are all finished
ExecutorService executor = Executors.newFixedThreadPool(config.threadcount);
for (i = 0; i < config.threadcount; i++) {
CPUSolverThread cpuSolverThread = new CPUSolverThread(utils, N, threadConstellations.get(i));
CPUSolverThread cpuSolverThread = new CPUSolverThread(utils, n, threadConstellations.get(i));
threads.add(cpuSolverThread);
executor.submit(cpuSolverThread);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ protected void save_(String filepath) throws IOException {
Kryo kryo = Constants.kryo;
try (Output output = new Output(new GZIPOutputStream(new FileOutputStream(filepath)))) {
kryo.writeObject(output,
new SolverState(N, System.currentTimeMillis() - start + storedDuration, constellations));
new SolverState(n, System.currentTimeMillis() - start + storedDuration, constellations));
output.flush();
}
}
Expand Down Expand Up @@ -181,18 +181,18 @@ public long getSolutions() {
}

private void genConstellations() {
// halfN half of N rounded up
final int halfN = (N + 1) / 2;
L = 1 << (N - 1);
mask = (1 << N) - 1;
// halfN half of n rounded up
final int halfN = (n + 1) / 2;
L = 1 << (n - 1);
mask = (1 << n) - 1;

// calculate starting constellations for no Queens in corners
for (int k = 1; k < halfN; k++) { // go through first col
for (int l = k + 1; l < N - 1; l++) { // go through last col
for (int i = k + 1; i < N - 1; i++) { // go through first row
if (i == N - 1 - l) // skip if occupied
for (int l = k + 1; l < n - 1; l++) { // go through last col
for (int i = k + 1; i < n - 1; i++) { // go through first row
if (i == n - 1 - l) // skip if occupied
continue;
for (int j = N - k - 2; j > 0; j--) { // go through last row
for (int j = n - k - 2; j > 0; j--) { // go through last row
if (j == i || l == j)
continue;

Expand All @@ -207,8 +207,8 @@ private void genConstellations() {
}
// calculating start constellations with the first Queen on the corner square
// (0,0)
for (int j = 1; j < N - 2; j++) { // j is idx of Queen in last row
for (int l = j + 1; l < N - 1; l++) { // l is idx of Queen in last col
for (int j = 1; j < n - 2; j++) { // j is idx of Queen in last row
for (int l = j + 1; l < n - 1; l++) { // l is idx of Queen in last col
ijklList.add(utils.toijkl(0, j, 0, l));
}
}
Expand All @@ -231,7 +231,7 @@ private void genConstellations() {
// ld, rd, col, start_queens_ijkl for each constellation
// occupy the board corresponding to the queens on the borders of the board
// we are starting in the first row that can be free, namely row 1
ld = (L >>> (i - 1)) | (1 << (N - k));
ld = (L >>> (i - 1)) | (1 << (n - k));
rd = (L >>> (i + 1)) | (1 << (l - 1));
col = 1 | L | (L >>> i) | (L >>> j);
// occupy diagonals of the queens j k l in the last row
Expand All @@ -242,7 +242,7 @@ private void genConstellations() {
// counts all subconstellations
counter = 0;
// generate all subconstellations
setPreQueens(ld, rd, col, k, l, 1, j == N - 1 ? 3 : 4);
setPreQueens(ld, rd, col, k, l, 1, j == n - 1 ? 3 : 4);
currentSize = constellations.size();
// jkl and sym and start are the same for all subconstellations
for (int a = 0; a < counter; a++) {
Expand All @@ -269,7 +269,7 @@ private void setPreQueens(int ld, int rd, int col, int k, int l, int row, int qu
// if not done or row k or l, just place queens and occupy the board and go
// further
else {
int free = (~(ld | rd | col | (LD >>> (N - 1 - row)) | (RD << (N - 1 - row)))) & mask;
int free = (~(ld | rd | col | (LD >>> (n - 1 - row)) | (RD << (n - 1 - row)))) & mask;
int bit;

while (free > 0) {
Expand Down
Loading

0 comments on commit 10d801d

Please sign in to comment.