简体   繁体   中英

Knapsack I/O classic problem to get least valuable items

The classic knapsack addresses the solution to get the most valuable items inside the knapsack which has a limited weight it can carry.

I am trying to get instead the least valuable items.

The following code is a very good one using Recursive dynamic programming from rosetacode http://rosettacode.org/wiki/Knapsack_problem/0-1#Recursive_dynamic_programming_algorithm


def total_value(items, max_weight):
    return  sum([x[2] for x in items]) if sum([x[1] for x in items]) <= max_weight else 0

cache = {}
def solve(items, max_weight):
    if not items:
        return ()
    if (items,max_weight) not in cache:
        head = items[0]
        tail = items[1:]
        include = (head,) + solve(tail, max_weight - head[1])
        dont_include = solve(tail, max_weight)
        if total_value(include, max_weight) > total_value(dont_include, max_weight):
            answer = include
        else:
            answer = dont_include
        cache[(items,max_weight)] = answer
    return cache[(items,max_weight)]

items = (
    ("map", 9, 150), ("compass", 13, 35), ("water", 153, 200), ("sandwich", 50, 160),
    ("glucose", 15, 60), ("tin", 68, 45), ("banana", 27, 60), ("apple", 39, 40),
    ("cheese", 23, 30), ("beer", 52, 10), ("suntan cream", 11, 70), ("camera", 32, 30),
    ("t-shirt", 24, 15), ("trousers", 48, 10), ("umbrella", 73, 40),
    ("waterproof trousers", 42, 70), ("waterproof overclothes", 43, 75),
    ("note-case", 22, 80), ("sunglasses", 7, 20), ("towel", 18, 12),
    ("socks", 4, 50), ("book", 30, 10),
    )
max_weight = 400

solution = solve(items, max_weight)
print "items:"
for x in solution:
    print x[0]
print "value:", total_value(solution, max_weight)
print "weight:", sum([x[1] for x in solution])

I have been trying to figure out how can i get the least valuable items looking on the internet with no luck so maybe somebody can help me with that.

I really apreciate your help in advance.

I'll try my best to guide you through what should be done to achieve this.

In order to make changes to this code and find the least valuable items with which you can fill the bag make a function which,

  • Takes in the most valuable items( solution in your code) as the input

  • Find the (I'll call it least_items ) items that you will be leaving behind

  • Check if the total weight of the items in least_items is greater
    than the max_weight.

    1. If yes find the most valuable items in least_items and remove them from least_items.This will be a place where you will have to initiate some sort of recursion to keep seperating the least
      valueable from the most valuable

    2. If no that means you could fill you knapsack with more items.So then you have to go back to the most valuable items you had and keep looking for the least valuable items until you fill the knapsack.Again some sort of recursion will have too be initiated

But take note that you will also have to include a terminating step so that the program stops when it has found the best solution.

This is not the best solution you could make though.I tried finding something better myself but unfortunately it demands more time than I thought.Feel free to leave any problems in the comments.I'll be happy to help.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM