introduce bitloops

This commit is contained in:
mike
2026-01-14 18:42:13 +01:00
parent 29aceb2180
commit 75f599318a
4 changed files with 48 additions and 35 deletions

View File

@@ -44,7 +44,9 @@ public class Main {
public static class Opts {
public int seed = (int) (System.nanoTime() ^ System.currentTimeMillis());
public int pop = 24;
public int clueSize = 24;
public int pop = 40;
public int offspring = 60;
public int gens = 700;
public String wordsPath = "nl_score_hints_v3.csv";
public double minSimplicity = 0; // 0 means no limit
@@ -153,7 +155,9 @@ public class Main {
private static void printSettings(Opts o) {
System.out.printf(Locale.ROOT, " %-14s: %d%n", "seed", o.seed);
System.out.printf(Locale.ROOT, " %-14s: %d%n", "clues", o.clueSize);
System.out.printf(Locale.ROOT, " %-14s: %d%n", "population", o.pop);
System.out.printf(Locale.ROOT, " %-14s: %d%n", "offspring", o.offspring);
System.out.printf(Locale.ROOT, " %-14s: %d%n", "generations", o.gens);
System.out.printf(Locale.ROOT, " %-14s: %s%n", "wordsPath", o.wordsPath);
System.out.printf(Locale.ROOT, " %-14s: %.2f%n", "minSimplicity", o.minSimplicity);
@@ -196,11 +200,13 @@ public class Main {
static void usage() {
System.out.println("""
Usage:
java puzzle.Main [--seed N] [--pop N] [--gens N] [--tries N] [--words FILE] [--min-simplicity N.N] [--threads N] [--reindex]
java puzzle.Main [--seed N] [--clues N] [--pop N] [--offspring N] [--gens N] [--tries N] [--words FILE] [--min-simplicity N.N] [--threads N] [--reindex]
Defaults:
--pop 18
--gens 500
--clues 18
--pop 40
--offspring 60
--gens 700
--words nl_score_hints.csv
--min-simplicity 0 (no limit)
--threads %d
@@ -221,9 +227,15 @@ public class Main {
if (a.equals("--seed")) {
out.seed = Integer.parseInt(v);
i++;
} else if (a.equals("--clues")) {
out.clueSize = Integer.parseInt(v);
i++;
} else if (a.equals("--pop")) {
out.pop = Integer.parseInt(v);
i++;
} else if (a.equals("--offspring")) {
out.offspring = Integer.parseInt(v);
i++;
} else if (a.equals("--gens")) {
out.gens = Integer.parseInt(v);
i++;
@@ -359,7 +371,7 @@ public class Main {
static PuzzleResult _attempt(Rng rng, Dict dict, Opts opts) {
TOTAL_ATTEMPTS.incrementAndGet();
var swe = new SwedishGenerator(rng, new int[STACK_SIZE], Clues.createEmpty());
var mask = swe.generateMask(opts.pop, opts.gens, Math.max(opts.pop, (int) Math.floor(opts.pop * 1.5)));
var mask = swe.generateMask(opts.clueSize, opts.pop, opts.gens, opts.offspring);
var filled = fillMask(rng, extractSlots(mask, dict.index()), mask.toGrid());

View File

@@ -59,7 +59,6 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
static final double SIZED = (double) SIZE;// ~18
static final long MASK_LO = (SIZE >= 64) ? -1L : (1L << SIZE) - 1;
static final long MASK_HI = (SIZE <= 64) ? 0L : (SIZE >= 128 ? -1L : (1L << (SIZE - 64)) - 1);
static final int TARGET_CLUES = 24;
static final int MAX_WORD_LENGTH = C <= R ? C : R;
static final int MAX_WORD_LENGTH_PLUS_ONE = MAX_WORD_LENGTH + 1;
static final int MIN_LEN = Config.MIN_LEN;
@@ -433,11 +432,11 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
/// does not modify the grid
long maskFitness(final Clues grid) {
long maskFitness(final Clues grid, int clueSize) {
long cHLo = 0L, cHHi = 0L, cVLo = 0L, cVHi = 0L;
long lo_cl = grid.lo, hi_cl = grid.hi;
long penalty = (((long) Math.abs(grid.clueCount() - TARGET_CLUES)) * 16000L);
long penalty = (((long) Math.abs(grid.clueCount() - clueSize)) * 16000L);
boolean hasSlots = false;
for (long bits = lo_cl; bits != X; bits &= bits - 1) {
@@ -600,9 +599,9 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return penalty;
}
Clues randomMask() {
Clues randomMask(final int clueSize) {
var g = Clues.createEmpty();
for (int placed = 0, guard = 0, idx; placed < TARGET_CLUES && guard < 4000; guard++) {
for (int placed = 0, guard = 0, idx; placed < clueSize && guard < 4000; guard++) {
idx = rng.randint(0, SIZE_MIN_1);
if (g.isClue(idx)) continue;
var d_idx = rng.randint2bitByte();
@@ -653,15 +652,15 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
}
public static void clearClues(Clues out, int idx) { if (!out.hasRoomForClue(OFFSETS_D_IDX[Slot.packSlotKey(idx, out.digitAt(idx))])) out.clearClue(idx); }
Clues hillclimb(Clues start, int limit) {
Clues hillclimb(Clues start, int clue_size, int limit) {
var best = start;
var bestF = maskFitness(best);
var bestF = maskFitness(best, clue_size);
var fails = 0;
while (fails < limit) {
cache.from(best);
var cand = mutate(best);
var f = maskFitness(cand);
var f = maskFitness(cand, clue_size);
if (f < bestF) {
best = cand;
bestF = f;
@@ -674,32 +673,32 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
return best;
}
public Clues generateMask(int popSize, int gens, int pairs) {
public Clues generateMask(int clueSize, int popSize, int gens, int offspring) {
class GridAndFit {
Clues grid;
long fite = -1;
GridAndFit(Clues grid) { this.grid = grid; }
long fit() {
if (fite == -1) this.fite = maskFitness(grid);
if (fite == -1) this.fite = maskFitness(grid, clueSize);
return this.fite;
}
}
if (Main.VERBOSE) System.out.println("generateMask init pop: " + popSize);
if (Main.VERBOSE) System.out.println("generateMask init pop: " + popSize + " clueSize: " + clueSize);
var pop = new ArrayList<GridAndFit>();
for (var i = 0; i < popSize; i++) {
pop.add(new GridAndFit(hillclimb(randomMask(), 180)));
pop.add(new GridAndFit(hillclimb(randomMask(clueSize), clueSize, 180)));
}
for (var gen = 0; gen < gens; gen++) {
if (Thread.currentThread().isInterrupted()) break;
var children = new ArrayList<GridAndFit>();
for (var k = 0; k < pairs; k++) {
for (var k = 0; k < offspring; k++) {
var p1 = pop.get(rng.randint(0, pop.size() - 1));
var p2 = pop.get(rng.randint(0, pop.size() - 1));
var child = crossover(p1.grid, p2.grid);
children.add(new GridAndFit(hillclimb(child, 70)));
children.add(new GridAndFit(hillclimb(child, clueSize, 70)));
}
pop.addAll(children);
@@ -707,7 +706,7 @@ public record SwedishGenerator(Rng rng, int[] stack, Clues cache) {
var next = new ArrayList<GridAndFit>();
for (var cand : pop) {
if (next.size() >= popSize) break;
if (next.size() >= offspring) break;
var ok = true;
for (var kept : next) {
if (cand.grid.similarity(kept.grid) > 0.92) {