I'm looking to maximize the number of stars given a certain budget and max limit on the combination.
Example question:
With a budget of 500 euro, visiting only the maximum allowed restaurants or less, dine and collect the most stars possible.
I'm looking to write an efficient algorithm, that could potentially process 1 million Restaurant instances for up to 10 max Restaurants.
Note, this is a cross post from a question I asked yesterday: Java: Get the most efficient combination of a large List of objects based on a field
The solution below will assign 15$ per star to the r8
Restaurant, which means that when generating the list, it puts that into the list first, and with the remaining 70$ it can only get 2 more stars giving a total of 4 stars. However, if it was smart enough to skip the r8
restaurant ( even though it's the best dollar per star ratio ) the r1
restaurant would actually be a better choice for the budget, as it's 100$ cost and 5 stars.
Can anyone help attempt the problem and beat the current solution?
import itertools
class Restaurant():
def __init__(self, cost, stars):
self.cost = cost
self.stars = stars
self.ratio = cost / stars
def display(self):
print("Cost: $" + str(self.cost))
print("Stars: " + str(self.stars))
print()
r1 = Restaurant(100, 5)
r2 = Restaurant(140, 3)
r3 = Restaurant(90, 4)
r4 = Restaurant(140, 3)
r5 = Restaurant(120, 4)
r6 = Restaurant(60, 1)
r7 = Restaurant(40, 1)
r8 = Restaurant(30, 2)
r9 = Restaurant(70, 2)
r10 = Restaurant(250, 5)
print()
print("***************")
print("** Unsorted: **")
print("***************")
print()
restaurants = [r1, r2, r3, r4, r5, r6, r7, r8, r9, r10]
for restaurant in restaurants:
print(restaurant.ratio, restaurant.stars)
print()
print("***************")
print("** Sorted: **")
print("***************")
print()
sorted_restaurants = sorted(restaurants, key = lambda x: x.ratio, reverse = True)
for restaurant in sorted_restaurants:
print(restaurant.ratio, restaurant.stars)
print()
print("*********************")
print("** Begin Rucksack: **")
print("*********************")
print()
max = 5
budget = 100
spent = 0
quantity = 0
rucksack = []
for i in itertools.count():
if len(rucksack) >= max or i == len(sorted_restaurants):
break
sorted_restaurants[i].display()
if sorted_restaurants[i].cost + spent <= budget:
spent = spent + sorted_restaurants[i].cost
rucksack.append(sorted_restaurants[i])
print("Total Cost: $" + str(sum([x.cost for x in rucksack])))
print("Total Stars: " + str(sum([x.stars for x in rucksack])))
print()
print("*****************")
print("** Final List: **")
print("*****************")
print()
for restaurant in rucksack:
restaurant.display()
budget
= max rucksack weight in kg,max
= number of items the knapsack can hold,stars
= some value on the item andcost
= item weight in kg – Brionesr8
Restaurant, which means that when generating the list, it puts that into the list first, and with the remaining 70$ it can only get 2 more stars. However, if it was smart enough to skip that ( even though it's the best dollar per star ratio, ther1
restaurant would actually be a better choice for the budget, as it's 100$ cost and 5 stars – Briones