Making Decisions with Ranker
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 also need to track the decision with the RewardTracker
and reward it for the amount of profit if a purchase is made. In this way, the Ranker will learn to rank the most profitable discount first.
Using the Ranker class
First, let’s create an instance of the Ranker
class.
from improveai import Ranker
ranker = Ranker(model_url)
Ranking items
To rank items, you can call the rank
method of the Ranker
class. This method accepts a list of items and an optional context.
discounts = [.10, .15, .20, .25, .30]
ranked_discounts = ranker.rank(discounts)
Selecting the highest-ranking discount
Now that we have the ranked discounts, we can choose the highest-ranking discount.
best_discount = ranked_discounts[0]
Tracking the decision with RewardTracker
Next, we need to track our decision using the RewardTracker
class.
Creating a RewardTracker instance
First, create a RewardTracker
instance with the required arguments.
from improveai import RewardTracker
tracker = RewardTracker("discounts", track_url)
Tracking the decision
Now we can track our decision using the track
method of the RewardTracker
class. This method accepts the selected item, a list of candidates, and an optional context.
reward_id = tracker.track(best_discount, discounts)
Rewarding the decision
Once a purchase is made, we can add a reward to the decision using the add_reward
method of the RewardTracker
class. This method requires a reward
value and a reward_id
that was obtained when tracking the chosen discount.
reward = profit_amount
tracker.add_reward(reward, reward_id)
Improve AI will automatically balance exploring new discounts with doubling down on the discounts expected to be most profitable. Over time and as more decisions are made, the model will learn to choose the optimal discount.
Conclusion
In this tutorial, we covered how to use the Ranker
class to make decisions, such as choosing a discount from a list of discounts. We also learned how to track the decision with the RewardTracker
and reward it based on the profit.
Getting Started
Improve AI is available for Python, Swift, and Java. See the Quick-Start Guide to learn more.
Thank you for your efforts to improve the world a little bit today.