Skip to content

Instantly share code, notes, and snippets.

@shshemi
Last active August 28, 2019 06:36
Show Gist options
  • Save shshemi/67ded14e86fd055e34109a4bab35a937 to your computer and use it in GitHub Desktop.
Save shshemi/67ded14e86fd055e34109a4bab35a937 to your computer and use it in GitHub Desktop.
def brute_force_knapsack(x_weights, x_prices, x_capacity):
item_count = x_weights.shape[0]
picks_space = 2 ** item_count
best_price = -1
best_picks = np.zeros(item_count)
for p in range(picks_space):
picks = [int(c) for c in f"{p:0{item_count}b}"]
price = np.dot(x_prices, picks)
weight = np.dot(x_weights, picks)
if weight <= x_capacity and price > best_price:
best_price = price
best_picks = picks
return best_picks
def create_knapsack(item_count=5):
x_weights = np.random.randint(1, 45, item_count)
x_prices = np.random.randint(1, 99, item_count)
x_capacity = np.random.randint(1, 99)
y = brute_force_knapsack(x_weights, x_prices, x_capacity)
return x_weights, x_prices, x_capacity, y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment