introduce bitloops

This commit is contained in:
mike
2026-01-18 01:29:46 +01:00
parent 20617cda57
commit 112c16c525
7 changed files with 262 additions and 105 deletions

View File

@@ -44,12 +44,12 @@ public class Main {
@NoArgsConstructor
public static class Opts {
static int SSIZE = 20;
static int SSIZE = 25;
public int seed = (int) (System.nanoTime() ^ System.currentTimeMillis());
public int clueSize = SSIZE;
public int pop = SSIZE * 2;
public int offspring = SSIZE * 3;
public int gens = 600;
public int pop = SSIZE*2;
public int offspring = SSIZE*3;
public int gens =1200;
public String wordsPath = "nl_score_hints_v3.csv";
public double minSimplicity = 0; // 0 means no limit
public int threads = Math.max(1, Runtime.getRuntime().availableProcessors());
@@ -368,11 +368,12 @@ public class Main {
try {
return _attempt(rng, dict, opts);
} catch (Exception e) {
e.printStackTrace();
System.err.println("Failed to operate" + e.getMessage());
return null;
}
}
static Clues generateClues() {
static Clued generateClues() {
String simple = "000 3000\n" +
" 3 \n" +
" 31 \n" +

View File

@@ -2,10 +2,13 @@ package puzzle;
import lombok.AllArgsConstructor;
import lombok.val;
import puzzle.Export.Clued;
import puzzle.Export.Gridded;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Objects;
import java.util.stream.IntStream;
import static java.lang.Long.*;
@@ -48,8 +51,8 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
long stop = 1L << msb;
rayLo &= -(stop << 1);
}
visitor.visit(key, rayLo, rayHi);
// if (Long.bitCount(rayLo) + Long.bitCount(rayHi) > 1)
visitor.visit(key, rayLo, rayHi);
}
private static void processSlot(Clues c, SlotVisitor visitor, int key) {
long rayLo = PATH_LO[key];
@@ -66,14 +69,13 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
// keep all lo (lo indices are < any hi index), but cut hi below stop
rayHi &= (stop - 1);
}
visitor.visit(key, rayLo, rayHi);
// if (Long.bitCount(rayLo) + Long.bitCount(rayHi) > 1)
visitor.visit(key, rayLo, rayHi);
}
public static Slot[] extractSlots(Clues grid, DictEntry[] index) {
var slots = new Slot[grid.clueCount()];
int[] N = new int[]{ 0 };
grid.forEachSlot((key, lo, hi) -> slots[N[0]++] = Slot.from(key, lo, hi, index[Slot.length(lo, hi)]));
return slots;
var slots = new ArrayList<Slot>(grid.clueCount());
grid.forEachSlot((key, lo, hi) -> slots.add(Slot.from(key, lo, hi, Objects.requireNonNull(index[Slot.length(lo, hi)]))));
return slots.toArray(Slot[]::new);
}
public static Slotinfo[] slots(Clues mask, DictEntry[] index) {
var slots = Masker.extractSlots(mask, index);
@@ -102,6 +104,7 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
public long maskFitness(final Clues grid, int clueSize) {
long cHLo = 0L, cHHi = 0L, cVLo = 0L, cVHi = 0L;
long cHLo2 = 0L, cHHi2 = 0L, cVLo2 = 0L, cVHi2 = 0L;
long lo_cl = grid.lo, hi_cl = grid.hi;
long penalty = (((long) Math.abs(grid.clueCount() - clueSize)) * 16000L);
boolean hasSlots = false;
@@ -130,9 +133,11 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
if ((rLo | rHi) != X) {
hasSlots = true;
if (Slot.horiz(key)) {
cHLo2 |= (cHLo & rLo); cHHi2 |= (cHHi & rHi);
cHLo |= rLo;
cHHi |= rHi;
} else {
cVLo2 |= (cVLo & rLo); cVHi2 |= (cVHi & rHi);
cVLo |= rLo;
cVHi |= rHi;
}
@@ -163,9 +168,11 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
if ((rLo | rHi) != X) {
hasSlots = true;
if (Slot.horiz(key)) {
cHLo2 |= (cHLo & rLo); cHHi2 |= (cHHi & rHi);
cHLo |= rLo;
cHHi |= rHi;
} else {
cVLo2 |= (cVLo & rLo); cVHi2 |= (cVHi & rHi);
cVLo |= rLo;
cVHi |= rHi;
}
@@ -241,8 +248,9 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
boolean h = (cHLo & (1L << clueIdx)) != X;
boolean v = (cVLo & (1L << clueIdx)) != X;
if (!h && !v) penalty += 1500;
else if (h && v) { /* ok */ } else if (h | v) penalty += 200;
else penalty += 600;
else if (h && v) { /* ok */ }
else if (((h ? cHLo2 : cVLo2) & (1L << clueIdx)) != X) penalty += 600;
else penalty += 200;
}
for (long bits = ~hi_cl & MASK_HI; bits != X; bits &= bits - 1) {
int clueIdx = numberOfTrailingZeros(bits);
@@ -251,8 +259,9 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
boolean h = (cHHi & (1L << clueIdx)) != X;
boolean v = (cVHi & (1L << clueIdx)) != X;
if (!h && !v) penalty += 1500;
else if (h && v) { /* ok */ } else if (h | v) penalty += 200;
else penalty += 600;
else if (h && v) { /* ok */ }
else if (((h ? cHHi2 : cVHi2) & (1L << clueIdx)) != X) penalty += 600;
else penalty += 200;
}
return penalty;
@@ -329,7 +338,7 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
for (var h = c.hi & ~c.rhi & ~c.vhi; h != X; h &= h - 1) clearCluesHi(c, numberOfTrailingZeros(h), 0);
for (var h = c.hi & ~c.rhi & c.vhi; h != X; h &= h - 1) clearCluesHi(c, numberOfTrailingZeros(h), 1);
for (var h = c.hi & c.rhi & ~c.vhi; h != X; h &= h - 1) clearCluesHi(c, (numberOfTrailingZeros(h)), 2);
for (var h = c.hi & c.rhi & ~c.vhi; h != X; h &= h - 1) clearCluesHi(c, (numberOfTrailingZeros(h)), 3);
for (var h = c.hi & c.rhi & c.vhi; h != X; h &= h - 1) clearCluesHi(c, (numberOfTrailingZeros(h)), 3);
return c;
}
@@ -421,7 +430,7 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
long lo, hi, vlo, vhi, rlo, rhi;
public static Clues createEmpty() { return new Clues(0, 0, 0, 0, 0, 0); }
public static Clues parse(String s) {
public static Clued parse(String s) {
var c = createEmpty();
var lines = s.split("\n");
for (int r = 0; r < Math.min(lines.length, R); r++) {
@@ -436,7 +445,7 @@ public record Masker(Rng rng, int[] stack, Clues cache) {
}
}
}
return c;
return new Clued(c);
}
public boolean cluelessLo(int idx) {
if (!isClueLo(idx)) return false;

View File

@@ -45,7 +45,6 @@ public class Meta {
val parts = string.split("\t", 3);
return new ShardLem(Lemma.from(parts[0]), Integer.parseInt(parts[1]), GSON.fromJson(parts[2], String[].class));
} catch (Exception e) {
e.printStackTrace();
return new ShardLem(Lemma.from("XXX"), -1, new String[0]);
}
}

View File

@@ -48,8 +48,8 @@ public record SwedishGenerator() {
public static final long MASK_HI = (SIZE <= 64) ? 0L : (SIZE >= 128 ? -1L : (1L << (SIZE - 64)) - 1);
public static final int MAX_WORD_LENGTH = C <= R ? C : R;
public static final int MAX_WORD_LENGTH_PLUS_ONE = MAX_WORD_LENGTH + 1;
public static final int MIN_LEN = Config.MIN_LEN;
public static final int MAX_TRIES_PER_SLOT = Config.MAX_TRIES_PER_SLOT;
public static final int MIN_LEN = 3;//Config.MIN_LEN;
public static final int MAX_TRIES_PER_SLOT = 700;//Config.MAX_TRIES_PER_SLOT;
public static final int STACK_SIZE = 64;
public static final char C_DASH = '\0';
public static final byte DASH = (byte) C_DASH;
@@ -194,6 +194,14 @@ public record SwedishGenerator() {
return k;
}
public static boolean increasing(int dir) { return (dir & 2) == 0; }
public static Grid grid(Slotinfo[] slots) {
long lo = X, hi = X;
for (var slot : slots) {
lo |= slot.lo;
hi |= slot.hi;
}
return new Grid(new byte[SIZE], ~lo, ~hi /*& 0xFF*/);
}
}
public static boolean isLo(int n) { return (n & 64) == 0; }
@@ -318,8 +326,7 @@ public record SwedishGenerator() {
int idx;
for (long b = lo & glo; b != X; b &= b - 1) if (g[idx = numberOfTrailingZeros(b)] != Lemma.byteAt(w, bitCount(lo & ((1L << idx) - 1)))) return false;
int bcLo = bitCount(lo);
for (long b = hi & ghi; b != X; b &= b - 1)
if (g[64 | (idx = numberOfTrailingZeros(b))] != Lemma.byteAt(w, bcLo + bitCount(hi & ((1L << idx) - 1)))) return false;
for (long b = hi & ghi; b != X; b &= b - 1) if (g[64 | (idx = numberOfTrailingZeros(b))] != Lemma.byteAt(w, bcLo + bitCount(hi & ((1L << idx) - 1)))) return false;
long maskLo = lo & ~glo, maskHi = hi & ~ghi;
if ((maskLo | maskHi) != X) {
@@ -393,9 +400,9 @@ public record SwedishGenerator() {
for (var t = 0; t < tries; t++) {
//var r = rng.nextFloat();
//var idxInArray = (int) (r * r * r * (L - 1));
int idxInArray = rng.biasedIndexPow3(L - 1);
var w = entry.words[idxs[idxInArray/*(int) (r * r * r * (L - 1))*/]];
var lemIdx = Lemma.unpackIndex(w);
int idxInArray = rng.biasedIndexPow3(L - 1);
var w = entry.words[idxs[idxInArray/*(int) (r * r * r * (L - 1))*/]];
var lemIdx = Lemma.unpackIndex(w);
if (Bit1029.get(used, lemIdx)) continue;
low = glo;
top = ghi;