Skip to content

Instantly share code, notes, and snippets.

@fedden
Created December 11, 2017 10:23
Show Gist options
  • Save fedden/384693e35a53171213ac801736aa009a to your computer and use it in GitHub Desktop.
Save fedden/384693e35a53171213ac801736aa009a to your computer and use it in GitHub Desktop.
def knapsack_evaluate(position, challenge):
if challenge == 1:
profits = [92, 57, 49, 68, 60, 43, 67, 84, 87, 72]
weights = [23, 31, 29, 44, 53, 38, 63, 85, 89, 82]
max_weight = 165
solution = [1, 1, 1, 1, 0, 1, 0, 0, 0, 0]
elif challenge == 2:
profits = [24, 13, 23, 15, 16]
weights = [12, 7, 11, 8, 9]
max_weight = 26
solution = [0, 1, 1, 1, 0]
elif challenge == 3:
profits = [50, 50, 64, 46, 50, 5]
weights = [56, 59, 80, 64, 75, 17]
max_weight = 190
solution = [1, 1, 0, 0, 1, 0]
elif challenge == 4:
profits = [70, 20, 39, 37, 7, 5, 10]
weights = [31, 10, 20, 19, 4, 3, 6]
max_weight = 50
solution = [1, 0, 0, 1, 0, 0, 0]
elif challenge == 5:
profits = [350, 400, 450, 20, 70, 8, 5, 5]
weights = [25, 35, 45, 5, 25, 3, 2, 2]
max_weight = 104
solution = [1, 0, 1, 1, 1, 0, 1, 1]
total_weight = 0
total_profit = 0
for i, p in enumerate(position):
if p == 1:
total_weight += weights[i]
total_profit += profits[i]
fitness = -1 if total_weight > max_weight else total_profit
done = np.array_equal(position, solution)
return fitness, done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment