← Lesson
Rewards and Policies
Joeven
Run
Reset
Python loads on first run
import random # Gold tool in each state. Reward +1 only if the first action matches. states = ["weather", "revenue", "users", "docs"] gold = {"weather": "search", "revenue": "sql", "users": "sql", "docs": "search"} actions = ["search", "sql"] def run(policy, n=200, seed=0): rng = random.Random(seed) total = 0.0 for _ in range(n): s = rng.choice(states) a = policy(s, rng) total += 1.0 if a == gold[s] else 0.0 return total / n def always_search(s, rng): return "search" def random_policy(s, rng): return rng.choice(actions) def keyword_policy(s, rng): if s in {"revenue", "users"}: return "sql" return "search" print("always search reward", run(always_search)) print("random reward", run(random_policy)) print("keyword policy reward", run(keyword_policy))
Run to execute this in your browser. Nothing is sent to a server.