|
|
| import json |
| import random |
| import numpy |
| import pickle |
|
|
| n = 17 |
| k_offset = 5 |
| num_train = 5000000 |
| num_test = 1000 |
|
|
| |
| |
| pos_const = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2] |
|
|
|
|
|
|
| import random |
| from typing import List, Tuple, Set, Optional |
|
|
| Latin = List[List[int]] |
|
|
| def cyclic_latin_square(n: int = 10) -> Latin: |
| return [[(i + j) % n for j in range(n)] for i in range(n)] |
|
|
| def is_latin_square(L: Latin) -> bool: |
| n = len(L) |
| target = set(range(n)) |
| for i in range(n): |
| if set(L[i]) != target: |
| return False |
| for j in range(n): |
| col = {L[i][j] for i in range(n)} |
| if col != target: |
| return False |
| return True |
|
|
| def _try_random_intercalate_move(L: Latin, rng: random.Random) -> bool: |
| """ |
| Try one random 2x2 intercalate flip. Return True if moved, else False. |
| """ |
| n = len(L) |
| r1, r2 = rng.sample(range(n), 2) |
| c1, c2 = rng.sample(range(n), 2) |
|
|
| a = L[r1][c1] |
| b = L[r1][c2] |
| if a == b: |
| return False |
|
|
| |
| |
| |
| if L[r2][c1] != b or L[r2][c2] != a: |
| return False |
|
|
| |
| |
| |
| L[r1][c1], L[r1][c2] = b, a |
| L[r2][c1], L[r2][c2] = a, b |
| return True |
|
|
| def mcmc_step(L: Latin, rng: random.Random, lazy_p: float = 0.1, max_trials: int = 200) -> None: |
| """ |
| One Markov step: |
| - with probability lazy_p: do nothing (aperiodicity) |
| - else: attempt up to max_trials random intercalate moves; if none found, do nothing |
| """ |
| if rng.random() < lazy_p: |
| return |
| for _ in range(max_trials): |
| if _try_random_intercalate_move(L, rng): |
| return |
| |
|
|
| def sample_latin_square_10( |
| rng: random.Random, |
| burn_in: int = 50_000, |
| steps_after: int = 20_000, |
| lazy_p: float = 0.1, |
| max_trials_per_step: int = 200 |
| ) -> Latin: |
| """ |
| Start from cyclic Latin square and run MCMC. |
| Return one approximately-uniform sample. |
| """ |
| L = cyclic_latin_square(10) |
|
|
| |
| for _ in range(burn_in): |
| mcmc_step(L, rng, lazy_p=lazy_p, max_trials=max_trials_per_step) |
|
|
| |
| for _ in range(steps_after): |
| mcmc_step(L, rng, lazy_p=lazy_p, max_trials=max_trials_per_step) |
|
|
| return [row[:] for row in L] |
|
|
| def make_functions( |
| n: int, |
| seed: Optional[int] = None, |
| burn_in: int = 500000, |
| steps_between_samples: int = 500000, |
| lazy_p: float = 0.1, |
| max_trials_per_step: int = 200 |
| ) -> List[Latin]: |
| """ |
| Generate n distinct 10x10 Latin squares via MCMC (approx uniform). |
| Distinctness is enforced by hashing full matrices. |
| """ |
| rng = random.Random(seed) |
| out: List[Latin] = [] |
| seen: Set[Tuple[Tuple[int, ...], ...]] = set() |
|
|
| |
| L = cyclic_latin_square(10) |
|
|
| |
| for _ in range(burn_in): |
| mcmc_step(L, rng, lazy_p=lazy_p, max_trials=max_trials_per_step) |
|
|
| while len(out) < n: |
| |
| for _ in range(steps_between_samples): |
| mcmc_step(L, rng, lazy_p=lazy_p, max_trials=max_trials_per_step) |
|
|
| sample = [row[:] for row in L] |
| key = tuple(tuple(row) for row in sample) |
| if key in seen: |
| continue |
| |
| if not is_latin_square(sample): |
| raise RuntimeError("Internal error: produced a non-Latin square (should not happen).") |
|
|
| seen.add(key) |
| out.append(sample) |
|
|
| return out |
|
|
| functions = make_functions(n=n) |
|
|
| print(functions) |
|
|
| |
|
|
| def generate_samples_anchored_global(num_samples): |
| samples = [] |
| |
| vocab = '0123456789' |
| for _ in range(num_samples): |
| |
| plain_digits = [random.randint(0, 9) for _ in range(n)] |
| |
| cipher_digits = [0] * n |
| |
| |
| |
| cipher_digits[0] = plain_digits[0] |
| |
| |
| for i in range(1, n): |
| |
| j = (i + k_offset) % n |
| val = functions[i][plain_digits[i]][plain_digits[j]] |
| |
| cipher_digits[i] = val |
|
|
| |
| |
| |
| |
| |
| |
| samples.append(cipher_digits+plain_digits) |
| |
| |
| return numpy.array(samples,dtype=numpy.uint16) |
|
|
| |
| train_samples = generate_samples_anchored_global(num_train) |
|
|
| print(train_samples[0]) |
| |
| train_samples.tofile('train.bin') |
|
|
|
|
| |
| |
| |
|
|
| test_samples = generate_samples_anchored_global(num_test) |
| test_samples.tofile('test.bin') |
| |
| |
| |
|
|
| print(f"Generated {num_train} train samples and {num_test} test samples for ANCHORED GLOBAL (mod 10) task.") |
| print(f"n={n}, k_offset={k_offset}") |
|
|
| meta = { |
| 'vocab_size': 11, |
| 'block_size': n * 2, |
| 'functions': functions |
| } |
| with open('meta.pkl', 'wb') as f: |
| pickle.dump(meta, f) |
|
|
|
|
|
|
| |
| |
| print("\n--- Verification Sample ---") |
| if test_samples is None: |
| print("No test samples generated for verification.") |
| else: |
|
|
| for t in range(len(test_samples)): |
| c_str = test_samples[0,0:n] |
| p_str = test_samples[0,n:2*n] |
| |
| |
|
|
| |
| c = [int(x) for x in c_str] |
| p_actual = [int(x) for x in p_str] |
| p_solved = [-1] * n |
|
|
| |
| p_solved[0] = c[0] |
| |
|
|
| |
| |
| |
| |
| |
| |
| solve_order = [12, 7, 2, 14, 9, 4, 16, 11, 6, 1, 13, 8, 3, 15, 10, 5] |
| |
| for i_solve in solve_order: |
| |
| i_depend_on = (i_solve + k_offset) % n |
| |
| val = -1 |
| for j in range(10): |
| if functions[i_solve][j][p_solved[i_depend_on]] == c[i_solve]: |
| val = j |
| break |
| if(val == -1): |
| print("Oh no, what happens!") |
| |
| |
| p_solved[i_solve] = val |
| |
|
|
| |
| |
| |
| if (p_str == numpy.array(p_solved)).all(): |
| |
| continue |
| else: |
| print("Verification FAILED.") |
| print(xxxxx) |