← All projects

AI / ML

RLinear Search

A reinforcement-learning take on a search-and-rescue problem that a published paper solves algorithmically. A Q-learning agent learns to find a moving target on a semi-infinite line and bring it back to base, with a live Tkinter visualizer. It trains over 500 episodes, then runs 100 test rounds and reports the average competitive ratio.

RLinear Search visualizer showing the agent, target and base on a number line during a test round
Type
Solo project
Category
AI / ML
Stack
Python · Gymnasium · NumPy · Tkinter · Codex
How the parts fit together
Architecture diagram: main.py to Train / test loop (start); Train / test loop and InfiniteLinearSearchEnv (action ⇄ reward); Train / test loop and Q-learning agent (state ⇄ action); Train / test loop to EnvironmentVisualizer (draw); Q-learning agent and Q-table file (save / load)

The loop runs episodes: the environment returns an observation, the agent picks an action from its Q-table, and the visualizer draws each step. Training saves the Q-table; testing loads it.

SheetStructure
ProjectRLinear Search
Parts6

How the agent learns: Q-Learning explained

Step 1 of 7

1 · The challenge

Imagine a line that never ends, with a base at one end. The goal is for an agent to start at the base, search for the target, and bring it back. Think of it as a rescue: the target is someone who needs to be found and brought home.

That sounds simple to us, because we can see the whole line. But the agent can’t. It doesn’t know where the target is, and the target keeps moving, drifting back and forth along the line, so the agent has to figure out how to find it.

∞ Base (0) the target is somewhere in here ? it may drift back and forth Agent

2 · What it can see

Picture a real robot on this line. It has no map, and it can’t see the target from a distance. It only finds the target when it’s practically on top of it. All it has is its own instruments, and they’re rough. At any moment it knows just five things.

Together, those five readings make up the agent’s state: a snapshot of its situation at that moment. Everything it learns is attached to a state.

Agent Which way am I facing? left or right How far right have I been? rounded to the nearest 50 Have I found the target? yes or no How far from base am I? in rough zones What stage am I in? search out · head back · search on · rescue

3 · Picking a move

Each step, the agent can go left, go right, or turn back toward base. After every move it notes the state it was in and how much score the move earned, so it learns which move is best in each state. Most of the time it takes that move. But some of the time it tries something random to see how good it is, like trying a new restaurant. At first that’s 1 move in 5, settling to 1 in 10.

← Step left → Step right ↩ Turn back At the start, out of every 10 moves: 8 best-rated, 2 new Later in training: 9 best-rated, 1 new

4 · Getting a score

Remember the score the agent notes after every move? Here’s where it comes from. Wasting time costs a little, and making progress earns a little. Finding the target earns a lot, and bringing it home earns the most. Nobody tells the agent how to search. It just chases a higher score.

Every step a small cost for wasting time −0.1 Making progress new ground, heading the right way +0.2 to +0.5 Finding the target +500 Bringing it home +1000 Bar lengths are compressed so the small scores are visible.

5 · Updating its notes

The agent keeps its notes as a cheat sheet: for every state, a rating for each move. Each rating is called a Q-value, and the Q stands for quality, meaning how good that move is in that state. After each move, the agent compares the score it got with what it expected, then nudges that rating up or down. Pleasant surprises raise it, and unpleasant ones lower it. Learning these quality ratings is what gives Q-learning its name.

Q-values (quality ratings) illustrative numbers step leftstep rightturn back Searching, far from base −0.4 2.0 → 2.6 0.3 Found it, heading home 9.4 −0.8 0.2 Back at base, searching 0.1 3.5 1.0 It stepped right and the result beat its expectation, so that rating nudges up.

6 · Practice

Training is a loop. At every step the agent looks at its state, makes a move, gets a score, and updates its notes. Then it starts over. That whole cycle is one step.

A run of steps, from the base until the target is home (or 5,000 steps run out), is called an attempt. The agent makes 500 attempts. As it improves, it gets less curious and updates its notes more gently.

0% 50% 100% 0100200300400500 attempts compared with where each one started curiosity learning speed Curiosity stops shrinking around attempt 70, at half its start (1 in 10). Learning speed keeps easing down until about attempt 358.

7 · The test

After training, curiosity is switched off and the agent runs 100 test rounds, always taking its best-rated move. Each round is scored against a perfect route: straight out to the target and straight back. A score of 1 means no wasted steps, and 2 means twice as many as needed.

No one told the agent how to search. Everything it knows came from the scores it earned along the way, one move at a time.

Perfect route target The agent’s route (illustration) Score = steps taken ÷ steps the perfect route needs 1 · perfect 2 · twice as long 3 · three times

Want the technical version? Open the assistant and ask “How does the RLinear Search agent decide what action to take?”

Approach

  • Built a custom Gymnasium environment, InfiniteLinearSearchEnv, with configurable target range, optional target movement (zigzag, with set probability and speed) and step limits
  • Structured the task into phases the agent learns to move through: initial exploration, return to base, full exploration, and rescue
  • Implemented a tabular Q-learning agent with epsilon-greedy exploration, where the exploration and learning rates both decay over 500 training episodes. The codebase also includes a phase-biased exploration policy and an experience-replay trainer
  • Shaped the reward: a small per-step penalty, bonuses for visiting new regions, +500 for finding the target, +1000 for completing the rescue, and penalties for counter-productive moves
  • Built a Tkinter visualizer showing the agent, target, path and live metrics, with dynamic scaling and panning; test mode runs many episodes and reports the average competitive ratio
  • Saved Q-tables to disk so trained agents can be reloaded and tested later. Built the environment and visualizer with Codex, validating results against the paper

Highlights

  • Recreates a published algorithmic approach using reinforcement learning
  • Optional moving target with configurable speed and probability
  • Real-time visualization of training and test episodes
  • Reports the average competitive ratio over many test runs

Ask my site

Ask about RLinear Search.

Answers come only from the content on this site. If it isn't here, it'll say so and point you to my email.

Answers are generated from this site's content, so double-check anything important.

Psst, this page has a cheat code.