Improve AI Basics - Ranking

painting of a dog trying to decide which button to push
"Monet painting of athletes on a podium in first, second, third place" by DALL-E

Ranking is a fundamental pattern in modern software development. From social media feeds, to product recommendations, to search engine results, it is often necessary to rank results before we display them.


Ranking can easily be performed with a single line of code:

wineRanking = sommelierModel.rank(wines)

DecisionModel.rank() is the fastest way to rank a large list of variants. It simply computes a score for each variant, then sorts the variants by their scores.

Just as with a normal decision, after the ranking is returned, rewards are assigned via addReward().

if (success) {

As with which(), the context is taken into consideration when making the ranking.

wineRanking = sommelierModel.given(entree).rank(wines)

On iOS and Android, contextual data such as operating system, country, language, time of day, and more is automatically included. Custom context can also be provided via given().

Ranking and Decisions

rank() is closely related to decisions made via which(), in fact, the following two statements are equivilent:

best = model.which("a", "b", "c")


best = model.rank(["a", "b", "c"])[0]

This equivilence means that a model trained on decisions from which() can also be used for rank() and vice versa.

“If you ain’t first you’re last.” - Ricky Bobby

Like which(), rank() only cares about the top position. The tradeoff for speed is that it doesn’t consider the relationships between the items themselves, and yet this simple pattern often performs quite well in practice.


Re-ranking is a powerful design pattern that allows seperation of concerns between content recommendation systems and optimizing the final display for the end user.

Rather than just competing for the top spot, re-ranking typically also considers the relationships between the items and their relative positions.

There are many different re-ranking algorithms, from listwise approaches with combinatorial complexity to pairwise approaches that essentially quicksort pairs of decisions. All of these can use the core foundation of which() and rank() to build higher level algorithms.

We will be writing more about re-ranking in the future, but in the mean time please contact [email protected] with any questions.

Recent Posts

2 minute read

Making Decisions with Ranker

1 minute read

In this tutorial, we’ll walk through how to use the Ranker class to make decisions, specifically choosing a discount to offer from a list of discounts. We’ll...