liveBook · Manning

Contextual bandit

As explained in Optimization Algorithms: AI techniques for design, planning, and control problems

A contextual multi-armed bandit is a bandit model in which an agent observes contextual information before choosing an action and receiving action-specific feedback.

A contextual bandit chooses an action after observing context, then receives feedback for the chosen action. Its goal is to earn more reward over repeated choices. Unlike a noncontextual multi-armed bandit, an action’s expected reward can depend on the current context, so the best choice can change. Unlike a Markov decision process, the choice does not require planning through future state transitions. section 12.1 section 12.1.6

Choose a delivery truck

Suppose the route is city or interstate. For each delivery, choose a truck size, engine, and tire type. With three sizes, three engines, and four tire types, there are 36 possible combinations. The reward represents the suitability of the selected combination, with added noise for uncertain conditions. The intended decision is the best combination for the observed route—not one fixed truck for every route. section 12.5

A practical selection loop is:

  • Generate an example for the shared route context and each available truck combination. section 12.5
  • Predict a probability distribution over combinations and sample an action from it. Sampling leaves room to explore rather than always taking the current favorite. section 12.5
  • Observe the chosen truck’s reward and use that feedback to continue training. Repeat, then test a choice for a specified route. section 12.1.6 section 12.5
def generate_combinations(shared_context, size_types, engine_types, tire_types):
    examples = [f"shared |User {shared_context}"]
    descriptions = []
    for i, size in enumerate(size_types):
        for j, engine in enumerate(engine_types):
            for k, tire in enumerate(tire_types):
                examples.append(f"|Action truck_size={size} engine={engine} 
                     tire={tire}")
                descriptions.append((i, j, k))
    return examples, descriptions

The action-generation listing shows how one shared context is paired with every size, engine, and tire combination before prediction. section 12.5

The Python implementation uses Vowpal Wabbit’s contextual-bandit exploration mode, an epsilon setting of 0.2, and PyTorch to sample from predicted probabilities. Try changing the available options or exploration setting and observing the resulting rewards. Check the reward signal before interpreting route-specific results: the displayed toy reward function accepts the route context but calculates reward from truck-option values without using that context. As written, it cannot demonstrate that different routes favor different trucks. section 12.5

How Optimization Algorithms: AI techniques for design, planning, and control problems covers it

How deep it goes
Works through action generation, probability sampling, training, and testing; the displayed reward function stops short of using route context.
Examples
Builds a city-or-interstate truck selector from size, engine, and tire combinations, with noisy rewards and a route-specific test.
What it assumes
Defines bandit distinctions before the Python example; states no formal prerequisites.
What you can do after
Apply it via Vowpal Wabbit to generate candidate actions, sample a choice, collect its reward, and test a truck selection.
How it connects
Connects multi-armed bandits, exploration, rewards, and combinatorial optimization; contrasts bandit decisions with Markov decision processes.

Also explained in

Compare how 3 books explain contextual bandit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Optimization Algorithms: AI techniques for design, planning, and control problems. The section links point to the parts of the book it draws on.