Commit 8ad62126 authored by Jirka Fink's avatar Jirka Fink
Browse files

Correction of 5th assignment

parent 2ee0ea48
......@@ -31,25 +31,32 @@ def solve_knapsack(weight, price, capacity):
assert len(weight) == len(price)
# Lists for dynamic programming
max_cost = [0] * (capacity+1) # Maximal possible cost reaching every total weight
last_index = [-1] * (capacity+1) # Index of the last added item
max_cost = [-1] * (capacity+1) # Maximal possible cost reaching every total weight
max_cost[0] = 0
used = [ [False] * (capacity+1) for _ in range(len(weight)) ]
for j, w, p in zip(range(len(weight)), weight, price): # Index, weight and price of each item
if p > 0: # Ignore items of non-positive items
for i in reversed(range(capacity-w+1)): # Try to insert the item j to all total weights
if max_cost[i+w] < max_cost[i] + p:
if max_cost[i+w] < max_cost[i] + p and max_cost[i] >= 0:
# Adding item i increases the cost
max_cost[i+w] = max_cost[i] + p
last_index[i+w] = j
used[j][i+w] = True
solution = []
m = numpy.argmax(max_cost) # Total weight with maximal cost
objective = max_cost[m]
# Using last_index reconstruct an optimal solution
while last_index[m] >= 0:
solution.append(last_index[m])
m -= weight[last_index[m]]
for j in reversed(range(len(weight))):
if used[j][m]:
solution.append(j)
m -= weight[j]
# Test at least feasibility
assert len(solution) == len(set(solution))
assert abs(objective - sum(price[a] for a in solution)) < 0.001
assert capacity >= sum(weight[a] for a in solution)
return (objective, solution)
......@@ -63,3 +70,5 @@ if __name__ == "__main__":
assert solve_knapsack([4,6,10], [2,5,9], 12) == (9, [2])
assert solve_knapsack([4,6,10], [2,5,6], 12) == (7, [1,0])
assert solve_knapsack([4,6,10], [2,5,6], 15) == (8, [2,0])
assert solve_knapsack([3,3,3,3], [1,2,4,10], 100) == (17, [3,2,1,0])
print("All tests passed")
......@@ -38,16 +38,16 @@ def run_test(filename, bound):
def main():
tests = {
"first": ("gap1.txt", 258.5, 1),
"second": ("gap2.txt", 274.3878787878788, 1),
"third": ("gap3.txt", 434.99189237716774, 1),
"fourth": ("gap4.txt", 417.7556653611704, 2),
"fifth": ("gap5.txt", 402.8431003822761, 1),
"sixth": ("gap6.txt", 522.2637096268518, 2),
"seventh": ("gap7.txt", 643.210566967029, 3),
"eigth": ("gap8.txt", 793.0298487147554, 6),
"ninth": ("gap9.txt", 480.81566820276504, 2),
"tenth": ("gap10.txt", 634.1139438624057, 5),
"first": ("gap1.txt", 260.0, .5),
"second": ("gap2.txt", 277.0, 1),
"third": ("gap3.txt", 437.6666666666667, 1),
"fourth": ("gap4.txt", 423.0, 2),
"fifth": ("gap5.txt", 403.0, 1),
"sixth": ("gap6.txt", 525.0, 2),
"seventh": ("gap7.txt", 646.0, 5),
"eigth": ("gap8.txt", 796.6, 10),
"ninth": ("gap9.txt", 482.0, 2),
"tenth": ("gap10.txt", 637.6666666666666, 4),
}
if len(sys.argv) == 1:
results = PrettyTable(["Instance name", "Points", "Filename", "Jobs", "Agents", "Time limit [s]", "Your time [s]", "Evaluation"])
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment