liveBook · Manning

Greedy algorithm

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

Greedy decoding constructs an output sequence by selecting the locally highest-scoring available choice at each decoding step.

A greedy algorithm builds a solution by making locally preferred choices as it goes. In cargo-bike loading, items are ranked by profit per kilogram and added while the 100 kg capacity allows. section 2.3

Cargo-bike loading

Adding items in efficiency order produces a feasible load of 92.17 kg and $100.62 profit. This is not optimal: replacing item 9 with item 5 raises profit to $102.22 while staying under capacity. section 2.3

Greedy decoding also selects how a trained model generates a TSP solution during inference. The resulting tour can be compared with the Concorde optimum; a greedy choice strategy does not itself guarantee an optimal solution. section 11.7 section 2.3

model, model_args = load_model(opts.model, extra_logging=True) #1
model.to(opts.device)
  
if isinstance(model, DataParallel):                            #2
    model = model.module                                       #2
  
model.set_decode_type("greedy")                                #3
  
model.eval()                                                   #4

The listing shows a pretrained PyTorch model being configured to use greedy decoding for evaluation. section 11.7

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

How deep it goes
Works through a constrained loading example and model configuration, but does not establish optimality guarantees.
Examples
Uses profit-per-kilogram cargo loading and greedy decoding of a pretrained model on a TSP instance.
What you can do after
Operate a pretrained PyTorch model by configuring greedy decoding for TSP inference.
How it connects
Connects greedy choice to heuristics, the knapsack problem, the traveling salesman problem, and supervised learning.

Also explained in

Compare how 12 books explain greedy algorithm →

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.