Exploration and tabular Q-learning
Quick answer
Q-learning estimates optimal action values from sampled transitions. An epsilon-greedy behavior policy sometimes chooses random actions to gather evidence and otherwise chooses a highest-value action. Q-learning uses a greedy continuation target even when its behavior explores. Examples are static Python snippets tested locally on CPU; Only Python standard-library modules are required. ByteMind execution is unverified.
One Q-learning update
from fractions import Fraction as Fq=F(3);target=-1+F(1,2)*max([4,8])print(target,q+F(1,4)*(target-q))Expected output (locally tested): 3 3. The sampled target happens to equal the old estimate.
How it works
Tabular Q(s,a) stores one value for each finite state-action pair. On a transition (s,a,r,s_next), target = r + gammamax_b Q(s_next,b) for a nonterminal next state; terminal target is r. TD error = target - Q(s,a), and update Q(s,a) += alphaTD error, with alpha the learning rate. Only that entry changes. Off-policy means the target policy differs from behavior; epsilon-greedy with uniform random exploration selects the unique greedy action with probability 1-epsilon+epsilon/|A|. A deterministic tie rule makes tests reproducible; random tie breaking can improve exploration. Decaying epsilon too quickly leaves some actions unvisited. Exact convergence claims require finite stationary MDPs, sufficient repeated visits, appropriate diminishing step sizes and discount conditions, not merely running a few episodes. Fixed alpha can be useful for tracking but does not imply exact convergence. Distinguish termination from external truncation: bootstrap from final observed next state at an external cap, then reset for collection. Never bootstrap from the reset observation. Evaluation freezes the learned table and disables exploration for a greedy-policy test. Report multiple seeds and capped episode outcomes; reward variance can persist after convergence because transitions are stochastic.
Epsilon-greedy probability
from fractions import Fraction as Fe=F(1,5);n=4print(1-e+e/n,e/n)Expected output (locally tested): 17/20 1/20. The unique greedy action can also be drawn during exploration.
Common mistakes
Do not update all actions in a state, use the reset state as successor, or claim every fixed-learning-rate run converges. Do not explore in final evaluation unless that stochastic policy is the intended target.
Practice and next steps
Given old Q=3, reward=-1, next values [4,8], gamma=0.5 and alpha=0.25, compute the TD target and updated value; repeat for a true terminal transition.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Exploration and tabular Q-learning — knowledge check
Apply exploration and tabular q-learning to small examples and debugging scenarios.
What policy is used in the Q-learning target?
What does alpha control?
Why explore?
Which next observation is valid after external truncation?
Does fixed alpha ensure exact convergence?