Continue working on Knapsack.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
*.sublime-workspace
|
*.sublime-workspace
|
||||||
__pycache__
|
__pycache__
|
||||||
|
*.pyc
|
||||||
|
|||||||
@@ -70,43 +70,48 @@ def solve_knapsack_depth_first_search(knapsack):
|
|||||||
|
|
||||||
def get_max_value(from_index, capacity):
|
def get_max_value(from_index, capacity):
|
||||||
value = 0
|
value = 0
|
||||||
items = knapsack.items
|
|
||||||
|
|
||||||
for i in range(from_index, num_items):
|
for i in range(from_index, num_items):
|
||||||
item = knapsack.items[i]
|
item = knapsack.items[i]
|
||||||
if item.weight <= capacity:
|
if item.weight <= capacity:
|
||||||
value += item.value
|
value += item.value
|
||||||
capacity -= item.weight
|
capacity -= item.weight
|
||||||
else:
|
else:
|
||||||
value += int((item.weight / knapsack.capacity) * item.value) + 1
|
value += int((capacity / item.weight) * item.value)
|
||||||
#value += int((item.weight / capacity) * item.value) + 1
|
break
|
||||||
return value
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
def search(index, capacity, value, path, result):
|
def search(index, capacity, value, path, result):
|
||||||
|
|
||||||
if capacity < 0:
|
|
||||||
return
|
|
||||||
|
|
||||||
if value > result["objective"]:
|
if value > result["objective"]:
|
||||||
result["objective"] = value
|
result["objective"] = value
|
||||||
result["path"] = path
|
result["path"] = list(path)
|
||||||
|
|
||||||
|
if capacity <= 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
if index == num_items:
|
||||||
|
return
|
||||||
|
|
||||||
|
# print("--- search")
|
||||||
|
# print("Index: {} capacity: {}, value: {}".format(index, capacity, value))
|
||||||
|
# print("Path {}".format(path))
|
||||||
|
|
||||||
# Take current item.
|
# Take current item.
|
||||||
max_value = get_max_value(index, capacity)
|
max_value = get_max_value(index, capacity)
|
||||||
if value + max_value > result["objective"]:
|
item = knapsack.items[index]
|
||||||
item = knapsack.items[index]
|
# print("max_value: {}".format(max_value))
|
||||||
new_index = index + 1
|
max_value_not = get_max_value(index + 1, capacity)
|
||||||
new_path = path + [1]
|
# print("max_value_not: {}".format(max_value_not))
|
||||||
new_capacity = capacity - item.weight
|
if item.weight <= capacity and value + max_value > result["objective"]:
|
||||||
new_value = value + item.value
|
path.append(1)
|
||||||
search(new_index, new_capacity, new_value, new_path, result)
|
search(index + 1, capacity - item.weight,
|
||||||
|
value + item.value, path, result)
|
||||||
|
path.pop()
|
||||||
|
|
||||||
# Do not take current item.
|
# Do not take current item.
|
||||||
new_index = index + 1
|
if value + max_value_not > result["objective"]:
|
||||||
if value + get_max_value(new_index, capacity) > result["objective"]:
|
path.append(0)
|
||||||
new_path = path + [0]
|
search(index + 1, capacity, value, path, result)
|
||||||
search(new_index, capacity, value, new_path, result)
|
path.pop()
|
||||||
|
|
||||||
def correct_path(path):
|
def correct_path(path):
|
||||||
path = path + [0] * (num_items - len(path))
|
path = path + [0] * (num_items - len(path))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python3
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import knapsack
|
import knapsack
|
||||||
@@ -7,12 +7,9 @@ import knapsack
|
|||||||
def solve_it(input_data):
|
def solve_it(input_data):
|
||||||
# Modify this code to run your optimization algorithm
|
# Modify this code to run your optimization algorithm
|
||||||
k = knapsack.input_data_to_knapsack(input_data)
|
k = knapsack.input_data_to_knapsack(input_data)
|
||||||
if len(k.items) <= 200:
|
r = knapsack.solve_knapsack_dynamic(k)
|
||||||
r = knapsack.solve_knapsack_dynamic(k)
|
# r = knapsack.solve_knapsack_depth_first_search(k)
|
||||||
elif len(k.items) == 400:
|
# r = knapsack.solve_knapsack_greedy(k)
|
||||||
r = knapsack.solve_knapsack_depth_first_search(k)
|
|
||||||
else:
|
|
||||||
r = knapsack.solve_knapsack_greedy(k)
|
|
||||||
return knapsack.result_to_output_data(r)
|
return knapsack.result_to_output_data(r)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user