Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # trinary_learning_bot.py
- import random
- from collections import deque
- # random.seed(0)
- # ---------- data (unchanged) ----------
- last = 10_000
- last_added = deque([0] * last, maxlen=last)
- targets = sorted(random.sample(range(1000, 10000), 5000))
- index_of = {v: i for i, v in enumerate(targets)}
- pairs = []
- total_trials = 50_000
- halfway = total_trials // 2
- while len(pairs) < total_trials:
- a, b = random.sample(targets, 2)
- ia, ib = index_of[a], index_of[b]
- if len(pairs) < halfway:
- if ia < ib:
- pairs.append(((a, b), False))
- elif ia > ib:
- pairs.append(((a, b), True))
- random.shuffle(pairs)
- # ---------- learner ----------
- w = {(p, c): 0.0 for p in range(4) for c in (-1, 0, 1)}
- error_buffer = deque(maxlen=20)
- correct_total = 0
- errors = 0
- last_error_at = 0
- # Dedicated RNG only for the replay line
- replay_rng = random.Random(200)
- def key_loop(pair, actual):
- global correct_total, errors, guess, last_error_at
- k1, k2 = pair
- da = [int(d) for d in f"{k1:04d}"]
- db = [int(d) for d in f"{k2:04d}"]
- score = 0.0
- first_diff = None
- for i in range(4):
- c = (da[i] > db[i]) - (da[i] < db[i])
- score += w[(i, c)]
- if c != 0 and first_diff is None:
- first_diff = (i, c)
- guess = score > 0
- ok = guess == actual
- if not ok:
- errors += 1
- last_error_at = trial_index
- error_buffer.append((da, db, actual, first_diff))
- # primary update
- s = 5.0 if actual else -5.0
- if first_diff is not None:
- w[first_diff] += s
- # replay with the tuned seed
- for _ in range(min(2, len(error_buffer))):
- pda, pdb, pactual, pfd = replay_rng.choice(error_buffer)
- pscore = sum(
- w[(i, (pda[i] > pdb[i]) - (pda[i] < pdb[i]))]
- for i in range(4)
- )
- if (pscore > 0) != pactual and pfd is not None:
- w[pfd] += 3.0 if pactual else -3.0
- v = 1 if ok else 0
- if len(last_added) == last:
- correct_total -= last_added[0]
- last_added.append(v)
- correct_total += v
- pct = correct_total / last * 100.0
- return pct, ok
- # ---------- run ----------
- for trial_index, ((k1, k2), actual) in enumerate(pairs):
- pct, ok = key_loop((k1, k2), actual)
- print(f"Trial {trial_index+1:05d}: "
- f"[t1={k1}] vs [t2={k2}] "
- f"guess={'Y' if guess else 'N'} actual={'Y' if actual else 'N'} "
- f"{'CORRECT' if ok else 'WRONG'}")
- print(f"Correct in last 10000: {pct:.2f}% (total errors: {errors})\n")
- if pct >= 100.0 and trial_index >= last:
- print(f"\n*** 100% stable since trial {last_error_at+1} ***")
- print(f"Total errors made: {errors}")
- break
Advertisement
Add Comment
Please, Sign In to add comment