Backpack corrections and updates for file validation
This commit is contained in:
+18
-16
@@ -4,18 +4,16 @@ import matplotlib.pyplot as plt
|
||||
import sys
|
||||
|
||||
def greedy(n, weight, value, capacity ):
|
||||
ratios = [(value[i] / weight[i], weight[i]) for i in range(n)]
|
||||
ratios.sort(reverse=True, key=lambda x: x[0])
|
||||
pairs = list(zip(value, weight))
|
||||
pairs.sort(reverse=True, key=lambda x: x[0] / x[1])
|
||||
|
||||
current_weight = 0
|
||||
current_value = 0
|
||||
items_taken = []
|
||||
currentCapacity = capacity
|
||||
|
||||
for ratio, weight in ratios:
|
||||
if current_weight + weight <= capacity and current_value + value[ratios.index((ratio, weight))] <= capacity:
|
||||
current_weight += weight
|
||||
current_value += value[ratios.index((ratio, weight))]
|
||||
items_taken.append(ratios.index((ratio, weight)))
|
||||
for i in range(n):
|
||||
if pairs[i][1] <= currentCapacity:
|
||||
current_value += pairs[i][0]
|
||||
currentCapacity -= pairs[i][1]
|
||||
|
||||
return current_value
|
||||
|
||||
@@ -61,6 +59,7 @@ def benchmark_containers(container_list, capacity):
|
||||
greedy_values.append(gr)
|
||||
dynamic_values.append(dyn)
|
||||
|
||||
print(f"greedy: {gr}, dynamic: {dyn}, rel_error: {(dyn - gr) / dyn * 100 if dyn != 0 else 0:.2f}%")
|
||||
rel_errors.append((dyn - gr) / dyn * 100 if dyn != 0 else 0)
|
||||
|
||||
return greedy_times, dynamic_times, rel_errors
|
||||
@@ -75,7 +74,7 @@ def benchmark_capacity(capacity_list, containers):
|
||||
# For each container number or capacity
|
||||
for capacity in capacity_list:
|
||||
weights = [random.randint(1, 10) for _ in range(containers)]
|
||||
values = [random.randint(1, 20) for _ in range(containers)]
|
||||
values = [random.randint(2, 20) for _ in range(containers)]
|
||||
|
||||
# Dynamic
|
||||
start = time.time()
|
||||
@@ -91,8 +90,9 @@ def benchmark_capacity(capacity_list, containers):
|
||||
dynamic_times.append(dyn_time)
|
||||
greedy_values.append(gr)
|
||||
dynamic_values.append(dyn)
|
||||
print(f"greedy: {gr}, dynamic: {dyn}, rel_error: {(dyn - gr) / dyn * 100 if dyn != 0 else 0:.2f}%")
|
||||
|
||||
rel_errors.append(abs((dyn - gr) / dyn) * 100 if dyn != 0 else 0)
|
||||
rel_errors.append((dyn - gr) / dyn * 100 if dyn != 0 else 0)
|
||||
|
||||
return greedy_times, dynamic_times, rel_errors
|
||||
|
||||
@@ -115,20 +115,22 @@ if __name__ == "__main__":
|
||||
capacity = int(lines[3].strip())
|
||||
|
||||
dynamic(n, weights, values, capacity, print_table=True)
|
||||
print(greedy(n, weights, values, capacity))
|
||||
exit(0)
|
||||
|
||||
# First test case - constant capacity, variable number of containers
|
||||
containers_list = list(range(5, 101, 5))
|
||||
containers_list = list(range(1, 51, 2))
|
||||
|
||||
# Constant capacity of 20
|
||||
greedy_times, dynamic_times, rel_errors = benchmark_containers(containers_list, 20)
|
||||
|
||||
plt.figure(figsize=(12,5))
|
||||
plt.subplot(1,2,1)
|
||||
plt.plot(containers_list, greedy_times, label='Zachłanny', marker='o')
|
||||
plt.plot(containers_list, dynamic_times, label='Dynamiczny', marker='o')
|
||||
plt.plot(containers_list, greedy_times, label='Zachłanny', )
|
||||
plt.plot(containers_list, dynamic_times, label='Dynamiczny', )
|
||||
plt.xlabel('Liczba kontenerów')
|
||||
plt.ylabel('Czas [ms]')
|
||||
# plt.yscale('log', base=2)
|
||||
plt.title('Czas działania dla zmiennej liczby kontenerów (B=20)')
|
||||
plt.legend()
|
||||
|
||||
@@ -149,8 +151,8 @@ if __name__ == "__main__":
|
||||
|
||||
plt.figure(figsize=(12,5))
|
||||
plt.subplot(1,2,1)
|
||||
plt.plot(capacity_list, greedy_times, label='Zachłanny', marker='o')
|
||||
plt.plot(capacity_list, dynamic_times, label='Dynamiczny' , marker='o')
|
||||
plt.plot(capacity_list, greedy_times, label='Zachłanny' )
|
||||
plt.plot(capacity_list, dynamic_times, label='Dynamiczny')
|
||||
plt.xlabel('Pojemność')
|
||||
plt.ylabel('Czas [ms]')
|
||||
plt.title('Czas działania dla zmiennej pojemności (n=50)')
|
||||
|
||||
Reference in New Issue
Block a user