introduce bitloops
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user