here2share

# trinary_learning_bot.py

Oct 26th, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. # trinary_learning_bot.py
  2.  
  3. import random
  4. from collections import deque
  5.  
  6. # random.seed(0)
  7.  
  8. # ---------- data (unchanged) ----------
  9. last = 10_000
  10. last_added = deque([0] * last, maxlen=last)
  11. targets = sorted(random.sample(range(1000, 10000), 5000))
  12. index_of = {v: i for i, v in enumerate(targets)}
  13.  
  14. pairs = []
  15. total_trials = 50_000
  16. halfway = total_trials // 2
  17. while len(pairs) < total_trials:
  18.     a, b = random.sample(targets, 2)
  19.     ia, ib = index_of[a], index_of[b]
  20.     if len(pairs) < halfway:
  21.         if ia < ib:
  22.             pairs.append(((a, b), False))
  23.     elif ia > ib:
  24.         pairs.append(((a, b), True))
  25. random.shuffle(pairs)
  26.  
  27. # ---------- learner ----------
  28. w = {(p, c): 0.0 for p in range(4) for c in (-1, 0, 1)}
  29. error_buffer = deque(maxlen=20)
  30. correct_total = 0
  31. errors = 0
  32. last_error_at = 0
  33.  
  34. # Dedicated RNG only for the replay line
  35. replay_rng = random.Random(200)
  36.  
  37. def key_loop(pair, actual):
  38.     global correct_total, errors, guess, last_error_at
  39.     k1, k2 = pair
  40.     da = [int(d) for d in f"{k1:04d}"]
  41.     db = [int(d) for d in f"{k2:04d}"]
  42.  
  43.     score = 0.0
  44.     first_diff = None
  45.     for i in range(4):
  46.         c = (da[i] > db[i]) - (da[i] < db[i])
  47.         score += w[(i, c)]
  48.         if c != 0 and first_diff is None:
  49.             first_diff = (i, c)
  50.  
  51.     guess = score > 0
  52.     ok = guess == actual
  53.  
  54.     if not ok:
  55.         errors += 1
  56.         last_error_at = trial_index
  57.         error_buffer.append((da, db, actual, first_diff))
  58.  
  59.         # primary update
  60.         s = 5.0 if actual else -5.0
  61.         if first_diff is not None:
  62.             w[first_diff] += s
  63.  
  64.         # replay with the tuned seed
  65.         for _ in range(min(2, len(error_buffer))):
  66.             pda, pdb, pactual, pfd = replay_rng.choice(error_buffer)
  67.             pscore = sum(
  68.                 w[(i, (pda[i] > pdb[i]) - (pda[i] < pdb[i]))]
  69.                 for i in range(4)
  70.             )
  71.             if (pscore > 0) != pactual and pfd is not None:
  72.                 w[pfd] += 3.0 if pactual else -3.0
  73.  
  74.     v = 1 if ok else 0
  75.     if len(last_added) == last:
  76.         correct_total -= last_added[0]
  77.     last_added.append(v)
  78.     correct_total += v
  79.     pct = correct_total / last * 100.0
  80.  
  81.     return pct, ok
  82.  
  83. # ---------- run ----------
  84. for trial_index, ((k1, k2), actual) in enumerate(pairs):
  85.     pct, ok = key_loop((k1, k2), actual)
  86.     print(f"Trial {trial_index+1:05d}: "
  87.           f"[t1={k1}] vs [t2={k2}]  "
  88.           f"guess={'Y' if guess else 'N'} actual={'Y' if actual else 'N'}  "
  89.           f"{'CORRECT' if ok else 'WRONG'}")
  90.     print(f"Correct in last 10000: {pct:.2f}%   (total errors: {errors})\n")
  91.     if pct >= 100.0 and trial_index >= last:
  92.         print(f"\n*** 100% stable since trial {last_error_at+1} ***")
  93.         print(f"Total errors made: {errors}")
  94.         break
Advertisement
Add Comment
Please, Sign In to add comment