Anton Hulan-Gioro
Anton Hulan-Gioro

Reputation: 3

How to create an algorithm for this budget allocation problem with Python?

I want to create an automation algorithm for this budget thing. If you spend 10k on display, you can get 500, if you spend the same amount on paid search, you can get 1000. if you spend 20k on display you can get 900 downloads, on paid search you can get 2000 downloads. picture is here please checkHow do I get the maxium downloads with any amount of budget given.

def maximize_downloads(budget, options): # Initialize variables max_downloads = 0 best_option = None

# Loop through all options
for option in options:
    cost, downloads = option
    
    # Check if option fits within budget
    if cost <= budget:
        # Check if this option gives more downloads than previous ones
        
        
        if downloads > max_downloads:
            max_downloads = downloads
            best_option = option
          
# Return the best option and the number of downloads it provides
return best_option, max_downloads

options = [(10000, 500), (10000, 1000), (20000, 900), (20000, 2000), (30000, 1200), (30000, 2500), (40000, 1450), (40000, 2700), (50000, 1600), (50000, 2850)]

budget = 35000

best_option, max_downloads = maximize_downloads(budget, options)

print(f"The best option is {best_option} with {max_downloads} downloads")

Upvotes: 0

Views: 128

Answers (1)

KingOtto
KingOtto

Reputation: 1483

You are facing a two-step optimization problem: First, get the max. possible number of downloads for your budget. Second, among all those options, get the one with the lowest possible cost.

  • Bring list down to those elements you can afford (1st arg in tuple must be <= budget)
  • Of those elements, get the maximum (-> max_downloads)
  • For this maximum, find the one with the lowest cost (-> best_option)

Attention: You may not be able to determine at which position in your options list that tuple is sitting, as you might have multiple equal tupels, i.e., same no. of downloads with same cost.

def maximize_downloads(budget, options):    
    
    max_downloads = max([downloads for cost, downloads in options if cost <= budget])
    best_option = min([cost for cost, downloads in options if downloads == max_downloads])
    
    return best_option, max_downloads

budget = 35000
options = [(10000, 500), (10000, 1000), (20000, 900), (20000, 2000), (30000, 1200), (30000, 2500), (40000, 1450), (40000, 2700), (50000, 1600), (50000, 2850)]

print(maximize_downloads(budget, options))
>> (30000, 2500)

Upvotes: 0

Related Questions